pi.h
Go to the documentation of this file.
1 //#############################################################################
2 //
3 // FILE: pi.h
4 //
5 // TITLE: C28x InstaSPIN Proportional-Integral (PI) controller library
6 // (floating point)
7 //
8 //#############################################################################
9 // $TI Release: MotorControl SDK v3.03.00.00 $
10 // $Release Date: Tue Sep 21 16:33:26 CDT 2021 $
11 // $Copyright:
12 // Copyright (C) 2017-2018 Texas Instruments Incorporated - http://www.ti.com/
13 //
14 // Redistribution and use in source and binary forms, with or without
15 // modification, are permitted provided that the following conditions
16 // are met:
17 //
18 // Redistributions of source code must retain the above copyright
19 // notice, this list of conditions and the following disclaimer.
20 //
21 // Redistributions in binary form must reproduce the above copyright
22 // notice, this list of conditions and the following disclaimer in the
23 // documentation and/or other materials provided with the
24 // distribution.
25 //
26 // Neither the name of Texas Instruments Incorporated nor the names of
27 // its contributors may be used to endorse or promote products derived
28 // from this software without specific prior written permission.
29 //
30 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 // $
42 //#############################################################################
43 
44 #ifndef PI_H
45 #define PI_H
46 
47 //*****************************************************************************
48 //
49 // If building with a C++ compiler, make all of the definitions in this header
50 // have a C binding.
51 //
52 //*****************************************************************************
53 #ifdef __cplusplus
54 extern "C"
55 {
56 #endif
57 
58 //*****************************************************************************
59 //
62 //
63 //*****************************************************************************
64 
65 #include "types.h"
67 
68 //*****************************************************************************
69 //
71 //
72 //*****************************************************************************
73 typedef struct _PI_Obj_
74 {
85 } PI_Obj;
87 
88 //*****************************************************************************
89 //
91 //
92 //*****************************************************************************
93 typedef struct _PI_Obj_ *PI_Handle;
94 
95 //*****************************************************************************
96 //
102 //
103 //*****************************************************************************
104 static inline float32_t
106 {
107  PI_Obj *obj = (PI_Obj *)handle;
108 
109  return(obj->fbackValue);
110 } // end of PI_getFbackValue() function
111 
112 //*****************************************************************************
113 //
119 //
120 //*****************************************************************************
121 static inline float32_t
123 {
124  PI_Obj *obj = (PI_Obj *)handle;
125 
126  return(obj->ffwdValue);
127 } // end of PI_getFfwdValue() function
128 
129 //*****************************************************************************
130 //
140 //
141 //*****************************************************************************
142 static inline void
144 {
145  PI_Obj *obj = (PI_Obj *)handle;
146 
147  *pKp = obj->Kp;
148  *pKi = obj->Ki;
149 
150  return;
151 } // end of PI_getGains() function
152 
153 //*****************************************************************************
154 //
160 //
161 //*****************************************************************************
162 static inline float32_t
164 {
165  PI_Obj *obj = (PI_Obj *)handle;
166 
167  return(obj->Ki);
168 } // end of PI_getKi() function
169 
170 //*****************************************************************************
171 //
177 //
178 //*****************************************************************************
179 static inline float32_t
181 {
182  PI_Obj *obj = (PI_Obj *)handle;
183 
184  return(obj->Kp);
185 } // end of PI_getKp() function
186 
187 //*****************************************************************************
188 //
199 //
200 //*****************************************************************************
201 static inline void
202 PI_getMinMax(PI_Handle handle, float32_t *pOutMin, float32_t *pOutMax)
203 {
204  PI_Obj *obj = (PI_Obj *)handle;
205 
206  *pOutMin = obj->outMin;
207  *pOutMax = obj->outMax;
208 
209  return;
210 } // end of PI_getMinMax() function
211 
212 //*****************************************************************************
213 //
219 //
220 //*****************************************************************************
221 static inline float32_t
223 {
224  PI_Obj *obj = (PI_Obj *)handle;
225 
226  return(obj->outMax);
227 } // end of PI_getOutMax() function
228 
229 //*****************************************************************************
230 //
236 //
237 //*****************************************************************************
238 static inline float32_t
240 {
241  PI_Obj *obj = (PI_Obj *)handle;
242 
243  return(obj->outMin);
244 } // end of PI_getOutMin() function
245 
246 //*****************************************************************************
247 //
253 //
254 //*****************************************************************************
255 static inline float32_t
257 {
258  PI_Obj *obj = (PI_Obj *)handle;
259 
260  return(obj->refValue);
261 } // end of PI_getRefValue() function
262 
263 //*****************************************************************************
264 //
270 //
271 //*****************************************************************************
272 static inline float32_t
274 {
275  PI_Obj *obj = (PI_Obj *)handle;
276 
277  return(obj->Ui);
278 } // end of PI_getUi() function
279 
280 //*****************************************************************************
281 //
290 //
291 //*****************************************************************************
292 extern PI_Handle
293 PI_init(void *pMemory, const size_t numBytes);
294 
295 extern PI_Handle
296 cla_PI_init(void *pMemory, const size_t numBytes);
297 
298 //*****************************************************************************
299 //
307 //
308 //*****************************************************************************
309 static inline void
311 {
312  PI_Obj *obj = (PI_Obj *)handle;
313 
314  obj->fbackValue = fbackValue;
315 
316  return;
317 } // end of PI_setFbackValue() function
318 
319 //*****************************************************************************
320 //
328 //
329 //*****************************************************************************
330 static inline void
332 {
333  PI_Obj *obj = (PI_Obj *)handle;
334 
335  obj->ffwdValue = ffwdValue;
336 
337  return;
338 } // end of PI_setFfwdValue() function
339 
340 //*****************************************************************************
341 //
351 //
352 //*****************************************************************************
353 static inline void
355 {
356  PI_Obj *obj = (PI_Obj *)handle;
357 
358  obj->Kp = Kp;
359  obj->Ki = Ki;
360 
361  return;
362 } // end of PI_setGains() function
363 
364 //*****************************************************************************
365 //
373 //
374 //*****************************************************************************
375 static inline void
377 {
378  PI_Obj *obj = (PI_Obj *)handle;
379 
380  obj->Ki = Ki;
381 
382  return;
383 } // end of PI_setKi() function
384 
385 //*****************************************************************************
386 //
394 //
395 //*****************************************************************************
396 static inline void
398 {
399  PI_Obj *obj = (PI_Obj *)handle;
400 
401  obj->Kp = Kp;
402 
403  return;
404 } // end of PI_setKp() function
405 
406 //*****************************************************************************
407 //
418 //
419 //*****************************************************************************
420 static inline void
422 {
423  PI_Obj *obj = (PI_Obj *)handle;
424 
425  obj->outMin = outMin;
426  obj->outMax = outMax;
427 
428  return;
429 } // end of PI_setMinMax() function
430 
431 //*****************************************************************************
432 //
440 //
441 //*****************************************************************************
442 static inline void
444 {
445  PI_Obj *obj = (PI_Obj *)handle;
446 
447  obj->outMax = outMax;
448 
449  return;
450 } // end of PI_setOutMax() function
451 
452 //*****************************************************************************
453 //
461 //
462 //*****************************************************************************
463 static inline void
465 {
466  PI_Obj *obj = (PI_Obj *)handle;
467 
468  obj->outMin = outMin;
469 
470  return;
471 } // end of PI_setOutMin() function
472 
473 //*****************************************************************************
474 //
482 //
483 //*****************************************************************************
484 static inline void
486 {
487  PI_Obj *obj = (PI_Obj *)handle;
488 
489  obj->refValue = refValue;
490 
491  return;
492 } // end of PI_setRefValue() function
493 
494 //*****************************************************************************
495 //
503 //
504 //*****************************************************************************
505 static inline void
507 {
508  PI_Obj *obj = (PI_Obj *)handle;
509 
510  obj->Ui = Ui;
511 
512  return;
513 } // end of PI_setUi() function
514 
515 //*****************************************************************************
516 //
530 //
531 //*****************************************************************************
532 static inline void
535  float32_t *pOutValue)
536 {
537  float32_t Error;
538  float32_t Kp = PI_getKp(handle);
539  float32_t Ki = PI_getKi(handle);
540  float32_t Up;
541  float32_t Ui = PI_getUi(handle);
542  float32_t outMax = PI_getOutMax(handle);
543  float32_t outMin = PI_getOutMin(handle);
544 
545  Error = refValue - fbackValue;
546 
547  //
548  // Compute the proportional output
549  //
550  Up = Kp * Error;
551 
552  //
553  // Compute the integral output
554  //
555  Ui = MATH_sat(Ui + (Ki * Error),outMax,outMin);
556 
557  PI_setUi(handle,Ui);
558  PI_setRefValue(handle,refValue);
560  PI_setFfwdValue(handle,ffwdValue);
561 
562  //
563  // Saturate the output
564  //
565  *pOutValue = MATH_sat(Up + Ui + ffwdValue,outMax,outMin);
566 
567  return;
568 } // end of PI_run_parallel() function
569 
570 //*****************************************************************************
571 //
585 //
586 //*****************************************************************************
587 static inline void
590  float32_t *pOutValue)
591 {
592  float32_t Error;
593  float32_t Kp = PI_getKp(handle);
594  float32_t Ki = PI_getKi(handle);
595  float32_t Up;
596  float32_t Ui = PI_getUi(handle);
597  float32_t outMax = PI_getOutMax(handle);
598  float32_t outMin = PI_getOutMin(handle);
599 
600  Error = refValue - fbackValue;
601 
602  //
603  // Compute the proportional output
604  //
605  Up = Kp * Error;
606 
607  //
608  // Compute the integral output with saturation
609  //
610 #ifdef __TMS320C28XX_CLA__
611  Ui = MATH_sat(Ui + (Ki * Up), outMax, outMin);
612 #else
613  Ui = __fsat(Ui + (Ki * Up), outMax, outMin);
614 #endif // __TMS320C28XX_CLA__
615 
616  PI_setUi(handle,Ui);
617  PI_setRefValue(handle, refValue);
618  PI_setFbackValue(handle, fbackValue);
619  PI_setFfwdValue(handle, ffwdValue);
620 
621  //
622  // Saturate the output
623  //
624 #ifdef __TMS320C28XX_CLA__
625  *pOutValue = MATH_sat(Up + Ui + ffwdValue, outMax, outMin);
626 #else
627  *pOutValue = __fsat(Up + Ui + ffwdValue, outMax, outMin);
628 #endif // __TMS320C28XX_CLA__
629 
630  return;
631 } // end of PI_run_series() function
632 
633 
634 //*****************************************************************************
635 //
647 //
648 //*****************************************************************************
649 static inline void
651  const float32_t fbackValue, float32_t *pOutValue)
652 {
653  float32_t Error;
654  float32_t Kp = PI_getKp(handle);
655  float32_t Ki = PI_getKi(handle);
656  float32_t Up;
657  float32_t Ui = PI_getUi(handle);
658  float32_t outMax = PI_getOutMax(handle);
659  float32_t outMin = PI_getOutMin(handle);
660 
661  Error = refValue - fbackValue;
662 
663  // Compute the proportional output
664  Up = Kp * Error;
665 
666  // Compute the integral output with saturation
667 #ifdef __TMS320C28XX_CLA__
668  Ui = MATH_sat(Ui + (Ki * Up), outMax, outMin);
669 #else
670  Ui = __fsat(Ui + (Ki * Up), outMax, outMin);
671 #endif // __TMS320C28XX_CLA__
672 
673  PI_setUi(handle,Ui);
674  PI_setRefValue(handle, refValue);
675  PI_setFbackValue(handle, fbackValue);
676 
677  // Saturate the output
678 #ifdef __TMS320C28XX_CLA__
679  *pOutValue = MATH_sat(Up + Ui, outMax, outMin);
680 #else
681  *pOutValue = __fsat(Up + Ui, outMax, outMin);
682 #endif // __TMS320C28XX_CLA__
683 
684  return;
685 } // end of PI_run_series() function
686 
687 //*****************************************************************************
688 //
689 // Close the Doxygen group.
691 //
692 //*****************************************************************************
693 
694 //*****************************************************************************
695 //
696 // Mark the end of the C bindings section for C++ compilers.
697 //
698 //*****************************************************************************
699 #ifdef __cplusplus
700 }
701 #endif
702 
703 #endif // end of PI_H defines
_PI_Obj_::fbackValue
float32_t fbackValue
the feedback input value
Definition: pi.h:80
PI_getMinMax
static void PI_getMinMax(PI_Handle handle, float32_t *pOutMin, float32_t *pOutMax)
Gets the minimum and maximum output value allowed in the PI controller.
Definition: pi.h:202
_PI_Obj_::Ki
float32_t Ki
the integral gain for the PI controller
Definition: pi.h:76
float32_t
float float32_t
Definition: sfra_f32.h:42
types.h
_PI_Obj_::outMin
float32_t outMin
controller
Definition: pi.h:82
PI_setUi
static void PI_setUi(PI_Handle handle, const float32_t Ui)
Sets the integrator start value in the PI controller.
Definition: pi.h:506
PI_init
PI_Handle PI_init(void *pMemory, const size_t numBytes)
Initializes the PI controller.
PI_getUi
static float32_t PI_getUi(PI_Handle handle)
Gets the integrator start value in the PI controller.
Definition: pi.h:273
PI_getGains
static void PI_getGains(PI_Handle handle, float32_t *pKp, float32_t *pKi)
Gets the gains in the PI controller.
Definition: pi.h:143
PI_setKi
static void PI_setKi(PI_Handle handle, const float32_t Ki)
Sets the integral gain in the PI controller.
Definition: pi.h:376
PI_run_parallel
static void PI_run_parallel(PI_Handle handle, const float32_t refValue, const float32_t fbackValue, const float32_t ffwdValue, float32_t *pOutValue)
Runs the parallel form of the PI controller.
Definition: pi.h:533
PI_getKp
static float32_t PI_getKp(PI_Handle handle)
Gets the proportional gain in the PI controller.
Definition: pi.h:180
_PI_Obj_::refValue
float32_t refValue
the reference input value
Definition: pi.h:79
math.h
PI_setOutMin
static void PI_setOutMin(PI_Handle handle, const float32_t outMin)
Sets the minimum output value allowed in the PI controller.
Definition: pi.h:464
PI_getOutMax
static float32_t PI_getOutMax(PI_Handle handle)
Gets the maximum output value allowed in the PI controller.
Definition: pi.h:222
PI_Obj
struct _PI_Obj_ PI_Obj
Defines the PI controller object.
PI_getFbackValue
static float32_t PI_getFbackValue(PI_Handle handle)
Gets the feedback value in the PI controller.
Definition: pi.h:105
cla_PI_init
PI_Handle cla_PI_init(void *pMemory, const size_t numBytes)
_PI_Obj_::Ui
float32_t Ui
controller
Definition: pi.h:77
PI_setKp
static void PI_setKp(PI_Handle handle, const float32_t Kp)
Sets the proportional gain in the PI controller.
Definition: pi.h:397
PI_getOutMin
static float32_t PI_getOutMin(PI_Handle handle)
Gets the minimum output value allowed in the PI controller.
Definition: pi.h:239
MATH_sat
static float32_t MATH_sat(const float32_t in, const float32_t max, const float32_t min)
Saturates the input value between the minimum and maximum values.
Definition: math.h:364
PI_getKi
static float32_t PI_getKi(PI_Handle handle)
Gets the integral gain in the PI controller.
Definition: pi.h:163
_PI_Obj_::ffwdValue
float32_t ffwdValue
the feedforward input value
Definition: pi.h:81
_PI_Obj_
Defines the PI controller object.
Definition: pi.h:73
_PI_Obj_::outMax
float32_t outMax
controller
Definition: pi.h:84
PI_setGains
static void PI_setGains(PI_Handle handle, const float32_t Kp, const float32_t Ki)
Sets the gains in the PI controller.
Definition: pi.h:354
PI_run
static void PI_run(PI_Handle handle, const float32_t refValue, const float32_t fbackValue, float32_t *pOutValue)
Runs the series form of the PI controller.
Definition: pi.h:650
_PI_Obj_::Kp
float32_t Kp
the proportional gain for the PI controller
Definition: pi.h:75
PI_setFbackValue
static void PI_setFbackValue(PI_Handle handle, const float32_t fbackValue)
Sets the feedback value in the PI controller.
Definition: pi.h:310
PI_setFfwdValue
static void PI_setFfwdValue(PI_Handle handle, const float32_t ffwdValue)
Sets the feedforward value in the PI controller.
Definition: pi.h:331
PI_setOutMax
static void PI_setOutMax(PI_Handle handle, const float32_t outMax)
Sets the maximum output value allowed in the PI controller.
Definition: pi.h:443
PI_setMinMax
static void PI_setMinMax(PI_Handle handle, const float32_t outMin, const float32_t outMax)
Sets the minimum and maximum output value allowed in the PI controller.
Definition: pi.h:421
PI_getFfwdValue
static float32_t PI_getFfwdValue(PI_Handle handle)
Gets the feedforward value in the PI controller.
Definition: pi.h:122
PI_getRefValue
static float32_t PI_getRefValue(PI_Handle handle)
Gets the reference value in the PI controller.
Definition: pi.h:256
PI_Handle
struct _PI_Obj_ * PI_Handle
Defines the PI handle.
Definition: pi.h:93
PI_setRefValue
static void PI_setRefValue(PI_Handle handle, const float32_t refValue)
Sets the reference value in the PI controller.
Definition: pi.h:485
PI_run_series
static void PI_run_series(PI_Handle handle, const float32_t refValue, const float32_t fbackValue, const float32_t ffwdValue, float32_t *pOutValue)
Runs the series form of the PI controller.
Definition: pi.h:588

Copyright 2023, Texas Instruments Incorporated