AM243x Motor Control SDK  09.00.00
dcl_df22.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/
3  *
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
15  * distribution.
16  *
17  * * Neither the name of Texas Instruments Incorporated nor the names of
18  * its contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _DCL_DF22_H_
35 #define _DCL_DF22_H_
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
50 #include "../dcl_common.h"
51 
52 //--- Direct Form 2 - 2nd order ----------------------------------------------
53 
57 typedef struct dcl_df22_sps
58 {
64 } DCL_DF22_SPS;
65 
66 #define DF22_SPS_DEFAULTS { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }
67 
70 typedef _DCL_VOLATILE struct dcl_df22
71 {
72  /* compensator parameter */
78 
79  /* internal storage */
82 
83  /* miscellaneous */
87 
90 #define DF22_DEFAULTS { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, \
91  &(DCL_DF22_SPS)DF22_SPS_DEFAULTS, &(DCL_CSS)DCL_CSS_DEFAULTS }
92 
101 #define DF22_INT_DEFAULTS .x1=0.0f, .x2=0.0f, .sps=&(DCL_DF22_SPS)DF22_SPS_DEFAULTS, \
102  .css=&(DCL_CSS)DCL_CSS_DEFAULTS
103 
109 #define DCL_initDF22() &(DCL_DF22)DF22_DEFAULTS
110 
117 #define DCL_initDF22asParam(_b0,_b1,_b2,_a1,_a2) &(DCL_DF22){ .b0=_b0, .b1=_b1, \
118  .b2=_b2, .a1=_a1, .a2=_a2, .a3=_a3, DF22_INT_DEFAULTS }
119 
130 #define DCL_initDF22asSPS(df_ptr,sps_ptr) \
131 ({ \
132  DCL_DF22* new_df = (df_ptr) ? df_ptr : DCL_initDF22(); \
133  DCL_DF22_SPS* new_sps = (sps_ptr) ? sps_ptr : &(DCL_DF22_SPS)DF22_SPS_DEFAULTS; \
134  if(sps_ptr) \
135  { \
136  *new_df = (DCL_DF22){ (new_sps)->b0, (new_sps)->b1, (new_sps)->b2, (new_sps)->a1,\
137  (new_sps)->a2, 0.0f, 0.0f, (DCL_DF22_SPS*)new_sps, &(DCL_CSS)DCL_CSS_DEFAULTS }; \
138  } \
139  new_df; \
140 })
141 
148 {
150  df->x1 = df->x2 = 0.0f;
151  DCL_restoreInts(ints);
152 }
153 
160 {
161  df->b0 = df->sps->b0;
162  df->b1 = df->sps->b1;
163  df->b2 = df->sps->b2;
164  df->a1 = df->sps->a1;
165  df->a2 = df->sps->a2;
166 }
167 
175 {
176  if (!DCL_getUpdateStatus(df))
177  {
180  df->b0 = df->sps->b0;
181  df->b1 = df->sps->b1;
182  df->b2 = df->sps->b2;
183  df->a1 = df->sps->a1;
184  df->a2 = df->sps->a2;
186  DCL_restoreInts(ints);
187  return true;
188  }
189  return false;
190 }
191 
202 {
203  if (DCL_getPendingStatus(df) && DCL_updateDF22(df))
204  {
206  return true;
207  }
208  return false;
209 }
210 
218 {
219  df->sps->b0 = df->b0;
220  df->sps->b1 = df->b1;
221  df->sps->b2 = df->b2;
222  df->sps->a1 = df->a1;
223  df->sps->a2 = df->a2;
224 }
225 
226 
234 {
235  return(DCL_isStablePn2(1.0f, df->sps->a1, df->sps->a2));
236 }
237 
248 {
249 
250 #ifdef DCL_ERROR_HANDLING_ENABLED
251  uint32_t err_code = dcl_none;
252  err_code |= DCL_isZero(cimagf(zpk->z1) + cimagf(zpk->z2)) ? dcl_none : dcl_param_invalid_err;
253  err_code |= DCL_isZero(cimagf(zpk->p1) + cimagf(zpk->p2)) ? dcl_none : dcl_param_invalid_err;
254  DCL_setError(df,err_code);
255  if (err_code)
256  {
257  DCL_getErrorInfo(df);
259  }
260 #endif
261 
262  float32_t beta1 = -(float32_t) crealf(zpk->z1 + zpk->z2);
263  float32_t beta0 = (float32_t) crealf(zpk->z1 * zpk->z2);
264  float32_t alpha1 = -(float32_t) crealf(zpk->p1 + zpk->p2);
265  float32_t alpha0 = (float32_t) crealf(zpk->p1 * zpk->p2);
266 
267  float32_t t_sec = df->css->t_sec;
268  float32_t a0p = 4.0f + (alpha1 * 2.0f * t_sec) + (alpha0 * t_sec * t_sec);
269 
270  df->sps->b0 = zpk->K * (4.0f + (beta1 * 2.0f * t_sec) + (beta0 * t_sec * t_sec)) / a0p;
271  df->sps->b1 = zpk->K * (-8.0f + (2.0f * beta0 * t_sec * t_sec)) / a0p;
272  df->sps->b2 = zpk->K * (4.0f - (beta1 * 2.0f * t_sec) + (beta0 * t_sec * t_sec)) / a0p;
273  df->sps->a1 = (-8.0f + (2.0f * alpha0 * t_sec * t_sec)) / a0p;
274  df->sps->a2 = (4.0f - (alpha1 * 2.0f * t_sec) + (alpha0 * t_sec * t_sec)) / a0p;
275 }
276 
288 {
289 
290 #ifdef DCL_ERROR_HANDLING_ENABLED
291  uint32_t err_code = dcl_none;
292  err_code |= (z >= 0.0f) ? dcl_none : dcl_param_invalid_err;
293  err_code |= (wn >= 0.0f) ? dcl_none : dcl_param_invalid_err;
294  if (err_code)
295  {
296  DCL_setError(df,err_code);
297  DCL_getErrorInfo(df);
299  }
300 #endif
301 
302  float32_t t_sec = df->css->t_sec;
303  float32_t v1 = wn * wn * t_sec * t_sec;
304  float32_t a2p = 1.0f / (4.0f + (4.0f * z * wn * t_sec) + v1);
305  df->sps->b0 = v1 * a2p;
306  df->sps->b1 = 2.0f * df->sps->b0;
307  df->sps->b2 = df->sps->b0;
308  df->sps->a1 = ((2.0f * v1) - 8.0f) * a2p;
309  df->sps->a2 = (4.0f - (4.0f * z * wn * t_sec) + v1) * a2p;
310 }
311 
324 {
325 
326 #ifdef DCL_ERROR_HANDLING_ENABLED
327  uint32_t err_code = dcl_none;
328  err_code |= (Kp < 0.0f) ? dcl_param_range_err : dcl_none;
329  err_code |= (Ki < 0.0f) ? dcl_param_range_err : dcl_none;
330  err_code |= (Kd < 0.0f) ? dcl_param_range_err : dcl_none;
331  err_code |= ((fc < 0.0f) || (fc > (1.0f / (2.0f * df->css->T)))) ? dcl_param_range_err : dcl_none;
332  if (err_code)
333  {
334  DCL_setError(df,err_code);
335  DCL_getErrorInfo(df);
337  }
338 #endif
339 
340  float32_t t_sec = df->css->t_sec;
341  float32_t tau = 1 / (2.0f * CONST_PI * fc);
342  float32_t c1 = 2.0f / (t_sec + (2.0f * tau));
343  float32_t c2 = c1 * (t_sec - (2.0f * tau)) / 2.0f;
344  float32_t Kdp = Kd * c1;
345  df->sps->b0 = Kp * (1 + Ki + Kdp);
346  df->sps->b1 = Kp * (c2 - 1 + Ki*c2 - 2*Kdp);
347  df->sps->b2 = Kp * (-c2 + Kdp);
348  df->sps->a1 = c2 - 1;
349  df->sps->a2 = -c2;
350 }
351 
364 {
365 
366 #ifdef DCL_ERROR_HANDLING_ENABLED
367  uint32_t err_code = dcl_none;
368  err_code |= (Kp < 0.0f) ? dcl_param_range_err : dcl_none;
369  err_code |= (Ki < 0.0f) ? dcl_param_range_err : dcl_none;
370  err_code |= (Kd < 0.0f) ? dcl_param_range_err : dcl_none;
371  err_code |= (fc < 0.0f) ? dcl_param_range_err : dcl_none;
372  err_code |= (fc > (1.0f / (2.0f * df->css->t_sec))) ? dcl_param_warn_err : dcl_none;
373  if (err_code)
374  {
375  DCL_setError(df,err_code);
376  DCL_getErrorInfo(df);
378  }
379 #endif
380 
381  float32_t t_sec = df->css->t_sec;
382  float32_t tau = 1.0f / (2.0f * CONST_PI * fc);
383  float32_t c1 = 2.0f / (t_sec + (2.0f * tau));
384  float32_t c2 = c1 * (t_sec - (2.0f * tau)) / 2.0f;
385  float32_t Kdp = Kd * c1;
386  df->sps->b0 = Kp + Ki + Kdp;
387  df->sps->b1 = (Kp * (c2 - 1)) + (Ki * c2) - (2.0f * Kdp);
388  df->sps->b2 = (-c2 * Kp) + Kdp;
389  df->sps->a1 = c2 - 1;
390  df->sps->a2 = -c2;
391 }
392 
401 {
402  float32_t v7 = (ek * df->b0) + df->x1;
403  df->x1 = (ek * df->b1) + df->x2 - (v7 * df->a1);
404  df->x2 = (ek * df->b2) - (v7 * df->a2);
405 
406  return(v7);
407 }
408 
418 {
419  return((ek * df->b0) + df->x1);
420 }
421 
430 {
431  df->x1 = (ek * df->b1) + df->x2 - (uk * df->a1);
432  df->x2 = (ek * df->b2) - (uk * df->a2);
433 }
434 
445 {
447  bool is_clamped = DCL_runClamp(&uk, Umax, Umin);
448  if(!is_clamped) DCL_runDF22PartialUpdate(df, ek, uk);
449  return(uk);
450 }
451 
454 #ifdef __cplusplus
455 }
456 #endif // extern "C"
457 
458 #endif // _DCL_DF22_H_
DCL_DF22_SPS::b2
float32_t b2
pos. coefficient to e(k-2)
Definition: dcl_df22.h:61
DCL_fupdateDF22
_DCL_CODE_ACCESS void DCL_fupdateDF22(DCL_DF22 *df)
Loads DF22 tuning parameter from its SPS parameter.
Definition: dcl_df22.h:159
CONST_PI
#define CONST_PI
Local definitions of the mathematical constant pi.
Definition: dcl_aux.h:55
DCL_ZPK3::K
float32_t K
Real gain.
Definition: dcl_zpk3.h:69
DCL_updateDF22
_DCL_CODE_ACCESS _DCL_CODE_SECTION bool DCL_updateDF22(DCL_DF22 *df)
Updates DF22 parameter from its SPS parameter with interrupt protection.
Definition: dcl_df22.h:174
DCL_isStableDF22
_DCL_CODE_ACCESS bool DCL_isStableDF22(DCL_DF22 *df)
Determines stability of the shadow compensator.
Definition: dcl_df22.h:233
dcl_df22::b2
float32_t b2
pos. coefficient to e(k-2)
Definition: dcl_df22.h:75
DCL_isStablePn2
_DCL_CODE_ACCESS bool DCL_isStablePn2(float32_t a0, float32_t a1, float32_t a2)
Determines stability of a second order polynomial with real coefficients P(z) = a0 z^2 + a1 z + a2.
Definition: dcl_stability.h:72
DCL_ZPK3::z2
float complex z2
Complex zeros 2.
Definition: dcl_zpk3.h:64
dcl_param_warn_err
@ dcl_param_warn_err
Parameter warning.
Definition: dcl_error.h:60
_DCL_CODE_SECTION
#define _DCL_CODE_SECTION
Defines dcl function section that users can specify in the linker file(.cmd) and to accelerate perfor...
Definition: dcl_common.h:60
DCL_runDF22PartialUpdate
_DCL_CODE_ACCESS void DCL_runDF22PartialUpdate(DCL_DF22 *df, float32_t ek, float32_t uk)
Update DF22 controller based on pre-computed control effort.
Definition: dcl_df22.h:429
DCL_runDF22
_DCL_CODE_ACCESS _DCL_CODE_SECTION float32_t DCL_runDF22(DCL_DF22 *df, float32_t ek)
Executes a 2nd order Direct Form 2 controller.
Definition: dcl_df22.h:400
DCL_clearPendingStatus
#define DCL_clearPendingStatus(p)
Definition: dcl_css.h:129
DCL_ZPK3::p2
float complex p2
Complex poles 2.
Definition: dcl_zpk3.h:67
DCL_setUpdateStatus
#define DCL_setUpdateStatus(p)
Macros to set and clear the update-in-progress flag.
Definition: dcl_css.h:116
dcl_df22::b1
float32_t b1
pos. coefficient to e(k-1)
Definition: dcl_df22.h:74
DF22_Handle
_DCL_VOLATILE struct dcl_df22 * DF22_Handle
DCL_loadDF22asParallelPID
_DCL_CODE_ACCESS void DCL_loadDF22asParallelPID(DCL_DF22 *df, float32_t Kp, float32_t Ki, float32_t Kd, float32_t fc)
Loads the shadow DF22 compensator coefficients to emulate a parallel form PID. Note: Sampling period ...
Definition: dcl_df22.h:363
DCL_runErrorHandler
#define DCL_runErrorHandler(ptr)
Prototype for basic error handler.
Definition: dcl_error.h:107
dcl_param_range_err
@ dcl_param_range_err
Parameter range exceeded.
Definition: dcl_error.h:58
DCL_getUpdateStatus
#define DCL_getUpdateStatus(p)
Determine whether a parameter update-in-progress flag is set.
Definition: dcl_css.h:123
DCL_clearUpdateStatus
#define DCL_clearUpdateStatus(p)
Definition: dcl_css.h:117
DCL_DF22
_DCL_VOLATILE struct dcl_df22 DCL_DF22
DCL_updateDF22SPS
_DCL_CODE_ACCESS void DCL_updateDF22SPS(DCL_DF22 *df)
Update SPS parameter with active param, userful when needing to update only few active param from SPS...
Definition: dcl_df22.h:217
_DCL_CODE_ACCESS
#define _DCL_CODE_ACCESS
Defines the scope of dcl functions (static inline/extern inline/none)
Definition: dcl_common.h:55
DCL_loadDF22asZPK
_DCL_CODE_ACCESS void DCL_loadDF22asZPK(DCL_DF22 *df, DCL_ZPK3 *zpk)
Loads the DF22 shadow coefficients from a ZPK3 description Note: Sampling period df->css->t_sec are u...
Definition: dcl_df22.h:247
DCL_loadDF22asSeriesPID
_DCL_CODE_ACCESS void DCL_loadDF22asSeriesPID(DCL_DF22 *df, float32_t Kp, float32_t Ki, float32_t Kd, float32_t fc)
Loads the shadow DF22 compensator coefficients to emulate a series form PID. Note: Sampling period df...
Definition: dcl_df22.h:323
DCL_ZPK3::p1
float complex p1
Complex poles 1.
Definition: dcl_zpk3.h:66
dcl_none
@ dcl_none
No error.
Definition: dcl_error.h:57
DCL_resetDF22
_DCL_CODE_ACCESS void DCL_resetDF22(DCL_DF22 *df)
Resets DF22 internal storage data with interrupt protection.
Definition: dcl_df22.h:147
dcl_interrupt_t
uint32_t dcl_interrupt_t
Definition: dcl_common.h:96
dcl_df22
DCL_DF22 object for storing df22 specific parameters.
Definition: dcl_df22.h:71
DCL_DF22_SPS::a2
float32_t a2
neg. coefficient to u(k-2)
Definition: dcl_df22.h:63
DCL_isZero
#define DCL_isZero(x)
Determines floating point numerical proximity to zero.
Definition: dcl_aux.h:77
DCL_pendingUpdateDF22
_DCL_CODE_ACCESS _DCL_CODE_SECTION bool DCL_pendingUpdateDF22(DCL_DF22 *df)
A conditional update based on the pending-for-update flag. If the pending status is set,...
Definition: dcl_df22.h:201
dcl_df22::a1
float32_t a1
neg. coefficient to u(k-1)
Definition: dcl_df22.h:76
dcl_df22::css
DCL_CSS * css
configuration & debugging
Definition: dcl_df22.h:85
dcl_df22::b0
float32_t b0
pos. coefficient to e(k)
Definition: dcl_df22.h:73
dcl_param_invalid_err
@ dcl_param_invalid_err
Parameter not valid.
Definition: dcl_error.h:59
dcl_df22::a2
float32_t a2
neg. coefficient to u(k-2)
Definition: dcl_df22.h:77
DCL_DF22_SPS
Defines DCL_DF22 shadow parameter set used for updating compensator parameter.
Definition: dcl_df22.h:58
dcl_df22::sps
DCL_DF22_SPS * sps
updates compensator parameter
Definition: dcl_df22.h:84
DCL_loadDF22asZwn
_DCL_CODE_ACCESS void DCL_loadDF22asZwn(DCL_DF22 *df, float32_t z, float32_t wn)
Loads the DF22 shadow coefficients from damping ratio and un-damped natural frequency using sample ra...
Definition: dcl_df22.h:287
DCL_DF22_SPS::b1
float32_t b1
pos. coefficient to e(k-1)
Definition: dcl_df22.h:60
DCL_CSS
Defines the controller common support structure.
Definition: dcl_css.h:57
DCL_getPendingStatus
#define DCL_getPendingStatus(p)
Determine whether a parameter pending-for-update flag is set.
Definition: dcl_css.h:135
DCL_restoreInts
#define DCL_restoreInts(v)
Definition: dcl_common.h:95
DCL_runClamp
_DCL_CODE_ACCESS bool DCL_runClamp(float32_t *data, float32_t Umax, float32_t Umin)
Saturates a control variable and returns true if either limit is exceeded.
Definition: dcl_clamp.h:59
DCL_ZPK3
Defines the DCL_ZPK3 controller structure.
Definition: dcl_zpk3.h:62
_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:68
dcl_df22::x2
float32_t x2
x2 = b2*e(k-2) - a2*u(k-2)
Definition: dcl_df22.h:81
DCL_DF22_SPS::a1
float32_t a1
neg. coefficient to u(k-1)
Definition: dcl_df22.h:62
DCL_getErrorInfo
#define DCL_getErrorInfo(ptr)
Macro to store error info in CSS.
Definition: dcl_error.h:97
DCL_setError
#define DCL_setError(ptr, code)
Macro to set error code in CSS.
Definition: dcl_error.h:79
dcl_df22::x1
float32_t x1
x1 = b1*e(k-1) - a1*u(k-1) + x2
Definition: dcl_df22.h:80
float32_t
float float32_t
Defines single,double precision data type. Note: Assumes ABI to be TI_EABI, does not support legacy T...
Definition: dcl_common.h:51
DCL_DF22_SPS::b0
float32_t b0
pos. coefficient to e(k)
Definition: dcl_df22.h:59
DCL_runDF22Clamp
_DCL_CODE_ACCESS _DCL_CODE_SECTION float32_t DCL_runDF22Clamp(DCL_DF22 *df, float32_t ek, float32_t Umax, float32_t Umin)
Executes a 2nd order Direct Form 2 controller with clamp.
Definition: dcl_df22.h:444
DCL_ZPK3::z1
float complex z1
Complex zeros 1.
Definition: dcl_zpk3.h:63
DCL_disableInts
#define DCL_disableInts()
Define enable and disable interrupt operations.
Definition: dcl_common.h:94
DCL_runDF22PartialCompute
_DCL_CODE_ACCESS float32_t DCL_runDF22PartialCompute(DCL_DF22 *df, float32_t ek)
Immediate computation to obtain DF22 servo error without updating the controller.
Definition: dcl_df22.h:417