hall_sensor.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. /*
  16. * 测量HALL_PLACE_OFFSET通用方式就是ST推荐的通过外力带动电机,
  17. * 测量电机U相反电动势和hall 1的上升沿之间的差值
  18. * 这里使用的是先通过hall_sensor_calibrate测量hall1,2,3,4,5,6
  19. * 对应的角度(偏差比较大),然后启动电机,让HALL_PLACE_OFFSET
  20. * 从0开始增加,每增加1度观察电机电流(看直流电源),
  21. * 找到一个电机平稳转动并且电流最小的角度作为HALL_PLACE_OFFSET
  22. */
  23. #define HALL_PLACE_OFFSET 210
  24. /*
  25. 100
  26. 101
  27. 001
  28. 011
  29. 010
  30. 110
  31. 4,5,1,3,2,6,4
  32. */
  33. //static u16 _hall_table[] = {0xFFFF, 292/*1*/, 54/*2*/, 1/*3*/, 180/*4*/, 229/*5*/, 115/*6*/, 0xFFFF};
  34. static u16 _hall_table[] = {0xFFFF, 121/*1*/, 240/*2*/, 190/*3*/, 13/*4*/, 58/*5*/, 306/*6*/, 0xFFFF};
  35. static hall_t _hall;
  36. static hall_sample_t h_samples;
  37. measure_time_t g_meas_hall = {.exec_max_time = 6,};
  38. #define read_hall(h,t) {h = get_hall_stat(HALL_READ_TIMES); t = _hall_table[h];}
  39. #define us_2_s(tick) ((float)tick / 1000000.0f)
  40. static float __inline _delta_seconds(u32 prev) {
  41. return (float)timer_count32_delta_us(prev, NULL)/1000000.0f;
  42. }
  43. static void __inline _hall_put_sample(u32 ticks) {
  44. hall_sample_t *s = &h_samples;
  45. s->index += 1;
  46. if (s->index >= SAMPLE_MAX_COUNT) {
  47. s->full = true;
  48. s->index = 0;
  49. }
  50. s->ticks[s->index] = ticks;
  51. }
  52. static float __inline _hall_avg_speed(void){
  53. hall_sample_t *s = &h_samples;
  54. float t_angle = (float)(PHASE_60_DEGREE * SAMPLE_MAX_COUNT);
  55. u32 t_ticks = 0;
  56. for (int i = 0; i < SAMPLE_MAX_COUNT; i++) {
  57. t_ticks += s->ticks[i];
  58. }
  59. if (t_ticks == 0.0f) {
  60. return 0.0f;
  61. }
  62. return (t_angle / us_2_s(t_ticks));
  63. }
  64. void hall_sensor_init(void) {
  65. mc_hall_init();
  66. memset(&_hall, 0, sizeof(_hall));
  67. read_hall(_hall.state, _hall.theta);
  68. _hall.phase_offset = HALL_PLACE_OFFSET;//mc_config_get()->hall_offset;
  69. co_task_create(_hall_detect_task, NULL, 512);
  70. }
  71. void hall_sensor_clear(void) {
  72. memset(&_hall, 0, sizeof(_hall));
  73. read_hall(_hall.state, _hall.theta);
  74. _hall.phase_offset = HALL_PLACE_OFFSET;//mc_config_get()->hall_offset;
  75. }
  76. static void _hall_detect_task(void *args) {
  77. while(1) {
  78. if (_hall.working) {
  79. u32 ticks_now = timer_count32_get();
  80. u32 delta_us = timer_count32_getus(ticks_now, _hall.ticks);
  81. if (delta_us >= (1200*1000)) {
  82. hall_sensor_clear();
  83. }
  84. }
  85. co_task_delay(100);
  86. }
  87. }
  88. u16 *hall_phase_angle(void) {
  89. return _hall_table;
  90. }
  91. float hall_sensor_get_theta(void){
  92. if (_hall.is_override_theta) {
  93. return _hall.override_theta;
  94. }
  95. if (!_hall.working) {
  96. read_hall(_hall.state, _hall.theta);
  97. return _hall.theta;
  98. }
  99. _hall.est_theta = _delta_seconds(_hall.ticks) * _hall.angle_speed * _hall.direction + _hall.theta;
  100. float est_delta = _hall.est_theta - _hall.theta;
  101. if (est_delta > PHASE_60_DEGREE) {
  102. _hall.est_theta = _hall.theta + PHASE_60_DEGREE;
  103. if (_hall.est_theta >= PHASE_360_DEGREE){
  104. _hall.est_theta -= PHASE_360_DEGREE;
  105. }
  106. }else if (est_delta < -PHASE_60_DEGREE){
  107. _hall.est_theta = _hall.theta - PHASE_60_DEGREE;
  108. if (_hall.est_theta < 0/*= -PHASE_360_DEGREE*/) {
  109. _hall.est_theta += PHASE_360_DEGREE;
  110. }
  111. }
  112. return _hall.est_theta;
  113. }
  114. void hall_sensor_set_theta(bool override, float theta){
  115. _hall.is_override_theta = override;
  116. _hall.override_theta = theta;
  117. }
  118. float hall_sensor_get_speed(void) {
  119. return _hall.rpm;
  120. }
  121. float hall_sensor_avg_speed(void) {
  122. return _hall.angle_speed_avg / 360 * 60.0f;
  123. }
  124. int hall_offset_increase(int inc) {
  125. if (_hall.phase_offset + inc >= 360) {
  126. _hall.phase_offset = _hall.phase_offset + inc - 360;
  127. }else {
  128. _hall.phase_offset += inc;
  129. }
  130. return _hall.phase_offset;
  131. }
  132. int hall_sensor_calibrate(float voltage, u16 *hall_table){
  133. if (hall_table == NULL) {
  134. hall_table = _hall_table;
  135. }
  136. foc_set_controller_mode(FOC_MODE_OPEN_LOOP);
  137. hall_sensor_set_theta(true, 0.0f);
  138. foc_set_dq_command(0.0f, 0.0f);
  139. foc_pwm_start(true);
  140. for (int i = 0;i < 1000;i++) {
  141. foc_set_dq_command((float)i * voltage / 1000.0f, 0.0f);
  142. delay_ms(1);
  143. }
  144. float sin_hall[8];
  145. float cos_hall[8];
  146. int hall_iterations[8];
  147. memset(sin_hall, 0, sizeof(sin_hall));
  148. memset(cos_hall, 0, sizeof(cos_hall));
  149. memset(hall_iterations, 0, sizeof(hall_iterations));
  150. delay_ms(2 * 1000);
  151. // Forwards
  152. for (int i = 0;i < 5;i++) {
  153. for (int j = 0;j < 360;j++) {
  154. hall_sensor_set_theta(true, j);
  155. delay_ms(5);
  156. int hall = get_hall_stat(7);
  157. float s, c;
  158. normal_sincosf(degree_2_pi(j), &s, &c);
  159. sin_hall[hall] += s;
  160. cos_hall[hall] += c;
  161. hall_iterations[hall]++;
  162. }
  163. }
  164. delay_ms(2 * 1000);
  165. // Reverse
  166. for (int i = 0;i < 5;i++) {
  167. for (int j = 360;j >= 0;j--) {
  168. hall_sensor_set_theta(true, j);
  169. delay_ms(5);
  170. int hall = get_hall_stat(7);
  171. float s, c;
  172. normal_sincosf(degree_2_pi(j), &s, &c);
  173. sin_hall[hall] += s;
  174. cos_hall[hall] += c;
  175. hall_iterations[hall]++;
  176. }
  177. }
  178. foc_pwm_start(false);
  179. hall_sensor_set_theta(false, 0.0f);
  180. foc_set_dq_command(0.0f, 0.0f);
  181. int fails = 0;
  182. for(int i = 0;i < 8;i++) {
  183. if (hall_iterations[i] > 30) {
  184. float ang = pi_2_degree(atan2f(sin_hall[i], cos_hall[i]));
  185. fast_norm_angle(&ang);
  186. hall_table[i] = (u16)ang;
  187. } else {
  188. hall_table[i] = 0xFFFF;
  189. fails++;
  190. }
  191. }
  192. return fails == 2;
  193. }
  194. static s32 _hall_position(u8 state_now, u8 state_prev) {
  195. s32 theta_now = 0xFFFFFFFF;
  196. switch (state_now) {
  197. case STATE_1:
  198. if (state_prev == STATE_5) {
  199. _hall.direction = POSITIVE;
  200. theta_now = _hall.phase_offset + PHASE_60_DEGREE;
  201. }else if (state_prev == STATE_3) {
  202. _hall.direction = NEGATIVE;
  203. theta_now = _hall.phase_offset + PHASE_120_DEGREE;
  204. }
  205. break;
  206. case STATE_2:
  207. if (state_prev == STATE_3) {
  208. _hall.direction = POSITIVE;
  209. theta_now = _hall.phase_offset + PHASE_180_DEGREE;
  210. }else if (state_prev == STATE_6) {
  211. _hall.direction = NEGATIVE;
  212. theta_now = _hall.phase_offset + PHASE_240_DEGREE;
  213. }
  214. break;
  215. case STATE_3:
  216. if (state_prev == STATE_1) {
  217. _hall.direction = POSITIVE;
  218. theta_now = _hall.phase_offset + PHASE_120_DEGREE;
  219. }else if (state_prev == STATE_2) {
  220. _hall.direction = NEGATIVE;
  221. theta_now = _hall.phase_offset + PHASE_180_DEGREE;
  222. }
  223. break;
  224. case STATE_4:
  225. if (state_prev == STATE_6) {
  226. _hall.direction = POSITIVE;
  227. theta_now = _hall.phase_offset + PHASE_300_DEGREE;
  228. }else if (state_prev == STATE_5) {
  229. _hall.direction = NEGATIVE;
  230. theta_now = _hall.phase_offset;
  231. }
  232. break;
  233. case STATE_5:
  234. if (state_prev == STATE_4) {
  235. _hall.direction = POSITIVE;
  236. theta_now = _hall.phase_offset;
  237. }else if (state_prev == STATE_1) {
  238. _hall.direction = NEGATIVE;
  239. theta_now = _hall.phase_offset + PHASE_60_DEGREE;
  240. }
  241. break;
  242. case STATE_6:
  243. if (state_prev == STATE_2) {
  244. _hall.direction = POSITIVE;
  245. theta_now = _hall.phase_offset + PHASE_240_DEGREE;
  246. }else if (state_prev == STATE_4) {
  247. _hall.direction = NEGATIVE;
  248. theta_now = _hall.phase_offset + PHASE_300_DEGREE;
  249. }
  250. break;
  251. default:
  252. return 0xFFFFFFFF;
  253. }
  254. if (theta_now >= 360) {
  255. theta_now -= 360;
  256. }
  257. return theta_now;
  258. }
  259. void hall_sensor_handler(void) {
  260. time_measure_start(&g_meas_hall);
  261. u8 state_now = get_hall_stat(HALL_READ_TIMES);
  262. u8 state_prev = _hall.state;
  263. u32 ticks_now = timer_count32_get();
  264. _hall.state = get_hall_stat(HALL_READ_TIMES);
  265. if (!_hall.working) {
  266. if(_hall_table[state_now] != 0xFFFF) {
  267. _hall.working = true;
  268. _hall.state = state_now;
  269. _hall.theta = _hall_table[state_now];
  270. _hall.ticks = ticks_now;
  271. }
  272. return;
  273. }
  274. s32 theta_now = _hall_position(_hall.state, state_prev);
  275. if (theta_now == 0xFFFFFFFF) {
  276. _hall.state = state_prev;
  277. return;
  278. }
  279. u32 delta_us = timer_count32_getus(ticks_now, _hall.ticks);
  280. if (delta_us == 0) {
  281. return;
  282. }
  283. os_disable_irq();
  284. float delta_time = us_2_s(delta_us);
  285. _hall_put_sample(delta_us);
  286. _hall.angle_speed = (float)PHASE_60_DEGREE / delta_time;
  287. if (!h_samples.full) {
  288. _hall.angle_speed_avg = _hall.angle_speed;
  289. }else {
  290. _hall.angle_speed_avg = _hall_avg_speed();
  291. }
  292. _hall.theta = theta_now;
  293. _hall.state = state_now;
  294. _hall.ticks = ticks_now;
  295. os_enable_irq();
  296. _hall.rpm = _hall.angle_speed / 360.0f * 60.0f;
  297. time_measure_end(&g_meas_hall);
  298. }