AM243x Motor Control SDK  09.02.00
fwc.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 FWC_H
34 #define FWC_H
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40 
54 #include <stdlib.h>
55 #include <stdbool.h>
56 
57 #include "dcl.h"
58 #include "math_types.h"
59 
60 //*****************************************************************************
61 //
63 //
64 //*****************************************************************************
65 typedef struct _FWC_Obj_
66 {
69  bool flagEnable;
70 } FWC_Obj, *FWC_Handle;
71 
72 //*****************************************************************************
73 // the function prototypes
74 
75 //*****************************************************************************
86 //*****************************************************************************
87 extern FWC_Handle FWC_init(void *pMemory, const size_t numBytes);
88 
89 //*****************************************************************************
90 //
103 //*****************************************************************************
104 extern void FWC_setParams(FWC_Handle handle,
105  const float32_t Kp, const float32_t Ki,
106  const float32_t angleMin_rad,
107  const float32_t angleMax_rad);
108 
109 //*****************************************************************************
115 //*****************************************************************************
116 static inline void FWC_disable(FWC_Handle handle)
117 {
118  FWC_Obj *obj = (FWC_Obj *)handle;
119 
120  obj->flagEnable = false;
121 }
122 
123 //*****************************************************************************
129 //*****************************************************************************
130 static inline void FWC_enable(FWC_Handle handle)
131 {
132  FWC_Obj *obj = (FWC_Obj *)handle;
133 
134  obj->flagEnable = true;
135 }
136 
137 //*****************************************************************************
145 //*****************************************************************************
146 static inline float32_t* FWC_getCurrentAngle_rad_addr(FWC_Handle handle)
147 {
148  FWC_Obj *obj = (FWC_Obj *)handle;
149 
150  return(&(obj->angleCurrent_rad));
151 }
152 
153 //*****************************************************************************
161 //*****************************************************************************
162 static inline float32_t FWC_getCurrentAngle_rad(FWC_Handle handle)
163 {
164  FWC_Obj *obj = (FWC_Obj *)handle;
165 
166  return(obj->angleCurrent_rad);
167 }
168 
169 //*****************************************************************************
177 //*****************************************************************************
178 static inline bool FWC_getFlagEnable(FWC_Handle handle)
179 {
180  FWC_Obj *obj = (FWC_Obj *)handle;
181 
182  return(obj->flagEnable);
183 }
184 
185 //*****************************************************************************
193 //*****************************************************************************
194 static inline void FWC_setCurrentAngle_rad(FWC_Handle handle,
195  const float32_t angleCurrent_rad)
196 {
197  FWC_Obj *obj = (FWC_Obj *)handle;
198 
199  obj->angleCurrent_rad = angleCurrent_rad;
200 }
201 
202 //*****************************************************************************
210 //*****************************************************************************
211 static inline void FWC_setFlagEnable(FWC_Handle handle, const bool flagEnable)
212 {
213  FWC_Obj *obj = (FWC_Obj *)handle;
214 
215  obj->flagEnable = flagEnable;
216 }
217 
218 //*****************************************************************************
225 //*****************************************************************************
226 static inline void FWC_setKi(FWC_Handle handle, const float32_t Ki)
227 {
228  FWC_Obj *obj = (FWC_Obj *)handle;
229 
230  obj->pi.Ki = Ki;
231 }
232 
233 //*****************************************************************************
242 //*****************************************************************************
243 static inline void FWC_setKp(FWC_Handle handle, const float32_t Kp)
244 {
245  FWC_Obj *obj = (FWC_Obj *)handle;
246 
247  obj->pi.Kp = Kp;
248 }
249 
250 //*****************************************************************************
259 //*****************************************************************************
260 static inline void FWC_setGains(FWC_Handle handle,
261  const float32_t Kp, const float32_t Ki)
262 {
263  FWC_Obj *obj = (FWC_Obj *)handle;
264 
265  obj->pi.Kp = Kp;
266  obj->pi.Ki = Ki;
267 }
268 
269 //*****************************************************************************
279 //*****************************************************************************
280 static inline void FWC_setAngleMinMax(FWC_Handle handle,
281  const float32_t angleMin_rad,
282  const float32_t angleMax_rad)
283 {
284  FWC_Obj *obj = (FWC_Obj *)handle;
285 
286  // set the minimum and maximum values of PI controller
287  obj->pi.Umax = angleMax_rad;
288  obj->pi.Umin = angleMin_rad;
289 }
290 
291 //*****************************************************************************
299 //*****************************************************************************
300 static inline void FWC_setAngleMax(FWC_Handle handle,
301  const float32_t angleMax_rad)
302 {
303  FWC_Obj *obj = (FWC_Obj *)handle;
304 
305  // set the minimum values of PI controller
306  // convert the maximum angle to negative value in PI controller
307  // the maximum angle = MATH_PI_OVER_TWO - minimum value of PI output
308  obj->pi.Umax = angleMax_rad;
309 }
310 
311 //*****************************************************************************
319 //*****************************************************************************
320 static inline void FWC_setAngleMin(FWC_Handle handle,
321  const float32_t angleMin_rad)
322 {
323  FWC_Obj *obj = (FWC_Obj *)handle;
324 
325  // set the minimum values of PI controller
326  // convert the maximum angle to negative value in PI controller
327  // the maximum angle = MATH_PI_OVER_TWO - minimum value of PI output
328  obj->pi.Umin = angleMin_rad;
329 }
330 
331 //*****************************************************************************
337 //*****************************************************************************
338 #define FWC_resetUi FWC_resetController //Backwards compatible
339 static inline void FWC_resetController(FWC_Handle handle)
340 {
341  FWC_Obj *obj = (FWC_Obj *)handle;
342 
343  // Sets the integrator start value in the PI controller
344  // reset the new integrator value to Zero
345  obj->pi.i10 = 0.0f;
346  // reset saturation to unclamped
347  obj->pi.i6 = 1.0f;
348 
349 }
350 
351 //*****************************************************************************
359 //*****************************************************************************
360 static inline void FWC_setUi(FWC_Handle handle, const float32_t Ui)
361 {
362  FWC_Obj *obj = (FWC_Obj *)handle;
363 
364  // set the new integrator value
365  obj->pi.i10 = Ui;
366 }
367 
368 //*****************************************************************************
369 //
378 //*****************************************************************************
379 static __attribute__((always_inline))
380 void FWC_computeCurrentAngle(FWC_Handle handle,
381  const float32_t Vs_V, const float32_t VsRef_V)
382 {
383  FWC_Obj *obj = (FWC_Obj *)handle;
384  float32_t angle_rad;
385 
386  float32_t Vs_in_V = Vs_V;
387  float32_t VsRef_in_V = VsRef_V;
388 
389  if(FWC_getFlagEnable(handle) == true)
390  {
391  // Perform the Field Weakening Control (FWC)
392  angle_rad = DCL_runPISeries(&(obj->pi),
393  VsRef_in_V,
394  Vs_in_V);
395 
396  obj->angleCurrent_rad = MATH_PI_OVER_TWO - angle_rad;
397  }
398  else
399  {
400  obj->angleCurrent_rad = MATH_PI_OVER_TWO;
401  }
402 
403  return;
404 }
405 
408 #ifdef __cplusplus
409 }
410 #endif
411 
412 #endif // FWC_H
FWC_setParams
void FWC_setParams(FWC_Handle handle, const float32_t Kp, const float32_t Ki, const float32_t angleMin_rad, const float32_t angleMax_rad)
Sets the Field Weakening Control (FWC) module parmaeters.
FWC_disable
static void FWC_disable(FWC_Handle handle)
Disables the FWC function.
Definition: fwc.h:116
FWC_setFlagEnable
static void FWC_setFlagEnable(FWC_Handle handle, const bool flagEnable)
Sets the enable flag.
Definition: fwc.h:211
FWC_enable
static void FWC_enable(FWC_Handle handle)
Enables the FWC function.
Definition: fwc.h:130
FWC_getCurrentAngle_rad_addr
static float32_t * FWC_getCurrentAngle_rad_addr(FWC_Handle handle)
Gets the stator current phase angle memory address.
Definition: fwc.h:146
FWC_setAngleMin
static void FWC_setAngleMin(FWC_Handle handle, const float32_t angleMin_rad)
Sets the minimum output values of FWC.
Definition: fwc.h:320
FWC_Obj::flagEnable
bool flagEnable
a flag to enable the controller
Definition: fwc.h:69
FWC_Obj::pi
DCL_PI pi
the fwc angle PI controller object
Definition: fwc.h:67
FWC_init
FWC_Handle FWC_init(void *pMemory, const size_t numBytes)
Initializes the Field Weakening Control (FWC) module.
FWC_setAngleMax
static void FWC_setAngleMax(FWC_Handle handle, const float32_t angleMax_rad)
Sets the maximum output values of FWC.
Definition: fwc.h:300
dcl.h
Top level header that contains all collections of Digital Controller Library functions.
FWC_setAngleMinMax
static void FWC_setAngleMinMax(FWC_Handle handle, const float32_t angleMin_rad, const float32_t angleMax_rad)
Sets the minimum and maximum output values of FWC.
Definition: fwc.h:280
FWC_setCurrentAngle_rad
static void FWC_setCurrentAngle_rad(FWC_Handle handle, const float32_t angleCurrent_rad)
Sets the stator current phase angle value.
Definition: fwc.h:194
FWC_setKi
static void FWC_setKi(FWC_Handle handle, const float32_t Ki)
Sets the integral gain (Ki) value.
Definition: fwc.h:226
DCL_runPISeries
_DCL_CRIT_ACCESS float32_t DCL_runPISeries(DCL_PI *pi, float32_t rk, float32_t yk)
Executes an inline series form PI controller.
Definition: dcl_pi.h:313
FWC_getCurrentAngle_rad
static float32_t FWC_getCurrentAngle_rad(FWC_Handle handle)
Gets the stator current phase angle value (angleCurrent_rad)
Definition: fwc.h:162
DCL_PI
_DCL_VOLATILE struct dcl_pi DCL_PI
FWC_setUi
static void FWC_setUi(FWC_Handle handle, const float32_t Ui)
Sets the integrator start value (Ui)
Definition: fwc.h:360
FWC_Obj
Defines the Field Weakening Control (FWC) object.
Definition: fwc.h:66
FWC_setGains
static void FWC_setGains(FWC_Handle handle, const float32_t Kp, const float32_t Ki)
Sets the gain values.
Definition: fwc.h:260
FWC_getFlagEnable
static bool FWC_getFlagEnable(FWC_Handle handle)
Gets the enable controller flag value from the FWC.
Definition: fwc.h:178
FWC_Obj::angleCurrent_rad
float32_t angleCurrent_rad
the stator current phase angle
Definition: fwc.h:68
FWC_setKp
static void FWC_setKp(FWC_Handle handle, const float32_t Kp)
Sets the proportional gain (Kp) value.
Definition: fwc.h:243
float32_t
float float32_t
Definition: dcl_common.h:58
FWC_resetController
static void FWC_resetController(FWC_Handle handle)
Definition: fwc.h:339
FWC_computeCurrentAngle
static void FWC_computeCurrentAngle(FWC_Handle handle, const float32_t Vs_V, const float32_t VsRef_V)
Runs the Field Weakening Control (FWC)
Definition: fwc.h:380