AM243x Motor Control SDK  09.02.00
volt_recons.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 VOLT_RECONS_H
34 #define VOLT_RECONS_H
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40 
52 #include <stdbool.h>
53 #include <stdlib.h>
54 #include <math.h>
55 
56 #include "math_types.h"
57 
58 //*****************************************************************************
59 //
61 //
62 //*****************************************************************************
63 typedef struct _VOLREC_Obj_
64 {
68  MATH_Vec3 x1;
69  MATH_Vec3 y1;
70  MATH_Vec3 Vin_V;
71  MATH_Vec3 Vs_V;
72 
76 
80 
83 
84  int32_t numSamples;
85  int32_t minSamples;
86  int32_t maxSamples;
87 
88  int32_t signPrev;
89  int32_t signCurr;
90  int32_t jitterCount;
91 
92  bool flagCalSf;
93 } VOLREC_Obj;
94 
95 //*****************************************************************************
96 //
98 //
99 //*****************************************************************************
100 typedef struct _VOLREC_Obj_ *VOLREC_Handle;
101 
102 
103 //*****************************************************************************
104 //
112 //
113 //*****************************************************************************
114 extern VOLREC_Handle VOLREC_init(void *pMemory, const size_t numBytes);
115 
116 //*****************************************************************************
117 //
121 //
122 //*****************************************************************************
123 extern void VOLREC_reset(VOLREC_Handle handle);
124 
125 
126 //*****************************************************************************
127 //
135 //
136 //*****************************************************************************
137 extern void VOLREC_setParams(VOLREC_Handle handle,
138  const float32_t filterPole_rps, const float32_t ctrlFreq_Hz);
139 
140 //*****************************************************************************
141 //
147 //
148 //*****************************************************************************
149 static inline void VOLREC_calcVolSF(VOLREC_Handle handle, float32_t VaSen)
150 {
151  VOLREC_Obj *obj = (VOLREC_Obj *)handle;
152 
153  float32_t VaNorm = fabsf(obj->Vin_V.value[0]);
154  obj->VaSen = VaSen;
155 
156  obj->signCurr = (VaNorm > obj->threshold) ? 1 : 0;
157  obj->numSamples++;
158 
159  obj->VaSenSum = obj->VaSenSum + (obj->VaSen * obj->VaSen);
160  obj->VaSum = obj->VaSum + (obj->Vin_V.value[0] * obj->Vin_V.value[0]);
161 
162  if((obj->signPrev != obj->signCurr) && (obj->signCurr == 1))
163  {
164  if(obj->numSamples > obj->minSamples)
165  {
166  float32_t invSamplesSqrt = sqrtf(1.0f / obj->numSamples);
167 
168  obj->VaSenRms = sqrtf(obj->VaSenSum) * invSamplesSqrt;
169  obj->VaRms = sqrtf(obj->VaSum) * invSamplesSqrt;
170 
171  obj->sfCalc = obj->sf * 0.8f + (obj->VaSenRms / obj->VaRms) * 0.2f;
172 
173  obj->sf = MATH_sat(obj->sfCalc, 0.95f, 0.85f);
174 
175  obj->VaSenSum = 0.0f;
176  obj->VaSum = 0.0f;
177 
178  obj->numSamples = 0;
179  obj->jitterCount = 0;
180  }
181  else
182  {
183  obj->numSamples = 0;
184 
185  if(obj->jitterCount < 25)
186  {
187  obj->jitterCount++;
188  }
189  }
190  }
191 
192  if((obj->numSamples > obj->maxSamples) || (obj->jitterCount >= 20))
193  {
194  obj->VaSenSum = 0.0f;
195  obj->VaSum = 0.0f;
196 
197  obj->jitterCount = 0;
198  obj->numSamples = 0;
199  }
200 
201  obj->signPrev = obj->signCurr;
202 
203  return;
204 }
205 
206 //*****************************************************************************
207 //
217 //
218 //*****************************************************************************
219 static inline void VOLREC_run(VOLREC_Handle handle,
220  float32_t Vdcbus, MATH_Vec3 *pVin, MATH_Vec2 *pVab)
221 {
222  VOLREC_Obj *obj = (VOLREC_Obj *)handle;
223  float32_t Vtemp;
224  MATH_Vec3 Vin;
225  uint32_t cn;
226 
227  // Scale the input Modulation functions with the DC bus voltage value
228  // and calculate the 3 Phase voltages
229  Vtemp = Vdcbus * MATH_ONE_OVER_THREE;
230 
231  Vin.value[0] = Vtemp * (pVin->value[0] * 2.0f - pVin->value[1] - pVin->value[2]);
232  Vin.value[1] = Vtemp * (pVin->value[1] * 2.0f - pVin->value[0] - pVin->value[2]);
233  Vin.value[2] = Vtemp * (pVin->value[2] * 2.0f - pVin->value[1] - pVin->value[0]);
234 
235  for(cn = 0; cn < 3; cn++)
236  {
237  // Compute the output
238  // y0 = (b0 * inputValue) + (a1 * y1);
239  obj->Vin_V.value[cn] = (obj->b0 * Vin.value[cn]) +
240  (obj->b1 * obj->x1.value[cn]) - (obj->a1 * obj->y1.value[cn]);
241 
242  obj->x1.value[cn] = Vin.value[cn];
243 
244  obj->y1.value[cn] = obj->Vin_V.value[cn];
245 
246  obj->Vs_V.value[cn] = obj->Vin_V.value[cn] * obj->sf;
247  }
248 
249  // Voltage transformation (a,b,c) -> (Alpha,Beta)
250  pVab->value[0] = (obj->Vs_V.value[0] * 2.0f -
251  (obj->Vs_V.value[1] + obj->Vs_V.value[2])) * MATH_ONE_OVER_THREE;
252 
253  pVab->value[1] = (obj->Vs_V.value[1] - obj->Vs_V.value[2]) *
254  MATH_ONE_OVER_SQRT_THREE;
255 
256  return;
257 }
258 
259 //*****************************************************************************
260 //
266 //
267 //*****************************************************************************
268 static inline bool VOLREC_getFlagCalSf(VOLREC_Handle handle)
269 {
270  VOLREC_Obj *obj = (VOLREC_Obj *)handle;
271 
272  return(obj->flagCalSf);
273 }
274 
275 //*****************************************************************************
276 //
282 //
283 //*****************************************************************************
284 static inline void VOLREC_setFlagCalSf(VOLREC_Handle handle, const bool flagCalSf)
285 {
286  VOLREC_Obj *obj = (VOLREC_Obj *)handle;
287 
288  obj->flagCalSf = flagCalSf;
289 
290  return;
291 }
292 
293 //*****************************************************************************
294 //
298 //
299 //*****************************************************************************
300 static inline void VOLREC_enableFlagCalSf(VOLREC_Handle handle)
301 {
302  VOLREC_Obj *obj = (VOLREC_Obj *)handle;
303 
304  obj->flagCalSf = true;
305 
306  return;
307 }
308 
309 //*****************************************************************************
310 //
314 //
315 //*****************************************************************************
316 static inline void VOLREC_disableFlagEnableSf(VOLREC_Handle handle)
317 {
318  VOLREC_Obj *obj = (VOLREC_Obj *)handle;
319 
320  obj->flagCalSf = false;
321 
322  return;
323 }
324 
327 #ifdef __cplusplus
328 }
329 #endif // extern "C"
330 
331 #endif // end of VOLT_RECONS_H defines
332 
333 
334 
335 
336 
337 
338 
339 
340 
341 
342 
343 
344 
345 
346 
347 
348 
349 
VOLREC_Obj::a1
float32_t a1
the denominator filter coefficient value for z^(-1)
Definition: volt_recons.h:65
VOLREC_Obj::Vin_V
MATH_Vec3 Vin_V
the input Phase voltage phase (V)
Definition: volt_recons.h:70
VOLREC_setFlagCalSf
static void VOLREC_setFlagCalSf(VOLREC_Handle handle, const bool flagCalSf)
Sets up the SF enable flag.
Definition: volt_recons.h:284
VOLREC_Obj::sf
float32_t sf
the scale factor of phase voltage
Definition: volt_recons.h:73
VOLREC_run
static void VOLREC_run(VOLREC_Handle handle, float32_t Vdcbus, MATH_Vec3 *pVin, MATH_Vec2 *pVab)
Runs the Phase Voltage reconstruction.
Definition: volt_recons.h:219
VOLREC_getFlagCalSf
static bool VOLREC_getFlagCalSf(VOLREC_Handle handle)
Gets the SF enable flag.
Definition: volt_recons.h:268
VOLREC_Obj::numSamples
int32_t numSamples
the sampling points
Definition: volt_recons.h:84
VOLREC_Obj::jitterCount
int32_t jitterCount
the jitter count due to noise on input
Definition: volt_recons.h:90
VOLREC_Obj::b0
float32_t b0
the numerator filter coefficient value for z^0
Definition: volt_recons.h:66
VOLREC_Obj::signPrev
int32_t signPrev
the flag to detect ZCD
Definition: volt_recons.h:88
VOLREC_Obj
Defines the VOLREC controller object.
Definition: volt_recons.h:64
VOLREC_Obj::minSamples
int32_t minSamples
the sampling points
Definition: volt_recons.h:85
VOLREC_enableFlagCalSf
static void VOLREC_enableFlagCalSf(VOLREC_Handle handle)
Enables the SF enable flag.
Definition: volt_recons.h:300
VOLREC_disableFlagEnableSf
static void VOLREC_disableFlagEnableSf(VOLREC_Handle handle)
Disables the SF enable flag.
Definition: volt_recons.h:316
VOLREC_Obj::sfCalc
float32_t sfCalc
the scale factor of phase voltage
Definition: volt_recons.h:74
VOLREC_Obj::VaSum
float32_t VaSum
the square calculation over one sine cycle
Definition: volt_recons.h:79
VOLREC_init
VOLREC_Handle VOLREC_init(void *pMemory, const size_t numBytes)
Initializes the Voltage reconstruct module.
VOLREC_Obj::b1
float32_t b1
the numerator filter coefficient value for z^(-1)
Definition: volt_recons.h:67
VOLREC_Obj::VaRms
float32_t VaRms
the RMS Value
Definition: volt_recons.h:82
VOLREC_Obj::x1
MATH_Vec3 x1
the input value at time sample n=-1
Definition: volt_recons.h:68
VOLREC_Obj::VaSen
float32_t VaSen
the input Phase voltage phase (V)
Definition: volt_recons.h:77
VOLREC_Obj::VaSenRms
float32_t VaSenRms
the RMS Value
Definition: volt_recons.h:81
VOLREC_Obj::maxSamples
int32_t maxSamples
the sampling points
Definition: volt_recons.h:86
VOLREC_setParams
void VOLREC_setParams(VOLREC_Handle handle, const float32_t filterPole_rps, const float32_t ctrlFreq_Hz)
set the Phase Voltage reconstruction parameters
VOLREC_Obj::threshold
float32_t threshold
the voltage level corresponding to zero i/p
Definition: volt_recons.h:75
VOLREC_calcVolSF
static void VOLREC_calcVolSF(VOLREC_Handle handle, float32_t VaSen)
Runs the Phase Voltage reconstruction.
Definition: volt_recons.h:149
VOLREC_Obj::y1
MATH_Vec3 y1
the output value at time sample n=-1
Definition: volt_recons.h:69
VOLREC_Obj::VaSenSum
float32_t VaSenSum
the square calculation over one sine cycle
Definition: volt_recons.h:78
VOLREC_Handle
struct _VOLREC_Obj_ * VOLREC_Handle
Defines the VOLREC_Handle.
Definition: volt_recons.h:100
VOLREC_reset
void VOLREC_reset(VOLREC_Handle handle)
reset the Phase Voltage reconstruction variables
VOLREC_Obj::signCurr
int32_t signCurr
the flag to detect ZCD
Definition: volt_recons.h:89
float32_t
float float32_t
Definition: dcl_common.h:58
VOLREC_Obj::flagCalSf
bool flagCalSf
Definition: volt_recons.h:92
VOLREC_Obj::Vs_V
MATH_Vec3 Vs_V
the input Phase voltage phase (V)
Definition: volt_recons.h:71