hall.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.0f)
  10. #if 0
  11. #define PHASE_60_DEGREE (983040*32)
  12. #define PHASE_120_DEGREE (1966080*32)
  13. #define PHASE_180_DEGREE (2949120*32)
  14. #define PHASE_240_DEGREE (3932160*32)
  15. #define PHASE_300_DEGREE (4915200*32)
  16. #define PHASE_360_DEGREE (5898240*32)
  17. #else
  18. #define PHASE_60_DEGREE (60.0f)
  19. #define PHASE_120_DEGREE (120.0f)
  20. #define PHASE_180_DEGREE (180.0f)
  21. #define PHASE_240_DEGREE (240.0f)
  22. #define PHASE_300_DEGREE (300.0f)
  23. #define PHASE_360_DEGREE (360.0f)
  24. #endif
  25. #define STATE_0 (uint8_t)0
  26. #define STATE_1 (uint8_t)1
  27. #define STATE_2 (uint8_t)2
  28. #define STATE_3 (uint8_t)3
  29. #define STATE_4 (uint8_t)4
  30. #define STATE_5 (uint8_t)5
  31. #define STATE_6 (uint8_t)6
  32. #define STATE_7 (uint8_t)7
  33. #define THETA_NONE (float)0xFFFF
  34. #define SAMPLE_MAX_COUNT 16
  35. typedef struct {
  36. u32 ticks[SAMPLE_MAX_COUNT];
  37. float angles[SAMPLE_MAX_COUNT];
  38. u32 ticks_sum;
  39. float angles_sum;
  40. u32 index;
  41. bool full;
  42. }hall_sample_t;
  43. typedef struct {
  44. float estimate_el_angle; //60度区间内的估计电角度
  45. float estimate_delta_angle;
  46. float measured_el_angle; //hall测量到的电角度
  47. float next_delta_angle;
  48. float delta_angle_ts; //每个控制周期角度的增加量,通过上次的速度计算得到
  49. float angle_comp_ts; //每个控制周期角度增加的补偿量,主要用在加减速,hall角度和60度有偏差的情况
  50. int comp_count;
  51. float immediately_el_speed; //当前的即时速度,主要用来判断电机转动是否达到稳定
  52. float el_speed; //当前的平均效果的电角速度, 单位:rad/s
  53. float rpm; //当前的电速度, 单位:RPM
  54. bool trns_detect; //速度变化超过阈值
  55. u8 hall_stat;
  56. u32 hall_ticks;
  57. s8 direction;
  58. float phase_offset;
  59. hall_sample_t samples;
  60. u32 sensor_error;
  61. float manual_angle;
  62. s32 angle_table[8];
  63. }hall_t;
  64. void hall_sensor_init(void);
  65. void hall_sensor_clear(void);
  66. float hall_sensor_get_theta(void); //return degree
  67. float hall_sensor_get_speed(void); //return rpm;
  68. int hall_offset_increase(int inc);
  69. s32 *hall_get_table(void);
  70. bool hall_detect_angle_finish(void);
  71. void hall_detect_angle(s16 angle);
  72. bool hall_detect_offset_finish(void);
  73. void hall_detect_offset(s16 angle);
  74. #endif /* _HALL_SENSOR_H__ */