hall_sensor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 (315)//(345) //315
  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. #define rand_angle(a) {if (a >= PHASE_360_DEGREE) a-=PHASE_360_DEGREE;else if (a < 0) a +=PHASE_360_DEGREE;};
  32. static float hall_speed[8];
  33. static float hall_angle[8];
  34. static void __inline _hall_put_sample(u32 ticks, u8 hall) {
  35. hall_sample_t *s = &_sensor_hander.samples[hall];
  36. s->ticks_sum -= s->ticks[s->index];
  37. s->ticks[s->index] = ticks;
  38. s->ticks_sum += s->ticks[s->index];
  39. s->index += 1;
  40. if (s->index >= SAMPLE_MAX_COUNT) {
  41. s->full = true;
  42. s->index = 0;
  43. }
  44. }
  45. static u32 __inline _hall_angle_us_speed(u8 hall){
  46. hall_sample_t *s = &_sensor_hander.samples[hall];
  47. if (s->ticks_sum == 0) {
  48. return 0;
  49. }
  50. if (!s->full) {
  51. return (s->ticks[s->index-1]);
  52. }else {
  53. return s->ticks_sum/SAMPLE_MAX_COUNT;
  54. }
  55. }
  56. static float __inline _hall_angle_speed(void){
  57. u32 sum = 0 ;
  58. u32 num = 0;
  59. for (int hall = 1; hall < 7; hall++) {
  60. hall_sample_t *s = &_sensor_hander.samples[hall];
  61. if (s->ticks_sum == 0) {
  62. continue;
  63. }
  64. if (!s->full) {
  65. sum +=(s->ticks[s->index-1]);
  66. num ++;
  67. }else {
  68. sum += s->ticks_sum;
  69. num += SAMPLE_MAX_COUNT;
  70. }
  71. }
  72. return (float)PHASE_60_DEGREE * (float)num / us_2_s(sum);
  73. }
  74. static bool __inline _hall_data_empty(u8 hall) {
  75. hall_sample_t *s = &_sensor_hander.samples[hall];
  76. if ((!s->full) && (s->index == 0)){
  77. return true;
  78. }
  79. return false;
  80. }
  81. static void hall_sensor_default(void) {
  82. memset(&_sensor_hander, 0, sizeof(_sensor_hander));
  83. _sensor_hander.phase_offset = HALL_PLACE_OFFSET;//mc_config_get()->hall_offset;
  84. _hall_init_el_angle();
  85. }
  86. void hall_sensor_init(void) {
  87. mc_hall_init();
  88. hall_sensor_default();
  89. co_task_create(_hall_detect_task, NULL, 512);
  90. }
  91. void hall_sensor_clear(void) {
  92. hall_sensor_default();
  93. }
  94. void hall_debug_log(void) {
  95. for (int i = 0; i < 8; i++) {
  96. if (i != 0 && i != 7) {
  97. sys_debug("hall speed: %d, %f - %f, %d\n", i, hall_speed[i], hall_angle[i], _sensor_hander.sensor_error);
  98. }
  99. }
  100. sys_debug("angle dir %d\n", _sensor_hander.direction);
  101. }
  102. static void _hall_detect_task(void *args) {
  103. while(1) {
  104. if (_sensor_hander.el_speed != 0) {
  105. u32 ticks_now = timer_count32_get();
  106. u32 delta_us = timer_count32_getus(ticks_now, _sensor_hander.hall_ticks);
  107. if (delta_us >= (1200*1000)) {
  108. hall_sensor_clear();
  109. }
  110. }
  111. co_task_delay(100);
  112. }
  113. }
  114. float hall_sensor_get_theta(void){
  115. if (_sensor_hander.is_override_angle) {
  116. return _sensor_hander.override_el_angle;
  117. }
  118. u32 us_now = timer_count32_delta_us(_sensor_hander.estimate_time_ticks, NULL);
  119. float ration = (float)us_now / (float)_sensor_hander.speed_us_for_estimate;
  120. float angle_step = (float)PHASE_60_DEGREE * ration;
  121. _sensor_hander.estimate_delta_angle = angle_step;
  122. if (angle_step >= PHASE_60_DEGREE) {
  123. angle_step = PHASE_60_DEGREE;
  124. }
  125. if (_sensor_hander.direction == POSITIVE) {
  126. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle + angle_step;
  127. }else {
  128. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle - angle_step;
  129. }
  130. rand_angle(_sensor_hander.estimate_el_angle);
  131. //log_chan_value(1, (int)_sensor_hander.estimate_el_angle);
  132. return _sensor_hander.estimate_el_angle;
  133. }
  134. void hall_sensor_set_theta(bool override, float theta){
  135. _sensor_hander.is_override_angle = override;
  136. _sensor_hander.override_el_angle = theta;
  137. }
  138. float hall_sensor_get_speed(void) {
  139. _sensor_hander.rpm = _sensor_hander.el_speed / 360.0f * 60.0f;
  140. return _sensor_hander.rpm;
  141. }
  142. float hall_sensor_avg_speed(void) {
  143. return _sensor_hander.el_speed / 360 * 60.0f;
  144. }
  145. int hall_offset_increase(int inc) {
  146. if (_sensor_hander.phase_offset + inc >= 360) {
  147. _sensor_hander.phase_offset = _sensor_hander.phase_offset + inc - 360;
  148. }else {
  149. _sensor_hander.phase_offset += inc;
  150. }
  151. return _sensor_hander.phase_offset;
  152. }
  153. int hall_sensor_calibrate(float voltage){
  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 < 100;i++) {
  159. foc_set_dq_command((float)i * voltage / 100.0f, 0.0f);
  160. co_task_delay(1);
  161. wdog_reload();
  162. }
  163. float sin_hall[8];
  164. float cos_hall[8];
  165. int hall_iterations[8];
  166. memset(sin_hall, 0, sizeof(sin_hall));
  167. memset(cos_hall, 0, sizeof(cos_hall));
  168. memset(_sensor_hander.angle_table, 0, sizeof(_sensor_hander.angle_table));
  169. memset(hall_iterations, 0, sizeof(hall_iterations));
  170. co_task_delay(2 * 1000);
  171. // Forwards
  172. #if 1
  173. for (int i = 0;i < 5;i++) {
  174. for (int j = 0;j < 360;j++) {
  175. hall_sensor_set_theta(true, j);
  176. co_task_delay(5);
  177. wdog_reload();
  178. int hall = get_hall_stat(7);
  179. float s, c;
  180. normal_sincosf(degree_2_pi(j), &s, &c);
  181. sin_hall[hall] += s;
  182. cos_hall[hall] += c;
  183. hall_iterations[hall]++;
  184. }
  185. }
  186. #endif
  187. #if 0
  188. //hall_sensor_set_theta(true, 360);
  189. //co_task_delay(2 * 1000);
  190. sys_debug("Revers\n");
  191. // Reverse
  192. for (int i = 0;i < 5;i++) {
  193. for (int j = 360;j >= 0;j--) {
  194. hall_sensor_set_theta(true, j);
  195. co_task_delay(5);
  196. wdog_reload();
  197. int hall = get_hall_stat(7);
  198. float s, c;
  199. normal_sincosf(degree_2_pi(j), &s, &c);
  200. sin_hall[hall] += s;
  201. cos_hall[hall] += c;
  202. hall_iterations[hall]++;
  203. }
  204. }
  205. #endif
  206. foc_pwm_start(false);
  207. hall_sensor_set_theta(false, 0.0f);
  208. foc_set_dq_command(0.0f, 0.0f);
  209. int fails = 0;
  210. for(int i = 0;i < 8;i++) {
  211. if (hall_iterations[i] > 30) {
  212. float ang = pi_2_degree(atan2f(sin_hall[i], cos_hall[i]));
  213. fast_norm_angle(&ang);
  214. _sensor_hander.angle_table[i] = (u16)ang;
  215. sys_debug("%d: %d\n", i, _sensor_hander.angle_table[i]);
  216. } else {
  217. fails++;
  218. _sensor_hander.angle_table[i] = 0xFFFF;
  219. }
  220. }
  221. return fails == 2;
  222. }
  223. static void _hall_init_el_angle(void) {
  224. _sensor_hander.hall_stat = get_hall_stat(HALL_READ_TIMES);
  225. switch ( _sensor_hander.hall_stat )
  226. {
  227. case STATE_5:
  228. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE/2;
  229. break;
  230. case STATE_1:
  231. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE + PHASE_60_DEGREE / 2;
  232. break;
  233. case STATE_3:
  234. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_120_DEGREE + PHASE_60_DEGREE / 2;
  235. break;
  236. case STATE_2:
  237. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_180_DEGREE + PHASE_60_DEGREE / 2;
  238. break;
  239. case STATE_6:
  240. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_240_DEGREE + PHASE_60_DEGREE / 2;
  241. break;
  242. case STATE_4:
  243. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_300_DEGREE + PHASE_60_DEGREE / 2;
  244. break;
  245. default:
  246. /* Bad hall sensor configutarion so update the speed reliability */
  247. _sensor_hander.sensor_error ++;
  248. break;
  249. }
  250. /* Initialize the measured angle */
  251. rand_angle(_sensor_hander.measured_el_angle);
  252. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
  253. _sensor_hander.hall_ticks = timer_count32_get();
  254. }
  255. /* 4,5,1,3,2,6,4 */
  256. static s32 _hall_position(u8 state_now, u8 state_prev) {
  257. s32 theta_now = 0xFFFFFFFF;
  258. switch (state_now) {
  259. case STATE_1:
  260. if (state_prev == STATE_5) {
  261. _sensor_hander.direction = POSITIVE;
  262. theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
  263. }else if (state_prev == STATE_3) {
  264. _sensor_hander.direction = NEGATIVE;
  265. theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
  266. }
  267. break;
  268. case STATE_2:
  269. if (state_prev == STATE_3) {
  270. _sensor_hander.direction = POSITIVE;
  271. theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
  272. }else if (state_prev == STATE_6) {
  273. _sensor_hander.direction = NEGATIVE;
  274. theta_now = _sensor_hander.phase_offset + PHASE_240_DEGREE;
  275. }
  276. break;
  277. case STATE_3:
  278. if (state_prev == STATE_1) {
  279. _sensor_hander.direction = POSITIVE;
  280. theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
  281. }else if (state_prev == STATE_2) {
  282. _sensor_hander.direction = NEGATIVE;
  283. theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
  284. }
  285. break;
  286. case STATE_4:
  287. if (state_prev == STATE_6) {
  288. _sensor_hander.direction = POSITIVE;
  289. theta_now = _sensor_hander.phase_offset + PHASE_300_DEGREE;
  290. }else if (state_prev == STATE_5) {
  291. _sensor_hander.direction = NEGATIVE;
  292. theta_now = _sensor_hander.phase_offset;
  293. }
  294. break;
  295. case STATE_5:
  296. if (state_prev == STATE_4) {
  297. _sensor_hander.direction = POSITIVE;
  298. theta_now = _sensor_hander.phase_offset;
  299. }else if (state_prev == STATE_1) {
  300. _sensor_hander.direction = NEGATIVE;
  301. theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
  302. }
  303. break;
  304. case STATE_6:
  305. if (state_prev == STATE_2) {
  306. _sensor_hander.direction = POSITIVE;
  307. theta_now = _sensor_hander.phase_offset + PHASE_240_DEGREE;
  308. }else if (state_prev == STATE_4) {
  309. _sensor_hander.direction = NEGATIVE;
  310. theta_now = _sensor_hander.phase_offset + PHASE_300_DEGREE;
  311. }
  312. break;
  313. default:
  314. _sensor_hander.sensor_error ++;
  315. return 0xFFFFFFFF;
  316. }
  317. rand_angle(theta_now);
  318. return theta_now;
  319. }
  320. void hall_sensor_handler(void) {
  321. if (_sensor_hander.is_override_angle) {
  322. sys_debug("irq: %d:%d\n", (int)get_hall_stat(HALL_READ_TIMES), (int)_sensor_hander.override_el_angle);
  323. return;
  324. }
  325. time_measure_start(&g_meas_hall);
  326. u8 hall_stat_now = get_hall_stat(HALL_READ_TIMES);
  327. u8 hall_stat_prev = _sensor_hander.hall_stat;
  328. u32 hall_ticks_now = timer_count32_get();
  329. s32 theta_now = _hall_position(hall_stat_now, hall_stat_prev);
  330. if (theta_now == 0xFFFFFFFF) {
  331. return;
  332. }
  333. u32 delta_us = timer_count32_getus(hall_ticks_now, _sensor_hander.hall_ticks);
  334. if (delta_us == 0) {
  335. return;
  336. }
  337. float delta_time = us_2_s(delta_us);
  338. _hall_put_sample(delta_us, hall_stat_prev);
  339. u32 us_speed_prev = _sensor_hander.speed_us_for_estimate;
  340. u32 us_speed_now = 0;
  341. if (_hall_data_empty(hall_stat_now)) {
  342. us_speed_now = delta_us;
  343. }else {
  344. us_speed_now = _hall_angle_us_speed(hall_stat_now);
  345. }
  346. if (us_speed_now != 0) {
  347. hall_speed[hall_stat_now] = (float)us_speed_prev / (float)us_speed_now;
  348. }
  349. hall_angle[hall_stat_now] = (float)_sensor_hander.estimate_delta_angle/(float)PHASE_60_DEGREE;
  350. os_disable_irq();
  351. _sensor_hander.estimate_delta_angle = 0;
  352. _sensor_hander.measured_el_angle = theta_now;
  353. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
  354. _sensor_hander.estimate_time_ticks = hall_ticks_now;
  355. _sensor_hander.speed_us_for_estimate = us_speed_now;
  356. os_enable_irq();
  357. _sensor_hander.el_speed = _hall_angle_speed();
  358. _sensor_hander.hall_stat = hall_stat_now;
  359. _sensor_hander.hall_ticks = hall_ticks_now;
  360. time_measure_end(&g_meas_hall);
  361. }