1aa56d68b8f296aa47b4bb5f9cc0522b84b92621
[bertos.git] / bertos / algo / pid_control.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  *
33  * \brief Proportional, integral, derivative controller (PID controller).
34  *
35  * \version $Id$
36  *
37  * \author Daniele Basile <asterix@develer.com>
38  *
39  * $WIZARD_MODULE = {
40  * "name" : "pid_control",
41  * "depends" : ["timer"],
42  * "configuration" : ""
43  * }
44  */
45
46 #ifndef ALGO_PID_CONTROL_H
47 #define ALGO_PID_CONTROL_H
48
49 #include <drv/timer.h>
50
51 /**
52  * Data type for pid coefficient.
53  */
54 typedef float pidk_t;
55 typedef float piddata_t;
56
57 /**
58  * PID context structure.
59  */
60 typedef struct PidCfg
61 {
62         pidk_t kp;              ///< Proportional term of PID control method (Gain).
63         pidk_t ki;              ///< Integral term of PID control method (Integral Gain).
64         pidk_t kd;              ///< Derivative of PID control method (Derivative Gain).
65
66         piddata_t i_max;        ///< Max value of integral term.
67         piddata_t i_min;        ///< Min value of integral term.
68
69         piddata_t out_max;      ///< Man value of output.
70         piddata_t out_min;      ///< Min value of output.
71
72         mtime_t sample_period;  ///< Sample period in milliseconds.
73
74 } PidCfg;
75
76
77 /**
78  * PID context structure.
79  */
80 typedef struct PidContext
81 {
82         const PidCfg *cfg;
83
84         piddata_t prev_err;     ///< Previous error.
85         piddata_t i_state;      ///< Integrator state (sum of all the preceding errors).
86
87 } PidContext;
88
89 /**
90  * Set Kp, Ki, Kd constants of PID control.
91  */
92 INLINE void pid_control_setPid(PidCfg *pid_cfg, pidk_t kp, pidk_t ki, pidk_t kd)
93 {
94         pid_cfg->kp = kp;
95         pid_cfg->ki = ki;
96         pid_cfg->kd = kd;
97 }
98
99 /**
100  * Set sample period for PID control.
101  */
102 INLINE void pid_control_setPeriod(PidCfg *pid_cfg, mtime_t sample_period)
103 {
104         pid_cfg->sample_period = sample_period;
105 }
106
107 /**
108  * Clear a pid control structure
109  */
110 INLINE void pid_control_reset(PidContext *pid_ctx)
111 {
112         pid_ctx->i_state = 0;
113         pid_ctx->prev_err = 0;
114 }
115
116 piddata_t pid_control_update(PidContext *pid_ctx, piddata_t target, piddata_t curr_pos);
117 void pid_control_init(PidContext *pid_ctx, const PidCfg *pid_cfg);
118
119 #endif /* ALGO_PID_CONTROL_H */