AM263x Motor Control SDK  09.02.00
dcl_refgen.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_REFGEN_H_
34 #define _DCL_REFGEN_H_
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
48 #include "dcl_fdlog.h"
49 #include "../dcl_common.h"
50 
53 #define DCL_REFGEN_MIN_INC 1.0e-06f
54 
57 typedef enum dcl_rgen_modes
58 {
67  REFGEN_SINE3 = 8
69 
72 typedef _DCL_VOLATILE struct dcl_refgen
73 {
93  uint32_t mode;
96 
99 #define DCL_REFGEN_DEFAULTS { 0.0f, 0.0f, \
100  0.0f, 0.0f, \
101  0.0f, 0.0f, 0.0f, \
102  0.0f, 1.0f, 1.0f, 1.0f, -1.0f, \
103  0.0f, 0.0f, 0.0f, 0.0f, \
104  0.0f, 0.0f, 0.0f, \
105  REFGEN_OFF, &(DCL_CSS)DCL_CSS_DEFAULTS }
106 
113 {
114  p->ampl = 0.0f;
115  p->freq = 0.0f;
116  p->rinc = 0.0f;
117  p->theta = 0.0f;
118  p->thetb = 0.0f;
119  p->thetc = 0.0f;
120  p->thinc = 0.0f;
121  p->yr = 0.0f;
122  p->ya = 0.0f;
123  p->yb = 0.0f;
124  p->yc = 0.0f;
125 }
126 
135 {
136 #ifdef DCL_ERROR_HANDLING_ENABLED
137  uint32_t err_code = dcl_none;
138  err_code |= (tr < 0.0f) ? dcl_param_range_err : dcl_none;
139  if (err_code)
140  {
141  DCL_setError(p,err_code);
142  DCL_getErrorInfo(p);
144  }
145 #endif
146 
147  // compute ramp increment
148  tr = (tr < p->css->T) ? p->css->T : tr;
149  p->rtgt = tgt;
150  p->rinc = p->css->T * (tgt - p->yr) / tr;
151 
152  // clamp minimum ramp increment
153  if (!(0.0f == p->rinc) && (fabsf(p->rinc) < DCL_REFGEN_MIN_INC))
154  {
155 #ifdef DCL_ERROR_HANDLING_ENABLED
157  DCL_getErrorInfo(p);
159 #endif
160  if (signbit(p->rinc))
161  {
162  p->rinc = -DCL_REFGEN_MIN_INC;
163  }
164  else
165  {
166  p->rinc = DCL_REFGEN_MIN_INC;
167  }
168  }
169 }
170 
179 {
180 #ifdef DCL_ERROR_HANDLING_ENABLED
181  uint32_t err_code = dcl_none;
182  err_code |= (tr < 0.0f) ? dcl_param_range_err : dcl_none;
183  if (err_code)
184  {
185  DCL_setError(p,err_code);
186  DCL_getErrorInfo(p);
188  }
189 #endif
190 
191  // compute amplitude increment
192  tr = (tr < p->css->T) ? p->css->T : tr;
193  p->amtgt = ampl;
194  p->aminc = p->css->T * (ampl - p->ampl) / tr;
195 
196  // clamp minimum amplitude increment
197  if ((p->aminc > 0.0f) && (fabsf(p->aminc) < DCL_REFGEN_MIN_INC))
198  {
199 #ifdef DCL_ERROR_HANDLING_ENABLED
201  DCL_getErrorInfo(p);
203 #endif
204  if (signbit(p->aminc))
205  {
206  p->aminc = -DCL_REFGEN_MIN_INC;
207  }
208  else
209  {
210  p->aminc = DCL_REFGEN_MIN_INC;
211  }
212  }
213 }
214 
223 {
224 #ifdef DCL_ERROR_HANDLING_ENABLED
225  uint32_t err_code = dcl_none;
226  err_code |= (tr < 0.0f) ? dcl_param_range_err : dcl_none;
227  if (err_code)
228  {
229  DCL_setError(p,err_code);
230  DCL_getErrorInfo(p);
232  }
233 #endif
234 
235  // compute frequency increment
236  tr = (tr < p->css->T) ? p->css->T : tr;
237  p->fmtgt = freq;
238  p->fminc = p->css->T * (freq - p->freq) / tr;
239 
240  // clamp minimum frequency increment
241  if ((p->fminc > 0.0f) && (fabsf(p->fminc) < DCL_REFGEN_MIN_INC))
242  {
243 #ifdef DCL_ERROR_HANDLING_ENABLED
245  DCL_getErrorInfo(p);
247 #endif
248  if (signbit(p->fminc))
249  {
250  p->fminc = -DCL_REFGEN_MIN_INC;
251  }
252  else
253  {
254  p->fminc = DCL_REFGEN_MIN_INC;
255  }
256  }
257 }
258 
266 {
267 #ifdef DCL_ERROR_HANDLING_ENABLED
268  uint32_t err_code = dcl_none;
269  err_code |= ((duty < 0.0f) || (duty > 1.0f)) ? dcl_param_range_err : dcl_none;
270  if (err_code)
271  {
272  DCL_setError(p,err_code);
273  DCL_getErrorInfo(p);
275  }
276 #endif
277 
278  p->duty = duty;
279 }
280 
289 {
290 #ifdef DCL_ERROR_HANDLING_ENABLED
291  uint32_t err_code = dcl_none;
292  err_code |= (max < min) ? dcl_param_range_err : dcl_none;
293  if (err_code)
294  {
295  DCL_setError(p,err_code);
296  DCL_getErrorInfo(p);
298  }
299 #endif
300 
301  p->umax = max;
302  p->umin = min;
303 }
304 
305 
312 void DCL_setRefgenMode(DCL_REFGEN *p, int16_t mode)
313 {
314  p->mode = mode;
315 }
316 
323 {
324 #ifdef DCL_ERROR_HANDLING_ENABLED
325  float32_t v;
326 #endif
327 
328  // static offset generator
329  if (!DCL_isZero(p->rinc))
330  {
331  if (fabsf(p->rinc) > fabsf(p->rtgt - p->yr))
332  {
333  p->yr = p->rtgt;
334  p->rinc = 0.0f;
335  // Adjustment complete
337  }
338  else
339  {
340 #ifdef DCL_ERROR_HANDLING_ENABLED
341  v = p->yr;
342 #endif
343  p->yr += p->rinc;
344 #ifdef DCL_ERROR_HANDLING_ENABLED
345  if (DCL_isZero(v - p->yr))
346  {
347  p->css->err = dcl_comp_err;
348  DCL_getErrorInfo(p);
350  }
351 #endif
352  // Adjustment running
354  }
355  }
356 
357  // amplitude modulator
358  if (!DCL_isZero(p->aminc))
359  {
360  if (fabsf(p->aminc) > fabsf(p->amtgt - p->ampl))
361  {
362  p->ampl = p->amtgt;
363  p->aminc = 0.0f;
364  }
365  else
366  {
367 #ifdef DCL_ERROR_HANDLING_ENABLED
368  v = p->ampl;
369 #endif
370  p->ampl += p->aminc;
371 #ifdef DCL_ERROR_HANDLING_ENABLED
372  if (DCL_isZero(v - p->ampl))
373  {
374  p->css->err = dcl_comp_err;
375  DCL_getErrorInfo(p);
377  }
378 #endif
379  }
380  }
381 
382  // frequency modulator
383  if (!DCL_isZero(p->fminc))
384  {
385  if (fabsf(p->fminc) > fabsf(p->fmtgt - p->freq))
386  {
387  p->freq = p->fmtgt;
388  p->fminc = 0.0f;
389  }
390  else
391  {
392 #ifdef DCL_ERROR_HANDLING_ENABLED
393  v = p->freq;
394 #endif
395  p->freq += p->fminc;
396 #ifdef DCL_ERROR_HANDLING_ENABLED
397  if (DCL_isZero(v - p->freq))
398  {
399  p->css->err = dcl_comp_err;
400  DCL_getErrorInfo(p);
402  }
403 #endif
404  }
405  }
406 
407  // angle increment
408  if (p->freq > 0.0f)
409  {
410  p->thinc = p->css->T * p->freq;
411  if (p->thinc < DCL_REFGEN_MIN_INC)
412  {
413 #ifdef DCL_ERROR_HANDLING_ENABLED
414  p->css->err = dcl_comp_err;
415  DCL_getErrorInfo(p);
417 #endif
418  p->thinc = DCL_REFGEN_MIN_INC;
419  }
420  }
421  else
422  {
423  p->thinc = 0.0f;
424  }
425 
426  p->theta += p->thinc;
427  p->theta -= (p->theta >= 1.0f) ? 1.0f : 0.0f;
428 
429  // dynamic signal generator
430  switch (p->mode)
431  {
432  case REFGEN_STATIC:
433  p->ya = 0.0f;
434  p->yb = 0.0f;
435  p->yc = 0.0f;
436  break;
437 
438  case REFGEN_SINE:
439  p->ya = sinf(CONST_PI * p->theta);
440  p->yb = 0.0f;
441  p->yc = 0.0f;
442  break;
443 
444  case REFGEN_SQUARE:
445  p->ya = (p->theta > 0.5f) ? 1.0f : 0.0f;
446  p->yb = 0.0f;
447  p->yc = 0.0f;
448  break;
449 
450  case REFGEN_SAW:
451  p->ya = p->theta;
452  p->yb = 0.0f;
453  p->yc = 0.0f;
454  break;
455 
456  case REFGEN_TRIANGLE:
457  if (p->theta < 0.5f)
458  {
459  p->ya = 2.0f * p->theta;
460  }
461  else
462  {
463  p->ya = 1.0f - 2.0f * (p->theta - 0.5f);
464  }
465  p->yb = 0.0f;
466  p->yc = 0.0f;
467  break;
468 
469  case REFGEN_PULSE:
470  p->ya = (p->theta > p->duty) ? 0.0f : 1.0f;
471  p->yb = 0.0f;
472  p->yc = 0.0f;
473  break;
474 
475  case REFGEN_SINE2:
476  p->ya = sinf(CONST_2PI * p->theta);
477  p->yb = cosf(CONST_2PI * p->theta);
478  p->yc = 0.0f;
479  break;
480 
481  case REFGEN_SINE3:
482  p->thetb = p->theta + 0.3333333333333f;
483  p->thetc = p->theta + 0.6666666666667f;
484  p->thetb -= (p->thetb >= 1.0f) ? 1.0f : 0.0f;
485  p->thetc -= (p->thetc >= 1.0f) ? 1.0f : 0.0f;
486  p->ya = sinf(CONST_2PI * p->theta);
487  p->yb = sinf(CONST_2PI * p->thetb);
488  p->yc = sinf(CONST_2PI * p->thetc);
489  break;
490 
491  case REFGEN_OFF:
492  default:
493  p->yr = 0.0f;
494  p->ya = 0.0f;
495  p->yb = 0.0f;
496  p->yc = 0.0f;
497  }
498 
499  // output sum & saturation
500  p->ya = DCL_runSat(p->ampl * p->ya + p->yr, p->umax, p->umin);
501  p->yb = DCL_runSat(p->ampl * p->yb + p->yr, p->umax, p->umin);
502  p->yc = DCL_runSat(p->ampl * p->yc + p->yr, p->umax, p->umin);
503 }
504 
512 {
513  return(p->ya);
514 }
515 
523 {
524  return(p->yb);
525 }
526 
534 {
535  return(p->yc);
536 }
537 
540 #ifdef __cplusplus
541 }
542 #endif // extern "C"
543 
544 #endif // _DCL_REFGEN_H_
dcl_refgen::mode
uint32_t mode
Operating mode.
Definition: dcl_refgen.h:93
DCL_resetRefgen
_DCL_CODE_ACCESS void DCL_resetRefgen(DCL_REFGEN *p)
Resets DCL_REFGEN dynamic data.
Definition: dcl_refgen.h:112
CONST_2PI
#define CONST_2PI
Definition: dcl_macro.h:56
REFGEN_TRIANGLE
@ REFGEN_TRIANGLE
Definition: dcl_refgen.h:64
DCL_setRefgenClamp
_DCL_CODE_ACCESS void DCL_setRefgenClamp(DCL_REFGEN *p, float32_t max, float32_t min)
Loads the REFGEN output clam limits.
Definition: dcl_refgen.h:288
dcl_refgen::theta
float32_t theta
Normalized angle - phase A.
Definition: dcl_refgen.h:87
CONST_PI
#define CONST_PI
Local definitions of the mathematical constant pi.
Definition: dcl_macro.h:55
dcl_refgen::thetb
float32_t thetb
Normalized angle - phase B.
Definition: dcl_refgen.h:88
DCL_setRefgenMode
_DCL_CODE_ACCESS void DCL_setRefgenMode(DCL_REFGEN *p, int16_t mode)
Sets the REFGEN operating mode.
Definition: dcl_refgen.h:312
REFGEN_PULSE
@ REFGEN_PULSE
Definition: dcl_refgen.h:65
dcl_refgen::freq
float32_t freq
Dynamic frequency.
Definition: dcl_refgen.h:86
REFGEN_STATIC
@ REFGEN_STATIC
Definition: dcl_refgen.h:60
DCL_getRefgenPhaseB
_DCL_CODE_ACCESS float32_t DCL_getRefgenPhaseB(DCL_REFGEN *p)
Returns the phase reference output.
Definition: dcl_refgen.h:522
DCL_REFGEN_MIN_INC
#define DCL_REFGEN_MIN_INC
Defines the minimum normalized increment.
Definition: dcl_refgen.h:53
DCL_setRefgenFreq
_DCL_CODE_ACCESS void DCL_setRefgenFreq(DCL_REFGEN *p, float32_t freq, float32_t tr)
Loads the REFGEN frequency.
Definition: dcl_refgen.h:222
DCL_setRefgenRamp
_DCL_CODE_ACCESS void DCL_setRefgenRamp(DCL_REFGEN *p, float32_t tgt, float32_t tr)
Loads the REFGEN ramp parameters.
Definition: dcl_refgen.h:134
DCL_setUpdateStatus
#define DCL_setUpdateStatus(p)
Macros to set and clear the update-in-progress flag.
Definition: dcl_css.h:115
dcl_none
@ dcl_none
No error.
Definition: dcl_error.h:57
dcl_refgen::yc
float32_t yc
Phase C output.
Definition: dcl_refgen.h:92
DCL_runErrorHandler
#define DCL_runErrorHandler(ptr)
Prototype for basic error handler.
Definition: dcl_error.h:108
dcl_refgen::css
DCL_CSS * css
Pointer to the common support structure.
Definition: dcl_refgen.h:94
dcl_refgen::yr
float32_t yr
Ramp generator output.
Definition: dcl_refgen.h:84
REFGEN_OFF
@ REFGEN_OFF
Definition: dcl_refgen.h:59
DCL_clearUpdateStatus
#define DCL_clearUpdateStatus(p)
Definition: dcl_css.h:116
dcl_refgen::fminc
float32_t fminc
Frequency increment.
Definition: dcl_refgen.h:79
REFGEN_SQUARE
@ REFGEN_SQUARE
Definition: dcl_refgen.h:62
_DCL_CODE_ACCESS
#define _DCL_CODE_ACCESS
Defines the scope of dcl functions.
Definition: dcl_common.h:63
dcl_refgen::ampl
float32_t ampl
Dynamic amplitude.
Definition: dcl_refgen.h:85
dcl_refgen::aminc
float32_t aminc
Amplitude increment.
Definition: dcl_refgen.h:77
DCL_setRefgenDuty
_DCL_CODE_ACCESS void DCL_setRefgenDuty(DCL_REFGEN *p, float32_t duty)
Loads the REFGEN pulse duty cycle.
Definition: dcl_refgen.h:265
DCL_refgen_modes
DCL_refgen_modes
Defines the REFGEN operating modes.
Definition: dcl_refgen.h:58
dcl_refgen
Defines the DCL_REFGEN structure.
Definition: dcl_refgen.h:73
dcl_refgen::thinc
float32_t thinc
Angular increment.
Definition: dcl_refgen.h:80
dcl_refgen::fmtgt
float32_t fmtgt
Frequency ramp value.
Definition: dcl_refgen.h:78
dcl_refgen::umin
float32_t umin
Minimum allowable output.
Definition: dcl_refgen.h:83
dcl_refgen::thetc
float32_t thetc
Normalized angle - phase C.
Definition: dcl_refgen.h:89
REFGEN_SINE3
@ REFGEN_SINE3
Definition: dcl_refgen.h:67
DCL_REFGEN
_DCL_VOLATILE struct dcl_refgen DCL_REFGEN
dcl_refgen::umax
float32_t umax
Maximum allowable output.
Definition: dcl_refgen.h:82
dcl_refgen::yb
float32_t yb
Phase B output.
Definition: dcl_refgen.h:91
DCL_isZero
#define DCL_isZero(x)
Determines floating point numerical proximity to zero.
Definition: dcl_macro.h:77
DCL_getRefgenPhaseA
_DCL_CODE_ACCESS float32_t DCL_getRefgenPhaseA(DCL_REFGEN *p)
Returns the phase reference output.
Definition: dcl_refgen.h:511
dcl_param_range_err
@ dcl_param_range_err
Parameter range exceeded.
Definition: dcl_error.h:58
dcl_refgen::duty
float32_t duty
Pulse duty cycle.
Definition: dcl_refgen.h:81
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
REFGEN_SINE
@ REFGEN_SINE
Definition: dcl_refgen.h:61
DCL_CSS
Defines the controller common support structure.
Definition: dcl_css.h:57
DCL_setRefgenAmpl
_DCL_CODE_ACCESS void DCL_setRefgenAmpl(DCL_REFGEN *p, float32_t ampl, float32_t tr)
Loads the REFGEN dynamic amplitude.
Definition: dcl_refgen.h:178
_DCL_VOLATILE
#define _DCL_VOLATILE
Defines volatile for DCL strctures.
Definition: dcl_common.h:79
DCL_getRefgenPhaseC
_DCL_CODE_ACCESS float32_t DCL_getRefgenPhaseC(DCL_REFGEN *p)
Returns the phase reference output.
Definition: dcl_refgen.h:533
dcl_fdlog.h
Defines a 32-bit floating-point data logger strcture and related functions.
dcl_refgen::rtgt
float32_t rtgt
Target ramp value.
Definition: dcl_refgen.h:74
DCL_getErrorInfo
#define DCL_getErrorInfo(ptr)
Macro to store error info in CSS.
Definition: dcl_error.h:98
dcl_refgen::amtgt
float32_t amtgt
Target amplitude value.
Definition: dcl_refgen.h:76
DCL_setError
#define DCL_setError(ptr, code)
Macro to set error code in CSS.
Definition: dcl_error.h:80
REFGEN_SINE2
@ REFGEN_SINE2
Definition: dcl_refgen.h:66
REFGEN_SAW
@ REFGEN_SAW
Definition: dcl_refgen.h:63
dcl_comp_err
@ dcl_comp_err
Computation error.
Definition: dcl_error.h:66
dcl_refgen::ya
float32_t ya
Phase A output.
Definition: dcl_refgen.h:90
float32_t
float float32_t
Definition: dcl_common.h:58
dcl_refgen::rinc
float32_t rinc
Ramp increment.
Definition: dcl_refgen.h:75
DCL_runRefgen
_DCL_CODE_ACCESS void DCL_runRefgen(DCL_REFGEN *p)
Runs the REFGEN module.
Definition: dcl_refgen.h:322