AM263x Motor Control SDK  09.02.00
dcl_pi2.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2024 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_PI2_H_
34 #define _DCL_PI2_H_
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
48 #include "../dcl_common.h"
49 
50 //--- Linear PI2 controller --------------------------------------------------
51 
55 typedef struct dcl_pi2_sps
56 {
61 } DCL_PI2_SPS;
62 
65 #define PI2_SPS_DEFAULTS { 1.0f, 0.0f, 1.0f, -1.0f }
66 
70 typedef _DCL_VOLATILE struct dcl_pi2
71 {
72  /* controller parameter */
77 
78  /* internal storage */
83 
84  /* miscellaneous */
88 
91 #define PI2_DEFAULTS { 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, \
92  &(DCL_PI2_SPS)PI2_SPS_DEFAULTS, &(DCL_CSS)DCL_CSS_DEFAULTS }
93 
102 #define PI2_INT_DEFAULTS .i6=0.0f, .i9=0.0f, .i12=1.0f, .i13=1.0f, \
103 .sps=&(DCL_PI2_SPS)PI2_SPS_DEFAULTS, .css=&(DCL_CSS)DCL_CSS_DEFAULTS
104 
110 #define DCL_initPI2() &(DCL_PI2)PI2_DEFAULTS
111 
118 #define DCL_initPI2asParam(kp,ki,umax,umin) &(DCL_PI2){ .Kp=kp, .Ki=ki, \
119  .Umax=umax, .Umin=umin, PI2_INT_DEFAULTS }
120 
131 #define DCL_initPI2asSPS(pi2_ptr,sps_ptr) \
132 ({ \
133  DCL_PI2* new_pi = (pi2_ptr) ? pi2_ptr : DCL_initPI2(); \
134  DCL_PI2_SPS* new_sps = (sps_ptr) ? sps_ptr : &(DCL_PI2_SPS)PI2_SPS_DEFAULTS; \
135  if(sps_ptr) \
136  { \
137  *new_pi = (DCL_PI){ (new_sps)->Kp, (new_sps)->Ki, (new_sps)->Umax, (new_sps)->Umin,\
138  0.0f, 0.0f, 1.0f, 1.0f, (DCL_PI_SPS*)new_sps, &(DCL_CSS)DCL_CSS_DEFAULTS }; \
139  } \
140  new_pi; \
141 })
142 
149 {
150  dcl_interrupt_t ints;
151  ints = DCL_disableInts();
152  pi2->i6 = pi2->i9 = 0.0f;
153  pi2->i12 = pi2->i13 = 1.0f;
154  DCL_restoreInts(ints);
155 }
156 
163 {
164 
165 #ifdef DCL_ERROR_HANDLING_ENABLED
166  uint32_t err_code = dcl_none;
167  err_code |= (pi2->sps->Umax <= pi2->sps->Umin) ? dcl_param_invalid_err : dcl_none;
168  err_code |= (pi2->css->T <= 0.0f) ? dcl_param_range_err : dcl_none;
169  err_code |= (pi2->sps->Kp < 0.0f) ? dcl_param_range_err : dcl_none;
170  err_code |= (pi2->sps->Ki < 0.0f) ? dcl_param_range_err : dcl_none;
171  if (err_code)
172  {
173  DCL_setError(pi2,err_code);
174  DCL_getErrorInfo(pi2);
175  DCL_runErrorHandler(pi2);
176  }
177 #endif
178 
179  pi2->Ki = pi2->sps->Ki;
180  pi2->Kp = pi2->sps->Kp;
181  pi2->Umax = pi2->sps->Umax;
182  pi2->Umin = pi2->sps->Umin;
183 }
184 
191 {
192 
193 #ifdef DCL_ERROR_HANDLING_ENABLED
194  uint32_t err_code = dcl_none;
195  err_code |= (pi2->sps->Umax <= pi2->sps->Umin) ? dcl_param_invalid_err : dcl_none;
196  err_code |= (pi2->css->T <= 0.0f) ? dcl_param_range_err : dcl_none;
197  err_code |= (pi2->sps->Kp < 0.0f) ? dcl_param_range_err : dcl_none;
198  err_code |= (pi2->sps->Ki < 0.0f) ? dcl_param_range_err : dcl_none;
199  if (err_code)
200  {
201  DCL_setError(pi2,err_code);
202  DCL_getErrorInfo(pi2);
203  DCL_runErrorHandler(pi2);
204  }
205 #endif
206 
207  dcl_interrupt_t ints;
208  ints = DCL_disableInts();
209  pi2->Ki = pi2->sps->Ki;
210  pi2->Kp = pi2->sps->Kp;
211  pi2->Umax = pi2->sps->Umax;
212  pi2->Umin = pi2->sps->Umin;
213  DCL_restoreInts(ints);
214 }
215 
226 {
227  if (DCL_getUpdateStatus(pi2))
228  {
231  return true;
232  }
233  return false;
234 }
235 
245 {
246  float32_t v1, v2, v5, v8, v10, v11, v14;
247  bool l1, l2, l3, l4, l5, l6;
248 
249  v1 = rk - yk;
250  v2 = pi2->Kp * v1;
251  v5 = (v1 * pi2->Ki * pi2->i12) + pi2->i6;
252  pi2->i6 = v5;
253  v8 = (v5 * pi2->i13) + pi2->i9;
254  pi2->i9 = v8;
255  v10 = v2 + v8;
256  v11 = DCL_runSat(v10,pi2->Umax,pi2->Umin);
257  v14 = v10 - v11;
258  l1 = (v1 > 0.0f) ? true : false;
259  l2 = (v14 > 0.0f) ? true : false;
260  l3 = (v14 == 0.0f) ? true : false;
261  l4 = (v5 > 0.0f) ? true : false;
262  l5 = l3 || (l1 ^ l2);
263  l6 = l3 || (l4 ^ l2);
264  pi2->i12 = (l5) ? 1.0f : 0.0f;
265  pi2->i13 = (l6) ? 1.0f : 0.0f;
266 
267 #ifdef DCL_TESTPOINTS_ENABLED
268  pi2->css->tpt = v8;
269 #endif
270 
271  return(v11);
272 }
273 
276 #ifdef __cplusplus
277 }
278 #endif // extern "C"
279 
280 #endif // _DCL_PI2_H_
dcl_pi2::Umax
float32_t Umax
Upper saturation limit.
Definition: dcl_pi2.h:75
DCL_PI2_SPS
Defines DCL_PI2 shadow parameter set used for updating controller parameter.
Definition: dcl_pi2.h:56
dcl_pi2::Umin
float32_t Umin
Lower saturation limit.
Definition: dcl_pi2.h:76
DCL_forceUpdatePI2
_DCL_CODE_ACCESS void DCL_forceUpdatePI2(DCL_PI2 *pi2)
Loads PI2 tuning parameter from its SPS parameter without interrupt protection.
Definition: dcl_pi2.h:162
dcl_none
@ dcl_none
No error.
Definition: dcl_error.h:57
DCL_updatePI2
_DCL_CODE_ACCESS bool DCL_updatePI2(DCL_PI2 *pi2)
A conditional update based on the update flag. If the update status is set, the function will update ...
Definition: dcl_pi2.h:225
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_clearUpdateStatus
#define DCL_clearUpdateStatus(p)
Definition: dcl_css.h:116
DCL_PI2_SPS::Umax
float32_t Umax
Upper saturation limit.
Definition: dcl_pi2.h:59
_DCL_CODE_ACCESS
#define _DCL_CODE_ACCESS
Defines the scope of dcl functions.
Definition: dcl_common.h:63
DCL_PI2_SPS::Umin
float32_t Umin
Lower saturation limit.
Definition: dcl_pi2.h:60
DCL_PI2
_DCL_VOLATILE struct dcl_pi2 DCL_PI2
dcl_interrupt_t
uint32_t dcl_interrupt_t
Definition: dcl_common.h:107
dcl_pi2::Ki
float32_t Ki
Integral gain.
Definition: dcl_pi2.h:74
dcl_pi2::Kp
float32_t Kp
Proportional gain.
Definition: dcl_pi2.h:73
DCL_updatePI2NoCheck
_DCL_CODE_ACCESS void DCL_updatePI2NoCheck(DCL_PI2 *pi2)
Loads PI2 tuning parameter from its SPS parameter with interrupt protection.
Definition: dcl_pi2.h:190
dcl_param_range_err
@ dcl_param_range_err
Parameter range exceeded.
Definition: dcl_error.h:58
dcl_pi2::i6
float32_t i6
Integrator 1 feedback storage.
Definition: dcl_pi2.h:79
DCL_runSat
#define DCL_runSat(data, Umax, Umin)
Macro to saturate a control variable but does not change the data itself unlike runClamp()
Definition: dcl_clamp.h:89
dcl_pi2::i9
float32_t i9
Integrator 2 feedback storage.
Definition: dcl_pi2.h:80
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_PI2_SPS::Ki
float32_t Ki
Integral gain.
Definition: dcl_pi2.h:58
_DCL_VOLATILE
#define _DCL_VOLATILE
Defines volatile for DCL strctures.
Definition: dcl_common.h:79
DCL_getErrorInfo
#define DCL_getErrorInfo(ptr)
Macro to store error info in CSS.
Definition: dcl_error.h:98
dcl_pi2::i13
float32_t i13
Saturation 2 multiplier, 1 means no saturation and 0 means fully saturated.
Definition: dcl_pi2.h:82
DCL_PI2_SPS::Kp
float32_t Kp
Proportional gain.
Definition: dcl_pi2.h:57
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_resetPI2
_DCL_CODE_ACCESS void DCL_resetPI2(DCL_PI2 *pi2)
Resets PI2 internal storage data with interrupt protection.
Definition: dcl_pi2.h:148
dcl_pi2::css
DCL_CSS * css
configuration & debugging
Definition: dcl_pi2.h:86
dcl_pi2
DCL_PI2 object for storing PI2 specific parameters PI2 - PI controller with double integrators.
Definition: dcl_pi2.h:71
float32_t
float float32_t
Definition: dcl_common.h:58
dcl_pi2::i12
float32_t i12
Saturation 1 multiplier, 1 means no saturation and 0 means fully saturated.
Definition: dcl_pi2.h:81
DCL_runPI2Series
_DCL_CRIT_ACCESS float32_t DCL_runPI2Series(DCL_PI2 *pi2, float32_t rk, float32_t yk)
Executes an inline series form PI2 controller on the FPU32.
Definition: dcl_pi2.h:244
DCL_disableInts
#define DCL_disableInts()
Define enable and disable interrupt operations.
Definition: dcl_common.h:105
dcl_pi2::sps
DCL_PI2_SPS * sps
updates controller parameter
Definition: dcl_pi2.h:85