AM243x Motor Control SDK  2025.00.00
dcl_pi.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023-2025 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 #ifndef _DCL_PI_H_
33 #define _DCL_PI_H_
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
47 #include "../dcl_common.h"
48 
49 //--- Linear PI controller ---------------------------------------------------
50 
54 typedef struct dcl_pi_sps
55 {
62 } DCL_PI_SPS;
63 
66 #define PI_SPS_DEFAULTS { 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, -1.0f }
67 
70 typedef _DCL_VOLATILE struct dcl_pi
71 {
72  /* controller parameter */
79 
80  /* internal storage */
84 
85  /* miscellaneous */
89 
92 #define PI_DEFAULTS { 1.0f, 0.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, \
93  &(DCL_PI_SPS)PI_SPS_DEFAULTS, &(DCL_CSS)DCL_CSS_DEFAULTS }
94 
105 #define PI_INT_DEFAULTS .i6=1.0f, .i10=0.0f, .i11=0.0f, \
106 .sps=&(DCL_PI_SPS)PI_SPS_DEFAULTS, .css=&(DCL_CSS)DCL_CSS_DEFAULTS
107 
113 #define DCL_initPI() &(DCL_PI)PI_DEFAULTS
114 
121 #define DCL_initPIasParam(kp,ki,umax,umin) &(DCL_PI){ .Kp=kp, .Ki=ki, \
122  .Umax=umax, .Umin=umin, PI_INT_DEFAULTS }
123 
134 #define DCL_initPIasSPS(pi_ptr,sps_ptr) \
135 ({ \
136  DCL_PI* new_pi = (pi_ptr) ? pi_ptr : DCL_initPI(); \
137  DCL_PI_SPS* new_sps = (sps_ptr) ? sps_ptr : &(DCL_PI_SPS)PI_SPS_DEFAULTS; \
138  if(sps_ptr) \
139  { \
140  *new_pi = (DCL_PI){ (new_sps)->Kp, (new_sps)->Ki, (new_sps)->Umax, (new_sps)->Umin,\
141  (new_sps)->Imax, (new_sps)->Imin, 1.0f, 0.0f, 0.0f, \
142  (DCL_PI_SPS*)new_sps, &(DCL_CSS)DCL_CSS_DEFAULTS }; \
143  } \
144  new_pi; \
145 })
146 
153 {
154  dcl_interrupt_t ints;
155 
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  dcl_interrupt_t ints;
200 
201  ints = DCL_disableInts();
202  DCL_forceUpdatePI(pi);
203  DCL_restoreInts(ints);
204 }
205 
216 {
217  if (DCL_getUpdateStatus(pi))
218  {
221  return true;
222  }
223  return false;
224 }
225 
237 {
238 #ifdef DCL_ERROR_HANDLING_ENABLED
239  uint32_t err_code = dcl_none;
240  err_code |= (zpk->K < 0.0f) ? dcl_param_range_err : dcl_none;
241  err_code |= (crealf(zpk->z1) > (1.0f / (2.0f * pi->css->T))) ? dcl_param_warn_err : dcl_none;
242  if (err_code)
243  {
244  DCL_setError(pi,err_code);
245  DCL_getErrorInfo(pi);
247  }
248 #endif
249 
250  float32_t T = pi->css->T;
251  float32_t z1 = (float32_t) crealf(zpk->z1);
252  pi->sps->Kp = zpk->K * (1.0f + (T * z1) / 2.0f);
253  pi->sps->Ki = (-2.0f * T * z1) / (2.0f + (T * z1));
254 
255 }
256 
267 {
268 #ifdef DCL_ERROR_HANDLING_ENABLED
269  uint32_t err_code = dcl_none;
270  err_code |= (zpk->K < 0.0f) ? dcl_param_range_err : dcl_none;
271  err_code |= (crealf(zpk->z1) > (1.0f / (2.0f * pi->css->T))) ? dcl_param_warn_err : dcl_none;
272  if (err_code)
273  {
274  DCL_setError(pi,err_code);
275  DCL_getErrorInfo(pi);
277  }
278 #endif
279 
280  float32_t T = pi->css->T;
281  float32_t z1 = (float32_t) crealf(zpk->z1);
282  pi->sps->Kp = zpk->K * (1.0f + (T * z1) / 2.0f);
283  pi->sps->Ki = -zpk->K * T * z1;
284 }
285 
295 {
296  float32_t v2, v4, v5, v9;
297 
298  v2 = pi->Kp * (rk - yk);
299  v4 = pi->i10 + (pi->Ki * pi->i6 * v2);
300  v5 = v2 + v4;
301  v9 = DCL_runSat(v5, pi->Umax, pi->Umin);
302  pi->i6 = (v5 == v9) ? 1.0f : 0.0f;
303  pi->i10 = v4;
304 
305 #ifdef DCL_TESTPOINTS_ENABLED
306  pi->css->tpt = v5;
307 #endif
308 
309  return(v9);
310 }
311 
322 {
323  float32_t v1, v2, v4, v5, v9;
324 
325  v1 = rk - yk;
326  v2 = pi->Kp * v1;
327  v4 = (v1 * pi->Ki * pi->i6) + pi->i10;
328  pi->i10 = v4;
329  v5 = v2 + v4;
330  v9 = DCL_runSat(v5, pi->Umax, pi->Umin);
331  pi->i6 = (v5 == v9) ? 1.0f : 0.0f;
332 
333 #ifdef DCL_TESTPOINTS_ENABLED
334  pi->css->tpt = v5;
335 #endif
336 
337  return(v9);
338 }
339 
351 {
352  float32_t v1, v5, v7, v8;
353  bool l11, l14, l17, l18, l19;
354 
355  v1 = rk - yk;
356  v5 = (v1 * pi->Ki * pi->i6) + pi->i10;
357  pi->i10 = v5;
358  v7 = (v1 * pi->Kp) + v5;
359  v8 = DCL_runSat(v7, pi->Umax, pi->Umin);
360  l17 = (v7 == v8) ? true : false;
361  l11 = (DCL_runSat(v5, pi->Imax, pi->Imin) == v5) ? true : false;
362  l19 = (v5 > 0) ? true : false;
363  l14 = (v1 > 0) ? true : false;
364  l18 = l17 && (!l11 || (l19 ^ l14));
365  pi->i6 = (l18) ? 1.0f : 0.0f;
366 
367 #ifdef DCL_TESTPOINTS_ENABLED
368  pi->css->tpt = v7;
369 #endif
370 
371  return(v8);
372 }
373 
383 {
384  float32_t v2, v4, v5, v8, v9;
385 
386  v2 = (rk - yk) * pi->Kp;
387  v8 = v2 * pi->Ki * pi->i6;
388  v4 = v8 + pi->i11 + pi->i10;
389  v5 = v2 + v4;
390  pi->i10 = v4;
391  pi->i11 = v8;
392  v9 = DCL_runSat(v5, pi->Umax, pi->Umin);
393  pi->i6 = (v5 == v9) ? 1.0f : 0.0f;
394 
395 #ifdef DCL_TESTPOINTS_ENABLED
396  pi->css->tpt = v4;
397 #endif
398 
399  return(v9);
400 }
401 
404 #ifdef __cplusplus
405 }
406 #endif // extern "C"
407 
408 #endif // _DCL_PI_H_
dcl_pi::i11
float32_t i11
Tustin integrator storage, only used in DCL_runPISeriesTustin()
Definition: dcl_pi.h:83
DCL_ZPK3::K
float32_t K
Real gain.
Definition: dcl_zpk3.h:67
DCL_PI_SPS::Kp
float32_t Kp
Proportional gain.
Definition: dcl_pi.h:56
DCL_PI_SPS::Umax
float32_t Umax
Upper saturation limit.
Definition: dcl_pi.h:58
DCL_PI_SPS::Umin
float32_t Umin
Lower saturation limit.
Definition: dcl_pi.h:59
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:382
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:76
DCL_PI_SPS::Ki
float32_t Ki
Integral gain.
Definition: dcl_pi.h:57
dcl_pi::Imax
float32_t Imax
Upper integrator saturation limit, only used in DCL_runPIParallelEnhanced()
Definition: dcl_pi.h:77
DCL_getUpdateStatus
#define DCL_getUpdateStatus(p)
Determine whether a parameter update-in-progress flag is set.
Definition: dcl_css.h:121
DCL_clearUpdateStatus
#define DCL_clearUpdateStatus(p)
Definition: dcl_css.h:115
_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:321
DCL_resetPI
_DCL_CODE_ACCESS void DCL_resetPI(DCL_PI *pi)
Resets PI internal storage data with interrupt protection.
Definition: dcl_pi.h:152
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:266
dcl_interrupt_t
uint32_t dcl_interrupt_t
Definition: dcl_common.h:109
DCL_PI_SPS::Imax
float32_t Imax
Upper integrator saturation limit, only used in DCL_runPIParallelEnhanced()
Definition: dcl_pi.h:60
dcl_pi
DCL_PI object for storing PI specific parameters.
Definition: dcl_pi.h:71
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:86
DCL_PI_SPS::Imin
float32_t Imin
Lower integrator saturation limit, only used in DCL_runPIParallelEnhanced()
Definition: dcl_pi.h:61
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:294
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:82
dcl_pi::Imin
float32_t Imin
Lower integrator saturation limit, only used in DCL_runPIParallelEnhanced()
Definition: dcl_pi.h:78
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:90
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:350
DCL_CSS
Defines the controller common support structure.
Definition: dcl_css.h:56
dcl_pi::Ki
float32_t Ki
Integral gain.
Definition: dcl_pi.h:74
DCL_restoreInts
#define DCL_restoreInts(v)
Definition: dcl_common.h:108
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:75
DCL_ZPK3
Defines the DCL_ZPK3 structure.
Definition: dcl_zpk3.h:60
_DCL_VOLATILE
#define _DCL_VOLATILE
Defines volatile for DCL strctures Flags can be defined in dcl.h or user files before including DCL l...
Definition: dcl_common.h:75
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:215
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:236
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:67
dcl_pi::Kp
float32_t Kp
Proportional gain.
Definition: dcl_pi.h:73
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:55
float32_t
float float32_t
Definition: dcl_common.h:58
dcl_pi::css
DCL_CSS * css
configuration & debugging
Definition: dcl_pi.h:87
DCL_ZPK3::z1
float complex z1
Complex zeros 1.
Definition: dcl_zpk3.h:61
dcl_pi::i6
float32_t i6
Saturation multiplier, 1 means no saturation and 0 means fully saturated.
Definition: dcl_pi.h:81
DCL_disableInts
#define DCL_disableInts()
Define enable and disable interrupt operations.
Definition: dcl_common.h:107