hall_sensor.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include <string.h>
  2. #include "bsp/bsp.h"
  3. #include "bsp/mc_hall_gpio.h"
  4. #include "os/co_task.h"
  5. #include "os/timer.h"
  6. #include "libs/utils.h"
  7. #include "libs/logger.h"
  8. #include "math/fast_math.h"
  9. #include "hall_sensor.h"
  10. #include "foc/foc_api.h"
  11. #include "app/nv_storage.h"
  12. #include "bsp/timer_count32.h"
  13. #define HALL_READ_TIMES 3
  14. static void _hall_detect_task(void *args);
  15. static void _hall_init_el_angle(void);
  16. #define PWM_T (0.000033f)
  17. #define HALL_PLACE_OFFSET (199)
  18. /*
  19. 100
  20. 101
  21. 001
  22. 011
  23. 010
  24. 110
  25. 4,5,1,3,2,6,4
  26. */
  27. static hall_sensor_t _sensor_hander;
  28. measure_time_t g_meas_hall = {.exec_max_time = 6,};
  29. #define read_hall(h,t) {h = get_hall_stat(HALL_READ_TIMES); t = _hall_table[h];}
  30. #define us_2_s(tick) ((float)tick / 1000000.0f)
  31. static void __inline _hall_put_sample(u32 ticks) {
  32. hall_sample_t *s = &_sensor_hander.samples;
  33. s->ticks_sum -= s->ticks[s->index];
  34. s->ticks[s->index] = ticks;
  35. s->ticks_sum += s->ticks[s->index];
  36. s->index += 1;
  37. if (s->index >= SAMPLE_MAX_COUNT) {
  38. s->full = true;
  39. s->index = 0;
  40. }
  41. }
  42. static float __inline _hall_avg_speed(void){
  43. hall_sample_t *s = &_sensor_hander.samples;
  44. if (s->ticks_sum == 0.0f) {
  45. return 0.0f;
  46. }
  47. return (((float)PHASE_60_DEGREE * (float)SAMPLE_MAX_COUNT) / (us_2_s(s->ticks_sum)));
  48. }
  49. static void hall_sensor_default(void) {
  50. memset(&_sensor_hander, 0, sizeof(_sensor_hander));
  51. _sensor_hander.phase_offset = HALL_PLACE_OFFSET;//mc_config_get()->hall_offset;
  52. _hall_init_el_angle();
  53. }
  54. void hall_sensor_init(void) {
  55. mc_hall_init();
  56. hall_sensor_default();
  57. co_task_create(_hall_detect_task, NULL, 512);
  58. }
  59. void hall_sensor_clear(void) {
  60. hall_sensor_default();
  61. }
  62. static void _hall_detect_task(void *args) {
  63. while(1) {
  64. if (_sensor_hander.el_speed != 0) {
  65. u32 ticks_now = timer_count32_get();
  66. u32 delta_us = timer_count32_getus(ticks_now, _sensor_hander.hall_ticks);
  67. if (delta_us >= (1200*1000)) {
  68. hall_sensor_clear();
  69. }
  70. }
  71. co_task_delay(100);
  72. }
  73. }
  74. float hall_sensor_get_theta(void){
  75. if (_sensor_hander.is_override_angle) {
  76. return _sensor_hander.override_el_angle;
  77. }
  78. float angle_step = _sensor_hander.el_speed * PWM_T;
  79. float compensated_step = angle_step + _sensor_hander.el_speed_compensate * PWM_T * 0.05f;
  80. _sensor_hander.estimate_delta_angle += compensated_step;
  81. #if 1
  82. if (_sensor_hander.direction == POSITIVE) {
  83. _sensor_hander.estimate_el_angle += compensated_step;
  84. }else {
  85. _sensor_hander.estimate_el_angle -= compensated_step;
  86. }
  87. #else
  88. if (_sensor_hander.estimate_delta_angle >= PHASE_60_DEGREE) {
  89. if (_sensor_hander.direction == POSITIVE) {
  90. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle + PHASE_60_DEGREE;
  91. }else {
  92. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle - PHASE_60_DEGREE;
  93. }
  94. }
  95. #endif
  96. return _sensor_hander.estimate_el_angle;
  97. }
  98. void hall_sensor_set_theta(bool override, float theta){
  99. _sensor_hander.is_override_angle = override;
  100. _sensor_hander.override_el_angle = theta;
  101. }
  102. float hall_sensor_get_speed(void) {
  103. _sensor_hander.rpm = _sensor_hander.el_speed / 360.0f * 60.0f;
  104. return _sensor_hander.rpm;
  105. }
  106. float hall_sensor_avg_speed(void) {
  107. return _sensor_hander.el_speed_avg / 360 * 60.0f;
  108. }
  109. int hall_offset_increase(int inc) {
  110. if (_sensor_hander.phase_offset + inc >= 360) {
  111. _sensor_hander.phase_offset = _sensor_hander.phase_offset + inc - 360;
  112. }else {
  113. _sensor_hander.phase_offset += inc;
  114. }
  115. return _sensor_hander.phase_offset;
  116. }
  117. static void _hall_init_el_angle(void) {
  118. _sensor_hander.hall_stat = get_hall_stat(HALL_READ_TIMES);
  119. switch ( _sensor_hander.hall_stat )
  120. {
  121. case STATE_5:
  122. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE/2;
  123. break;
  124. case STATE_1:
  125. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE + PHASE_60_DEGREE / 2;
  126. break;
  127. case STATE_3:
  128. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_120_DEGREE + PHASE_60_DEGREE / 2;
  129. break;
  130. case STATE_2:
  131. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset - PHASE_120_DEGREE - PHASE_60_DEGREE / 2;
  132. break;
  133. case STATE_6:
  134. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset - PHASE_60_DEGREE - PHASE_60_DEGREE / 2;
  135. break;
  136. case STATE_4:
  137. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset - PHASE_60_DEGREE / 2;
  138. break;
  139. default:
  140. /* Bad hall sensor configutarion so update the speed reliability */
  141. _sensor_hander.sensor_error = true;
  142. break;
  143. }
  144. /* Initialize the measured angle */
  145. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
  146. _sensor_hander.hall_ticks = timer_count32_get();
  147. }
  148. static s32 _hall_position(u8 state_now, u8 state_prev) {
  149. s32 theta_now = 0xFFFFFFFF;
  150. switch (state_now) {
  151. case STATE_1:
  152. if (state_prev == STATE_5) {
  153. _sensor_hander.direction = POSITIVE;
  154. theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
  155. }else if (state_prev == STATE_3) {
  156. _sensor_hander.direction = NEGATIVE;
  157. theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
  158. }
  159. break;
  160. case STATE_2:
  161. if (state_prev == STATE_3) {
  162. _sensor_hander.direction = POSITIVE;
  163. theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
  164. }else if (state_prev == STATE_6) {
  165. _sensor_hander.direction = NEGATIVE;
  166. theta_now = _sensor_hander.phase_offset - PHASE_120_DEGREE;
  167. }
  168. break;
  169. case STATE_3:
  170. if (state_prev == STATE_1) {
  171. _sensor_hander.direction = POSITIVE;
  172. theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
  173. }else if (state_prev == STATE_2) {
  174. _sensor_hander.direction = NEGATIVE;
  175. theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
  176. }
  177. break;
  178. case STATE_4:
  179. if (state_prev == STATE_6) {
  180. _sensor_hander.direction = POSITIVE;
  181. theta_now = _sensor_hander.phase_offset - PHASE_60_DEGREE;
  182. }else if (state_prev == STATE_5) {
  183. _sensor_hander.direction = NEGATIVE;
  184. theta_now = _sensor_hander.phase_offset;
  185. }
  186. break;
  187. case STATE_5:
  188. if (state_prev == STATE_4) {
  189. _sensor_hander.direction = POSITIVE;
  190. theta_now = _sensor_hander.phase_offset;
  191. }else if (state_prev == STATE_1) {
  192. _sensor_hander.direction = NEGATIVE;
  193. theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
  194. }
  195. break;
  196. case STATE_6:
  197. if (state_prev == STATE_2) {
  198. _sensor_hander.direction = POSITIVE;
  199. theta_now = _sensor_hander.phase_offset - PHASE_120_DEGREE;
  200. }else if (state_prev == STATE_4) {
  201. _sensor_hander.direction = NEGATIVE;
  202. theta_now = _sensor_hander.phase_offset - PHASE_60_DEGREE;
  203. }
  204. break;
  205. default:
  206. return 0xFFFFFFFF;
  207. }
  208. return theta_now;
  209. }
  210. void hall_sensor_handler(void) {
  211. time_measure_start(&g_meas_hall);
  212. u8 hall_stat_now = get_hall_stat(HALL_READ_TIMES);
  213. u8 hall_stat_prev = _sensor_hander.hall_stat;
  214. u32 hall_ticks_now = timer_count32_get();
  215. s32 theta_now = _hall_position(hall_stat_now, hall_stat_prev);
  216. if (theta_now == 0xFFFFFFFF) {
  217. return;
  218. }
  219. u32 delta_us = timer_count32_getus(hall_ticks_now, _sensor_hander.hall_ticks);
  220. if (delta_us == 0) {
  221. return;
  222. }
  223. float delta_time = us_2_s(delta_us);
  224. _hall_put_sample(delta_us);
  225. if (_sensor_hander.samples.full) {
  226. _sensor_hander.el_speed_avg = _hall_avg_speed();
  227. ///_sensor_hander.estimate_angle_aliment = true;
  228. }else {
  229. _sensor_hander.el_speed_avg = (float)PHASE_60_DEGREE / delta_time;
  230. }
  231. os_disable_irq();
  232. _sensor_hander.measured_el_angle = theta_now;
  233. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
  234. if (_sensor_hander.estimate_angle_aliment) {
  235. float delta_angle = (float)PHASE_60_DEGREE - _sensor_hander.estimate_delta_angle;
  236. _sensor_hander.el_speed_compensate = delta_angle / delta_time;
  237. }else {
  238. _sensor_hander.el_speed_compensate = 0.0f;
  239. }
  240. _sensor_hander.estimate_delta_angle = 0.0f;
  241. _sensor_hander.el_speed = _sensor_hander.el_speed_avg;
  242. os_enable_irq();
  243. _sensor_hander.hall_stat = hall_stat_now;
  244. _sensor_hander.hall_ticks = hall_ticks_now;
  245. time_measure_end(&g_meas_hall);
  246. }