remove CVS
[aversive.git] / modules / devices / control_system / filters / pid / pid.h
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2006)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: pid.h,v 1.4.4.7 2008-05-09 08:24:33 zer0 Exp $
19  *
20  */
21
22 #ifndef _PID_H_
23 #define _PID_H_
24
25 #include <aversive.h>
26 #include <stdlib.h>
27
28 #include <pid_config.h>
29
30
31 /** this is the pid_filter structure*/
32 struct pid_filter
33 {
34         int16_t gain_P; /**< Gain of Proportionnal module */
35         int16_t gain_I; /**< Gain of Integral module */
36         int16_t gain_D; /**< Gain of Derivate module */
37
38         uint8_t out_shift; /**< big common divisor for output */
39
40         uint8_t derivate_nb_samples; /**< sample count for derivate filter */
41         uint8_t index; /**< index in circular buffer below */
42         int32_t prev_samples[PID_DERIVATE_FILTER_MAX_SIZE]; /**< previous in (circular buf) */
43         
44         int32_t max_in; /**<  In saturation levels */
45         int32_t max_I; /**<   Integral saturation levels */
46         int32_t max_out; /**< Out saturation levels */
47
48         int32_t integral; /**< previous integral parameter */
49         int32_t prev_D;   /**< previous derivate parameter */
50         int32_t prev_out; /**< previous out command (for debug only) */
51 };
52
53 /** init pid */
54 void pid_init(struct pid_filter *p);
55
56 /** reset state (derivate and intergral state) */
57 void pid_reset(struct pid_filter *p);
58
59 /* Use these functions to change one parameter on pid_filter structure */
60 void pid_set_gains(struct pid_filter *p, int16_t gp, int16_t gi, int16_t gd) ;
61 void pid_set_maximums(struct pid_filter *p, int32_t max_in, int32_t max_I, int32_t max_out);
62 void pid_set_out_shift(struct pid_filter *p, uint8_t out_shift);
63 int8_t pid_set_derivate_filter(struct pid_filter *p, uint8_t nb_samples);
64
65 /* accessors of all parameter of pid structure*/
66 int16_t pid_get_gain_P(struct pid_filter *p);
67 int16_t pid_get_gain_I(struct pid_filter *p);
68 int16_t pid_get_gain_D(struct pid_filter *p);
69 int32_t pid_get_max_in(struct pid_filter *p);
70 int32_t pid_get_max_I(struct pid_filter *p);
71 int32_t pid_get_max_out(struct pid_filter *p);
72 uint8_t pid_get_out_shift(struct pid_filter *p);
73 uint8_t pid_get_derivate_filter(struct pid_filter *p);
74
75 /** get the sum of all nput samples since the filter initialisation */
76 int32_t pid_get_value_I(struct pid_filter *p);
77
78 /** get previous input value */
79 int32_t pid_get_value_in(struct pid_filter *p);
80
81 /** get previous derivate value (without gain) */
82 int32_t pid_get_value_D(struct pid_filter *p);
83
84 /** get previous output value */
85 int32_t pid_get_value_out(struct pid_filter *p);
86
87 /** PID process */
88 int32_t pid_do_filter(void *p, int32_t in);
89         
90         
91 #endif