hall.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef _HALL_SENSOR_H__
  2. #define _HALL_SENSOR_H__
  3. #include "os/os_types.h"
  4. #include "bsp/bsp.h"
  5. #include "math/fix_math.h"
  6. #define NEGATIVE (int8_t)-1
  7. #define POSITIVE (int8_t)1
  8. //S32Q19 格式
  9. #define PHASE_0_DEGREE (0)
  10. #define PHASE_60_DEGREE (983040*32)
  11. #define PHASE_120_DEGREE (1966080*32)
  12. #define PHASE_180_DEGREE (2949120*32)
  13. #define PHASE_240_DEGREE (3932160*32)
  14. #define PHASE_300_DEGREE (4915200*32)
  15. #define PHASE_360_DEGREE (5898240*32)
  16. #define STATE_0 (uint8_t)0
  17. #define STATE_1 (uint8_t)1
  18. #define STATE_2 (uint8_t)2
  19. #define STATE_3 (uint8_t)3
  20. #define STATE_4 (uint8_t)4
  21. #define STATE_5 (uint8_t)5
  22. #define STATE_6 (uint8_t)6
  23. #define STATE_7 (uint8_t)7
  24. #define THETA_NONE (float)0xFFFF
  25. #define SAMPLE_MAX_COUNT 3
  26. typedef struct {
  27. u32 ticks[SAMPLE_MAX_COUNT];
  28. s32q19_t angles[SAMPLE_MAX_COUNT];
  29. u32 ticks_sum;
  30. s32q19_t angles_sum;
  31. u32 index;
  32. bool full;
  33. }hall_sample_t;
  34. typedef struct {
  35. s32q19_t estimate_el_angle; //60度区间内的估计电角度
  36. s32q19_t estimate_delta_angle;
  37. u32 estimate_time_ticks;
  38. s32q19_t measured_el_angle; //hall测量到的电角度
  39. s32q19_t next_delta_angle;
  40. s32q5_t estimate_el_speed;
  41. s32q5_t immediately_el_speed; //当前的即时速度,主要用来判断电机转动是否达到稳定
  42. s32q5_t el_speed; //当前的平均效果的电角速度, 单位:rad/s
  43. s32q5_t rpm; //当前的电速度, 单位:RPM
  44. bool trns_detect; //速度变化超过阈值
  45. u8 hall_stat;
  46. u32 hall_ticks;
  47. s8 direction;
  48. s32q19_t phase_offset;
  49. hall_sample_t samples;
  50. u32 sensor_error;
  51. s32q19_t angle_table[8];
  52. }hall_sensor_t;
  53. void hall_sensor_init(void);
  54. void hall_sensor_clear(void);
  55. s16q5_t hall_sensor_get_theta(void); //return degree
  56. s32q5_t hall_sensor_get_speed(void); //return rpm;
  57. int hall_offset_increase(int inc);
  58. s32 *hall_get_table(void);
  59. #endif /* _HALL_SENSOR_H__ */