MotorWare f2806x Module API Documentation
32b/pid.h
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2012, 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
48 
49 
54 
55 
56 // Include the algorithm overview defined in modules/<module>/docs/doxygen/doxygen.h
58 
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
63 
64 // **************************************************************************
65 // the defines
66 
67 
68 
69 // **************************************************************************
70 // the typedefs
71 
74 typedef struct _PID_Obj_
75 {
76  _iq Kp;
77  _iq Ki;
78  _iq Kd;
79 
80  _iq Ui;
81 
84 
87 
88 } PID_Obj;
89 
90 
93 typedef struct _PID_Obj_ *PID_Handle;
94 
95 
96 // **************************************************************************
97 // the function prototypes
98 
102 static inline _iq PID_getFbackValue(PID_Handle handle)
103 {
104  PID_Obj *obj = (PID_Obj *)handle;
105 
106  return(obj->fbackValue);
107 } // end of PID_getFbackValue() function
108 
109 
115 static inline void PID_getGains(PID_Handle handle,_iq *pKp,_iq *pKi,_iq *pKd)
116 {
117  PID_Obj *obj = (PID_Obj *)handle;
118 
119  *pKp = obj->Kp;
120  *pKi = obj->Ki;
121  *pKd = obj->Kd;
122 
123  return;
124 } // end of PID_getGains() function
125 
126 
130 static inline _iq PID_getKd(PID_Handle handle)
131 {
132  PID_Obj *obj = (PID_Obj *)handle;
133 
134  return(obj->Kd);
135 } // end of PID_getKd() function
136 
137 
141 static inline _iq PID_getKi(PID_Handle handle)
142 {
143  PID_Obj *obj = (PID_Obj *)handle;
144 
145  return(obj->Ki);
146 } // end of PID_getKi() function
147 
148 
152 static inline _iq PID_getKp(PID_Handle handle)
153 {
154  PID_Obj *obj = (PID_Obj *)handle;
155 
156  return(obj->Kp);
157 } // end of PID_getKp() function
158 
159 
164 static inline void PID_getMinMax(PID_Handle handle,_iq *pOutMin,_iq *pOutMax)
165 {
166  PID_Obj *obj = (PID_Obj *)handle;
167 
168  *pOutMin = obj->outMin;
169  *pOutMax = obj->outMax;
170 
171  return;
172 } // end of PID_getMinMax() function
173 
174 
178 static inline _iq PID_getOutMax(PID_Handle handle)
179 {
180  PID_Obj *obj = (PID_Obj *)handle;
181 
182  return(obj->outMax);
183 } // end of PID_getOutMax() function
184 
185 
189 static inline _iq PID_getOutMin(PID_Handle handle)
190 {
191  PID_Obj *obj = (PID_Obj *)handle;
192 
193  return(obj->outMin);
194 } // end of PID_getOutMin() function
195 
196 
200 static inline _iq PID_getRefValue(PID_Handle handle)
201 {
202  PID_Obj *obj = (PID_Obj *)handle;
203 
204  return(obj->refValue);
205 } // end of PID_getRefValue() function
206 
207 
211 static inline _iq PID_getUi(PID_Handle handle)
212 {
213  PID_Obj *obj = (PID_Obj *)handle;
214 
215  return(obj->Ui);
216 } // end of PID_getUi() function
217 
218 
223 extern PID_Handle PID_init(void *pMemory,const size_t numBytes);
224 
225 
231 static inline void PID_run(PID_Handle handle,const _iq refValue,const _iq fbackValue,_iq *pOutValue)
232 {
233  PID_Obj *obj = (PID_Obj *)handle;
234 
235  _iq Error;
236  _iq Up,Ui;
237 
238 
239  Error = refValue - fbackValue;
240 
241  Ui = obj->Ui; // load the previous integral output
242  Up = _IQmpy(obj->Kp,Error); // Compute the proportional output
243  Ui = _IQsat(Ui + _IQmpy(obj->Ki,Up),obj->outMax,obj->outMin); // Compute the integral output
244 
245  obj->Ui = Ui; // store the intetral output
246  obj->refValue = refValue;
247  obj->fbackValue = fbackValue;
248 
249  *pOutValue = _IQsat(Up + Ui,obj->outMax,obj->outMin); // Saturate the output
250 
251  return;
252 } // end of PID_run() function
253 
254 
260 static inline void PID_run_spd(PID_Handle handle,const _iq refValue,const _iq fbackValue,_iq *pOutValue)
261 {
262  PID_Obj *obj = (PID_Obj *)handle;
263 
264  _iq Error;
265  _iq Up,Ui;
266 
267  Error = refValue - fbackValue;
268 
269  Ui = obj->Ui; // load the previous integral output
270  Up = _IQmpy(obj->Kp,Error); // Compute the proportional output
271  Ui = _IQsat(Ui + _IQmpy(obj->Ki,Error),obj->outMax,obj->outMin); // Compute the integral output
272 
273  obj->Ui = Ui; // store the intetral output
274  obj->refValue = refValue;
275  obj->fbackValue = fbackValue;
276 
277  *pOutValue = _IQsat(Up + Ui,obj->outMax,obj->outMin); // Saturate the output
278 
279  return;
280 } // end of PID_run_spd() function
281 
282 
286 static inline void PID_setFbackValue(PID_Handle handle,const _iq fbackValue)
287 {
288  PID_Obj *obj = (PID_Obj *)handle;
289 
290  obj->fbackValue = fbackValue;
291 
292  return;
293 } // end of PID_setFbackValue() function
294 
295 
301 static inline void PID_setGains(PID_Handle handle,const _iq Kp,const _iq Ki,const _iq Kd)
302 {
303  PID_Obj *obj = (PID_Obj *)handle;
304 
305  obj->Kp = Kp;
306  obj->Ki = Ki;
307  obj->Kd = Kd;
308 
309  return;
310 } // end of PID_setGains() function
311 
312 
316 static inline void PID_setKd(PID_Handle handle,const _iq Kd)
317 {
318  PID_Obj *obj = (PID_Obj *)handle;
319 
320  obj->Kd = Kd;
321 
322  return;
323 } // end of PID_setKd() function
324 
325 
329 static inline void PID_setKi(PID_Handle handle,const _iq Ki)
330 {
331  PID_Obj *obj = (PID_Obj *)handle;
332 
333  obj->Ki = Ki;
334 
335  return;
336 } // end of PID_setKi() function
337 
338 
342 static inline void PID_setKp(PID_Handle handle,const _iq Kp)
343 {
344  PID_Obj *obj = (PID_Obj *)handle;
345 
346  obj->Kp = Kp;
347 
348  return;
349 } // end of PID_setKp() function
350 
351 
356 static inline void PID_setMinMax(PID_Handle handle,const _iq outMin,const _iq outMax)
357 {
358  PID_Obj *obj = (PID_Obj *)handle;
359 
360  obj->outMin = outMin;
361  obj->outMax = outMax;
362 
363  return;
364 } // end of PID_setMinMax() function
365 
366 
370 static inline void PID_setOutMax(PID_Handle handle,const _iq outMax)
371 {
372  PID_Obj *obj = (PID_Obj *)handle;
373 
374  obj->outMax = outMax;
375 
376  return;
377 } // end of PID_setOutMax() function
378 
379 
383 static inline void PID_setOutMin(PID_Handle handle,const _iq outMin)
384 {
385  PID_Obj *obj = (PID_Obj *)handle;
386 
387  obj->outMin = outMin;
388 
389  return;
390 } // end of PID_setOutMin() function
391 
392 
396 static inline void PID_setRefValue(PID_Handle handle,const _iq refValue)
397 {
398  PID_Obj *obj = (PID_Obj *)handle;
399 
400  obj->refValue = refValue;
401 
402  return;
403 } // end of PID_setRefValue() function
404 
405 
409 static inline void PID_setUi(PID_Handle handle,const _iq Ui)
410 {
411  PID_Obj *obj = (PID_Obj *)handle;
412 
413  obj->Ui = Ui;
414 
415  return;
416 } // end of PID_setUi() function
417 
418 
419 #ifdef __cplusplus
420 }
421 #endif // extern "C"
422 
424 #endif //end of _PID_H_ definition
425 
static _iq PID_getRefValue(PID_Handle handle)
Gets the reference value in the PID controller.
Definition: 32b/pid.h:200
Contains the public interface to the types definitions.
static void PID_setKi(PID_Handle handle, const _iq Ki)
Sets the integral gain in the PID controller.
Definition: 32b/pid.h:329
static void PID_setUi(PID_Handle handle, const _iq Ui)
Sets the integrator start value in the PID controller.
Definition: 32b/pid.h:409
static _iq PID_getKp(PID_Handle handle)
Gets the proportional gain in the PID controller.
Definition: 32b/pid.h:152
static _iq PID_getOutMax(PID_Handle handle)
Gets the maximum output value allowed in the PID controller.
Definition: 32b/pid.h:178
_iq Kd
the derivative gain for the PID controller
Definition: 32b/pid.h:78
_iq Ki
the integral gain for the PID controller
Definition: 32b/pid.h:77
static void PID_getMinMax(PID_Handle handle, _iq *pOutMin, _iq *pOutMax)
Gets the minimum and maximum output value allowed in the PID controller.
Definition: 32b/pid.h:164
struct _PID_Obj_ PID_Obj
Defines the PID controller object.
static void PID_setMinMax(PID_Handle handle, const _iq outMin, const _iq outMax)
Sets the minimum and maximum output value allowed in the PID controller.
Definition: 32b/pid.h:356
static void PID_setRefValue(PID_Handle handle, const _iq refValue)
Sets the reference value in the PID controller.
Definition: 32b/pid.h:396
static _iq PID_getFbackValue(PID_Handle handle)
Gets the feedback value in the PID controller.
Definition: 32b/pid.h:102
long _iq
static _iq PID_getUi(PID_Handle handle)
Gets the integrator start value in the PID controller.
Definition: 32b/pid.h:211
static void PID_setGains(PID_Handle handle, const _iq Kp, const _iq Ki, const _iq Kd)
Sets the gains in the PID controller.
Definition: 32b/pid.h:301
_iq fbackValue
the feedback input value
Definition: 32b/pid.h:83
#define _IQmpy(A, B)
static void PID_setOutMin(PID_Handle handle, const _iq outMin)
Sets the minimum output value allowed in the PID controller.
Definition: 32b/pid.h:383
static void PID_getGains(PID_Handle handle, _iq *pKp, _iq *pKi, _iq *pKd)
Gets the gains in the PID controller.
Definition: 32b/pid.h:115
_iq outMin
the minimum output value allowed for the PID controller
Definition: 32b/pid.h:85
struct _PID_Obj_ * PID_Handle
Defines the PID handle.
Definition: 32b/pid.h:93
static _iq PID_getOutMin(PID_Handle handle)
Gets the minimum output value allowed in the PID controller.
Definition: 32b/pid.h:189
PID_Handle PID_init(void *pMemory, const size_t numBytes)
Initializes the PID controller.
Definition: 32b/pid.c:56
_iq refValue
the reference input value
Definition: 32b/pid.h:82
_iq outMax
the maximum output value allowed for the PID controller
Definition: 32b/pid.h:86
_iq Ui
the integrator start value for the PID controller
Definition: 32b/pid.h:80
static void PID_run_spd(PID_Handle handle, const _iq refValue, const _iq fbackValue, _iq *pOutValue)
Runs the PID controller for speed.
Definition: 32b/pid.h:260
static void PID_setOutMax(PID_Handle handle, const _iq outMax)
Sets the maximum output value allowed in the PID controller.
Definition: 32b/pid.h:370
_iq Kp
the proportional gain for the PID controller
Definition: 32b/pid.h:76
#define _IQsat(A, Pos, Neg)
static _iq PID_getKd(PID_Handle handle)
Gets the derivative gain in the PID controller.
Definition: 32b/pid.h:130
static void PID_setKd(PID_Handle handle, const _iq Kd)
Sets the derivative gain in the PID controller.
Definition: 32b/pid.h:316
Defines the PID controller object.
Definition: 32b/pid.h:74
static void PID_setFbackValue(PID_Handle handle, const _iq fbackValue)
Sets the feedback value in the PID controller.
Definition: 32b/pid.h:286
static void PID_setKp(PID_Handle handle, const _iq Kp)
Sets the proportional gain in the PID controller.
Definition: 32b/pid.h:342
static _iq PID_getKi(PID_Handle handle)
Gets the integral gain in the PID controller.
Definition: 32b/pid.h:141
static void PID_run(PID_Handle handle, const _iq refValue, const _iq fbackValue, _iq *pOutValue)
Runs the PID controller.
Definition: 32b/pid.h:231