00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdio.h>
00022 #include <control_system_manager.h>
00023
00024 #define DEBUG 0
00025
00026 #if DEBUG == 1
00027 #define debug_printf(args...) do { printf(args); } while(0)
00028 #else
00029 #define debug_printf(args...) do { } while(0)
00030 #endif
00031
00039 static inline uint32_t
00040 safe_filter(int32_t (*f)(void *, int32_t), void * param, int32_t value)
00041 {
00042 int32_t (*f_tmp)(void *, int32_t);
00043 void * param_tmp;
00044 uint8_t flags;
00045 IRQ_LOCK(flags);
00046 f_tmp = f;
00047 param_tmp = param;
00048 IRQ_UNLOCK(flags);
00049 if (f_tmp) {
00050 return f_tmp(param_tmp, value);
00051 }
00052 return value;
00053 }
00054
00062 static inline uint32_t
00063 safe_getprocessout(int32_t (*f)(void *), void * param)
00064 {
00065 int32_t (*f_tmp)(void *);
00066 void * param_tmp;
00067 uint8_t flags;
00068 IRQ_LOCK(flags);
00069 f_tmp = f;
00070 param_tmp = param;
00071 IRQ_UNLOCK(flags);
00072 if (f_tmp) {
00073 return f_tmp(param_tmp);
00074 }
00075 return 0;
00076 }
00077
00085 static inline void
00086 safe_setprocessin(void (*f)(void *, int32_t), void * param, int32_t value)
00087 {
00088 void (*f_tmp)(void *, int32_t);
00089 void * param_tmp;
00090 uint8_t flags;
00091 IRQ_LOCK(flags);
00092 f_tmp = f;
00093 param_tmp = param;
00094 IRQ_UNLOCK(flags);
00095 if (f_tmp) {
00096 f_tmp(param_tmp, value);
00097 }
00098 }
00099
00100
00101
00102 void cs_init(struct cs* cs)
00103 {
00104 uint8_t flags;
00105 IRQ_LOCK(flags);
00106 cs->consign_filter = NULL;
00107 cs->consign_filter_params = NULL;
00108
00109 cs->correct_filter = NULL;
00110 cs->correct_filter_params = NULL;
00111
00112 cs->feedback_filter = NULL;
00113 cs->feedback_filter_params = NULL;
00114
00115 cs->process_out = NULL;
00116 cs->process_out_params = NULL;
00117
00118 cs->process_in = NULL;
00119 cs->process_in_params = NULL;
00120
00121 cs->consign_value = 0;
00122 cs->error_value = 0;
00123 cs->out_value = 0;
00124 IRQ_UNLOCK(flags);
00125
00126 return;
00127 }
00128
00129
00130 void cs_set_consign_filter(struct cs* cs, int32_t (*consign_filter)(void*, int32_t), void* consign_filter_params)
00131 {
00132 uint8_t flags;
00133 IRQ_LOCK(flags);
00134 cs->consign_filter = consign_filter;
00135 cs->consign_filter_params = consign_filter_params;
00136 IRQ_UNLOCK(flags);
00137 }
00138
00139
00140
00141 void cs_set_correct_filter(struct cs* cs, int32_t (*correct_filter)(void*, int32_t), void* correct_filter_params)
00142 {
00143 uint8_t flags;
00144 IRQ_LOCK(flags);
00145 cs->correct_filter = correct_filter;
00146 cs->correct_filter_params = correct_filter_params;
00147 IRQ_UNLOCK(flags);
00148 }
00149
00150
00151 void cs_set_feedback_filter(struct cs* cs, int32_t (*feedback_filter)(void*, int32_t), void* feedback_filter_params)
00152 {
00153 uint8_t flags;
00154 IRQ_LOCK(flags);
00155 cs->feedback_filter = feedback_filter;
00156 cs->feedback_filter_params = feedback_filter_params;
00157 IRQ_UNLOCK(flags);
00158 }
00159
00160
00161 void cs_set_process_in(struct cs* cs, void (*process_in)(void*, int32_t), void* process_in_params)
00162 {
00163 uint8_t flags;
00164 IRQ_LOCK(flags);
00165 cs->process_in = process_in;
00166 cs->process_in_params = process_in_params;
00167 IRQ_UNLOCK(flags);
00168 }
00169
00170
00171
00172 void cs_set_process_out(struct cs* cs, int32_t (*process_out)(void*), void* process_out_params)
00173 {
00174 uint8_t flags;
00175 IRQ_LOCK(flags);
00176 cs->process_out = process_out;
00177 cs->process_out_params = process_out_params;
00178 IRQ_UNLOCK(flags);
00179 }
00180
00181
00182
00183 int32_t cs_do_process(struct cs* cs, int32_t consign)
00184 {
00185 #if DEBUG == 1
00186 static int i=0;
00187 #endif
00188 int32_t process_out_value = 0;
00189
00190
00191 cs->consign_value = consign;
00192
00193 debug_printf("%d %ld ", i++, consign);
00194
00195
00196 cs->filtered_consign_value = consign = safe_filter(cs->consign_filter, cs->consign_filter_params, consign);
00197
00198 debug_printf("%ld ", cs->filtered_consign_value);
00199
00200
00201 process_out_value = safe_getprocessout(cs->process_out, cs->process_out_params);
00202
00203 debug_printf("%ld ", process_out_value);
00204
00205
00206 process_out_value = safe_filter(cs->feedback_filter, cs->feedback_filter_params, process_out_value);
00207 cs->filtered_feedback_value = process_out_value;
00208
00209 debug_printf("%ld ", process_out_value);
00210
00211
00212 cs->error_value = cs->filtered_consign_value - process_out_value ;
00213
00214 debug_printf("%ld ", cs->error_value);
00215
00216
00217 cs->out_value = safe_filter(cs->correct_filter, cs->correct_filter_params, cs->error_value);
00218
00219 debug_printf("%ld\n", cs->out_value);
00220
00221
00222 safe_setprocessin (cs->process_in, cs->process_in_params, cs->out_value);
00223
00224
00225 return (cs->out_value);
00226 }
00227
00228
00229
00230 void cs_manage(void * data)
00231 {
00232 struct cs* cs = data;
00233 cs_do_process(cs, cs->consign_value);
00234 }
00235
00236
00237
00238 int32_t cs_get_out(struct cs* cs)
00239 {
00240 int32_t tmp;
00241 uint8_t flags;
00242 IRQ_LOCK(flags);
00243 tmp = cs->out_value;
00244 IRQ_UNLOCK(flags);
00245
00246 return tmp;
00247 }
00248
00249
00250
00251 int32_t cs_get_error(struct cs* cs)
00252 {
00253 int32_t tmp;
00254 uint8_t flags;
00255 IRQ_LOCK(flags);
00256 tmp = cs->error_value;
00257 IRQ_UNLOCK(flags);
00258
00259 return tmp;
00260 }
00261
00262
00263
00264 int32_t cs_get_consign(struct cs* cs)
00265 {
00266 int32_t tmp;
00267 uint8_t flags;
00268 IRQ_LOCK(flags);
00269 tmp = cs->consign_value;
00270 IRQ_UNLOCK(flags);
00271
00272 return tmp;
00273 }
00274
00275 int32_t cs_get_filtered_consign(struct cs* cs)
00276 {
00277 int32_t tmp;
00278 uint8_t flags;
00279 IRQ_LOCK(flags);
00280 tmp = cs->filtered_consign_value;
00281 IRQ_UNLOCK(flags);
00282
00283 return tmp;
00284 }
00285
00286 int32_t cs_get_filtered_feedback(struct cs* cs)
00287 {
00288 int32_t tmp;
00289 uint8_t flags;
00290 IRQ_LOCK(flags);
00291 tmp = cs->filtered_feedback_value;
00292 IRQ_UNLOCK(flags);
00293
00294 return tmp;
00295 }
00296
00297
00298
00299 void cs_set_consign(struct cs* cs, int32_t v)
00300 {
00301 uint8_t flags;
00302
00303 IRQ_LOCK(flags);
00304 cs->consign_value = v;
00305 IRQ_UNLOCK(flags);
00306 }