AM263x Motor Control SDK  09.02.00
dcl_df11.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 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 {
143  dcl_interrupt_t ints;
144  ints = DCL_disableInts();
145  df->d1 = df->d2 = 0.0f;
146  DCL_restoreInts(ints);
147 }
148 
155 {
156  df->b0 = df->sps->b0;
157  df->b1 = df->sps->b1;
158  df->a1 = df->sps->a1;
159 }
160 
167 {
168  dcl_interrupt_t ints;
169  ints = DCL_disableInts();
170  df->b0 = df->sps->b0;
171  df->b1 = df->sps->b1;
172  df->a1 = df->sps->a1;
173  DCL_restoreInts(ints);
174 }
175 
186 {
187  if (DCL_getUpdateStatus(df))
188  {
191  return true;
192  }
193  return false;
194 }
195 
203 {
204  return(DCL_isStablePn1(df->sps->a1));
205 }
206 
217 {
218 #ifdef DCL_ERROR_HANDLING_ENABLED
219  uint32_t err_code = dcl_none;
220  err_code |= DCL_isZero(cimagf(zpk->z1)) ? dcl_none : dcl_param_warn_err;
221  err_code |= DCL_isZero(cimagf(zpk->p1)) ? dcl_none : dcl_param_warn_err;
222  if (err_code)
223  {
224  DCL_setError(df,err_code);
225  DCL_getErrorInfo(df);
227  }
228 #endif
229 
230  float32_t T = df->css->T;
231  float32_t a0p = 2.0f - (float32_t) crealf(zpk->p1) * T;
232  df->sps->b0 = zpk->K * (2.0f - (float32_t) crealf(zpk->z1) * T) / a0p;
233  df->sps->b1 = zpk->K * (-2.0f - (float32_t) crealf(zpk->z1) * T) / a0p;
234  df->sps->a1 = (-2.0f - (float32_t) crealf(zpk->p1) * T) / a0p;
235 }
236 
247 {
248 #ifdef DCL_ERROR_HANDLING_ENABLED
249  uint32_t err_code = dcl_none;
250  err_code |= (Kp < 0.0f) ? dcl_param_invalid_err : dcl_none;
251  err_code |= (Ki < 0.0f) ? dcl_param_invalid_err : dcl_none;
252  if (err_code)
253  {
254  DCL_setError(df,err_code);
255  DCL_getErrorInfo(df);
257  }
258 #endif
259 
260  float32_t T = df->css->T;
261  df->sps->b0 = Kp * ((Ki * T) + 2.0f) / 2.0f;
262  df->sps->b1 = Kp * ((Ki * T) - 2.0f) / 2.0f;
263  df->sps->a1 = -1.0f;
264 }
265 
274 {
275  df->d2 = (ek * df->b0) + (df->d1 * df->b1) - (df->d2 * df->a1);
276  df->d1 = ek;
277 
278  return(df->d2);
279 }
280 
283 #ifdef __cplusplus
284 }
285 #endif // extern "C"
286 
287 #endif // _DCL_DF11_H_
DCL_forceUpdateDF11
_DCL_CODE_ACCESS void DCL_forceUpdateDF11(DCL_DF11 *df)
Loads DF11 tuning parameter from its SPS parameter without interrupt protection.
Definition: dcl_df11.h:154
DCL_ZPK3::K
float32_t K
Real gain.
Definition: dcl_zpk3.h:69
DCL_updateDF11NoCheck
_DCL_CODE_ACCESS void DCL_updateDF11NoCheck(DCL_DF11 *df)
Loads DF11 tuning parameter from its SPS parameter with interrupt protection.
Definition: dcl_df11.h:166
DCL_updateDF11
_DCL_CODE_ACCESS bool DCL_updateDF11(DCL_DF11 *df)
A conditional update based on the update flag. If the update status is set, the function will update ...
Definition: dcl_df11.h:185
dcl_none
@ dcl_none
No error.
Definition: dcl_error.h:57
DCL_DF11
_DCL_VOLATILE struct dcl_df11 DCL_DF11
DCL_runErrorHandler
#define DCL_runErrorHandler(ptr)
Prototype for basic error handler.
Definition: dcl_error.h:108
DCL_getUpdateStatus
#define DCL_getUpdateStatus(p)
Determine whether a parameter update-in-progress flag is set.
Definition: dcl_css.h:122
dcl_df11
DCL_DF11 object for storing df11 specific parameters.
Definition: dcl_df11.h:69
DCL_clearUpdateStatus
#define DCL_clearUpdateStatus(p)
Definition: dcl_css.h:116
DCL_runDF11
_DCL_CRIT_ACCESS float32_t DCL_runDF11(DCL_DF11 *df, float32_t ek)
Executes a 1st order Direct Form 1 controller.
Definition: dcl_df11.h:273
_DCL_CODE_ACCESS
#define _DCL_CODE_ACCESS
Defines the scope of dcl functions.
Definition: dcl_common.h:63
DCL_ZPK3::p1
float complex p1
Complex poles 1.
Definition: dcl_zpk3.h:66
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.
Definition: dcl_df11.h:246
dcl_interrupt_t
uint32_t dcl_interrupt_t
Definition: dcl_common.h:107
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_macro.h:77
DCL_loadDF11asZPK
_DCL_CODE_ACCESS void DCL_loadDF11asZPK(DCL_DF11 *df, DCL_ZPK3 *zpk)
Loads the DF11 shadow coefficients from a ZPK3 description.
Definition: dcl_df11.h:216
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_param_warn_err
@ dcl_param_warn_err
Parameter warning.
Definition: dcl_error.h:60
DCL_isStableDF11
_DCL_CODE_ACCESS bool DCL_isStableDF11(DCL_DF11 *df)
Determines stability of the shadow DF11 compensator.
Definition: dcl_df11.h:202
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_restoreInts
#define DCL_restoreInts(v)
Definition: dcl_common.h:106
dcl_param_invalid_err
@ dcl_param_invalid_err
Parameter not valid.
Definition: dcl_error.h:59
DCL_ZPK3
Defines the DCL_ZPK3 structure.
Definition: dcl_zpk3.h:62
_DCL_VOLATILE
#define _DCL_VOLATILE
Defines volatile for DCL strctures.
Definition: dcl_common.h:79
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_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: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:70
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
float32_t
float float32_t
Definition: dcl_common.h:58
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:105