AM261x Motor Control SDK  10.02.00
dcl_df22.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 
33 #ifndef _DCL_DF22_H_
34 #define _DCL_DF22_H_
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
49 #include "../dcl_common.h"
50 
51 //--- Direct Form 2 - 2nd order ----------------------------------------------
52 
56 typedef struct dcl_df22_sps
57 {
63 } DCL_DF22_SPS;
64 
65 #define DF22_SPS_DEFAULTS { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }
66 
69 typedef _DCL_VOLATILE struct dcl_df22
70 {
71  /* compensator parameter */
77 
78  /* internal storage */
81 
82  /* miscellaneous */
86 
89 #define DF22_DEFAULTS { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, \
90  &(DCL_DF22_SPS)DF22_SPS_DEFAULTS, &(DCL_CSS)DCL_CSS_DEFAULTS }
91 
100 #define DF22_INT_DEFAULTS .x1=0.0f, .x2=0.0f, .sps=&(DCL_DF22_SPS)DF22_SPS_DEFAULTS, \
101  .css=&(DCL_CSS)DCL_CSS_DEFAULTS
102 
108 #define DCL_initDF22() &(DCL_DF22)DF22_DEFAULTS
109 
116 #define DCL_initDF22asParam(_b0,_b1,_b2,_a1,_a2) &(DCL_DF22){ .b0=_b0, .b1=_b1, \
117  .b2=_b2, .a1=_a1, .a2=_a2, .a3=_a3, DF22_INT_DEFAULTS }
118 
129 #define DCL_initDF22asSPS(df_ptr,sps_ptr) \
130 ({ \
131  DCL_DF22* new_df = (df_ptr) ? df_ptr : DCL_initDF22(); \
132  DCL_DF22_SPS* new_sps = (sps_ptr) ? sps_ptr : &(DCL_DF22_SPS)DF22_SPS_DEFAULTS; \
133  if(sps_ptr) \
134  { \
135  *new_df = (DCL_DF22){ (new_sps)->b0, (new_sps)->b1, (new_sps)->b2, (new_sps)->a1,\
136  (new_sps)->a2, 0.0f, 0.0f, (DCL_DF22_SPS*)new_sps, &(DCL_CSS)DCL_CSS_DEFAULTS }; \
137  } \
138  new_df; \
139 })
140 
147 {
148  dcl_interrupt_t ints;
149 
150  ints = DCL_disableInts();
151  df->x1 = df->x2 = 0.0f;
152  DCL_restoreInts(ints);
153 }
154 
161 {
162  df->b0 = df->sps->b0;
163  df->b1 = df->sps->b1;
164  df->b2 = df->sps->b2;
165  df->a1 = df->sps->a1;
166  df->a2 = df->sps->a2;
167 }
168 
175 {
176  dcl_interrupt_t ints;
177 
178  ints = DCL_disableInts();
180  DCL_restoreInts(ints);
181 }
182 
193 {
194  if (DCL_setUpdateStatus(df))
195  {
198  return true;
199  }
200  return false;
201 }
202 
210 {
211  return(DCL_isStablePn2(1.0f, df->sps->a1, df->sps->a2));
212 }
213 
224 {
225 
226 #ifdef DCL_ERROR_HANDLING_ENABLED
227  uint32_t err_code = dcl_none;
228  err_code |= DCL_isZero(cimagf(zpk->z1) + cimagf(zpk->z2)) ? dcl_none : dcl_param_invalid_err;
229  err_code |= DCL_isZero(cimagf(zpk->p1) + cimagf(zpk->p2)) ? dcl_none : dcl_param_invalid_err;
230  if (err_code)
231  {
232  DCL_setError(df,err_code);
233  DCL_getErrorInfo(df);
235  }
236 #endif
237 
238  float32_t beta1 = -(float32_t) crealf(zpk->z1 + zpk->z2);
239  float32_t beta0 = (float32_t) crealf(zpk->z1 * zpk->z2);
240  float32_t alpha1 = -(float32_t) crealf(zpk->p1 + zpk->p2);
241  float32_t alpha0 = (float32_t) crealf(zpk->p1 * zpk->p2);
242 
243  float32_t T = df->css->T;
244  float32_t a0p = 4.0f + (alpha1 * 2.0f * T) + (alpha0 * T * T);
245 
246  df->sps->b0 = zpk->K * (4.0f + (beta1 * 2.0f * T) + (beta0 * T * T)) / a0p;
247  df->sps->b1 = zpk->K * (-8.0f + (2.0f * beta0 * T * T)) / a0p;
248  df->sps->b2 = zpk->K * (4.0f - (beta1 * 2.0f * T) + (beta0 * T * T)) / a0p;
249  df->sps->a1 = (-8.0f + (2.0f * alpha0 * T * T)) / a0p;
250  df->sps->a2 = (4.0f - (alpha1 * 2.0f * T) + (alpha0 * T * T)) / a0p;
251 }
252 
264 {
265 
266 #ifdef DCL_ERROR_HANDLING_ENABLED
267  uint32_t err_code = dcl_none;
268  err_code |= (z >= 0.0f) ? dcl_none : dcl_param_invalid_err;
269  err_code |= (wn >= 0.0f) ? dcl_none : dcl_param_invalid_err;
270  if (err_code)
271  {
272  DCL_setError(df,err_code);
273  DCL_getErrorInfo(df);
275  }
276 #endif
277 
278  float32_t T = df->css->T;
279  float32_t v1 = wn * wn * T * T;
280  float32_t a2p = 1.0f / (4.0f + (4.0f * z * wn * T) + v1);
281  df->sps->b0 = v1 * a2p;
282  df->sps->b1 = 2.0f * df->sps->b0;
283  df->sps->b2 = df->sps->b0;
284  df->sps->a1 = ((2.0f * v1) - 8.0f) * a2p;
285  df->sps->a2 = (4.0f - (4.0f * z * wn * T) + v1) * a2p;
286 }
287 
300 {
301 
302 #ifdef DCL_ERROR_HANDLING_ENABLED
303  uint32_t err_code = dcl_none;
304  err_code |= (Kp < 0.0f) ? dcl_param_range_err : dcl_none;
305  err_code |= (Ki < 0.0f) ? dcl_param_range_err : dcl_none;
306  err_code |= (Kd < 0.0f) ? dcl_param_range_err : dcl_none;
307  err_code |= ((fc < 0.0f) || (fc > (1.0f / (2.0f * df->css->T)))) ? dcl_param_range_err : dcl_none;
308  if (err_code)
309  {
310  DCL_setError(df,err_code);
311  DCL_getErrorInfo(df);
313  }
314 #endif
315 
316  float32_t T = df->css->T;
317  float32_t tau = 1 / (2.0f * CONST_PI * fc);
318  float32_t c1 = 2.0f / (T + (2.0f * tau));
319  float32_t c2 = c1 * (T - (2.0f * tau)) / 2.0f;
320  float32_t Kdp = Kd * c1;
321  df->sps->b0 = Kp * (1 + Ki + Kdp);
322  df->sps->b1 = Kp * (c2 - 1 + Ki*c2 - 2*Kdp);
323  df->sps->b2 = Kp * (-c2 + Kdp);
324  df->sps->a1 = c2 - 1;
325  df->sps->a2 = -c2;
326 }
327 
340 {
341 
342 #ifdef DCL_ERROR_HANDLING_ENABLED
343  uint32_t err_code = dcl_none;
344  err_code |= (Kp < 0.0f) ? dcl_param_range_err : dcl_none;
345  err_code |= (Ki < 0.0f) ? dcl_param_range_err : dcl_none;
346  err_code |= (Kd < 0.0f) ? dcl_param_range_err : dcl_none;
347  err_code |= (fc < 0.0f) ? dcl_param_range_err : dcl_none;
348  err_code |= (fc > (1.0f / (2.0f * df->css->T))) ? dcl_param_warn_err : dcl_none;
349  if (err_code)
350  {
351  DCL_setError(df,err_code);
352  DCL_getErrorInfo(df);
354  }
355 #endif
356 
357  float32_t T = df->css->T;
358  float32_t tau = 1.0f / (2.0f * CONST_PI * fc);
359  float32_t c1 = 2.0f / (T + (2.0f * tau));
360  float32_t c2 = c1 * (T - (2.0f * tau)) / 2.0f;
361  float32_t Kdp = Kd * c1;
362  df->sps->b0 = Kp + Ki + Kdp;
363  df->sps->b1 = (Kp * (c2 - 1)) + (Ki * c2) - (2.0f * Kdp);
364  df->sps->b2 = (-c2 * Kp) + Kdp;
365  df->sps->a1 = c2 - 1;
366  df->sps->a2 = -c2;
367 }
368 
377 {
378  float32_t v7 = (ek * df->b0) + df->x1;
379  df->x1 = (ek * df->b1) + df->x2 - (v7 * df->a1);
380  df->x2 = (ek * df->b2) - (v7 * df->a2);
381 
382  return(v7);
383 }
384 
394 {
395  return((ek * df->b0) + df->x1);
396 }
397 
406 {
407  df->x1 = (ek * df->b1) + df->x2 - (uk * df->a1);
408  df->x2 = (ek * df->b2) - (uk * df->a2);
409 }
410 
421 {
423  bool is_clamped = DCL_runClamp(&uk, Umax, Umin);
424  if(!is_clamped) DCL_runDF22PartialUpdate(df, ek, uk);
425  return(uk);
426 }
427 
430 #ifdef __cplusplus
431 }
432 #endif // extern "C"
433 
434 #endif // _DCL_DF22_H_
DCL_DF22_SPS::b2
float32_t b2
pos. coefficient to e(k-2)
Definition: dcl_df22.h:60
CONST_PI
#define CONST_PI
Local definitions of the mathematical constant pi.
Definition: dcl_macro.h:53
DCL_runDF22PartialCompute
_DCL_CRIT_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:393
DCL_ZPK3::K
float32_t K
Real gain.
Definition: dcl_zpk3.h:67
DCL_isStableDF22
_DCL_CODE_ACCESS bool DCL_isStableDF22(DCL_DF22 *df)
Determines stability of the shadow compensator.
Definition: dcl_df22.h:209
dcl_df22::b2
float32_t b2
pos. coefficient to e(k-2)
Definition: dcl_df22.h:74
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:70
DCL_ZPK3::z2
float complex z2
Complex zeros 2.
Definition: dcl_zpk3.h:62
DCL_ZPK3::p2
float complex p2
Complex poles 2.
Definition: dcl_zpk3.h:65
DCL_setUpdateStatus
#define DCL_setUpdateStatus(p)
Macros to set and clear the update-in-progress flag.
Definition: dcl_css.h:114
dcl_none
@ dcl_none
No error.
Definition: dcl_error.h:57
dcl_df22::b1
float32_t b1
pos. coefficient to e(k-1)
Definition: dcl_df22.h:73
DCL_updateDF22NoCheck
_DCL_CODE_ACCESS void DCL_updateDF22NoCheck(DCL_DF22 *df)
Loads DF22 tuning parameter from its SPS parameter with interrupt protection.
Definition: dcl_df22.h:174
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.
Definition: dcl_df22.h:339
DCL_runErrorHandler
#define DCL_runErrorHandler(ptr)
Prototype for basic error handler.
Definition: dcl_error.h:108
DCL_clearUpdateStatus
#define DCL_clearUpdateStatus(p)
Definition: dcl_css.h:115
DCL_DF22
_DCL_VOLATILE struct dcl_df22 DCL_DF22
DCL_updateDF22
_DCL_CODE_ACCESS bool DCL_updateDF22(DCL_DF22 *df)
A conditional update based on the update flag. If the update status is set, the function will update ...
Definition: dcl_df22.h:192
DCL_forceUpdateDF22
_DCL_CODE_ACCESS void DCL_forceUpdateDF22(DCL_DF22 *df)
Loads DF22 tuning parameter from its SPS parameter without interrupt protection.
Definition: dcl_df22.h:160
_DCL_CODE_ACCESS
#define _DCL_CODE_ACCESS
Defines the scope of dcl functions.
Definition: dcl_common.h:63
DCL_loadDF22asZPK
_DCL_CODE_ACCESS void DCL_loadDF22asZPK(DCL_DF22 *df, DCL_ZPK3 *zpk)
Loads the DF22 shadow coefficients from a ZPK3 description.
Definition: dcl_df22.h:223
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.
Definition: dcl_df22.h:299
DCL_ZPK3::p1
float complex p1
Complex poles 1.
Definition: dcl_zpk3.h:64
DCL_runDF22Clamp
_DCL_CRIT_ACCESS 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:420
DCL_resetDF22
_DCL_CODE_ACCESS void DCL_resetDF22(DCL_DF22 *df)
Resets DF22 internal storage data with interrupt protection.
Definition: dcl_df22.h:146
dcl_interrupt_t
uint32_t dcl_interrupt_t
Definition: dcl_common.h:109
DCL_runDF22PartialUpdate
_DCL_CRIT_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:405
dcl_df22
DCL_DF22 object for storing df22 specific parameters.
Definition: dcl_df22.h:70
DCL_DF22_SPS::a2
float32_t a2
neg. coefficient to u(k-2)
Definition: dcl_df22.h:62
DCL_isZero
#define DCL_isZero(x)
Determines floating point numerical proximity to zero.
Definition: dcl_macro.h:75
dcl_param_range_err
@ dcl_param_range_err
Parameter range exceeded.
Definition: dcl_error.h:58
dcl_df22::a1
float32_t a1
neg. coefficient to u(k-1)
Definition: dcl_df22.h:75
dcl_df22::css
DCL_CSS * css
configuration & debugging
Definition: dcl_df22.h:84
dcl_df22::b0
float32_t b0
pos. coefficient to e(k)
Definition: dcl_df22.h:72
dcl_df22::a2
float32_t a2
neg. coefficient to u(k-2)
Definition: dcl_df22.h:76
DCL_DF22_SPS
Defines DCL_DF22 shadow parameter set used for updating compensator parameter.
Definition: dcl_df22.h:57
dcl_df22::sps
DCL_DF22_SPS * sps
updates compensator parameter
Definition: dcl_df22.h:83
dcl_param_warn_err
@ dcl_param_warn_err
Parameter warning.
Definition: dcl_error.h:60
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:263
DCL_DF22_SPS::b1
float32_t b1
pos. coefficient to e(k-1)
Definition: dcl_df22.h:59
DCL_CSS
Defines the controller common support structure.
Definition: dcl_css.h:56
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_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:57
DCL_ZPK3
Defines the DCL_ZPK3 structure.
Definition: dcl_zpk3.h:60
DCL_runDF22
_DCL_CRIT_ACCESS float32_t DCL_runDF22(DCL_DF22 *df, float32_t ek)
Executes a 2nd order Direct Form 2 controller.
Definition: dcl_df22.h:376
_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_df22::x2
float32_t x2
x2 = b2*e(k-2) - a2*u(k-2)
Definition: dcl_df22.h:80
DCL_DF22_SPS::a1
float32_t a1
neg. coefficient to u(k-1)
Definition: dcl_df22.h:61
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_df22::x1
float32_t x1
x1 = b1*e(k-1) - a1*u(k-1) + x2
Definition: dcl_df22.h:79
float32_t
float float32_t
Definition: dcl_common.h:58
DCL_DF22_SPS::b0
float32_t b0
pos. coefficient to e(k)
Definition: dcl_df22.h:58
DCL_ZPK3::z1
float complex z1
Complex zeros 1.
Definition: dcl_zpk3.h:61
DCL_disableInts
#define DCL_disableInts()
Define enable and disable interrupt operations.
Definition: dcl_common.h:107