AM243x Motor Control SDK  09.02.00
mtpa.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 MTPA_H
34 #define MTPA_H
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif
40 
52 #include <stdlib.h>
53 #include <stdint.h>
54 #include <stdbool.h>
55 #include <math.h>
56 
57 #include "math_types.h"
58 
60 #define MTPA_LUT_INDEX_LD_MAX 20
61 
62 
64 #define MTPA_LUT_INDEX_LQ_MAX 20
65 
67 #define MTPA_LUT_DELTA_CURRENT_LD_A 0.5
68 
70 #define MTPA_LUT_DELTA_CURRENT_LD_A 0.5
71 
72 //*****************************************************************************
73 //
75 //
76 //*****************************************************************************
77 typedef struct _MTPA_Obj_
78 {
79  MATH_Vec2 Idq_ref_A;
86  uint32_t indexMax_Ld;
87  uint32_t indexMax_Lq;
88  bool flagEnable;
89 } MTPA_Obj, *MTPA_Handle;
90 
91 
94 
97 
98 //*****************************************************************************
99 //
100 // Prototypes for the APIs
101 //
102 //*****************************************************************************
103 
104 //*****************************************************************************
112 //*****************************************************************************
113 extern void MTPA_computeParameters(MTPA_Handle handle,
114  const float32_t Ls_d_H,
115  const float32_t Ls_q_H,
116  const float32_t flux_Wb);
117 
118 //*****************************************************************************
119 //
127 //
128 //*****************************************************************************
129 extern MTPA_Handle MTPA_init(void *pMemory, const size_t numBytes);
130 
131 //*****************************************************************************
136 //*****************************************************************************
137 static inline void MTPA_disable(MTPA_Handle handle)
138 {
139  MTPA_Obj *obj = (MTPA_Obj *)handle;
140 
141  obj->flagEnable = false;
142 
143  return;
144 } // end of MTPA_disable() function
145 
146 //*****************************************************************************
151 //*****************************************************************************
152 static inline void MTPA_enable(MTPA_Handle handle)
153 {
154  MTPA_Obj *obj = (MTPA_Obj *)handle;
155 
156  obj->flagEnable = true;
157 
158  return;
159 } // end of MTPA_enable() function
160 
161 //*****************************************************************************
167 //*****************************************************************************
168 static inline float32_t *MTPA_getCurrentAnglePhase_rad_addr(MTPA_Handle handle)
169 {
170  MTPA_Obj *obj = (MTPA_Obj *)handle;
171 
172  return(&(obj->angleCurrent_rad));
173 } // end of MTPA_getCurrentAnglePhase_rad_addr() function
174 
175 //*****************************************************************************
181 //*****************************************************************************
182 static inline float32_t MTPA_getCurrentAngle_rad(MTPA_Handle handle)
183 {
184  MTPA_Obj *obj = (MTPA_Obj *)handle;
185 
186  return(obj->angleCurrent_rad);
187 } // end of MTPA_getCurrentAngle_rad() function
188 
189 //*****************************************************************************
195 //*****************************************************************************
196 static inline float32_t MTPA_getKconst(MTPA_Handle handle)
197 {
198  MTPA_Obj *obj = (MTPA_Obj *)handle;
199 
200  return(obj->kconst);
201 } // end of MTPA_getKconst() function
202 
203 //*****************************************************************************
209 //*****************************************************************************
210 static inline float32_t *MTPA_getIs_ref_A_addr(MTPA_Handle handle)
211 {
212  MTPA_Obj *obj = (MTPA_Obj *)handle;
213 
214  return(&(obj->Is_ref_A));
215 } // end of CTRL_getId_A_addr() function
216 
217 //*****************************************************************************
223 //*****************************************************************************
224 static inline bool MTPA_getFlagEnable(MTPA_Handle handle)
225 {
226  MTPA_Obj *obj = (MTPA_Obj *)handle;
227 
228  return(obj->flagEnable);
229 } // end of MTPA_getFlagEnable() function
230 
231 //*****************************************************************************
237 //*****************************************************************************
238 static inline float32_t MTPA_getId_ref_A(MTPA_Handle handle)
239 {
240  MTPA_Obj *obj = (MTPA_Obj *)handle;
241 
242  return(obj->Idq_ref_A.value[0]);
243 } // end of MTPA_getId_ref_A() function
244 
245 //*****************************************************************************
251 //*****************************************************************************
252 static inline float32_t MTPA_getIq_ref_A(MTPA_Handle handle)
253 {
254  MTPA_Obj *obj = (MTPA_Obj *)handle;
255 
256  return(obj->Idq_ref_A.value[1]);
257 } // end of MTPA_getIq_ref_A() function
258 
259 //*****************************************************************************
265 //*****************************************************************************
266 static inline float32_t MTPA_getIs_ref_A(MTPA_Handle handle)
267 {
268  MTPA_Obj *obj = (MTPA_Obj *)handle;
269 
270  return(obj->Is_ref_A);
271 } // end of MTPA_getIs_ref_A() function
272 
273 //*****************************************************************************
279 //*****************************************************************************
280 static inline
281 void MTPA_setFlagEnable(MTPA_Handle handle, const bool flagEnable)
282 {
283  MTPA_Obj *obj = (MTPA_Obj *)handle;
284 
285  obj->flagEnable = flagEnable;
286 
287  return;
288 } // end of MTPA_setFlag_enable() function
289 
290 //*****************************************************************************
296 //*****************************************************************************
297 static inline
298 void MTPA_setCurrentAngle_rad(MTPA_Handle handle, const float32_t angleCurrent_rad)
299 {
300  MTPA_Obj *obj = (MTPA_Obj *)handle;
301 
302  obj->angleCurrent_rad = angleCurrent_rad;
303 
304  return;
305 } // end of MTPA_setCurrentAngle_rad() function
306 
307 //*****************************************************************************
313 //*****************************************************************************
314 static inline
315 void MTPA_setId_ref_A(MTPA_Handle handle, const float32_t Id_ref_A)
316 {
317  MTPA_Obj *obj = (MTPA_Obj *)handle;
318 
319  obj->Idq_ref_A.value[0] = Id_ref_A;
320 
321  return;
322 } // end of MTPA_setId_ref_A() function
323 
324 //*****************************************************************************
330 //*****************************************************************************
331 static inline
332 void MTPA_setIq_ref_A(MTPA_Handle handle, const float32_t Iq_ref_A)
333 {
334  MTPA_Obj *obj = (MTPA_Obj *)handle;
335 
336  obj->Idq_ref_A.value[1] = Iq_ref_A;
337 
338  return;
339 } // end of MTPA_setIq_ref_A() function
340 
341 //*****************************************************************************
347 //*****************************************************************************
348 static inline void
349 MTPA_setIs_ref_A(MTPA_Handle handle, const float32_t Is_ref_A)
350 {
351  MTPA_Obj *obj = (MTPA_Obj *)handle;
352 
353  obj->Is_ref_A = Is_ref_A;
354 
355  return;
356 } // end of MTPA_setIs_ref_A() function
357 
358 //*****************************************************************************
366 //*****************************************************************************
367 static inline
368 float32_t MTPA_updateLs_d_withLUT(MTPA_Handle handle, const float32_t Is_A)
369 {
370  MTPA_Obj *obj = (MTPA_Obj *)handle;
371 
372  uint32_t indexLdCnt = 0;
373 
374  indexLdCnt = (uint32_t)(Is_A/obj->deltaIs_Ld_A);
375 
376  if(indexLdCnt > obj->indexMax_Ld)
377  {
378  indexLdCnt = obj->indexMax_Ld;
379  }
380 
381  return(MTPA_Ld_tableData_H[indexLdCnt]);
382 } // end of MTPA_updateLs_d_withLUT() function
383 
384 //*****************************************************************************
392 //*****************************************************************************
393 static inline
394 float32_t MTPA_updateLs_q_withLUT(MTPA_Handle handle, const float32_t Is_A)
395 {
396  MTPA_Obj *obj = (MTPA_Obj *)handle;
397 
398  uint16_t indexLqCnt = 0;
399 
400  indexLqCnt = (uint16_t)(Is_A/obj->deltaIs_Lq_A);
401 
402  if(indexLqCnt > obj->indexMax_Lq)
403  {
404  indexLqCnt = obj->indexMax_Lq;
405  }
406 
407  return(MTPA_Ld_tableData_H[indexLqCnt]);
408 } // end of MTPA_updateLs_q_withLUT() function
409 
410 //*****************************************************************************
416 //*****************************************************************************
417 static __attribute__((always_inline))
418 void MTPA_computeCurrentReference(MTPA_Handle handle, const float32_t Is_ref_A)
419 {
420  MTPA_Obj *obj = (MTPA_Obj *)handle;
421 
422 
423  obj->Is_ref_A = fabsf(Is_ref_A);
424 
425  // Perform the Maximum torque per ampere (MTPA)
426  if(MTPA_getFlagEnable(handle) == true &&
427  (obj->kconst != 0.0) && (obj->Is_ref_A != 0.0))
428  {
429  obj->Idq_ref_A.value[0] = obj->kconst -
430  (float32_t)sqrtf((obj->kconst * obj->kconst) +
431  (0.5f * (obj->Is_ref_A * obj->Is_ref_A)));
432 
433  obj->Idq_ref_A.value[1] = sqrtf((obj->Is_ref_A * obj->Is_ref_A) -
434  (obj->Idq_ref_A.value[0] * obj->Idq_ref_A.value[0]));
435 
436  if(Is_ref_A < 0.0)
437  {
438  obj->Idq_ref_A.value[1] = -obj->Idq_ref_A.value[1];
439  }
440  }
441  else
442  {
443  obj->Idq_ref_A.value[0] = 0.0;
444  obj->Idq_ref_A.value[1] = Is_ref_A;
445  }
446 
447  return;
448 } // end of MTPA_computeCurrentReference() function
449 
450 
451 //*****************************************************************************
456 //*****************************************************************************
457 static __attribute__((always_inline))
458 float32_t MTPA_computeCurrentAngle(MTPA_Handle handle, const float32_t Is_ref_A)
459 {
460  MTPA_Obj *obj = (MTPA_Obj *)handle;
461 
462  obj->Is_ref_A = fabsf(Is_ref_A);
463 
464  if(MTPA_getFlagEnable(handle) == true)
465  {
466  // Perform the Maximum torque per ampere (MTPA)
467  if((obj->kconst != 0.0) && (obj->Is_ref_A != 0.0))
468  {
469  obj->gconst = obj->kconst / obj->Is_ref_A;
470 
471  obj->angleCurrent_rad = acosf(obj->gconst -
472  sqrtf(obj->gconst * obj->gconst + 0.5f));
473  }
474  else
475  {
476  obj->angleCurrent_rad = MATH_PI_OVER_TWO;
477  }
478  }
479  else
480  {
481  obj->angleCurrent_rad = MATH_PI_OVER_TWO;
482  }
483 
484  return(obj->angleCurrent_rad);
485 } // end of MTPA_computeCurrentAngle() function
486 
490 #ifdef __cplusplus
491 }
492 #endif
493 
494 #endif // MTPA_H
MTPA_getCurrentAngle_rad
static float32_t MTPA_getCurrentAngle_rad(MTPA_Handle handle)
Gets the stator current phase angle value (angleCurrent_rad)
Definition: mtpa.h:182
MTPA_Obj::deltaIs_Ld_A
float32_t deltaIs_Ld_A
the Is detla for Ls lookup table
Definition: mtpa.h:84
MTPA_LUT_INDEX_LQ_MAX
#define MTPA_LUT_INDEX_LQ_MAX
Defines the maximum length of LUT for Lq.
Definition: mtpa.h:64
MTPA_getFlagEnable
static bool MTPA_getFlagEnable(MTPA_Handle handle)
Gets the enable controller flag value from the controller.
Definition: mtpa.h:224
MTPA_computeCurrentReference
static void MTPA_computeCurrentReference(MTPA_Handle handle, const float32_t Is_ref_A)
Compute the current reference of MTPA.
Definition: mtpa.h:418
MTPA_Obj::indexMax_Lq
uint32_t indexMax_Lq
the Is detla for Lq lookup table
Definition: mtpa.h:87
MTPA_setCurrentAngle_rad
static void MTPA_setCurrentAngle_rad(MTPA_Handle handle, const float32_t angleCurrent_rad)
Sets the stator current phase angle value.
Definition: mtpa.h:298
MTPA_setFlagEnable
static void MTPA_setFlagEnable(MTPA_Handle handle, const bool flagEnable)
Sets the enable flag.
Definition: mtpa.h:281
MTPA_computeParameters
void MTPA_computeParameters(MTPA_Handle handle, const float32_t Ls_d_H, const float32_t Ls_q_H, const float32_t flux_Wb)
Computes the motor constant for MTPA module.
MTPA_getIs_ref_A_addr
static float32_t * MTPA_getIs_ref_A_addr(MTPA_Handle handle)
Gets the stator current reference memory address.
Definition: mtpa.h:210
MTPA_init
MTPA_Handle MTPA_init(void *pMemory, const size_t numBytes)
Initializes the maximum torque per ampere (MTPA) module.
MTPA_Obj::Idq_ref_A
MATH_Vec2 Idq_ref_A
the d/q-axis current reference
Definition: mtpa.h:79
MTPA_Obj::kconst
float32_t kconst
the K constant for mtpa
Definition: mtpa.h:82
MTPA_getCurrentAnglePhase_rad_addr
static float32_t * MTPA_getCurrentAnglePhase_rad_addr(MTPA_Handle handle)
Gets the stator current phase angle memory address.
Definition: mtpa.h:168
MTPA_Obj::Is_ref_A
float32_t Is_ref_A
the vector stator current reference
Definition: mtpa.h:81
MTPA_setIq_ref_A
static void MTPA_setIq_ref_A(MTPA_Handle handle, const float32_t Iq_ref_A)
Sets the quadrature current (Iq_ref) reference value.
Definition: mtpa.h:332
MTPA_getIs_ref_A
static float32_t MTPA_getIs_ref_A(MTPA_Handle handle)
Gets the stator current reference value (Is_ref_A)
Definition: mtpa.h:266
MTPA_Obj::gconst
float32_t gconst
the G constant for mtpa
Definition: mtpa.h:83
MTPA_LUT_INDEX_LD_MAX
#define MTPA_LUT_INDEX_LD_MAX
Defines the maximum length of LUT for Ld.
Definition: mtpa.h:60
MTPA_Obj::angleCurrent_rad
float32_t angleCurrent_rad
the vector stator current reference
Definition: mtpa.h:80
MTPA_Obj::deltaIs_Lq_A
float32_t deltaIs_Lq_A
the Is detla for Ls lookup table
Definition: mtpa.h:85
MTPA_getIq_ref_A
static float32_t MTPA_getIq_ref_A(MTPA_Handle handle)
Gets the quadrature current reference value (Iq_ref_A)
Definition: mtpa.h:252
MTPA_Obj
Defines the MTPA object.
Definition: mtpa.h:78
MTPA_enable
static void MTPA_enable(MTPA_Handle handle)
Enables the MTPA.
Definition: mtpa.h:152
MTPA_computeCurrentAngle
static float32_t MTPA_computeCurrentAngle(MTPA_Handle handle, const float32_t Is_ref_A)
Compute the current angle of MTPA.
Definition: mtpa.h:458
MTPA_disable
static void MTPA_disable(MTPA_Handle handle)
Disables the MTPA.
Definition: mtpa.h:137
MTPA_Ld_tableData_H
const float32_t MTPA_Ld_tableData_H[MTPA_LUT_INDEX_LD_MAX+1]
Defines the Ld array.
MTPA_updateLs_q_withLUT
static float32_t MTPA_updateLs_q_withLUT(MTPA_Handle handle, const float32_t Is_A)
Update the motor inductances.
Definition: mtpa.h:394
MTPA_Obj::indexMax_Ld
uint32_t indexMax_Ld
the Is detla for Ld lookup table
Definition: mtpa.h:86
MTPA_setIs_ref_A
static void MTPA_setIs_ref_A(MTPA_Handle handle, const float32_t Is_ref_A)
Sets the stator current reference value (Is_ref)
Definition: mtpa.h:349
MTPA_Obj::flagEnable
bool flagEnable
a flag to enable the controller
Definition: mtpa.h:88
MTPA_getKconst
static float32_t MTPA_getKconst(MTPA_Handle handle)
Gets the motor constant of MTPA.
Definition: mtpa.h:196
MTPA_updateLs_d_withLUT
static float32_t MTPA_updateLs_d_withLUT(MTPA_Handle handle, const float32_t Is_A)
Update the motor inductances.
Definition: mtpa.h:368
MTPA_setId_ref_A
static void MTPA_setId_ref_A(MTPA_Handle handle, const float32_t Id_ref_A)
Sets the direct current (Id_ref) reference vector value.
Definition: mtpa.h:315
float32_t
float float32_t
Definition: dcl_common.h:58
MTPA_getId_ref_A
static float32_t MTPA_getId_ref_A(MTPA_Handle handle)
Gets the direct current reference value (Id_ref_A)
Definition: mtpa.h:238
MTPA_Lq_tableData_H
const float32_t MTPA_Lq_tableData_H[MTPA_LUT_INDEX_LQ_MAX+1]
Defines the Lq array.