hall_sensor.c 9.1 KB

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