AM263x Motor Control SDK  09.00.00
dcl_df13.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_DF13_H_
35 #define _DCL_DF13_H_
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
50 #include "../dcl_common.h"
51 
52 //--- Direct Form 1 - 3rd order ----------------------------------------------
53 
57 typedef struct dcl_df13_sps {
65 } DCL_DF13_SPS;
66 
67 #define DF13_SPS_DEFAULTS { 0.25f, 0.25f, 0.25f, 0.25f, 0.0f, 0.0f, 0.0f }
68 
71 typedef _DCL_VOLATILE struct dcl_df13
72 {
73  /* compensator parameter */
81 
82  /* internal storage */
89 
90  /* miscellaneous */
94 
97 #define DF13_DEFAULTS { 0.25f, 0.25f, 0.25f, 0.25f, 0.0f, 0.0f, 0.0f, \
98  0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, \
99  &(DCL_DF13_SPS)DF13_SPS_DEFAULTS, &(DCL_CSS)DCL_CSS_DEFAULTS }
100 
109 #define DF13_INT_DEFAULTS .d1=0.0f, .d2=0.0f, .d3=0.0f, .d4=0.0f, .d5=0.0f, \
110  .d6=0.0f, .sps=&(DCL_DF13_SPS)DF13_SPS_DEFAULTS, \
111  .css=&(DCL_CSS)DCL_CSS_DEFAULTS
112 
118 #define DCL_initDF13() &(DCL_DF13)DF13_DEFAULTS
119 
126 #define DCL_initDF13asParam(_b0,_b1,_b2,_b3,_a1,_a2,_a3) &(DCL_DF13){ .b0=_b0, .b1=_b1, \
127  .b2=_b2, .b3=_b3, .a1=_a1, .a2=_a2, .a3=_a3, DF13_INT_DEFAULTS }
128 
139 #define DCL_initDF13asSPS(df_ptr,sps_ptr) \
140 ({ \
141  DCL_DF13* new_df = (df_ptr) ? df_ptr : DCL_initDF13(); \
142  DCL_DF13_SPS* new_sps = (sps_ptr) ? sps_ptr : &(DCL_DF13_SPS)DF13_SPS_DEFAULTS; \
143  if(sps_ptr) \
144  { \
145  *new_df =(DCL_DF13){ (new_sps)->b0, (new_sps)->b1, (new_sps)->b2, (new_sps)->b3, \
146  (new_sps)->a1, (new_sps)->a2, (new_sps)->a3, 0.0f, 0.0f, 0.0f, 0.0f, \
147  0.0f, 0.0f, (DCL_DF13_SPS*)new_sps, &(DCL_CSS)DCL_CSS_DEFAULTS }; \
148  } \
149  new_df; \
150 })
151 
159 {
161  df->d1 = df->d2 = df->d3 = df->d4 = df->d5 = df->d6 = 0.0f;
162  DCL_restoreInts(ints);
163 }
164 
171 {
172  df->b0 = df->sps->b0;
173  df->b1 = df->sps->b1;
174  df->b2 = df->sps->b2;
175  df->b3 = df->sps->b3;
176  df->a1 = df->sps->a1;
177  df->a2 = df->sps->a2;
178  df->a3 = df->sps->a3;
179 }
180 
188 {
189  if (!DCL_getUpdateStatus(df))
190  {
193  df->b0 = df->sps->b0;
194  df->b1 = df->sps->b1;
195  df->b2 = df->sps->b2;
196  df->b3 = df->sps->b3;
197  df->a1 = df->sps->a1;
198  df->a2 = df->sps->a2;
199  df->a3 = df->sps->a3;
201  DCL_restoreInts(ints);
202  return true;
203  }
204  return false;
205 }
206 
217 {
218  if (DCL_getPendingStatus(df) && DCL_updateDF13(df))
219  {
221  return true;
222  }
223  return false;
224 }
225 
233 {
234  df->sps->b0 = df->b0;
235  df->sps->b1 = df->b1;
236  df->sps->b2 = df->b2;
237  df->sps->b3 = df->b3;
238  df->sps->a1 = df->a1;
239  df->sps->a2 = df->a2;
240  df->sps->a3 = df->a3;
241 }
242 
250 {
251  return(DCL_isStablePn3(1.0f, df->sps->a1, df->sps->a2, df->sps->a3));
252 }
253 
263 {
264 #ifdef DCL_ERROR_HANDLING_ENABLED
265  uint32_t err_code = dcl_none;
266  err_code |= DCL_isZero(cimagf(zpk->z1) + cimagf(zpk->z2) + cimagf(zpk->z3)) ? dcl_none : dcl_param_invalid_err;
267  err_code |= DCL_isZero(cimagf(zpk->p1) + cimagf(zpk->p2) + cimagf(zpk->p3)) ? dcl_none : dcl_param_invalid_err;
268  if (err_code)
269  {
270  DCL_setError(df,err_code);
271  DCL_getErrorInfo(df);
273  }
274 #endif
275 
276  float32_t beta2 = -(float32_t) crealf(zpk->z1 + zpk->z2 + zpk->z3);
277  float32_t beta1 = (float32_t) crealf((zpk->z1 * zpk->z2) + (zpk->z2 * zpk->z3) + (zpk->z1 * zpk->z3));
278  float32_t beta0 = -(float32_t) crealf(zpk->z1 * zpk->z2 * zpk->z3);
279 
280  float32_t alpha2 = -(float32_t) crealf(zpk->p1 + zpk->p2 + zpk->p3);
281  float32_t alpha1 = (float32_t) crealf((zpk->p1 * zpk->p2) + (zpk->p2 * zpk->p3) + (zpk->p1 * zpk->p3));
282  float32_t alpha0 = -(float32_t) crealf(zpk->p1 * zpk->p2 * zpk->p3);
283 
284  float32_t t_sec = df->css->t_sec;
285 
286  float32_t a0p = 8.0f + (alpha2 * 4.0f * t_sec) + (alpha1 * 2.0f * t_sec * t_sec) + (alpha0 * t_sec * t_sec * t_sec);
287 
288  df->sps->b0 = zpk->K * (8.0f + (beta2 * 4.0f * t_sec) + (beta1 * 2.0f * t_sec * t_sec) + (beta0 * t_sec * t_sec * t_sec)) / a0p;
289  df->sps->b1 = zpk->K * (-24.0f - (beta2 * 4.0f * t_sec) + (beta1 * 2.0f * t_sec * t_sec) + (3.0f * beta0 * t_sec * t_sec * t_sec)) / a0p;
290  df->sps->b2 = zpk->K * (24.0f - (beta2 * 4.0f * t_sec) - (beta1 * 2.0f * t_sec * t_sec) + (3.0f * beta0 * t_sec * t_sec * t_sec)) / a0p;
291  df->sps->b3 = zpk->K * (-8.0f + (beta2 * 4.0f * t_sec) - (beta1 * 2.0f * t_sec * t_sec) + (beta0 * t_sec * t_sec * t_sec)) / a0p;
292 
293  df->sps->a1 = (-24.0f - (alpha2 * 4.0f * t_sec) + (alpha1 * 2.0f * t_sec * t_sec) + (3.0f * alpha0 * t_sec * t_sec * t_sec)) / a0p;
294  df->sps->a2 = (24.0f - (alpha2 * 4.0f * t_sec) - (alpha1 * 2.0f * t_sec * t_sec) + (3.0f * alpha0 * t_sec * t_sec * t_sec)) / a0p;
295  df->sps->a3 = (-8.0f + (alpha2 * 4.0f * t_sec) - (alpha1 * 2.0f * t_sec * t_sec) + (alpha0 * t_sec * t_sec * t_sec)) / a0p;
296 }
297 
306 {
307  float32_t v4 = (ek * df->b0) + (df->d1 * df->b1) + (df->d2 * df->b2) + (df->d3 * df->b3) - (df->d4 * df->a1) - (df->d5 * df->a2) - (df->d6 * df->a3);
308  df->d3 = df->d2;
309  df->d2 = df->d1;
310  df->d1 = ek;
311  df->d6 = df->d5;
312  df->d5 = df->d4;
313  df->d4 = v4;
314 
315  return(v4);
316 }
317 
327 {
328  float32_t v4 = (ek * df->b0) + (df->d1 * df->b1) + (df->d2 * df->b2) + (df->d3 * df->b3) - (df->d4 * df->a1) - (df->d5 * df->a2) - (df->d6 * df->a3);
329  return(v4);
330 }
331 
340 {
341  df->d3 = df->d2;
342  df->d2 = df->d1;
343  df->d1 = ek;
344  df->d6 = df->d5;
345  df->d5 = df->d4;
346  df->d4 = uk;
347 }
348 
359 {
361  bool is_clamped = DCL_runClamp(&uk, Umax, Umin);
362  if(!is_clamped) DCL_runDF13PartialUpdate(df, ek, uk);
363  return(uk);
364 }
365 
368 #ifdef __cplusplus
369 }
370 #endif // extern "C"
371 
372 #endif // _DCL_DF13_H_
DCL_isStablePn3
_DCL_CODE_ACCESS bool DCL_isStablePn3(float32_t a0, float32_t a1, float32_t a2, float32_t a3)
Determines stability of a third order polynomial with real coefficients P(z) = a0 z^3 + a1 z^2 + a2 z...
Definition: dcl_stability.h:93
DCL_fupdateDF13
_DCL_CODE_ACCESS void DCL_fupdateDF13(DCL_DF13 *df)
Loads DF13 tuning parameter from its SPS parameter.
Definition: dcl_df13.h:170
DCL_DF13_SPS::b2
float32_t b2
pos. coefficient to e(k-2)
Definition: dcl_df13.h:60
DCL_ZPK3::K
float32_t K
Real gain.
Definition: dcl_zpk3.h:69
DCL_loadDF13asZPK
_DCL_CODE_ACCESS void DCL_loadDF13asZPK(DCL_DF13 *df, DCL_ZPK3 *zpk)
Loads the DF13 shadow coefficients from a ZPK3 description. Note: Sampling period df->css->t_sec are ...
Definition: dcl_df13.h:262
DCL_updateDF13
_DCL_CODE_ACCESS _DCL_CODE_SECTION bool DCL_updateDF13(DCL_DF13 *df)
Updates DF13 parameter from its SPS parameter with interrupt protection.
Definition: dcl_df13.h:187
DF13_Handle
_DCL_VOLATILE struct dcl_df13 * DF13_Handle
DCL_ZPK3::z2
float complex z2
Complex zeros 2.
Definition: dcl_zpk3.h:64
DCL_isStableDF13
_DCL_CODE_ACCESS bool DCL_isStableDF13(DCL_DF13 *df)
Determines stability of the shadow compensator.
Definition: dcl_df13.h:249
dcl_df13::b3
float32_t b3
pos. coefficient to e(k-3)
Definition: dcl_df13.h:77
DCL_DF13_SPS::b3
float32_t b3
pos. coefficient to e(k-3)
Definition: dcl_df13.h:61
_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_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_df13::d2
float32_t d2
e(k-2)
Definition: dcl_df13.h:84
DCL_resetDF13
_DCL_CODE_ACCESS void DCL_resetDF13(DCL_DF13 *df)
Resets DF13 internal storage data with interrupt protection Implemented as inline C function.
Definition: dcl_df13.h:158
DCL_runDF13PartialUpdate
_DCL_CODE_ACCESS void DCL_runDF13PartialUpdate(DCL_DF13 *df, float32_t ek, float32_t uk)
Update DF13 controller based on pre-computed control effort.
Definition: dcl_df13.h:339
dcl_df13::sps
DCL_DF13_SPS * sps
updates compensator parameter
Definition: dcl_df13.h:91
DCL_DF13_SPS::a2
float32_t a2
neg. coefficient to u(k-2)
Definition: dcl_df13.h:63
dcl_df13::a2
float32_t a2
neg. coefficient to u(k-2)
Definition: dcl_df13.h:79
DCL_runErrorHandler
#define DCL_runErrorHandler(ptr)
Prototype for basic error handler.
Definition: dcl_error.h:107
DCL_getUpdateStatus
#define DCL_getUpdateStatus(p)
Determine whether a parameter update-in-progress flag is set.
Definition: dcl_css.h:123
DCL_runDF13Clamp
_DCL_CODE_ACCESS _DCL_CODE_SECTION float32_t DCL_runDF13Clamp(DCL_DF13 *df, float32_t ek, float32_t Umax, float32_t Umin)
Executes a 3rd order Direct Form 1 controller with clamp.
Definition: dcl_df13.h:358
DCL_clearUpdateStatus
#define DCL_clearUpdateStatus(p)
Definition: dcl_css.h:117
DCL_DF13_SPS::b0
float32_t b0
pos. coefficient to e(k)
Definition: dcl_df13.h:58
dcl_df13::d1
float32_t d1
e(k-1)
Definition: dcl_df13.h:83
dcl_df13::a3
float32_t a3
neg. coefficient to u(k-3)
Definition: dcl_df13.h:80
_DCL_CODE_ACCESS
#define _DCL_CODE_ACCESS
Defines the scope of dcl functions (static inline/extern inline/none)
Definition: dcl_common.h:55
DCL_ZPK3::p1
float complex p1
Complex poles 1.
Definition: dcl_zpk3.h:66
dcl_df13::css
DCL_CSS * css
configuration & debugging
Definition: dcl_df13.h:92
dcl_df13::b1
float32_t b1
pos. coefficient to e(k-1)
Definition: dcl_df13.h:75
dcl_none
@ dcl_none
No error.
Definition: dcl_error.h:57
DCL_pendingUpdateDF13
_DCL_CODE_ACCESS _DCL_CODE_SECTION bool DCL_pendingUpdateDF13(DCL_DF13 *df)
A conditional update based on the pending-for-update flag. If the pending status is set,...
Definition: dcl_df13.h:216
DCL_DF13_SPS::a1
float32_t a1
neg. coefficient to u(k-1)
Definition: dcl_df13.h:62
dcl_df13
DCL_DF13 object for storing df13 specific parameters.
Definition: dcl_df13.h:72
dcl_interrupt_t
uint32_t dcl_interrupt_t
Definition: dcl_common.h:96
DCL_ZPK3::z3
float complex z3
Complex zeros 3.
Definition: dcl_zpk3.h:65
DCL_isZero
#define DCL_isZero(x)
Determines floating point numerical proximity to zero.
Definition: dcl_aux.h:77
DCL_runDF13PartialCompute
_DCL_CODE_ACCESS float32_t DCL_runDF13PartialCompute(DCL_DF13 *df, float32_t ek)
Immediate computation to obtain DF13 servo error without updating the controller.
Definition: dcl_df13.h:326
DCL_runDF13
_DCL_CODE_ACCESS _DCL_CODE_SECTION float32_t DCL_runDF13(DCL_DF13 *df, float32_t ek)
Executes a 3rd order Direct Form 1 controller.
Definition: dcl_df13.h:305
dcl_param_invalid_err
@ dcl_param_invalid_err
Parameter not valid.
Definition: dcl_error.h:59
DCL_DF13_SPS
Defines the DCL_DF13 shadow parameter set used for updating compensator parameter.
Definition: dcl_df13.h:57
dcl_df13::d4
float32_t d4
u(k-1)
Definition: dcl_df13.h:86
DCL_DF13
_DCL_VOLATILE struct dcl_df13 DCL_DF13
dcl_df13::a1
float32_t a1
neg. coefficient to u(k-1)
Definition: dcl_df13.h:78
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_df13::d6
float32_t d6
u(k-3)
Definition: dcl_df13.h:88
DCL_restoreInts
#define DCL_restoreInts(v)
Definition: dcl_common.h:95
DCL_ZPK3::p3
float complex p3
Complex poles 3.
Definition: dcl_zpk3.h:68
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_df13::b2
float32_t b2
pos. coefficient to e(k-2)
Definition: dcl_df13.h:76
DCL_getErrorInfo
#define DCL_getErrorInfo(ptr)
Macro to store error info in CSS.
Definition: dcl_error.h:97
DCL_updateDF13SPS
_DCL_CODE_ACCESS void DCL_updateDF13SPS(DCL_DF13 *df)
Update SPS parameter with active param, userful when needing to update only few active param from SPS...
Definition: dcl_df13.h:232
DCL_setError
#define DCL_setError(ptr, code)
Macro to set error code in CSS.
Definition: dcl_error.h:79
dcl_df13::d5
float32_t d5
u(k-2)
Definition: dcl_df13.h:87
dcl_df13::d3
float32_t d3
e(k-3)
Definition: dcl_df13.h:85
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_DF13_SPS::b1
float32_t b1
pos. coefficient to e(k-1)
Definition: dcl_df13.h:59
DCL_DF13_SPS::a3
float32_t a3
neg. coefficient to u(k-3)
Definition: dcl_df13.h:64
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_df13::b0
float32_t b0
pos. coefficient to e(k)
Definition: dcl_df13.h:74