MotorWare f2806x Module API Documentation
32b/pi.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 _PI_H_
33 #define _PI_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 _PI_Obj_
75 {
76  _iq Kp;
77  _iq Ki;
78 
79  _iq Ui;
80 
84 
87 
88 } PI_Obj;
89 
90 
93 typedef struct _PI_Obj_ *PI_Handle;
94 
95 
96 // **************************************************************************
97 // the function prototypes
98 
102 static inline _iq PI_getFbackValue(PI_Handle handle)
103 {
104  PI_Obj *obj = (PI_Obj *)handle;
105 
106  return(obj->fbackValue);
107 } // end of PI_getFbackValue() function
108 
109 
113 static inline _iq PI_getFfwdValue(PI_Handle handle)
114 {
115  PI_Obj *obj = (PI_Obj *)handle;
116 
117  return(obj->ffwdValue);
118 } // end of PI_getFfwdValue() function
119 
120 
125 static inline void PI_getGains(PI_Handle handle,_iq *pKp,_iq *pKi)
126 {
127  PI_Obj *obj = (PI_Obj *)handle;
128 
129  *pKp = obj->Kp;
130  *pKi = obj->Ki;
131 
132  return;
133 } // end of PI_getGains() function
134 
135 
139 static inline _iq PI_getKi(PI_Handle handle)
140 {
141  PI_Obj *obj = (PI_Obj *)handle;
142 
143  return(obj->Ki);
144 } // end of PI_getKi() function
145 
146 
150 static inline _iq PI_getKp(PI_Handle handle)
151 {
152  PI_Obj *obj = (PI_Obj *)handle;
153 
154  return(obj->Kp);
155 } // end of PI_getKp() function
156 
157 
162 static inline void PI_getMinMax(PI_Handle handle,_iq *pOutMin,_iq *pOutMax)
163 {
164  PI_Obj *obj = (PI_Obj *)handle;
165 
166  *pOutMin = obj->outMin;
167  *pOutMax = obj->outMax;
168 
169  return;
170 } // end of PI_getMinMax() function
171 
172 
176 static inline _iq PI_getOutMax(PI_Handle handle)
177 {
178  PI_Obj *obj = (PI_Obj *)handle;
179 
180  return(obj->outMax);
181 } // end of PI_getOutMax() function
182 
183 
187 static inline _iq PI_getOutMin(PI_Handle handle)
188 {
189  PI_Obj *obj = (PI_Obj *)handle;
190 
191  return(obj->outMin);
192 } // end of PI_getOutMin() function
193 
194 
198 static inline _iq PI_getRefValue(PI_Handle handle)
199 {
200  PI_Obj *obj = (PI_Obj *)handle;
201 
202  return(obj->refValue);
203 } // end of PI_getRefValue() function
204 
205 
209 static inline _iq PI_getUi(PI_Handle handle)
210 {
211  PI_Obj *obj = (PI_Obj *)handle;
212 
213  return(obj->Ui);
214 } // end of PI_getUi() function
215 
216 
221 extern PI_Handle PI_init(void *pMemory,const size_t numBytes);
222 
223 
227 static inline void PI_setFbackValue(PI_Handle handle,const _iq fbackValue)
228 {
229  PI_Obj *obj = (PI_Obj *)handle;
230 
231  obj->fbackValue = fbackValue;
232 
233  return;
234 } // end of PI_setFbackValue() function
235 
236 
240 static inline void PI_setFfwdValue(PI_Handle handle,const _iq ffwdValue)
241 {
242  PI_Obj *obj = (PI_Obj *)handle;
243 
244  obj->ffwdValue = ffwdValue;
245 
246  return;
247 } // end of PI_setFfwdValue() function
248 
249 
254 static inline void PI_setGains(PI_Handle handle,const _iq Kp,const _iq Ki)
255 {
256  PI_Obj *obj = (PI_Obj *)handle;
257 
258  obj->Kp = Kp;
259  obj->Ki = Ki;
260 
261  return;
262 } // end of PI_setGains() function
263 
264 
268 static inline void PI_setKi(PI_Handle handle,const _iq Ki)
269 {
270  PI_Obj *obj = (PI_Obj *)handle;
271 
272  obj->Ki = Ki;
273 
274  return;
275 } // end of PI_setKi() function
276 
277 
281 static inline void PI_setKp(PI_Handle handle,const _iq Kp)
282 {
283  PI_Obj *obj = (PI_Obj *)handle;
284 
285  obj->Kp = Kp;
286 
287  return;
288 } // end of PI_setKp() function
289 
290 
295 static inline void PI_setMinMax(PI_Handle handle,const _iq outMin,const _iq outMax)
296 {
297  PI_Obj *obj = (PI_Obj *)handle;
298 
299  obj->outMin = outMin;
300  obj->outMax = outMax;
301 
302  return;
303 } // end of PI_setMinMax() function
304 
305 
309 static inline void PI_setOutMax(PI_Handle handle,const _iq outMax)
310 {
311  PI_Obj *obj = (PI_Obj *)handle;
312 
313  obj->outMax = outMax;
314 
315  return;
316 } // end of PI_setOutMax() function
317 
318 
322 static inline void PI_setOutMin(PI_Handle handle,const _iq outMin)
323 {
324  PI_Obj *obj = (PI_Obj *)handle;
325 
326  obj->outMin = outMin;
327 
328  return;
329 } // end of PI_setOutMin() function
330 
331 
335 static inline void PI_setRefValue(PI_Handle handle,const _iq refValue)
336 {
337  PI_Obj *obj = (PI_Obj *)handle;
338 
339  obj->refValue = refValue;
340 
341  return;
342 } // end of PI_setRefValue() function
343 
344 
348 static inline void PI_setUi(PI_Handle handle,const _iq Ui)
349 {
350  PI_Obj *obj = (PI_Obj *)handle;
351 
352  obj->Ui = Ui;
353 
354  return;
355 } // end of PI_setUi() function
356 
357 
364 static inline void PI_run_parallel(PI_Handle handle,const _iq refValue,const _iq fbackValue,
365  const _iq ffwdValue,_iq *pOutValue)
366 {
367  _iq Error;
368  _iq Kp = PI_getKp(handle);
369  _iq Ki = PI_getKi(handle);
370  _iq Up;
371  _iq Ui = PI_getUi(handle);
372  _iq outMax = PI_getOutMax(handle);
373  _iq outMin = PI_getOutMin(handle);
374 
375  Error = refValue - fbackValue;
376 
377  Up = _IQmpy(Kp,Error); // Compute the proportional output
378  Ui = _IQsat(Ui + _IQmpy(Ki,Error),outMax,outMin); // Compute the integral output
379 
380  PI_setUi(handle,Ui);
381  PI_setRefValue(handle,refValue);
382  PI_setFbackValue(handle,fbackValue);
383  PI_setFfwdValue(handle,ffwdValue);
384 
385  *pOutValue = _IQsat(Up + Ui + ffwdValue,outMax,outMin); // Saturate the output
386 
387  return;
388 } // end of PI_run_parallel() function
389 
390 
397 static inline void PI_run_series(PI_Handle handle,const _iq refValue,const _iq fbackValue,
398  const _iq ffwdValue,_iq *pOutValue)
399 {
400  _iq Error;
401  _iq Kp = PI_getKp(handle);
402  _iq Ki = PI_getKi(handle);
403  _iq Up;
404  _iq Ui = PI_getUi(handle);
405  _iq outMax = PI_getOutMax(handle);
406  _iq outMin = PI_getOutMin(handle);
407 
408 
409  Error = refValue - fbackValue;
410 
411  Up = _IQmpy(Kp,Error); // Compute the proportional output
412 
413  Ui = _IQsat(Ui + _IQmpy(Ki,Up),outMax,outMin); // Compute the integral output with saturation
414 
415  PI_setUi(handle,Ui);
416  PI_setRefValue(handle,refValue);
417  PI_setFbackValue(handle,fbackValue);
418  PI_setFfwdValue(handle,ffwdValue);
419 
420  *pOutValue = _IQsat(Up + Ui + ffwdValue,outMax,outMin); // Saturate the output
421 
422  return;
423 } // end of PI_run_series() function
424 
425 
426 #ifdef __cplusplus
427 }
428 #endif // extern "C"
429 
431 #endif //end of _PI_H_ definition
432 
static _iq PI_getOutMin(PI_Handle handle)
Gets the minimum output value allowed in the PI controller.
Definition: 32b/pi.h:187
_iq ffwdValue
the feedforward input value
Definition: 32b/pi.h:83
Contains the public interface to the types definitions.
static void PI_setFfwdValue(PI_Handle handle, const _iq ffwdValue)
Sets the feedforward value in the PI controller.
Definition: 32b/pi.h:240
Defines the PI controller object.
Definition: 32b/pi.h:74
static _iq PI_getRefValue(PI_Handle handle)
Gets the reference value in the PI controller.
Definition: 32b/pi.h:198
static void PI_setUi(PI_Handle handle, const _iq Ui)
Sets the integrator start value in the PI controller.
Definition: 32b/pi.h:348
_iq Ui
the integrator start value for the PI controller
Definition: 32b/pi.h:79
static void PI_setOutMin(PI_Handle handle, const _iq outMin)
Sets the minimum output value allowed in the PI controller.
Definition: 32b/pi.h:322
_iq Ki
the integral gain for the PI controller
Definition: 32b/pi.h:77
long _iq
static void PI_setKp(PI_Handle handle, const _iq Kp)
Sets the proportional gain in the PI controller.
Definition: 32b/pi.h:281
static void PI_setKi(PI_Handle handle, const _iq Ki)
Sets the integral gain in the PI controller.
Definition: 32b/pi.h:268
static _iq PI_getKi(PI_Handle handle)
Gets the integral gain in the PI controller.
Definition: 32b/pi.h:139
#define _IQmpy(A, B)
PI_Handle PI_init(void *pMemory, const size_t numBytes)
Initializes the PI controller.
Definition: 32b/pi.c:56
_iq Kp
the proportional gain for the PI controller
Definition: 32b/pi.h:76
struct _PI_Obj_ PI_Obj
Defines the PI controller object.
_iq fbackValue
the feedback input value
Definition: 32b/pi.h:82
static _iq PI_getUi(PI_Handle handle)
Gets the integrator start value in the PI controller.
Definition: 32b/pi.h:209
static void PI_getMinMax(PI_Handle handle, _iq *pOutMin, _iq *pOutMax)
Gets the minimum and maximum output value allowed in the PI controller.
Definition: 32b/pi.h:162
static _iq PI_getFbackValue(PI_Handle handle)
Gets the feedback value in the PI controller.
Definition: 32b/pi.h:102
static void PI_setOutMax(PI_Handle handle, const _iq outMax)
Sets the maximum output value allowed in the PI controller.
Definition: 32b/pi.h:309
static void PI_run_parallel(PI_Handle handle, const _iq refValue, const _iq fbackValue, const _iq ffwdValue, _iq *pOutValue)
Runs the parallel form of the PI controller.
Definition: 32b/pi.h:364
_iq outMax
the maximum output value allowed for the PI controller
Definition: 32b/pi.h:86
_iq refValue
the reference input value
Definition: 32b/pi.h:81
static void PI_setMinMax(PI_Handle handle, const _iq outMin, const _iq outMax)
Sets the minimum and maximum output value allowed in the PI controller.
Definition: 32b/pi.h:295
static void PI_setRefValue(PI_Handle handle, const _iq refValue)
Sets the reference value in the PI controller.
Definition: 32b/pi.h:335
static _iq PI_getFfwdValue(PI_Handle handle)
Gets the feedforward value in the PI controller.
Definition: 32b/pi.h:113
struct _PI_Obj_ * PI_Handle
Defines the PI handle.
Definition: 32b/pi.h:93
static void PI_setFbackValue(PI_Handle handle, const _iq fbackValue)
Sets the feedback value in the PI controller.
Definition: 32b/pi.h:227
#define _IQsat(A, Pos, Neg)
static _iq PI_getKp(PI_Handle handle)
Gets the proportional gain in the PI controller.
Definition: 32b/pi.h:150
static _iq PI_getOutMax(PI_Handle handle)
Gets the maximum output value allowed in the PI controller.
Definition: 32b/pi.h:176
_iq outMin
the minimum output value allowed for the PI controller
Definition: 32b/pi.h:85
static void PI_getGains(PI_Handle handle, _iq *pKp, _iq *pKi)
Gets the gains in the PI controller.
Definition: 32b/pi.h:125
static void PI_setGains(PI_Handle handle, const _iq Kp, const _iq Ki)
Sets the gains in the PI controller.
Definition: 32b/pi.h:254
static void PI_run_series(PI_Handle handle, const _iq refValue, const _iq fbackValue, const _iq ffwdValue, _iq *pOutValue)
Runs the series form of the PI controller.
Definition: 32b/pi.h:397