hall_sensor.c 7.9 KB

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