00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <string.h>
00023 #include <pid.h>
00024
00025
00027 void pid_init(struct pid_filter *p)
00028 {
00029 uint8_t flags;
00030 IRQ_LOCK(flags);
00031 memset(p, 0, sizeof(*p));
00032 p->gain_P = 1 ;
00033 p->derivate_nb_samples = 1;
00034 IRQ_UNLOCK(flags);
00035 }
00036
00039 void pid_reset(struct pid_filter *p)
00040 {
00041 uint8_t flags;
00042 IRQ_LOCK(flags);
00043 memset(p->prev_samples, 0, sizeof(p->prev_samples));
00044 p->integral = 0;
00045 p->prev_D = 0;
00046 p->prev_out = 0;
00047 IRQ_UNLOCK(flags);
00048 }
00049
00050 void pid_set_gains(struct pid_filter *p, int16_t gp, int16_t gi, int16_t gd)
00051 {
00052 uint8_t flags;
00053 IRQ_LOCK(flags);
00054 p->gain_P = gp;
00055 p->gain_I = gi;
00056 p->gain_D = gd;
00057 IRQ_UNLOCK(flags);
00058 }
00059
00060 void pid_set_maximums(struct pid_filter *p, int32_t max_in, int32_t max_I, int32_t max_out)
00061 {
00062 uint8_t flags;
00063 IRQ_LOCK(flags);
00064 p->max_in = max_in;
00065 p->max_I = max_I;
00066 p->max_out = max_out;
00067 IRQ_UNLOCK(flags);
00068 }
00069
00070 void pid_set_out_shift(struct pid_filter *p, uint8_t out_shift)
00071 {
00072 uint8_t flags;
00073 IRQ_LOCK(flags);
00074 p->out_shift=out_shift;
00075 IRQ_UNLOCK(flags);
00076 }
00077
00078 int8_t pid_set_derivate_filter(struct pid_filter *p, uint8_t nb_samples)
00079 {
00080 uint8_t flags;
00081 int8_t ret;
00082 IRQ_LOCK(flags);
00083 if (nb_samples > PID_DERIVATE_FILTER_MAX_SIZE) {
00084 ret = -1;
00085 }
00086 else {
00087 p->derivate_nb_samples = nb_samples;
00088 ret = 0;
00089 }
00090 IRQ_UNLOCK(flags);
00091 return ret;
00092 }
00093
00094 int16_t pid_get_gain_P(struct pid_filter *p)
00095 {
00096 return (p->gain_P);
00097 }
00098
00099 int16_t pid_get_gain_I(struct pid_filter *p)
00100 {
00101 return (p->gain_I);
00102 }
00103
00104 int16_t pid_get_gain_D(struct pid_filter *p)
00105 {
00106 return (p->gain_D);
00107 }
00108
00109
00110 int32_t pid_get_max_in(struct pid_filter *p)
00111 {
00112 return (p->max_in);
00113 }
00114
00115 int32_t pid_get_max_I(struct pid_filter *p)
00116 {
00117 return (p->max_I);
00118 }
00119
00120 int32_t pid_get_max_out(struct pid_filter *p)
00121 {
00122 return (p->max_out);
00123 }
00124
00125
00126 uint8_t pid_get_out_shift(struct pid_filter *p)
00127 {
00128 return (p->out_shift);
00129 }
00130
00131 uint8_t pid_get_derivate_filter(struct pid_filter *p)
00132 {
00133 return (p->derivate_nb_samples);
00134 }
00135
00136 int32_t pid_get_value_I(struct pid_filter *p)
00137 {
00138 uint8_t flags;
00139 int32_t ret;
00140 IRQ_LOCK(flags);
00141 ret = (p->integral);
00142 IRQ_UNLOCK(flags);
00143 return ret;
00144 }
00145
00146 int32_t pid_get_value_in(struct pid_filter *p)
00147 {
00148 uint8_t flags;
00149 int32_t ret;
00150 IRQ_LOCK(flags);
00151 ret = p->prev_samples[p->index];
00152 IRQ_UNLOCK(flags);
00153 return ret;
00154 }
00155
00156 int32_t pid_get_value_D(struct pid_filter *p)
00157 {
00158 uint8_t flags;
00159 int32_t ret;
00160 IRQ_LOCK(flags);
00161 ret = p->prev_D;
00162 IRQ_UNLOCK(flags);
00163 return ret;
00164 }
00165
00166 int32_t pid_get_value_out(struct pid_filter *p)
00167 {
00168 uint8_t flags;
00169 int32_t ret;
00170 IRQ_LOCK(flags);
00171 ret = (p->prev_out);
00172 IRQ_UNLOCK(flags);
00173 return ret;
00174 }
00175
00176
00177 int32_t pid_do_filter(void * data, int32_t in)
00178 {
00179 int32_t derivate ;
00180 int32_t command ;
00181 struct pid_filter * p = data;
00182 uint8_t prev_index;
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200 prev_index = p->index + 1;
00201 if (prev_index >= p->derivate_nb_samples)
00202 prev_index = 0;
00203
00204
00205 if (p->max_in)
00206 S_MAX(in, p->max_in) ;
00207
00208 derivate = in - p->prev_samples[prev_index];
00209 p->integral += in ;
00210
00211 if (p->max_I)
00212 S_MAX(p->integral, p->max_I) ;
00213
00214
00215 command = in * p->gain_P +
00216 p->integral * p->gain_I +
00217 (derivate * p->gain_D) / p->derivate_nb_samples ;
00218
00219 if ( command < 0 )
00220 command = -( -command >> p->out_shift );
00221 else
00222 command = command >> p->out_shift ;
00223
00224 if (p->max_out)
00225 S_MAX (command, p->max_out) ;
00226
00227
00228
00229 p->prev_samples[p->index] = in ;
00230 p->index = prev_index;
00231 p->prev_D = derivate ;
00232 p->prev_out = command ;
00233
00234 return command;
00235 }