AM263x Motor Control SDK  09.00.00
dcl_df11.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_DF11_H_
35 #define _DCL_DF11_H_
36 
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40 
50 #include "../dcl_common.h"
51 
52 //--- Direct Form 1 - 1st order ----------------------------------------------
53 
57 typedef struct dcl_df11_sps
58 {
62 } DCL_DF11_SPS;
63 
64 #define DF11_SPS_DEFAULTS { 0.5f, 0.5f, 1.0f }
65 
68 typedef _DCL_VOLATILE struct dcl_df11
69 {
70  /* compensator parameter */
74 
75  /* internal storage */
78 
79  /* miscellaneous */
83 
86 #define DF11_DEFAULTS { 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, \
87  &(DCL_DF11_SPS)DF11_SPS_DEFAULTS, &(DCL_CSS)DCL_CSS_DEFAULTS }
88 
96 #define DF11_INT_DEFAULTS .d1=0.0f, .d2=0.0f, .sps=&(DCL_DF11_SPS)DF11_SPS_DEFAULTS, .css=&(DCL_CSS)DCL_CSS_DEFAULTS
97 
103 #define DCL_initDF11() &(DCL_DF11)DF11_DEFAULTS
104 
111 #define DCL_initDF11asParam(_b0,_b1,_a1) &(DCL_DF11){ .b0=_b0, .b1=_b1, .a1=_a1, \
112  DF11_INT_DEFAULTS }
113 
124 #define DCL_initDF11asSPS(df_ptr,sps_ptr) \
125 ({ \
126  DCL_DF11* new_df = (df_ptr) ? df_ptr : DCL_initDF11(); \
127  DCL_DF11_SPS* new_sps = (sps_ptr) ? sps_ptr : &(DCL_DF11_SPS)DF11_SPS_DEFAULTS;\
128  if(sps_ptr) \
129  { \
130  *new_df = (DCL_DF11){ (new_sps)->b0, (new_sps)->b1, (new_sps)->a1, \
131  0.0f, 0.0f, (DCL_DF11_SPS*)new_sps, &(DCL_CSS)DCL_CSS_DEFAULTS }; \
132  } \
133  new_df; \
134 })
135 
142 {
144  df->d1 = df->d2 = 0.0f;
145  DCL_restoreInts(ints);
146 }
147 
154 {
155  df->b0 = df->sps->b0;
156  df->b1 = df->sps->b1;
157  df->a1 = df->sps->a1;
158 }
159 
167 {
168  if (!DCL_getUpdateStatus(df))
169  {
172  df->b0 = df->sps->b0;
173  df->b1 = df->sps->b1;
174  df->a1 = df->sps->a1;
176  DCL_restoreInts(ints);
177  return true;
178  }
179  return false;
180 }
181 
192 {
193  if (DCL_getPendingStatus(df) && DCL_updateDF11(df))
194  {
196  return true;
197  }
198  return false;
199 }
200 
208 {
209  df->sps->b0 = df->b0;
210  df->sps->b1 = df->b1;
211  df->sps->a1 = df->a1;
212 }
213 
221 {
222  return(DCL_isStablePn1(df->sps->a1));
223 }
224 
235 {
236 #ifdef DCL_ERROR_HANDLING_ENABLED
237  uint32_t err_code = dcl_none;
238  err_code |= DCL_isZero(cimagf(zpk->z1)) ? dcl_none : dcl_param_warn_err;
239  err_code |= DCL_isZero(cimagf(zpk->p1)) ? dcl_none : dcl_param_warn_err;
240  if (err_code)
241  {
242  DCL_setError(df,err_code);
243  DCL_getErrorInfo(df);
245  }
246 #endif
247 
248  float32_t t_sec = df->css->t_sec;
249  float32_t a0p = 2.0f - (float32_t) crealf(zpk->p1) * t_sec;
250  df->sps->b0 = zpk->K * (2.0f - (float32_t) crealf(zpk->z1) * t_sec) / a0p;
251  df->sps->b1 = zpk->K * (-2.0f - (float32_t) crealf(zpk->z1) * t_sec) / a0p;
252  df->sps->a1 = (-2.0f - (float32_t) crealf(zpk->p1) * t_sec) / a0p;
253 }
254 
265 {
266 #ifdef DCL_ERROR_HANDLING_ENABLED
267  uint32_t err_code = dcl_none;
268  err_code |= (Kp < 0.0f) ? dcl_param_invalid_err : dcl_none;
269  err_code |= (Ki < 0.0f) ? dcl_param_invalid_err : dcl_none;
270  if (err_code)
271  {
272  DCL_setError(df,err_code);
273  DCL_getErrorInfo(df);
275  }
276 #endif
277 
278  float32_t t_sec = df->css->t_sec;
279  df->sps->b0 = Kp * ((Ki * t_sec) + 2.0f) / 2.0f;
280  df->sps->b1 = Kp * ((Ki * t_sec) - 2.0f) / 2.0f;
281  df->sps->a1 = -1.0f;
282 }
283 
292 {
293  df->d2 = (ek * df->b0) + (df->d1 * df->b1) - (df->d2 * df->a1);
294  df->d1 = ek;
295 
296  return(df->d2);
297 }
298 
301 #ifdef __cplusplus
302 }
303 #endif // extern "C"
304 
305 #endif // _DCL_DF11_H_
DCL_ZPK3::K
float32_t K
Real gain.
Definition: dcl_zpk3.h:69
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_clearPendingStatus
#define DCL_clearPendingStatus(p)
Definition: dcl_css.h:129
DCL_setUpdateStatus
#define DCL_setUpdateStatus(p)
Macros to set and clear the update-in-progress flag.
Definition: dcl_css.h:116
DCL_DF11
_DCL_VOLATILE struct dcl_df11 DCL_DF11
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_df11
DCL_DF11 object for storing df11 specific parameters.
Definition: dcl_df11.h:69
DCL_pendingUpdateDF11
_DCL_CODE_ACCESS _DCL_CODE_SECTION bool DCL_pendingUpdateDF11(DCL_DF11 *df)
A conditional update based on the pending-for-update flag. If the pending status is set,...
Definition: dcl_df11.h:191
DCL_clearUpdateStatus
#define DCL_clearUpdateStatus(p)
Definition: dcl_css.h:117
_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_none
@ dcl_none
No error.
Definition: dcl_error.h:57
DCL_isStablePn1
_DCL_CODE_ACCESS bool DCL_isStablePn1(float32_t a1)
Determines stability of a first order real polynomial P(z) = z + a1.
Definition: dcl_stability.h:58
DCL_resetDF11
_DCL_CODE_ACCESS void DCL_resetDF11(DCL_DF11 *df)
Resets DF11 internal storage data with interrupt protection.
Definition: dcl_df11.h:141
DCL_loadDF11asPI
_DCL_CODE_ACCESS void DCL_loadDF11asPI(DCL_DF11 *df, float32_t Kp, float32_t Ki)
Loads compensator coefficients to emulate series form PI Note: Sampling period df->css->t_sec are use...
Definition: dcl_df11.h:264
dcl_interrupt_t
uint32_t dcl_interrupt_t
Definition: dcl_common.h:96
dcl_df11::d2
float32_t d2
u(k-1)
Definition: dcl_df11.h:77
DCL_isZero
#define DCL_isZero(x)
Determines floating point numerical proximity to zero.
Definition: dcl_aux.h:77
DCL_updateDF11SPS
_DCL_CODE_ACCESS void DCL_updateDF11SPS(DCL_DF11 *df)
Update SPS parameter with active param, userful when needing to update only few active param from SPS...
Definition: dcl_df11.h:207
DCL_loadDF11asZPK
_DCL_CODE_ACCESS void DCL_loadDF11asZPK(DCL_DF11 *df, DCL_ZPK3 *zpk)
Loads the DF11 shadow coefficients from a ZPK3 description Note: Sampling period df->css->t_sec are u...
Definition: dcl_df11.h:234
dcl_param_invalid_err
@ dcl_param_invalid_err
Parameter not valid.
Definition: dcl_error.h:59
DCL_DF11_SPS::a1
float32_t a1
neg. coefficient to u(k-1)
Definition: dcl_df11.h:61
DCL_DF11_SPS::b0
float32_t b0
pos. coefficient to e(k)
Definition: dcl_df11.h:59
DCL_isStableDF11
_DCL_CODE_ACCESS bool DCL_isStableDF11(DCL_DF11 *df)
Determines stability of the shadow DF11 compensator.
Definition: dcl_df11.h:220
DCL_DF11_SPS::b1
float32_t b1
pos. coefficient to e(k-1)
Definition: dcl_df11.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_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_DF11_SPS
Defines DCL_DF11 shadow parameter set used for updating compensator parameter.
Definition: dcl_df11.h:58
dcl_df11::css
DCL_CSS * css
Pointer to the common support structure.
Definition: dcl_df11.h:81
DCL_runDF11
_DCL_CODE_ACCESS _DCL_CODE_SECTION float32_t DCL_runDF11(DCL_DF11 *df, float32_t ek)
Executes a 1st order Direct Form 1 controller.
Definition: dcl_df11.h:291
dcl_df11::a1
float32_t a1
neg. coefficient to u(k-1)
Definition: dcl_df11.h:73
DCL_getErrorInfo
#define DCL_getErrorInfo(ptr)
Macro to store error info in CSS.
Definition: dcl_error.h:97
DF11_Handle
_DCL_VOLATILE struct dcl_df11 * DF11_Handle
DCL_setError
#define DCL_setError(ptr, code)
Macro to set error code in CSS.
Definition: dcl_error.h:79
DCL_updateDF11
_DCL_CODE_ACCESS _DCL_CODE_SECTION bool DCL_updateDF11(DCL_DF11 *df)
Updates DF11 parameter from its SPS parameter with interrupt protection.
Definition: dcl_df11.h:166
dcl_df11::b0
float32_t b0
pos. coefficient to e(k)
Definition: dcl_df11.h:71
dcl_df11::sps
DCL_DF11_SPS * sps
Pointer to the shadow parameter set.
Definition: dcl_df11.h:80
DCL_fupdateDF11
_DCL_CODE_ACCESS void DCL_fupdateDF11(DCL_DF11 *df)
Loads DF11 tuning parameter from its SPS parameter.
Definition: dcl_df11.h:153
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_df11::d1
float32_t d1
e(k-1)
Definition: dcl_df11.h:76
dcl_df11::b1
float32_t b1
pos. coefficient to e(k-1)
Definition: dcl_df11.h:72
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