hall.h
Go to the documentation of this file.
1 //#############################################################################
2 // $TI Release: MotorControl SDK v1.00.00.00 $
3 // $Release Date: Mon Mar 11 18:37:41 CDT 2019 $
4 // $Copyright:
5 // Copyright (C) 2017-2019 Texas Instruments Incorporated - http://www.ti.com/
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //
14 // Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the
17 // distribution.
18 //
19 // Neither the name of Texas Instruments Incorporated nor the names of
20 // its contributors may be used to endorse or promote products derived
21 // from this software without specific prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // $
35 //#############################################################################
36 
37 #ifndef _HALL_H_
38 #define _HALL_H_
39 
44 
45 //*****************************************************************************
46 //
47 // If building with a C++ compiler, make all of the definitions in this header
48 // have a C binding.
49 //
50 //*****************************************************************************
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #endif
55 
56 //*****************************************************************************
57 //
60 //
61 //*****************************************************************************
62 
63 // the includes
64 #ifdef __TMS320C28XX_CLA__
65 #include "libraries/math/src/float/CLAmath.h"
66 #else
67 #include <math.h>
68 #endif
69 
71 
72 //modules
73 #include "userParams.h"
74 #include "gpio.h"
75 
76 // **************************************************************************
77 // the defines
78 
79 
80 // **************************************************************************
81 // the typedefs
82 
83 // State machine typedef for HALL status
84 typedef enum
85 {
86  HALL_IDLE = 0,
91 
92 #ifdef HALL_CAL
93 #define CAL_BUF_NUM 13
94 #endif //HALL_CAL
95 
96 
99 typedef struct _HALL_Obj_
100 {
101  float32_t capScaler; // Scaler converting 1/N CPU cycles
102  float32_t pwmScaler; // Scaler converting 1/N CPU cycles
107  uint32_t timeStampCAP; //
108  uint32_t timeCountPWM; //
109  uint32_t timeCount[2]; //
110  uint32_t timeCountMax; //
111 
112  float32_t thetaBuff[7]; // 1~6 are valid value
117  uint16_t gpioHallU; // the GPIO for Hall U
118  uint16_t gpioHallV; // the GPIO for Hall V
119  uint16_t gpioHallW; // the GPIO for Hall W
120  uint16_t hallIndex; //
121  uint16_t hallIndexPrev; //
122  uint16_t hallPrev[7]; //
123  uint16_t hallDirection; //
125 
126 #ifdef HALL_CAL
127  float32_t thetaCalBuff[7]; //
128  float32_t thetaIndexBuff[CAL_BUF_NUM]; //
129  uint16_t hallIndexNow; //
130  uint16_t hallIndexPre; //
131  uint16_t hallIndexFlag; //
132 #endif //HALL_CAL
133 } HALL_Obj;
134 
135 
138 typedef struct _HALL_Obj_ *HALL_Handle;
139 
140 // **************************************************************************
141 // the function prototypes
142 
147 extern HALL_Handle HALL_init(void *pMemory, const size_t numBytes);
148 
151 extern void HALL_setParams(HALL_Handle handle, const USER_Params *pUserParams);
152 
155 extern void HALL_setAngleBuf(HALL_Handle handle, const float32_t *ptrAngleBuf);
156 
159 extern void HALL_resetParams(HALL_Handle handle);
160 
163 extern void HALL_setGPIOs(HALL_Handle handle, const uint16_t gpioHallU,
164  const uint16_t gpioHallV, const uint16_t gpioHallW);
165 
169 {
170  HALL_Obj *obj = (HALL_Obj *)handle;
171 
172  return(obj->speedHall_Hz);
173 }
174 
178 static inline void HALL_setSpeed_Hz(HALL_Handle handle, const float32_t speedHall_Hz)
179 {
180  HALL_Obj *obj = (HALL_Obj *)handle;
181 
182  obj->speedHall_Hz = speedHall_Hz;
183 
184  return;
185 }
186 
190 static inline void HALL_setTimeStamp(HALL_Handle handle, const uint32_t timeStamp)
191 {
192  HALL_Obj *obj = (HALL_Obj *)handle;
193 
194  obj->timeStampCAP = timeStamp;
195 
196  return;
197 }
198 
202 {
203  HALL_Obj *obj = (HALL_Obj *)handle;
204 
205  return(obj->thetaHall_rad);
206 }
207 
210 static inline uint16_t HALL_getInputState(HALL_Handle handle)
211 {
212  HALL_Obj *obj = (HALL_Obj *)handle;
213 
214  uint32_t hallState = 0;
215 
216  hallState = GPIO_readPin(obj->gpioHallU);
217  hallState += GPIO_readPin(obj->gpioHallV)<<1;
218  hallState += GPIO_readPin(obj->gpioHallW)<<2;
219 
220  return(hallState & 0x0007);
221 }
222 
223 
224 #ifdef HALL_CAL
225 static inline void HALL_calibrateIndexAngle(HALL_Handle handle, float32_t angle)
229 {
230  HALL_Obj *obj = (HALL_Obj *)handle;
231  obj->hallIndexNow = HALL_getInputState(handle);
232 
233  if(obj->hallIndexNow != obj->hallIndexPre)
234  {
235  obj->hallIndexPre = obj->hallIndexNow;
236 
237  if(obj->hallIndexFlag == 0)
238  {
239  obj->thetaIndexBuff[obj->hallIndexNow] = angle;
240 
241  if(obj->hallIndexNow == 0x01)
242  {
243  obj->hallIndexFlag = 1;
244  }
245  }
246  else
247  {
248  obj->thetaIndexBuff[6 + obj->hallIndexNow] = angle;
249 
250  if(obj->hallIndexNow == 0x01)
251  {
252  obj->hallIndexFlag = 0;
253  }
254  }
255 
256  obj->thetaCalBuff[obj->hallIndexNow] = 0.5f *
257  (obj->thetaIndexBuff[obj->hallIndexNow] +
258  obj->thetaIndexBuff[6 + obj->hallIndexNow]);
259  }
260 
261  return;
262 }
263 #endif //HALL_CAL
264 
268 static inline void HALL_setForceAngleAndIndex(HALL_Handle handle, float32_t speedRef)
269 {
270  HALL_Obj *obj = (HALL_Obj *)handle;
271  obj->hallIndex = HALL_getInputState(handle);
272 
273  if(speedRef > 0.0f)
274  {
275  obj->hallIndexPrev = obj->hallPrev[obj->hallIndex];
276  obj->hallDirection = 0;
277 
278  obj->thetaHall_rad = obj->thetaBuff[obj->hallIndex] - obj->thetaDelta_rad;
279  }
280  else
281  {
282  obj->hallIndexPrev = obj->hallIndex;
283  obj->hallDirection = 1;
284 
285  obj->thetaHall_rad = obj->thetaBuff[obj->hallIndex] + obj->thetaDelta_rad;
286  }
287 
288  if(obj->thetaHall_rad >= MATH_PI)
289  {
291  }
292  else if(obj->thetaHall_rad <= -MATH_PI)
293  {
295  }
296 
297  return;
298 }
299 
303 #define HALL_calcAngle HALL_run
304 
305 static inline void HALL_run(HALL_Handle handle, float32_t speedRef)
306 {
307  HALL_Obj *obj = (HALL_Obj *)handle;
308 
309  obj->hallIndex = HALL_getInputState(handle);
310  obj->timeCountPWM++;
311 
312  if(obj->hallIndex != obj->hallIndexPrev)
313  {
314  if(speedRef > 0.0f)
315  {
316  obj->thetaHall_rad = obj->thetaBuff[obj->hallIndex] + obj->thetaDelta_rad;
317  obj->hallDirection = 0;
318  }
319  else
320  {
321  obj->thetaHall_rad = obj->thetaBuff[obj->hallIndex] - obj->thetaDelta_rad;
322  obj->hallDirection = 1;
323  }
324 
325  obj->hallIndexPrev = obj->hallIndex;
326 
329  ((float32_t)(obj->timeCountPWM + obj->timeCount[1] + obj->timeCount[0]));
330 
331  if(obj->hallDirection == 0)
332  {
333  obj->speedCAP_Hz = speedCAP_Hz;
334  obj->speedPWM_Hz = speedPWM_Hz;
335  }
336  else
337  {
338  obj->speedCAP_Hz = -speedCAP_Hz;
339  obj->speedPWM_Hz = -speedPWM_Hz;
340  }
341 
342  if(speedPWM_Hz > obj->speedSwitch_Hz)
343  {
344  obj->speedHall_Hz = obj->speedCAP_Hz;
345  }
346  else
347  {
348  obj->speedHall_Hz = obj->speedPWM_Hz;
349  }
350 
351  obj->timeCount[1] = obj->timeCount[0];
352  obj->timeCount[0] = obj->timeCountPWM;
353  obj->timeCountPWM = 0;
354  }
355 
356  if(obj->timeCountPWM > obj->timeCountMax)
357  {
358  obj->speedCAP_Hz = 0.0f;
359  obj->speedPWM_Hz = 0.0f;
360  obj->speedHall_Hz = 0.0f;
361  obj->timeCountPWM = 0;
362  }
363 
364  if(obj->thetaHall_rad >= MATH_PI)
365  {
367  }
368  else if(obj->thetaHall_rad <= -MATH_PI)
369  {
371  }
372 
373  return;
374 }
375 
376 //*****************************************************************************
377 //
378 // Close the Doxygen group.
380 //
381 //*****************************************************************************
382 
383 //*****************************************************************************
384 //
385 // Mark the end of the C bindings section for C++ compilers.
386 //
387 //*****************************************************************************
388 #ifdef __cplusplus
389 }
390 #endif
391 
392 #endif //end of _HALL_H_ definition
393 
_HALL_Obj_::speedHall_Hz
float32_t speedHall_Hz
Definition: hall.h:105
_HALL_Obj_::hallStatus
HALL_Status_e hallStatus
Definition: hall.h:124
_HALL_Obj_::hallIndexPrev
uint16_t hallIndexPrev
Definition: hall.h:121
HALL_resetParams
void HALL_resetParams(HALL_Handle handle)
Resets the hall estimator parameters.
_HALL_Obj_::timeCountPWM
uint32_t timeCountPWM
Definition: hall.h:108
_HALL_Obj_::thetaDelta_pu
float32_t thetaDelta_pu
Definition: hall.h:115
_HALL_Obj_::thetaBuff
float32_t thetaBuff[7]
Definition: hall.h:112
float32_t
float float32_t
Definition: sfra_f32.h:42
MATH_TWO_PI
#define MATH_TWO_PI
Defines 2*pi.
Definition: math.h:154
HALL_Obj
struct _HALL_Obj_ HALL_Obj
Defines the HALL controller object.
HALL_CALIBRATION
Definition: hall.h:88
_HALL_Obj_::thetaDelta_rad
float32_t thetaDelta_rad
Definition: hall.h:114
HALL_setForceAngleAndIndex
static void HALL_setForceAngleAndIndex(HALL_Handle handle, float32_t speedRef)
Sets the force angle and index for next step of the hall estimator.
Definition: hall.h:268
_HALL_Obj_::speedCAP_Hz
float32_t speedCAP_Hz
Definition: hall.h:103
MATH_PI
#define MATH_PI
Defines pi.
Definition: math.h:140
HALL_setGPIOs
void HALL_setGPIOs(HALL_Handle handle, const uint16_t gpioHallU, const uint16_t gpioHallV, const uint16_t gpioHallW)
Sets GPIO for hall sensors input.
_HALL_Obj_::speedPWM_Hz
float32_t speedPWM_Hz
Definition: hall.h:104
HALL_IDLE
Definition: hall.h:86
_HALL_Obj_::hallPrev
uint16_t hallPrev[7]
Definition: hall.h:122
_HALL_Obj_::timeCount
uint32_t timeCount[2]
Definition: hall.h:109
_HALL_Obj_
Defines the HALL controller object.
Definition: hall.h:99
math.h
_HALL_Obj_::pwmScaler
float32_t pwmScaler
Definition: hall.h:102
HALL_setAngleBuf
void HALL_setAngleBuf(HALL_Handle handle, const float32_t *ptrAngleBuf)
Sets the angle buffer value for hall estimator.
_HALL_Obj_::thetaHall_pu
float32_t thetaHall_pu
Definition: hall.h:116
HALL_ALIGNMENT
Definition: hall.h:87
_HALL_Obj_::capScaler
float32_t capScaler
Definition: hall.h:101
HALL_init
HALL_Handle HALL_init(void *pMemory, const size_t numBytes)
Initializes the HALL controller.
HALL_setTimeStamp
static void HALL_setTimeStamp(HALL_Handle handle, const uint32_t timeStamp)
Sets a value to the time stamp for the hall estimator.
Definition: hall.h:190
_HALL_Obj_::hallIndex
uint16_t hallIndex
Definition: hall.h:120
_HALL_Obj_::hallDirection
uint16_t hallDirection
Definition: hall.h:123
_HALL_Obj_::timeStampCAP
uint32_t timeStampCAP
Definition: hall.h:107
HALL_getAngle_rad
static float32_t HALL_getAngle_rad(HALL_Handle handle)
Gets the angle from the hall estimator, rad.
Definition: hall.h:201
HALL_Status_e
HALL_Status_e
Definition: hall.h:84
HALL_setSpeed_Hz
static void HALL_setSpeed_Hz(HALL_Handle handle, const float32_t speedHall_Hz)
Sets a value to the speed for the hall estimator.
Definition: hall.h:178
_HALL_Obj_::gpioHallU
uint16_t gpioHallU
Definition: hall.h:117
_HALL_Obj_::gpioHallW
uint16_t gpioHallW
Definition: hall.h:119
HALL_getInputState
static uint16_t HALL_getInputState(HALL_Handle handle)
Gets the hall sensors input GPIO state.
Definition: hall.h:210
HALL_RUN
Definition: hall.h:89
HALL_Handle
struct _HALL_Obj_ * HALL_Handle
Defines the HALL handle.
Definition: hall.h:138
HALL_run
static void HALL_run(HALL_Handle handle, float32_t speedRef)
Definition: hall.h:305
_HALL_Obj_::speedSwitch_Hz
float32_t speedSwitch_Hz
Definition: hall.h:106
_USER_Params_
Defines a structure for the user parameters.
Definition: include/userParams.h:98
_HALL_Obj_::thetaHall_rad
float32_t thetaHall_rad
Definition: hall.h:113
_HALL_Obj_::timeCountMax
uint32_t timeCountMax
Definition: hall.h:110
_HALL_Obj_::gpioHallV
uint16_t gpioHallV
Definition: hall.h:118
HALL_getSpeed_Hz
static float32_t HALL_getSpeed_Hz(HALL_Handle handle)
Gets the feedback speed from hall estimator.
Definition: hall.h:168
HALL_setParams
void HALL_setParams(HALL_Handle handle, const USER_Params *pUserParams)
Sets the hall estimator parameters.

Copyright 2023, Texas Instruments Incorporated