hall_sensor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. return _sensor_hander.estimate_el_angle;
  132. }
  133. void hall_sensor_set_theta(bool override, float theta){
  134. _sensor_hander.is_override_angle = override;
  135. _sensor_hander.override_el_angle = theta;
  136. }
  137. float hall_sensor_get_speed(void) {
  138. _sensor_hander.rpm = _sensor_hander.el_speed / 360.0f * 60.0f;
  139. return _sensor_hander.rpm;
  140. }
  141. float hall_sensor_avg_speed(void) {
  142. return _sensor_hander.el_speed / 360 * 60.0f;
  143. }
  144. int hall_offset_increase(int inc) {
  145. if (_sensor_hander.phase_offset + inc >= 360) {
  146. _sensor_hander.phase_offset = _sensor_hander.phase_offset + inc - 360;
  147. }else {
  148. _sensor_hander.phase_offset += inc;
  149. }
  150. return _sensor_hander.phase_offset;
  151. }
  152. int hall_sensor_calibrate(float voltage){
  153. foc_set_controller_mode(FOC_MODE_OPEN_LOOP);
  154. hall_sensor_set_theta(true, 0.0f);
  155. foc_set_dq_command(0.0f, 0.0f);
  156. foc_pwm_start(true);
  157. for (int i = 0;i < 100;i++) {
  158. foc_set_dq_command((float)i * voltage / 100.0f, 0.0f);
  159. co_task_delay(1);
  160. wdog_reload();
  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. co_task_delay(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. co_task_delay(5);
  174. wdog_reload();
  175. int hall = get_hall_stat(7);
  176. float s, c;
  177. normal_sincosf(degree_2_pi(j), &s, &c);
  178. sin_hall[hall] += s;
  179. cos_hall[hall] += c;
  180. hall_iterations[hall]++;
  181. }
  182. }
  183. //hall_sensor_set_theta(true, 360);
  184. //co_task_delay(2 * 1000);
  185. sys_debug("Revers\n");
  186. // Reverse
  187. for (int i = 0;i < 5;i++) {
  188. for (int j = 360;j >= 0;j--) {
  189. hall_sensor_set_theta(true, j);
  190. co_task_delay(5);
  191. wdog_reload();
  192. int hall = get_hall_stat(7);
  193. float s, c;
  194. normal_sincosf(degree_2_pi(j), &s, &c);
  195. sin_hall[hall] += s;
  196. cos_hall[hall] += c;
  197. hall_iterations[hall]++;
  198. }
  199. }
  200. foc_pwm_start(false);
  201. hall_sensor_set_theta(false, 0.0f);
  202. foc_set_dq_command(0.0f, 0.0f);
  203. int fails = 0;
  204. for(int i = 0;i < 8;i++) {
  205. if (hall_iterations[i] > 30) {
  206. } else {
  207. fails++;
  208. }
  209. }
  210. return fails == 2;
  211. }
  212. static void _hall_init_el_angle(void) {
  213. _sensor_hander.hall_stat = get_hall_stat(HALL_READ_TIMES);
  214. switch ( _sensor_hander.hall_stat )
  215. {
  216. case STATE_5:
  217. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE/2;
  218. break;
  219. case STATE_1:
  220. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE + PHASE_60_DEGREE / 2;
  221. break;
  222. case STATE_3:
  223. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_120_DEGREE + PHASE_60_DEGREE / 2;
  224. break;
  225. case STATE_2:
  226. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_180_DEGREE + PHASE_60_DEGREE / 2;
  227. break;
  228. case STATE_6:
  229. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_240_DEGREE + PHASE_60_DEGREE / 2;
  230. break;
  231. case STATE_4:
  232. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_300_DEGREE + PHASE_60_DEGREE / 2;
  233. break;
  234. default:
  235. /* Bad hall sensor configutarion so update the speed reliability */
  236. _sensor_hander.sensor_error ++;
  237. break;
  238. }
  239. /* Initialize the measured angle */
  240. rand_angle(_sensor_hander.measured_el_angle);
  241. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
  242. _sensor_hander.hall_ticks = timer_count32_get();
  243. }
  244. /* 4,5,1,3,2,6,4 */
  245. static s32 _hall_position(u8 state_now, u8 state_prev) {
  246. s32 theta_now = 0xFFFFFFFF;
  247. switch (state_now) {
  248. case STATE_1:
  249. if (state_prev == STATE_5) {
  250. _sensor_hander.direction = POSITIVE;
  251. theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
  252. }else if (state_prev == STATE_3) {
  253. _sensor_hander.direction = NEGATIVE;
  254. theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
  255. }
  256. break;
  257. case STATE_2:
  258. if (state_prev == STATE_3) {
  259. _sensor_hander.direction = POSITIVE;
  260. theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
  261. }else if (state_prev == STATE_6) {
  262. _sensor_hander.direction = NEGATIVE;
  263. theta_now = _sensor_hander.phase_offset + PHASE_240_DEGREE;
  264. }
  265. break;
  266. case STATE_3:
  267. if (state_prev == STATE_1) {
  268. _sensor_hander.direction = POSITIVE;
  269. theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
  270. }else if (state_prev == STATE_2) {
  271. _sensor_hander.direction = NEGATIVE;
  272. theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
  273. }
  274. break;
  275. case STATE_4:
  276. if (state_prev == STATE_6) {
  277. _sensor_hander.direction = POSITIVE;
  278. theta_now = _sensor_hander.phase_offset + PHASE_300_DEGREE;
  279. }else if (state_prev == STATE_5) {
  280. _sensor_hander.direction = NEGATIVE;
  281. theta_now = _sensor_hander.phase_offset;
  282. }
  283. break;
  284. case STATE_5:
  285. if (state_prev == STATE_4) {
  286. _sensor_hander.direction = POSITIVE;
  287. theta_now = _sensor_hander.phase_offset;
  288. }else if (state_prev == STATE_1) {
  289. _sensor_hander.direction = NEGATIVE;
  290. theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
  291. }
  292. break;
  293. case STATE_6:
  294. if (state_prev == STATE_2) {
  295. _sensor_hander.direction = POSITIVE;
  296. theta_now = _sensor_hander.phase_offset + PHASE_240_DEGREE;
  297. }else if (state_prev == STATE_4) {
  298. _sensor_hander.direction = NEGATIVE;
  299. theta_now = _sensor_hander.phase_offset + PHASE_300_DEGREE;
  300. }
  301. break;
  302. default:
  303. _sensor_hander.sensor_error ++;
  304. return 0xFFFFFFFF;
  305. }
  306. rand_angle(theta_now);
  307. return theta_now;
  308. }
  309. void hall_sensor_handler(void) {
  310. if (_sensor_hander.is_override_angle) {
  311. log_chan_value(1, _sensor_hander.override_el_angle);
  312. log_chan_value(2, (int)get_hall_stat(HALL_READ_TIMES));
  313. return;
  314. }
  315. time_measure_start(&g_meas_hall);
  316. u8 hall_stat_now = get_hall_stat(HALL_READ_TIMES);
  317. u8 hall_stat_prev = _sensor_hander.hall_stat;
  318. u32 hall_ticks_now = timer_count32_get();
  319. s32 theta_now = _hall_position(hall_stat_now, hall_stat_prev);
  320. if (theta_now == 0xFFFFFFFF) {
  321. return;
  322. }
  323. u32 delta_us = timer_count32_getus(hall_ticks_now, _sensor_hander.hall_ticks);
  324. if (delta_us == 0) {
  325. return;
  326. }
  327. float delta_time = us_2_s(delta_us);
  328. _hall_put_sample(delta_us, hall_stat_prev);
  329. u32 us_speed_prev = _sensor_hander.speed_us_for_estimate;
  330. u32 us_speed_now = 0;
  331. if (_hall_data_empty(hall_stat_now)) {
  332. us_speed_now = delta_us;
  333. }else {
  334. us_speed_now = _hall_angle_us_speed(hall_stat_now);
  335. }
  336. if (us_speed_now != 0) {
  337. hall_speed[hall_stat_now] = (float)us_speed_prev / (float)us_speed_now;
  338. }
  339. hall_angle[hall_stat_now] = (float)_sensor_hander.estimate_delta_angle/(float)PHASE_60_DEGREE;
  340. os_disable_irq();
  341. _sensor_hander.estimate_delta_angle = 0;
  342. _sensor_hander.measured_el_angle = theta_now;
  343. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
  344. _sensor_hander.estimate_time_ticks = hall_ticks_now;
  345. _sensor_hander.speed_us_for_estimate = us_speed_now;
  346. os_enable_irq();
  347. _sensor_hander.el_speed = _hall_angle_speed();
  348. _sensor_hander.hall_stat = hall_stat_now;
  349. _sensor_hander.hall_ticks = hall_ticks_now;
  350. time_measure_end(&g_meas_hall);
  351. }