hall_sensor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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(hall_iterations, 0, sizeof(hall_iterations));
  169. co_task_delay(2 * 1000);
  170. // Forwards
  171. for (int i = 0;i < 5;i++) {
  172. for (int j = 0;j < 360;j++) {
  173. hall_sensor_set_theta(true, j);
  174. co_task_delay(50);
  175. wdog_reload();
  176. int hall = get_hall_stat(7);
  177. float s, c;
  178. normal_sincosf(degree_2_pi(j), &s, &c);
  179. sin_hall[hall] += s;
  180. cos_hall[hall] += c;
  181. hall_iterations[hall]++;
  182. }
  183. }
  184. //hall_sensor_set_theta(true, 360);
  185. //co_task_delay(2 * 1000);
  186. sys_debug("Revers\n");
  187. // Reverse
  188. for (int i = 0;i < 5;i++) {
  189. for (int j = 360;j >= 0;j--) {
  190. hall_sensor_set_theta(true, j);
  191. co_task_delay(50);
  192. wdog_reload();
  193. int hall = get_hall_stat(7);
  194. float s, c;
  195. normal_sincosf(degree_2_pi(j), &s, &c);
  196. sin_hall[hall] += s;
  197. cos_hall[hall] += c;
  198. hall_iterations[hall]++;
  199. }
  200. }
  201. foc_pwm_start(false);
  202. hall_sensor_set_theta(false, 0.0f);
  203. foc_set_dq_command(0.0f, 0.0f);
  204. int fails = 0;
  205. for(int i = 0;i < 8;i++) {
  206. if (hall_iterations[i] > 30) {
  207. } else {
  208. fails++;
  209. }
  210. }
  211. return fails == 2;
  212. }
  213. static void _hall_init_el_angle(void) {
  214. _sensor_hander.hall_stat = get_hall_stat(HALL_READ_TIMES);
  215. switch ( _sensor_hander.hall_stat )
  216. {
  217. case STATE_5:
  218. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE/2;
  219. break;
  220. case STATE_1:
  221. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE + PHASE_60_DEGREE / 2;
  222. break;
  223. case STATE_3:
  224. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_120_DEGREE + PHASE_60_DEGREE / 2;
  225. break;
  226. case STATE_2:
  227. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_180_DEGREE + PHASE_60_DEGREE / 2;
  228. break;
  229. case STATE_6:
  230. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_240_DEGREE + PHASE_60_DEGREE / 2;
  231. break;
  232. case STATE_4:
  233. _sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_300_DEGREE + PHASE_60_DEGREE / 2;
  234. break;
  235. default:
  236. /* Bad hall sensor configutarion so update the speed reliability */
  237. _sensor_hander.sensor_error ++;
  238. break;
  239. }
  240. /* Initialize the measured angle */
  241. rand_angle(_sensor_hander.measured_el_angle);
  242. _sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
  243. _sensor_hander.hall_ticks = timer_count32_get();
  244. }
  245. /* 4,5,1,3,2,6,4 */
  246. static s32 _hall_position(u8 state_now, u8 state_prev) {
  247. s32 theta_now = 0xFFFFFFFF;
  248. switch (state_now) {
  249. case STATE_1:
  250. if (state_prev == STATE_5) {
  251. _sensor_hander.direction = POSITIVE;
  252. theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
  253. }else if (state_prev == STATE_3) {
  254. _sensor_hander.direction = NEGATIVE;
  255. theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
  256. }
  257. break;
  258. case STATE_2:
  259. if (state_prev == STATE_3) {
  260. _sensor_hander.direction = POSITIVE;
  261. theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
  262. }else if (state_prev == STATE_6) {
  263. _sensor_hander.direction = NEGATIVE;
  264. theta_now = _sensor_hander.phase_offset + PHASE_240_DEGREE;
  265. }
  266. break;
  267. case STATE_3:
  268. if (state_prev == STATE_1) {
  269. _sensor_hander.direction = POSITIVE;
  270. theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
  271. }else if (state_prev == STATE_2) {
  272. _sensor_hander.direction = NEGATIVE;
  273. theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
  274. }
  275. break;
  276. case STATE_4:
  277. if (state_prev == STATE_6) {
  278. _sensor_hander.direction = POSITIVE;
  279. theta_now = _sensor_hander.phase_offset + PHASE_300_DEGREE;
  280. }else if (state_prev == STATE_5) {
  281. _sensor_hander.direction = NEGATIVE;
  282. theta_now = _sensor_hander.phase_offset;
  283. }
  284. break;
  285. case STATE_5:
  286. if (state_prev == STATE_4) {
  287. _sensor_hander.direction = POSITIVE;
  288. theta_now = _sensor_hander.phase_offset;
  289. }else if (state_prev == STATE_1) {
  290. _sensor_hander.direction = NEGATIVE;
  291. theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
  292. }
  293. break;
  294. case STATE_6:
  295. if (state_prev == STATE_2) {
  296. _sensor_hander.direction = POSITIVE;
  297. theta_now = _sensor_hander.phase_offset + PHASE_240_DEGREE;
  298. }else if (state_prev == STATE_4) {
  299. _sensor_hander.direction = NEGATIVE;
  300. theta_now = _sensor_hander.phase_offset + PHASE_300_DEGREE;
  301. }
  302. break;
  303. default:
  304. _sensor_hander.sensor_error ++;
  305. return 0xFFFFFFFF;
  306. }
  307. rand_angle(theta_now);
  308. return theta_now;
  309. }
  310. void hall_sensor_handler(void) {
  311. if (_sensor_hander.is_override_angle) {
  312. sys_debug("%d:%d\n", (int)get_hall_stat(HALL_READ_TIMES), (int)_sensor_hander.override_el_angle);
  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. }