AM243x Motor Control SDK  09.02.00
dcl_pi.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 Texas Instruments Incorporated
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * 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
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef _DCL_PI_H_
34 #define _DCL_PI_H_
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
48 #include "../dcl_common.h"
49 
50 //--- Linear PI controller ---------------------------------------------------
51 
55 typedef struct dcl_pi_sps
56 {
63 } DCL_PI_SPS;
64 
67 #define PI_SPS_DEFAULTS { 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, -1.0f }
68 
71 typedef _DCL_VOLATILE struct dcl_pi
72 {
73  /* controller parameter */
80 
81  /* internal storage */
85 
86  /* miscellaneous */
90 
93 #define PI_DEFAULTS { 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, \
94  &(DCL_PI_SPS)PI_SPS_DEFAULTS, &(DCL_CSS)DCL_CSS_DEFAULTS }
95 
106 #define PI_INT_DEFAULTS .i6=1.0f, .i10=0.0f, .i11=0.0f, \
107 .sps=&(DCL_PI_SPS)PI_SPS_DEFAULTS, .css=&(DCL_CSS)DCL_CSS_DEFAULTS
108 
114 #define DCL_initPI() &(DCL_PI)PI_DEFAULTS
115 
122 #define DCL_initPIasParam(kp,ki,umax,umin) &(DCL_PI){ .Kp=kp, .Ki=ki, \
123  .Umax=umax, .Umin=umin, PI_INT_DEFAULTS }
124 
135 #define DCL_initPIasSPS(pi_ptr,sps_ptr) \
136 ({ \
137  DCL_PI* new_pi = (pi_ptr) ? pi_ptr : DCL_initPI(); \
138  DCL_PI_SPS* new_sps = (sps_ptr) ? sps_ptr : &(DCL_PI_SPS)PI_SPS_DEFAULTS; \
139  if(sps_ptr) \
140  { \
141  *new_pi = (DCL_PI){ (new_sps)->Kp, (new_sps)->Ki, (new_sps)->Umax, (new_sps)->Umin,\
142  (new_sps)->Imax, (new_sps)->Imin, 1.0f, 0.0f, 0.0f, \
143  (DCL_PI_SPS*)new_sps, &(DCL_CSS)DCL_CSS_DEFAULTS }; \
144  } \
145  new_pi; \
146 })
147 
154 {
155  dcl_interrupt_t ints;
156  ints = DCL_disableInts();
157  pi->i6 = 1.0f;
158  pi->i10 = pi->i11 = 0.0f;
159  DCL_restoreInts(ints);
160 }
161 
168 {
169 
170 #ifdef DCL_ERROR_HANDLING_ENABLED
171  uint32_t err_code = dcl_none;
172  err_code |= (pi->sps->Umax <= pi->sps->Umin) ? dcl_param_invalid_err : dcl_none;
173  err_code |= (pi->css->T <= 0.0f) ? dcl_param_range_err : dcl_none;
174  err_code |= (pi->sps->Kp < 0.0f) ? dcl_param_range_err : dcl_none;
175  err_code |= (pi->sps->Ki < 0.0f) ? dcl_param_range_err : dcl_none;
176  if (err_code)
177  {
178  DCL_setError(pi,err_code);
179  DCL_getErrorInfo(pi);
181  }
182 #endif
183 
184  pi->Kp = pi->sps->Kp;
185  pi->Ki = pi->sps->Ki;
186  pi->Umax = pi->sps->Umax;
187  pi->Umin = pi->sps->Umin;
188  pi->Imax = pi->sps->Imax;
189  pi->Imin = pi->sps->Imin;
190 }
191 
198 {
199 
200 #ifdef DCL_ERROR_HANDLING_ENABLED
201  uint32_t err_code = dcl_none;
202  err_code |= (pi->sps->Umax <= pi->sps->Umin) ? dcl_param_invalid_err : dcl_none;
203  err_code |= (pi->css->T <= 0.0f) ? dcl_param_range_err : dcl_none;
204  err_code |= (pi->sps->Kp < 0.0f) ? dcl_param_range_err : dcl_none;
205  err_code |= (pi->sps->Ki < 0.0f) ? dcl_param_range_err : dcl_none;
206  if (err_code)
207  {
208  DCL_setError(pi,err_code);
209  DCL_getErrorInfo(pi);
211  }
212 #endif
213 
214  dcl_interrupt_t ints;
215  ints = DCL_disableInts();
216  pi->Kp = pi->sps->Kp;
217  pi->Ki = pi->sps->Ki;
218  pi->Umax = pi->sps->Umax;
219  pi->Umin = pi->sps->Umin;
220  pi->Imax = pi->sps->Imax;
221  pi->Imin = pi->sps->Imin;
222  DCL_restoreInts(ints);
223 }
224 
235 {
236  if (DCL_getUpdateStatus(pi))
237  {
240  return true;
241  }
242  return false;
243 }
244 
256 {
257 #ifdef DCL_ERROR_HANDLING_ENABLED
258  uint32_t err_code = dcl_none;
259  err_code |= (zpk->K < 0.0f) ? dcl_param_range_err : dcl_none;
260  err_code |= (crealf(zpk->z1) > (1.0f / (2.0f * pi->css->T))) ? dcl_param_warn_err : dcl_none;
261  if (err_code)
262  {
263  DCL_setError(pi,err_code);
264  DCL_getErrorInfo(pi);
266  }
267 #endif
268 
269  float32_t T = pi->css->T;
270  float32_t z1 = (float32_t) crealf(zpk->z1);
271  pi->sps->Kp = zpk->K * (1.0f + (T * z1) / 2.0f);
272  pi->sps->Ki = (-2.0f * T * z1) / (2.0f + (T * z1));
273 
274 }
275 
286 {
287 #ifdef DCL_ERROR_HANDLING_ENABLED
288  uint32_t err_code = dcl_none;
289  err_code |= (zpk->K < 0.0f) ? dcl_param_range_err : dcl_none;
290  err_code |= (crealf(zpk->z1) > (1.0f / (2.0f * pi->css->T))) ? dcl_param_warn_err : dcl_none;
291  if (err_code)
292  {
293  DCL_setError(pi,err_code);
294  DCL_getErrorInfo(pi);
296  }
297 #endif
298 
299  float32_t T = pi->css->T;
300  float32_t z1 = (float32_t) crealf(zpk->z1);
301  pi->sps->Kp = zpk->K * (1.0f + (T * z1) / 2.0f);
302  pi->sps->Ki = -zpk->K * T * z1;
303 }
304 
314 {
315  float32_t v2, v4, v5, v9;
316 
317  v2 = pi->Kp * (rk - yk);
318  v4 = pi->i10 + (pi->Ki * pi->i6 * v2);
319  v5 = v2 + v4;
320  v9 = DCL_runSat(v5, pi->Umax, pi->Umin);
321  pi->i6 = (v5 == v9) ? 1.0f : 0.0f;
322  pi->i10 = v4;
323 
324 #ifdef DCL_TESTPOINTS_ENABLED
325  pi->css->tpt = v5;
326 #endif
327 
328  return(v9);
329 }
330 
341 {
342  float32_t v1, v2, v4, v5, v9;
343 
344  v1 = rk - yk;
345  v2 = pi->Kp * v1;
346  v4 = (v1 * pi->Ki * pi->i6) + pi->i10;
347  pi->i10 = v4;
348  v5 = v2 + v4;
349  v9 = DCL_runSat(v5, pi->Umax, pi->Umin);
350  pi->i6 = (v5 == v9) ? 1.0f : 0.0f;
351 
352 #ifdef DCL_TESTPOINTS_ENABLED
353  pi->css->tpt = v5;
354 #endif
355 
356  return(v9);
357 }
358 
370 {
371  float32_t v1, v5, v7, v8;
372  bool l11, l14, l17, l18, l19;
373 
374  v1 = rk - yk;
375  v5 = (v1 * pi->Ki * pi->i6) + pi->i10;
376  pi->i10 = v5;
377  v7 = (v1 * pi->Kp) + v5;
378  v8 = DCL_runSat(v7, pi->Umax, pi->Umin);
379  l17 = (v7 == v8) ? true : false;
380  l11 = (DCL_runSat(v5, pi->Imax, pi->Imin) == v5) ? true : false;
381  l19 = (v5 > 0) ? true : false;
382  l14 = (v1 > 0) ? true : false;
383  l18 = l17 && (!l11 || (l19 ^ l14));
384  pi->i6 = (l18) ? 1.0f : 0.0f;
385 
386 #ifdef DCL_TESTPOINTS_ENABLED
387  pi->css->tpt = v7;
388 #endif
389 
390  return(v8);
391 }
392 
402 {
403  float32_t v2, v4, v5, v8, v9;
404 
405  v2 = (rk - yk) * pi->Kp;
406  v8 = v2 * pi->Ki * pi->i6;
407  v4 = v8 + pi->i11 + pi->i10;
408  v5 = v2 + v4;
409  pi->i10 = v4;
410  pi->i11 = v8;
411  v9 = DCL_runSat(v5, pi->Umax, pi->Umin);
412  pi->i6 = (v5 == v9) ? 1.0f : 0.0f;
413 
414 #ifdef DCL_TESTPOINTS_ENABLED
415  pi->css->tpt = v4;
416 #endif
417 
418  return(v9);
419 }
420 
423 #ifdef __cplusplus
424 }
425 #endif // extern "C"
426 
427 #endif // _DCL_PI_H_
dcl_pi::i11
float32_t i11
Tustin integrator storage, only used in DCL_runPISeriesTustin()
Definition: dcl_pi.h:84
DCL_ZPK3::K
float32_t K
Real gain.
Definition: dcl_zpk3.h:69
DCL_PI_SPS::Kp
float32_t Kp
Proportional gain.
Definition: dcl_pi.h:57
DCL_PI_SPS::Umax
float32_t Umax
Upper saturation limit.
Definition: dcl_pi.h:59
DCL_PI_SPS::Umin
float32_t Umin
Lower saturation limit.
Definition: dcl_pi.h:60
DCL_forceUpdatePI
_DCL_CODE_ACCESS void DCL_forceUpdatePI(DCL_PI *pi)
Loads PI tuning parameter from its SPS parameter without interrupt protection
Definition: dcl_pi.h:167
dcl_none
@ dcl_none
No error.
Definition: dcl_error.h:57
DCL_runPISeriesTustin
_DCL_CRIT_ACCESS float32_t DCL_runPISeriesTustin(DCL_PI *pi, float32_t rk, float32_t yk)
Executes a series form PI controller with Tustin integrator.
Definition: dcl_pi.h:401
DCL_runErrorHandler
#define DCL_runErrorHandler(ptr)
Prototype for basic error handler.
Definition: dcl_error.h:108
dcl_pi::Umin
float32_t Umin
Lower control saturation limit.
Definition: dcl_pi.h:77
DCL_PI_SPS::Ki
float32_t Ki
Integral gain.
Definition: dcl_pi.h:58
dcl_pi::Imax
float32_t Imax
Upper integrator saturation limit, only used in DCL_runPIParallelEnhanced()
Definition: dcl_pi.h:78
DCL_getUpdateStatus
#define DCL_getUpdateStatus(p)
Determine whether a parameter update-in-progress flag is set.
Definition: dcl_css.h:122
DCL_clearUpdateStatus
#define DCL_clearUpdateStatus(p)
Definition: dcl_css.h:116
_DCL_CODE_ACCESS
#define _DCL_CODE_ACCESS
Defines the scope of dcl functions.
Definition: dcl_common.h:63
DCL_runPIParallel
_DCL_CRIT_ACCESS float32_t DCL_runPIParallel(DCL_PI *pi, float32_t rk, float32_t yk)
Executes a parallel form PI controller Implemented as inline C function.
Definition: dcl_pi.h:340
DCL_resetPI
_DCL_CODE_ACCESS void DCL_resetPI(DCL_PI *pi)
Resets PI internal storage data with interrupt protection.
Definition: dcl_pi.h:153
DCL_loadParallelPIasZPK
_DCL_CODE_ACCESS void DCL_loadParallelPIasZPK(DCL_PI *pi, DCL_ZPK3 *zpk)
Configures a parallel PI controller in "zero-pole-gain" form.
Definition: dcl_pi.h:285
dcl_interrupt_t
uint32_t dcl_interrupt_t
Definition: dcl_common.h:107
DCL_PI_SPS::Imax
float32_t Imax
Upper integrator saturation limit, only used in DCL_runPIParallelEnhanced()
Definition: dcl_pi.h:61
dcl_pi
DCL_PI object for storing PI specific parameters.
Definition: dcl_pi.h:72
dcl_param_range_err
@ dcl_param_range_err
Parameter range exceeded.
Definition: dcl_error.h:58
dcl_pi::sps
DCL_PI_SPS * sps
updates controller parameter
Definition: dcl_pi.h:87
DCL_PI_SPS::Imin
float32_t Imin
Lower integrator saturation limit, only used in DCL_runPIParallelEnhanced()
Definition: dcl_pi.h:62
DCL_runPISeries
_DCL_CRIT_ACCESS float32_t DCL_runPISeries(DCL_PI *pi, float32_t rk, float32_t yk)
Executes an inline series form PI controller.
Definition: dcl_pi.h:313
dcl_param_warn_err
@ dcl_param_warn_err
Parameter warning.
Definition: dcl_error.h:60
dcl_pi::i10
float32_t i10
Integrator feedback storage.
Definition: dcl_pi.h:83
dcl_pi::Imin
float32_t Imin
Lower integrator saturation limit, only used in DCL_runPIParallelEnhanced()
Definition: dcl_pi.h:79
DCL_PI
_DCL_VOLATILE struct dcl_pi DCL_PI
DCL_runSat
#define DCL_runSat(data, Umax, Umin)
Macro to saturate a control variable but does not change the data itself unlike runClamp()
Definition: dcl_clamp.h:89
DCL_runPIParallelEnhanced
_DCL_CRIT_ACCESS float32_t DCL_runPIParallelEnhanced(DCL_PI *pi, float32_t rk, float32_t yk)
Executes a parallel form PI controller with enhanced anti-windup logic incorporating an additional in...
Definition: dcl_pi.h:369
DCL_CSS
Defines the controller common support structure.
Definition: dcl_css.h:57
dcl_pi::Ki
float32_t Ki
Integral gain.
Definition: dcl_pi.h:75
DCL_restoreInts
#define DCL_restoreInts(v)
Definition: dcl_common.h:106
dcl_param_invalid_err
@ dcl_param_invalid_err
Parameter not valid.
Definition: dcl_error.h:59
dcl_pi::Umax
float32_t Umax
Upper control saturation limit.
Definition: dcl_pi.h:76
DCL_ZPK3
Defines the DCL_ZPK3 structure.
Definition: dcl_zpk3.h:62
_DCL_VOLATILE
#define _DCL_VOLATILE
Defines volatile for DCL strctures.
Definition: dcl_common.h:79
DCL_updatePI
_DCL_CODE_ACCESS bool DCL_updatePI(DCL_PI *pi)
A conditional update based on the update flag. If the update status is set, the function will update ...
Definition: dcl_pi.h:234
DCL_loadSeriesPIasZPK
_DCL_CODE_ACCESS void DCL_loadSeriesPIasZPK(DCL_PI *pi, DCL_ZPK3 *zpk)
Configures a series PI controller in "zero-pole-gain" form.
Definition: dcl_pi.h:255
DCL_getErrorInfo
#define DCL_getErrorInfo(ptr)
Macro to store error info in CSS.
Definition: dcl_error.h:98
DCL_setError
#define DCL_setError(ptr, code)
Macro to set error code in CSS.
Definition: dcl_error.h:80
_DCL_CRIT_ACCESS
#define _DCL_CRIT_ACCESS
Defines the scope of critical dcl functions.
Definition: dcl_common.h:70
dcl_pi::Kp
float32_t Kp
Proportional gain.
Definition: dcl_pi.h:74
DCL_updatePINoCheck
_DCL_CODE_ACCESS void DCL_updatePINoCheck(DCL_PI *pi)
Loads PI tuning parameter from its SPS parameter with interrupt protection.
Definition: dcl_pi.h:197
DCL_PI_SPS
Defines DCL_PI shadow parameter set used for updating controller parameter.
Definition: dcl_pi.h:56
float32_t
float float32_t
Definition: dcl_common.h:58
dcl_pi::css
DCL_CSS * css
configuration & debugging
Definition: dcl_pi.h:88
DCL_ZPK3::z1
float complex z1
Complex zeros 1.
Definition: dcl_zpk3.h:63
dcl_pi::i6
float32_t i6
Saturation multiplier, 1 means no saturation and 0 means fully saturated.
Definition: dcl_pi.h:82
DCL_disableInts
#define DCL_disableInts()
Define enable and disable interrupt operations.
Definition: dcl_common.h:105