AM263x Motor Control SDK  09.00.00
dcl_pidf64.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 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 EXPgResS 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_PID64_H_
34 #define _DCL_PID64_H_
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
48 #include "../dcl_common.h"
49 
50 //--- Linear PID 64bit controller --------------------------------------------------
51 
55 typedef struct dcl_pid64_sps {
65 
68 #define PIDF64_SPS_DEFAULTS { 1.0L, 0.0L, 0.0L, 1.0L, 1.0L, 0.0L, 1.0L, -1.0L }
69 
72 typedef _DCL_VOLATILE struct dcl_pidf64 {
73  /* controller parameter */
82 
83  /* internal storage */
88 
89  /* miscellaneous */
93 
96 #define PIDF64_DEFAULTS { 1.0L, 0.0L, 0.0L, 1.0L, 1.0L, 0.0L, \
97  1.0L, -1.0L, 0.0L, 0.0L, 0.0L, 1.0L, \
98 &(DCL_PIDF64_SPS)PIDF64_SPS_DEFAULTS, &(DCL_CSSF64)DCL_CSSF64_DEFAULTS }
99 
108 #define PIDF64_INT_DEFAULTS .d2=0.0L, .d3=0.0L, .i10=0.0L, .i14=1.0L, \
109  .sps=&(DCL_PIDF64_SPS)PI_SPS_DEFAULTS, .css=&(DCL_CSSF64)DCL_CSS_DEFAULTS
110 
116 #define DCL_initPIDF64() &(DCL_PIDF64)PIDF64_DEFAULTS
117 
124 #define DCL_initPIDF64asParam(kp,ki,kd,kr,_c1,_c2,umax,umin) &(DCL_PIDF64){ .Kp=kp, .Ki=ki, .Kd=kd, .Kr=kr, \
125  .c1=_c1, .c2=_c2, .Umax=umax, .Umin=umin, PIDF64_INT_DEFAULTS }
126 
137 #define DCL_initPIDF64asSPS(pid_ptr,sps_ptr) \
138 ({ \
139  DCL_PIDF64* new_pid = (pid_ptr) ? pid_ptr : DCL_initPID(); \
140  DCL_PIDF64_SPS* new_sps = (sps_ptr) ? sps_ptr : &(DCL_PIDF64_SPS)PID_SPS_DEFAULTS; \
141  if(sps_ptr) \
142  { \
143  *new_pi = (DCL_PID){ (new_sps)->Kp, (new_sps)->Ki, (new_sps)->Kd,(new_sps)->Kr,\
144  (new_sps)->c1, (new_sps)->c2, (new_sps)->Umax, (new_sps)->Umin, 0.0L, 0.0L, \
145  0.0L, 1.0L,(DCL_PIDF64_SPS*)new_sps, &(DCL_CSS)DCL_CSS_DEFAULTS }; \
146  } \
147  new_pid; \
148 })
149 
156 {
158  pid->d2 = pid->d3 = pid->i10 = 0.0L;
159  pid->i14 = 1.0L;
160  DCL_restoreInts(ints);
161 }
162 
169 {
170 
171 #ifdef DCL_ERROR_HANDLING_ENABLED
172  float64_t tau = (2.0L - pid->sps->c1 * p->css->t_sec) / (2.0L * pid->sps->c1);
173  float64_t ec2 = pid->sps->c1 * (pid->css->t_sec - 2.0L * tau) / 2.0L;
174  uint32_t err_code = dcl_none;
175  err_code |= DCL_isValue(pid->sps->c2, ec2) ? dcl_none : dcl_param_invalid_err;
176  err_code |= (pid->sps->Umax > pid->sps->Umin) ? dcl_none : dcl_param_invalid_err;
177  err_code |= (pid->css->t_sec > 0.0L) ? dcl_none : dcl_param_range_err;
178  err_code |= ((pid->sps->Kp > 0.0L) && (pid->sps->Ki > 0.0L) && (pid->sps->Kd > 0.0L) && (pid->sps->Kr > 0.0L)) ? dcl_none : dcl_param_range_err ;
179  if (err_code)
180  {
181  DCL_setError(pid,err_code);
182  DCL_getErrorInfo(pid);
183  DCL_runErrorHandler(pid);
184  }
185 #endif
186 
187  pid->Kp = pid->sps->Kp;
188  pid->Ki = pid->sps->Ki;
189  pid->Kd = pid->sps->Kd;
190  pid->Kr = pid->sps->Kr;
191  pid->c1 = pid->sps->c1;
192  pid->c2 = pid->sps->c2;
193  pid->Umax = pid->sps->Umax;
194  pid->Umin = pid->sps->Umin;
195 }
196 
197 
205 {
206 
207 #ifdef DCL_ERROR_HANDLING_ENABLED
208  float64_t tau = (2.0L - pid->sps->c1 * pid->css->t_sec) / (2.0L * pid->sps->c1);
209  float64_t ec2 = pid->sps->c1 * (pid->css->t_sec - 2.0L * tau) / 2.0L;
210  uint32_t err_code = dcl_none;
211  err_code |= DCL_isValue(pid->sps->c2, ec2) ? dcl_none : dcl_param_invalid_err;
212  err_code |= (pid->sps->Umax > pid->sps->Umin) ? dcl_none : dcl_param_invalid_err;
213  err_code |= (pid->css->t_sec > 0.0L) ? dcl_none : dcl_param_range_err;
214  err_code |= ((pid->sps->Kp > 0.0L) && (pid->sps->Ki > 0.0L) && (pid->sps->Kd > 0.0L) && (pid->sps->Kr > 0.0L)) ? dcl_none : dcl_param_range_err ;
215  if (err_code)
216  {
217  DCL_setError(pid,err_code);
218  DCL_getErrorInfo(pid);
219  DCL_runErrorHandler(pid);
220  }
221 #endif
222 
223  if (!DCL_getUpdateStatus(pid))
224  {
226  DCL_setUpdateStatus(pid);
227  pid->Kp = pid->sps->Kp;
228  pid->Ki = pid->sps->Ki;
229  pid->Kd = pid->sps->Kd;
230  pid->Kr = pid->sps->Kr;
231  pid->c1 = pid->sps->c1;
232  pid->c2 = pid->sps->c2;
233  pid->Umax = pid->sps->Umax;
234  pid->Umin = pid->sps->Umin;
236  DCL_restoreInts(ints);
237  return true;
238  }
239  return false;
240 }
241 
252 {
253  if (DCL_getPendingStatus(pid) && DCL_updatePIDF64(pid))
254  {
256  return true;
257  }
258  return false;
259 }
260 
268 {
269  pid->sps->Kp = pid->Kp;
270  pid->sps->Ki = pid->Ki;
271  pid->sps->Kd = pid->Kd;
272  pid->sps->Kr = pid->Kr;
273  pid->sps->c1 = pid->c1;
274  pid->sps->c2 = pid->c2;
275  pid->sps->Umax = pid->Umax;
276  pid->sps->Umin = pid->Umin;
277 }
278 
288 {
289 
290 #ifdef DCL_ERROR_HANDLING_ENABLED
291  uint32_t err_code;
292  err_code = ((fc >= 1.0L / (2.0L * pid->css->t_sec)) || (fc <= 0.0L)) ? dcl_param_range_err : dcl_none;
293  if (err_code)
294  {
295  DCL_setError(pid,err_code);
296  DCL_getErrorInfo(pid);
297  DCL_runErrorHandler(pid);
298  }
299 #endif
300 
301  float64_t t_sec = pid->css->t_sec;
302  float64_t tau = 1.0L / (2.0L * CONST_PI_F64 * fc);
303  pid->sps->c1 = 2.0L / (t_sec + (2.0L * tau));
304  pid->sps->c2 = (t_sec - (2.0L * tau)) / (t_sec + (2.0L * tau));
305 }
306 
318 {
319 
320 #ifdef DCL_ERROR_HANDLING_ENABLED
321  uint32_t err_code;
322  err_code = ((fc >= 1.0L / (2.0L * t_sec)) || (fc <= 0.0L)) ? dcl_param_range_err : dcl_none;
323  if (err_code)
324  {
325  DCL_setError(pid,err_code);
326  DCL_getErrorInfo(pid);
327  DCL_runErrorHandler(pid);
328  }
329 #endif
330 
331  float64_t tau = 1.0L / (2.0L * CONST_PI_F64 * fc);
332  pid->c1 = 2.0L / (t_sec + (2.0L * tau));
333  pid->c2 = (t_sec - (2.0L * tau)) / (t_sec + (2.0L * tau));
334 }
335 
344 {
345  float64_t tau = ((2.0L - pid->c1 * pid->css->t_sec) / (2.0L * pid->c1));
346  return(1.0L / (2.0L * CONST_PI_F64 * tau));
347 }
348 
359 {
360  float64_t v1, v4, v5, v8, v9, v10, v12;
361 
362  v5 = (pid->Kr * rk) - yk;
363  v8 = ((rk - yk) * pid->Ki * pid->Kp * pid->i14) + pid->i10;
364  pid->i10 = v8;
365  v1 = yk * pid->Kd * pid->c1;
366  v4 = v1 - pid->d2 - pid->d3;
367  pid->d2 = v1;
368  pid->d3 = v4 * pid->c2;
369  v9 = ((v5 - v4) * pid->Kp) + v8;
370  v10 = DCL_runSat(v9, pid->Umax, pid->Umin);
371  v12 = (v10 == v9) ? 1.0 : 0.0;
372  pid->i14 = v12 * lk;
373 
374 #ifdef DCL_TESTPOINTS_ENABLED
375  pid->css->tpt = v5;
376 #endif
377 
378  return(v10);
379 }
380 
391 {
392  float64_t v1, v4, v5, v6, v8, v9, v10, v12;
393 
394  v5 = rk - yk;
395  v6 = v5 * pid->Kp;
396  v8 = v5 * pid->Ki * pid->i14 + pid->i10;
397  pid->i10 = v8;
398  v1 = v5 * pid->Kd * pid->c1;
399  v4 = v1 - pid->d2 - pid->d3;
400  pid->d2 = v1;
401  pid->d3 = v4 * pid->c2;
402  v9 = v6 + v8 + v4;
403  v10 = DCL_runSat(v9, pid->Umax, pid->Umin);
404  v12 = (v10 == v9) ? 1.0f : 0.0f;
405  pid->i14 = v12 * lk;
406 
407 #ifdef DCL_TESTPOINTS_ENABLED
408  pid->css->tpt = v8;
409 #endif
410 
411  return(v10);
412 }
413 
416 #ifdef __cplusplus
417 }
418 #endif // extern "C"
419 
420 #endif // _DCL_PIDF64_H_
dcl_pidf64::c1
float64_t c1
D-term filter coefficient 1, default is 1.
Definition: dcl_pidf64.h:78
DCL_isValue
#define DCL_isValue(x, y)
Determines numerical proximity to specified value.
Definition: dcl_aux.h:72
DCL_PIDF64_SPS::Umax
float64_t Umax
Upper saturation limit.
Definition: dcl_pidf64.h:62
DCL_PIDF64_SPS::c1
float64_t c1
D-term filter coefficient 1, default is 1.
Definition: dcl_pidf64.h:60
DCL_setPIDF64filterBW
_DCL_CODE_ACCESS void DCL_setPIDF64filterBW(DCL_PIDF64 *pid, float64_t fc)
Loads the derivative path filter shadow coefficients Note: Sampling period pid->css->t_sec are used i...
Definition: dcl_pidf64.h:287
dcl_pidf64::Kp
float64_t Kp
Proportional gain.
Definition: dcl_pidf64.h:74
DCL_runPIDF64Parallel
_DCL_CODE_ACCESS float64_t DCL_runPIDF64Parallel(DCL_PIDF64 *pid, float64_t rk, float64_t yk, float64_t lk)
Executes an parallel form PID64 controller.
Definition: dcl_pidf64.h:390
DCL_resetPIDF64
_DCL_CODE_ACCESS void DCL_resetPIDF64(DCL_PIDF64 *pid)
Resets PID64 internal storage data with interrupt protection.
Definition: dcl_pidf64.h:155
DCL_clearPendingStatus
#define DCL_clearPendingStatus(p)
Definition: dcl_css.h:129
dcl_pidf64::d3
float64_t d3
D path feedback value (c2)
Definition: dcl_pidf64.h:85
DCL_setUpdateStatus
#define DCL_setUpdateStatus(p)
Macros to set and clear the update-in-progress flag.
Definition: dcl_css.h:116
DCL_getPIDF64filterBW
_DCL_CODE_ACCESS float64_t DCL_getPIDF64filterBW(DCL_PIDF64 *pid)
Returns the active derivative path filter bandwidth in Hz Note: Sampling period pid->css->t_sec are u...
Definition: dcl_pidf64.h:343
DCL_updatePIDF64
_DCL_CODE_ACCESS bool DCL_updatePIDF64(DCL_PIDF64 *pid)
Updates PID parameter from its SPS parameter with interrupt protection.
Definition: dcl_pidf64.h:204
dcl_pidf64::Umin
float64_t Umin
Lower saturation limit.
Definition: dcl_pidf64.h:81
DCL_pendingUpdatePIDF64
_DCL_CODE_ACCESS bool DCL_pendingUpdatePIDF64(DCL_PIDF64 *pid)
A conditional update based on the pending-for-update flag. If the pending status is set,...
Definition: dcl_pidf64.h:251
dcl_pidf64::Kd
float64_t Kd
Derivative gain.
Definition: dcl_pidf64.h:76
DCL_runErrorHandler
#define DCL_runErrorHandler(ptr)
Prototype for basic error handler.
Definition: dcl_error.h:107
dcl_pidf64::Umax
float64_t Umax
Upper saturation limit.
Definition: dcl_pidf64.h:80
DCL_PIDF64
_DCL_VOLATILE struct dcl_pidf64 DCL_PIDF64
dcl_param_range_err
@ dcl_param_range_err
Parameter range exceeded.
Definition: dcl_error.h:58
DCL_PIDF64_SPS::Kd
float64_t Kd
Derivative gain.
Definition: dcl_pidf64.h:58
DCL_getUpdateStatus
#define DCL_getUpdateStatus(p)
Determine whether a parameter update-in-progress flag is set.
Definition: dcl_css.h:123
DCL_PIDF64_SPS::Kp
float64_t Kp
Proportional gain.
Definition: dcl_pidf64.h:56
DCL_clearUpdateStatus
#define DCL_clearUpdateStatus(p)
Definition: dcl_css.h:117
DCL_PIDF64_SPS::Umin
float64_t Umin
Lower saturation limit.
Definition: dcl_pidf64.h:63
_DCL_CODE_ACCESS
#define _DCL_CODE_ACCESS
Defines the scope of dcl functions (static inline/extern inline/none)
Definition: dcl_common.h:55
dcl_pidf64::c2
float64_t c2
D-term filter coefficient 2, default is 0.
Definition: dcl_pidf64.h:79
dcl_pidf64::Kr
float64_t Kr
Set point weight, default is 1.
Definition: dcl_pidf64.h:77
dcl_pidf64::d2
float64_t d2
D path feedback value (Kd * c1)
Definition: dcl_pidf64.h:84
dcl_none
@ dcl_none
No error.
Definition: dcl_error.h:57
dcl_interrupt_t
uint32_t dcl_interrupt_t
Definition: dcl_common.h:96
DCL_PIDF64_SPS::Ki
float64_t Ki
Integral gain.
Definition: dcl_pidf64.h:57
DCL_PIDF64_SPS
Defines DCL_PIDF64 shadow PID64 controller structure used for updating controller parameter.
Definition: dcl_pidf64.h:55
dcl_pidf64::i10
float64_t i10
I path feedback value.
Definition: dcl_pidf64.h:86
dcl_pidf64::sps
DCL_PIDF64_SPS * sps
updates controller parameter
Definition: dcl_pidf64.h:90
CONST_PI_F64
#define CONST_PI_F64
Definition: dcl_aux.h:57
dcl_param_invalid_err
@ dcl_param_invalid_err
Parameter not valid.
Definition: dcl_error.h:59
DCL_CSSF64
Defines the 64bit CSS structure.
Definition: dcl_css.h:72
PIDF64_Handle
_DCL_VOLATILE struct dcl_pidf64 * PIDF64_Handle
DCL_updatePIDF64SPS
_DCL_CODE_ACCESS void DCL_updatePIDF64SPS(DCL_PIDF64 *pid)
Update SPS parameter with active param, userful when needing to update only few active param from SPS...
Definition: dcl_pidf64.h:267
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_pidf64
DCL_PIDF64 object for storing 64bit PID specific parameters.
Definition: dcl_pidf64.h:72
DCL_getPendingStatus
#define DCL_getPendingStatus(p)
Determine whether a parameter pending-for-update flag is set.
Definition: dcl_css.h:135
dcl_pidf64::i14
float64_t i14
I path saturation storage.
Definition: dcl_pidf64.h:87
DCL_restoreInts
#define DCL_restoreInts(v)
Definition: dcl_common.h:95
_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_runPIDF64Series
_DCL_CODE_ACCESS float64_t DCL_runPIDF64Series(DCL_PIDF64 *pid, float64_t rk, float64_t yk, float64_t lk)
Executes an ideal form PID64 controller.
Definition: dcl_pidf64.h:358
float64_t
double float64_t
Definition: dcl_common.h:52
DCL_PIDF64_SPS::Kr
float64_t Kr
Set point weight, default is 1.
Definition: dcl_pidf64.h:59
dcl_pidf64::css
DCL_CSSF64 * css
configuration & debugging
Definition: dcl_pidf64.h:91
DCL_getErrorInfo
#define DCL_getErrorInfo(ptr)
Macro to store error info in CSS.
Definition: dcl_error.h:97
DCL_setError
#define DCL_setError(ptr, code)
Macro to set error code in CSS.
Definition: dcl_error.h:79
dcl_pidf64::Ki
float64_t Ki
Integral gain.
Definition: dcl_pidf64.h:75
DCL_setActivePIDF64filterBW
_DCL_CODE_ACCESS void DCL_setActivePIDF64filterBW(DCL_PIDF64 *pid, float64_t fc, float64_t t_sec)
Loads the PID64 derivative path filter active coefficients Note: Sampling period pid->css->t_sec are ...
Definition: dcl_pidf64.h:317
DCL_PIDF64_SPS::c2
float64_t c2
D-term filter coefficient 2, default is 0.
Definition: dcl_pidf64.h:61
DCL_fupdatePIDF64
_DCL_CODE_ACCESS void DCL_fupdatePIDF64(DCL_PIDF64 *pid)
Loads PIDF64 tuning parameter from its SPS parameter.
Definition: dcl_pidf64.h:168
DCL_disableInts
#define DCL_disableInts()
Define enable and disable interrupt operations.
Definition: dcl_common.h:94