MotorWare f2806x Module API Documentation

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

float/pid.h
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2 * Copyright (c) 2014, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * --/COPYRIGHT--*/
32 #ifndef _PID_H_
33 #define _PID_H_
34
40
41
42 // **************************************************************************
43 // the includes
44
45 // modules
46 #include "sw/modules/types/src/types.h"
47 #include "sw/modules/filter/src/float/filter_fo.h"
48 #include "sw/modules/math/src/float/math.h"
49
50
55
56
57 // Include the algorithm overview defined in modules/<module>/docs/doxygen/doxygen.h
59
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63
64
65 // **************************************************************************
66 // the defines
67
68
69
70 // **************************************************************************
71 // the typedefs
72
75 typedef struct _PID_Obj_
76 {
77 float_t Kp;
78 float_t Ki;
79 float_t Kd;
80
81 float_t Ui;
82
83 float_t refValue;
84 float_t fbackValue;
85 float_t ffwdValue;
86
87 float_t outMin;
88 float_t outMax;
89
90 FILTER_FO_Handle derFilterHandle;
91 FILTER_FO_Obj derFilter;
92 } PID_Obj;
93
94
97 typedef struct _PID_Obj_ *PID_Handle;
98
99
100 // **************************************************************************
101 // the function prototypes
102
113 void PID_getDerFilterParams(PID_Handle handle,
114 float_t *b0,float_t *b1,
115 float_t *a1,float_t *x1,float_t *y1);
116
117
121 static inline float_t PID_getFbackValue(PID_Handle handle)
122 {
123 PID_Obj *obj = (PID_Obj *)handle;
124
125 return(obj->fbackValue);
126 } // end of PID_getFbackValue() function
127
128
132 static inline float_t PID_getFfwdValue(PID_Handle handle)
133 {
134 PID_Obj *obj = (PID_Obj *)handle;
135
136 return(obj->ffwdValue);
137 } // end of PID_getFfwdValue() function
138
139
145 static inline void PID_getGains(PID_Handle handle,float_t *pKp,float_t *pKi,float_t *pKd)
146 {
147 PID_Obj *obj = (PID_Obj *)handle;
148
149 *pKp = obj->Kp;
150 *pKi = obj->Ki;
151 *pKd = obj->Kd;
152
153 return;
154 } // end of PID_getGains() function
155
156
160 static inline float_t PID_getKd(PID_Handle handle)
161 {
162 PID_Obj *obj = (PID_Obj *)handle;
163
164 return(obj->Kd);
165 } // end of PID_getKd() function
166
167
171 static inline float_t PID_getKi(PID_Handle handle)
172 {
173 PID_Obj *obj = (PID_Obj *)handle;
174
175 return(obj->Ki);
176 } // end of PID_getKi() function
177
178
182 static inline float_t PID_getKp(PID_Handle handle)
183 {
184 PID_Obj *obj = (PID_Obj *)handle;
185
186 return(obj->Kp);
187 } // end of PID_getKp() function
188
189
194 static inline void PID_getMinMax(PID_Handle handle,float_t *pOutMin,float_t *pOutMax)
195 {
196 PID_Obj *obj = (PID_Obj *)handle;
197
198 *pOutMin = obj->outMin;
199 *pOutMax = obj->outMax;
200
201 return;
202 } // end of PID_getMinMax() function
203
204
208 static inline float_t PID_getOutMax(PID_Handle handle)
209 {
210 PID_Obj *obj = (PID_Obj *)handle;
211
212 return(obj->outMax);
213 } // end of PID_getOutMax() function
214
215
219 static inline float_t PID_getOutMin(PID_Handle handle)
220 {
221 PID_Obj *obj = (PID_Obj *)handle;
222
223 return(obj->outMin);
224 } // end of PID_getOutMin() function
225
226
230 static inline float_t PID_getRefValue(PID_Handle handle)
231 {
232 PID_Obj *obj = (PID_Obj *)handle;
233
234 return(obj->refValue);
235 } // end of PID_getRefValue() function
236
237
241 static inline float_t PID_getUi(PID_Handle handle)
242 {
243 PID_Obj *obj = (PID_Obj *)handle;
244
245 return(obj->Ui);
246 } // end of PID_getUi() function
247
248
253 extern PID_Handle PID_init(void *pMemory,const size_t numBytes);
254
255
266 void PID_setDerFilterParams(PID_Handle handle,
267 const float_t b0,const float_t b1,
268 const float_t a1,const float_t x1,const float_t y1);
269
270
274 static inline void PID_setFbackValue(PID_Handle handle,const float_t fbackValue)
275 {
276 PID_Obj *obj = (PID_Obj *)handle;
277
278 obj->fbackValue = fbackValue;
279
280 return;
281 } // end of PID_setFbackValue() function
282
283
287 static inline void PID_setFfwdValue(PID_Handle handle,const float_t ffwdValue)
288 {
289 PID_Obj *obj = (PID_Obj *)handle;
290
291 obj->ffwdValue = ffwdValue;
292
293 return;
294 } // end of PID_setFfwdValue() function
295
296
302 static inline void PID_setGains(PID_Handle handle,const float_t Kp,const float_t Ki, const float_t Kd)
303 {
304 PID_Obj *obj = (PID_Obj *)handle;
305
306 obj->Kp = Kp;
307 obj->Ki = Ki;
308 obj->Kd = Kd;
309
310 return;
311 } // end of PID_setGains() function
312
313
317 static inline void PID_setKd(PID_Handle handle,const float_t Kd)
318 {
319 PID_Obj *obj = (PID_Obj *)handle;
320
321 obj->Kd = Kd;
322
323 return;
324 } // end of PID_setKd() function
325
326
330 static inline void PID_setKi(PID_Handle handle,const float_t Ki)
331 {
332 PID_Obj *obj = (PID_Obj *)handle;
333
334 obj->Ki = Ki;
335
336 return;
337 } // end of PID_setKi() function
338
339
343 static inline void PID_setKp(PID_Handle handle,const float_t Kp)
344 {
345 PID_Obj *obj = (PID_Obj *)handle;
346
347 obj->Kp = Kp;
348
349 return;
350 } // end of PID_setKp() function
351
352
357 static inline void PID_setMinMax(PID_Handle handle,const float_t outMin,const float_t outMax)
358 {
359 PID_Obj *obj = (PID_Obj *)handle;
360
361 obj->outMin = outMin;
362 obj->outMax = outMax;
363
364 return;
365 } // end of PID_setMinMax() function
366
367
371 static inline void PID_setOutMax(PID_Handle handle,const float_t outMax)
372 {
373 PID_Obj *obj = (PID_Obj *)handle;
374
375 obj->outMax = outMax;
376
377 return;
378 } // end of PID_setOutMax() function
379
380
384 static inline void PID_setOutMin(PID_Handle handle,const float_t outMin)
385 {
386 PID_Obj *obj = (PID_Obj *)handle;
387
388 obj->outMin = outMin;
389
390 return;
391 } // end of PID_setOutMin() function
392
393
397 static inline void PID_setRefValue(PID_Handle handle,const float_t refValue)
398 {
399 PID_Obj *obj = (PID_Obj *)handle;
400
401 obj->refValue = refValue;
402
403 return;
404 } // end of PID_setRefValue() function
405
406
410 static inline void PID_setUi(PID_Handle handle,const float_t Ui)
411 {
412 PID_Obj *obj = (PID_Obj *)handle;
413
414 obj->Ui = Ui;
415
416 return;
417 } // end of PID_setUi() function
418
419
426 static inline void PID_run_parallel(PID_Handle handle,const float_t refValue,const float_t fbackValue,
427 const float_t ffwdValue,float_t *pOutValue)
428 {
429 PID_Obj *obj = (PID_Obj *)handle;
430
431 float_t Error;
432 float_t Kp = PID_getKp(handle);
433 float_t Ki = PID_getKi(handle);
434 float_t Kd = PID_getKd(handle);
435 float_t Up;
436 float_t Ui = PID_getUi(handle);
437 float_t Ud_tmp,Ud;
438 float_t outMax = PID_getOutMax(handle);
439 float_t outMin = PID_getOutMin(handle);
440
441 Error = refValue - fbackValue;
442
443 Up = Kp * Error; // Compute the proportional output
444 Ui = MATH_sat(Ui + (Ki * Error),outMax,outMin); // Compute the integral output
445
446 Ud_tmp = Kd * Error; // Compute the derivative term
447 Ud = FILTER_FO_run(obj->derFilterHandle,Ud_tmp);
448
449 PID_setUi(handle,Ui);
450 PID_setRefValue(handle,refValue);
451 PID_setFbackValue(handle,fbackValue);
452 PID_setFfwdValue(handle,ffwdValue);
453
454 *pOutValue = MATH_sat(Up + Ui + Ud + ffwdValue,outMax,outMin); // Saturate the output
455
456 return;
457 } // end of PID_run_parallel() function
458
459
466 static inline void PID_run_series(PID_Handle handle,const float_t refValue,const float_t fbackValue,
467 const float_t ffwdValue,float_t *pOutValue)
468 {
469 PID_Obj *obj = (PID_Obj *)handle;
470
471 float_t Error;
472 float_t Kp = PID_getKp(handle);
473 float_t Ki = PID_getKi(handle);
474 float_t Kd = PID_getKd(handle);
475 float_t Up;
476 float_t Ui = PID_getUi(handle);
477 float_t Ud_tmp,Ud;
478 float_t outMax = PID_getOutMax(handle);
479 float_t outMin = PID_getOutMin(handle);
480
481
482 Error = refValue - fbackValue;
483
484 Up = Kp * Error; // Compute the proportional output
485 Ui = MATH_sat(Ui + (Ki * Up),outMax,outMin); // Compute the integral output with saturation
486
487 Ud_tmp = Kd * Ui; // Compute the derivative term
488 Ud = FILTER_FO_run(obj->derFilterHandle,Ud_tmp);
489
490 PID_setUi(handle,Ui);
491 PID_setRefValue(handle,refValue);
492 PID_setFbackValue(handle,fbackValue);
493 PID_setFfwdValue(handle,ffwdValue);
494
495 *pOutValue = MATH_sat(Up + Ui + Ud + ffwdValue,outMax,outMin); // Saturate the output
496
497 return;
498 } // end of PID_run_series() function
499
500
501 #ifdef __cplusplus
502 }
503 #endif // extern "C"
504
506 #endif //end of _PID_H_ definition
507
filter_fo.h
Contains the public interface to the first-order filter (FILTER_FO) module routines.
types.h
Contains the public interface to the types definitions.
PID_getUi
static float_t PID_getUi(PID_Handle handle)
Gets the integrator start value in the PID controller.
Definition: float/pid.h:241
PID_setGains
static void PID_setGains(PID_Handle handle, const float_t Kp, const float_t Ki, const float_t Kd)
Sets the gains in the PID controller.
Definition: float/pid.h:302
PID_setFbackValue
static void PID_setFbackValue(PID_Handle handle, const float_t fbackValue)
Sets the feedback value in the PID controller.
Definition: float/pid.h:274
PID_setKi
static void PID_setKi(PID_Handle handle, const float_t Ki)
Sets the integral gain in the PID controller.
Definition: float/pid.h:330
PID_setMinMax
static void PID_setMinMax(PID_Handle handle, const float_t outMin, const float_t outMax)
Sets the minimum and maximum output value allowed in the PID controller.
Definition: float/pid.h:357
PID_getKd
static float_t PID_getKd(PID_Handle handle)
Gets the derivative gain in the PID controller.
Definition: float/pid.h:160
PID_getKi
static float_t PID_getKi(PID_Handle handle)
Gets the integral gain in the PID controller.
Definition: float/pid.h:171
PID_setRefValue
static void PID_setRefValue(PID_Handle handle, const float_t refValue)
Sets the reference value in the PID controller.
Definition: float/pid.h:397
math.h
Contains the public interface to the math (MATH) module routines.
_PID_Obj_::Kd
_iq Kd
the derivative gain for the PID controller
Definition: 32b/pid.h:78
_PID_Obj_::Ki
_iq Ki
the integral gain for the PID controller
Definition: 32b/pid.h:77
PID_getOutMin
static float_t PID_getOutMin(PID_Handle handle)
Gets the minimum output value allowed in the PID controller.
Definition: float/pid.h:219
PID_setOutMax
static void PID_setOutMax(PID_Handle handle, const float_t outMax)
Sets the maximum output value allowed in the PID controller.
Definition: float/pid.h:371
PID_setKd
static void PID_setKd(PID_Handle handle, const float_t Kd)
Sets the derivative gain in the PID controller.
Definition: float/pid.h:317
PID_setKp
static void PID_setKp(PID_Handle handle, const float_t Kp)
Sets the proportional gain in the PID controller.
Definition: float/pid.h:343
_PID_Obj_::outMax
float_t outMax
the maximum output value allowed for the PID controller
Definition: float/pid.h:88
PID_getGains
static void PID_getGains(PID_Handle handle, float_t *pKp, float_t *pKi, float_t *pKd)
Gets the gains in the PID controller.
Definition: float/pid.h:145
PID_setFfwdValue
static void PID_setFfwdValue(PID_Handle handle, const float_t ffwdValue)
Sets the feedforward value in the PID controller.
Definition: float/pid.h:287
PID_Obj
struct _PID_Obj_ PID_Obj
Defines the PID controller object.
_PID_Obj_::refValue
float_t refValue
the reference input value
Definition: float/pid.h:83
PID_getRefValue
static float_t PID_getRefValue(PID_Handle handle)
Gets the reference value in the PID controller.
Definition: float/pid.h:230
PID_setDerFilterParams
void PID_setDerFilterParams(PID_Handle handle, const float_t b0, const float_t b1, const float_t a1, const float_t x1, const float_t y1)
Sets the derivative filter parameters.
Definition: float/pid.c:94
FILTER_FO_run
static _iq FILTER_FO_run(FILTER_FO_Handle handle, const _iq inputValue)
Runs a first-order filter of the form y[n] = b0*x[n] + b1*x[n-1] - a1*y[n-1].
Definition: 32b/filter_fo.h:189
_FILTER_FO_
Defines the first-order filter (FILTER_FO) object.
Definition: 32b/filter_fo.h:71
PID_getOutMax
static float_t PID_getOutMax(PID_Handle handle)
Gets the maximum output value allowed in the PID controller.
Definition: float/pid.h:208
_PID_Obj_::Ki
float_t Ki
the integral gain for the PID controller
Definition: float/pid.h:78
PID_getMinMax
static void PID_getMinMax(PID_Handle handle, float_t *pOutMin, float_t *pOutMax)
Gets the minimum and maximum output value allowed in the PID controller.
Definition: float/pid.h:194
MATH_sat
static float_t MATH_sat(const float_t in, const float_t max, const float_t min)
Saturates the input value between the minimum and maximum values.
Definition: float/math.h:218
PID_run_parallel
static void PID_run_parallel(PID_Handle handle, const float_t refValue, const float_t fbackValue, const float_t ffwdValue, float_t *pOutValue)
Runs the parallel form of the PID controller.
Definition: float/pid.h:426
_PID_Obj_::fbackValue
_iq fbackValue
the feedback input value
Definition: 32b/pid.h:83
_PID_Obj_::fbackValue
float_t fbackValue
the feedback input value
Definition: float/pid.h:84
_PID_Obj_::derFilterHandle
FILTER_FO_Handle derFilterHandle
the derivative filter handle
Definition: float/pid.h:90
PID_getFfwdValue
static float_t PID_getFfwdValue(PID_Handle handle)
Gets the feedforward value in the PID controller.
Definition: float/pid.h:132
_PID_Obj_::outMin
_iq outMin
the minimum output value allowed for the PID controller
Definition: 32b/pid.h:85
PID_Handle
struct _PID_Obj_ * PID_Handle
Defines the PID handle.
Definition: float/pid.h:97
PID_setOutMin
static void PID_setOutMin(PID_Handle handle, const float_t outMin)
Sets the minimum output value allowed in the PID controller.
Definition: float/pid.h:384
_PID_Obj_::ffwdValue
float_t ffwdValue
the feedforward input value
Definition: float/pid.h:85
_PID_Obj_::Ui
float_t Ui
the integrator start value for the PID controller
Definition: float/pid.h:81
_PID_Obj_::derFilter
FILTER_FO_Obj derFilter
the derivative filter object
Definition: float/pid.h:91
PID_init
PID_Handle PID_init(void *pMemory, const size_t numBytes)
Initializes the PID controller.
Definition: 32b/pid.c:56
PID_getFbackValue
static float_t PID_getFbackValue(PID_Handle handle)
Gets the feedback value in the PID controller.
Definition: float/pid.h:121
PID_getKp
static float_t PID_getKp(PID_Handle handle)
Gets the proportional gain in the PID controller.
Definition: float/pid.h:182
_PID_Obj_::Kp
float_t Kp
the proportional gain for the PID controller
Definition: float/pid.h:77
PID_setUi
static void PID_setUi(PID_Handle handle, const float_t Ui)
Sets the integrator start value in the PID controller.
Definition: float/pid.h:410
PID_getDerFilterParams
void PID_getDerFilterParams(PID_Handle handle, float_t *b0, float_t *b1, float_t *a1, float_t *x1, float_t *y1)
Gets the derivative filter parameters.
Definition: float/pid.c:56
_PID_Obj_::refValue
_iq refValue
the reference input value
Definition: 32b/pid.h:82
_PID_Obj_::outMax
_iq outMax
the maximum output value allowed for the PID controller
Definition: 32b/pid.h:86
_PID_Obj_::Ui
_iq Ui
the integrator start value for the PID controller
Definition: 32b/pid.h:80
_PID_Obj_::Kd
float_t Kd
the derivative gain for the PID controller
Definition: float/pid.h:79
_PID_Obj_::outMin
float_t outMin
the minimum output value allowed for the PID controller
Definition: float/pid.h:87
_PID_Obj_::Kp
_iq Kp
the proportional gain for the PID controller
Definition: 32b/pid.h:76
_FILTER_FO_Obj_
Defines the first-order filter (FILTER_FO) object.
Definition: float/filter_fo.h:69
_PID_Obj_
Defines the PID controller object.
Definition: 32b/pid.h:74
PID_run_series
static void PID_run_series(PID_Handle handle, const float_t refValue, const float_t fbackValue, const float_t ffwdValue, float_t *pOutValue)
Runs the series form of the PID controller.
Definition: float/pid.h:466
float_t
float float_t
Defines the portable data type for 32 bit, signed floating-point data.
Definition: types.h:121