hall_sensor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include <string.h>
  2. #include "bsp/bsp.h"
  3. #include "bsp/mc_hall_gpio.h"
  4. #include "os/co_task.h"
  5. #include "libs/utils.h"
  6. #include "math/fast_math.h"
  7. #include "hall_sensor.h"
  8. #include "foc/foc_api.h"
  9. #define HALL_READ_TIMES 3
  10. /*
  11. * 测量HALL_PLACE_OFFSET通用方式就是ST推荐的通过外力带动电机,
  12. * 测量电机U相反电动势和hall 1的上升沿之间的差值
  13. * 这里使用的是先通过hall_sensor_calibrate测量hall1,2,3,4,5,6
  14. * 对应的角度(偏差比较大),然后启动电机,让HALL_PLACE_OFFSET
  15. * 从0开始增加,每增加1度观察电机电流(看直流电源),
  16. * 找到一个电机平稳转动并且电流最小的角度作为HALL_PLACE_OFFSET
  17. */
  18. #define HALL_PLACE_OFFSET 213.0f
  19. /*
  20. 100
  21. 101
  22. 001
  23. 011
  24. 010
  25. 110
  26. 4,5,1,3,2,6,4
  27. */
  28. static u16 _hall_table[] = {0xFFFF, 292/*1*/, 54/*2*/, 1/*3*/, 180/*4*/, 229/*5*/, 115/*6*/, 0xFFFF};
  29. //static u16 _hall_table[] = {0xFFFF, 257/*1*/, 36/*2*/, 344/*3*/, 159/*4*/, 222/*5*/, 88/*6*/, 0xFFFF};
  30. static hall_t _hall;
  31. static hall_sample_t h_samples;
  32. #define read_hall(h,t) {h = get_hall_stat(HALL_READ_TIMES); t = _hall_table[h];}
  33. #define tick_2_s(tick) ((float)tick / (float)SYSTEM_CLOCK)
  34. static u32 __inline delta_ticks(u32 prev) {
  35. u32 now = cpu_counts_abs();
  36. if (now >= prev) {
  37. return (now - prev);
  38. }
  39. return (0xFFFFFFFFU - prev + now) + 1;
  40. }
  41. static u32 __inline get_seconds(void) {
  42. return co_task_sys64_ticks()/1000;
  43. }
  44. static void _hall_put_sample(float angle, u32 ticks) {
  45. hall_sample_t *s = &h_samples;
  46. s->index += 1;
  47. if (s->index >= SAMPLE_MAX_COUNT) {
  48. s->full = true;
  49. s->index = 0;
  50. }
  51. s->angle[s->index] = angle;
  52. s->ticks[s->index] = ticks;
  53. }
  54. static float __inline _hall_avg_speed(void){
  55. hall_sample_t *s = &h_samples;
  56. float t_angle = 0.0f;
  57. u32 t_ticks = 0;
  58. for (int i = 0; i < SAMPLE_MAX_COUNT; i++) {
  59. t_angle += s->angle[i];
  60. t_ticks += s->ticks[i];
  61. }
  62. if (t_ticks == 0.0f) {
  63. return 0.0f;
  64. }
  65. return (t_angle / tick_2_s(t_ticks));
  66. }
  67. void hall_sensor_init(void) {
  68. mc_hall_init();
  69. memset(&_hall, 0, sizeof(_hall));
  70. read_hall(_hall.state, _hall.theta);
  71. #ifdef HALL_PLACE_OFFSET
  72. _hall.phase_offset = HALL_PLACE_OFFSET;
  73. #endif
  74. }
  75. static void _detect_timeout(void){
  76. u32 now = get_seconds();
  77. if (now > _hall.second + 4) {//4s内没有霍尔中断,速度清零
  78. if (_hall.degree_per_s > 0) {
  79. _hall.degree_per_s = 0;
  80. _hall.e_rpm = _hall.e_filted_rpm = 0;
  81. }
  82. }
  83. }
  84. float hall_sensor_get_theta(void){
  85. if (_hall.is_override_theta) {
  86. return _hall.override_theta;
  87. }
  88. if (!_hall.working) {
  89. read_hall(_hall.state, _hall.theta);
  90. return _hall.theta;
  91. }
  92. _hall.est_theta = tick_2_s(delta_ticks(_hall.ticks)) * _hall.degree_per_s + _hall.theta;
  93. float est_delta = _hall.est_theta - _hall.theta;
  94. if (est_delta > PHASE_60_DEGREE) {
  95. _hall.est_theta = _hall.theta + PHASE_60_DEGREE;
  96. if (_hall.est_theta >= PHASE_360_DEGREE){
  97. _hall.est_theta -= PHASE_360_DEGREE;
  98. }
  99. }else if (est_delta < -PHASE_60_DEGREE){
  100. _hall.est_theta = _hall.theta - PHASE_60_DEGREE;
  101. if (_hall.est_theta <= -PHASE_360_DEGREE) {
  102. _hall.est_theta += PHASE_360_DEGREE;
  103. }
  104. }
  105. return _hall.est_theta;
  106. }
  107. void hall_sensor_set_theta(bool override, float theta){
  108. _hall.is_override_theta = override;
  109. _hall.override_theta = theta;
  110. }
  111. float hall_sensor_get_speed(void) {
  112. _detect_timeout();
  113. return _hall.e_rpm;
  114. }
  115. float hall_sensor_avg_speed(void) {
  116. _detect_timeout();
  117. return _hall.e_filted_rpm;
  118. }
  119. int hall_sensor_calibrate(float voltage, u16 *hall_table){
  120. foc_set_controller_mode(FOC_MODE_OPEN_LOOP);
  121. hall_sensor_set_theta(true, 0.0f);
  122. foc_set_dq_command(0.0f, 0.0f);
  123. foc_pwm_start(true);
  124. for (int i = 0;i < 1000;i++) {
  125. foc_set_dq_command((float)i * voltage / 1000.0f, 0.0f);
  126. delay_ms(1000);
  127. }
  128. float sin_hall[8];
  129. float cos_hall[8];
  130. int hall_iterations[8];
  131. memset(sin_hall, 0, sizeof(sin_hall));
  132. memset(cos_hall, 0, sizeof(cos_hall));
  133. memset(hall_iterations, 0, sizeof(hall_iterations));
  134. delay_ms(50 * 1000);
  135. // Forwards
  136. for (int i = 0;i < 3;i++) {
  137. for (int j = 0;j < 360;j++) {
  138. hall_sensor_set_theta(true, j);
  139. delay_ms(10 * 1000);
  140. int hall = get_hall_stat(7);
  141. float s, c;
  142. normal_sincosf(degree_2_pi(j), &s, &c);
  143. sin_hall[hall] += s;
  144. cos_hall[hall] += c;
  145. hall_iterations[hall]++;
  146. }
  147. }
  148. // Reverse
  149. for (int i = 0;i < 3;i++) {
  150. for (int j = 360;j >= 0;j--) {
  151. hall_sensor_set_theta(true, j);
  152. delay_ms(10 * 1000);
  153. int hall = get_hall_stat(7);
  154. float s, c;
  155. normal_sincosf(degree_2_pi(j), &s, &c);
  156. sin_hall[hall] += s;
  157. cos_hall[hall] += c;
  158. hall_iterations[hall]++;
  159. }
  160. }
  161. foc_pwm_start(false);
  162. hall_sensor_set_theta(false, 0.0f);
  163. foc_set_dq_command(0.0f, 0.0f);
  164. int fails = 0;
  165. for(int i = 0;i < 8;i++) {
  166. if (hall_iterations[i] > 30) {
  167. float ang = pi_2_degree(atan2f(sin_hall[i], cos_hall[i]));
  168. fast_norm_angle(&ang);
  169. hall_table[i] = (u16)ang;
  170. } else {
  171. hall_table[i] = 0xFFFF;
  172. fails++;
  173. }
  174. }
  175. return fails == 2;
  176. }
  177. #ifdef HALL_PLACE_OFFSET
  178. void hall_sensor_handler(void) {
  179. u8 state_now = get_hall_stat(HALL_READ_TIMES);
  180. float theta_now = _hall_table[state_now];
  181. u8 state_prev = _hall.state;
  182. if (!_hall.working) {
  183. if(theta_now != 0xFFFF) {
  184. _hall.working = true;
  185. _hall.state = state_now;
  186. _hall.theta = theta_now;
  187. _hall.ticks = cpu_counts_abs();
  188. }
  189. return;
  190. }
  191. switch (state_now) {
  192. case STATE_1:
  193. if (state_prev == STATE_5) {
  194. _hall.direction = POSITIVE;
  195. theta_now = _hall.phase_offset + PHASE_60_DEGREE;
  196. }else if (state_prev == STATE_3) {
  197. _hall.direction = NEGATIVE;
  198. theta_now = _hall.phase_offset + PHASE_120_DEGREE;
  199. }
  200. break;
  201. case STATE_2:
  202. if (state_prev == STATE_3) {
  203. _hall.direction = POSITIVE;
  204. theta_now = _hall.phase_offset + PHASE_180_DEGREE;
  205. }else if (state_prev == STATE_6) {
  206. _hall.direction = NEGATIVE;
  207. theta_now = _hall.phase_offset + PHASE_240_DEGREE;
  208. }
  209. break;
  210. case STATE_3:
  211. if (state_prev == STATE_1) {
  212. _hall.direction = POSITIVE;
  213. theta_now = _hall.phase_offset + PHASE_120_DEGREE;
  214. }else if (state_prev == STATE_2) {
  215. _hall.direction = NEGATIVE;
  216. theta_now = _hall.phase_offset + PHASE_180_DEGREE;
  217. }
  218. break;
  219. case STATE_4:
  220. if (state_prev == STATE_6) {
  221. _hall.direction = POSITIVE;
  222. theta_now = _hall.phase_offset + PHASE_300_DEGREE;
  223. }else if (state_prev == STATE_5) {
  224. _hall.direction = NEGATIVE;
  225. theta_now = _hall.phase_offset;
  226. }
  227. break;
  228. case STATE_5:
  229. if (state_prev == STATE_4) {
  230. _hall.direction = POSITIVE;
  231. theta_now = _hall.phase_offset;
  232. }else if (state_prev == STATE_1) {
  233. _hall.direction = NEGATIVE;
  234. theta_now = _hall.phase_offset + PHASE_60_DEGREE;
  235. }
  236. break;
  237. case STATE_6:
  238. if (state_prev == STATE_2) {
  239. _hall.direction = POSITIVE;
  240. theta_now = _hall.phase_offset + PHASE_240_DEGREE;
  241. }else if (state_prev == STATE_4) {
  242. _hall.direction = NEGATIVE;
  243. theta_now = _hall.phase_offset + PHASE_300_DEGREE;
  244. }
  245. break;
  246. default:
  247. return;
  248. }
  249. float delta_time = tick_2_s(delta_ticks(_hall.ticks));
  250. if (delta_time == 0.0f) { //may be errors ???
  251. return;
  252. }
  253. float delta_theta = (_hall.direction == POSITIVE)?60.0f : -60.0f;
  254. _hall_put_sample(delta_theta, delta_ticks(_hall.ticks));
  255. if (!h_samples.full) {
  256. _hall.degree_per_s = delta_theta / delta_time;
  257. }else {
  258. _hall.degree_per_s = _hall_avg_speed();
  259. _hall.e_filted_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
  260. }
  261. _hall.ticks = cpu_counts_abs();
  262. _hall.second = get_seconds();
  263. _hall.theta = theta_now;
  264. _hall.state = state_now;
  265. _hall.e_rpm = _hall.degree_per_s / 360.0f * 60.0f;
  266. //printf("speed :%.4f - %.4f - %.4f - %d\n", _hall.degree_per_s, delta_theta, delta_time, (int)_hall.e_rpm);
  267. }
  268. #else
  269. void hall_sensor_handler1(void) {
  270. u8 state_now = get_hall_stat(HALL_READ_TIMES);
  271. float theta_now = _hall_table[state_now];
  272. u8 state_prev = _hall.state;
  273. float theta_prev = _hall.theta;
  274. if (!_hall.working) {
  275. if(theta_now != 0xFFFF) {
  276. _hall.working = true;
  277. _hall.state = state_now;
  278. _hall.theta = theta_now;
  279. _hall.ticks = task_ticks_abs();
  280. }
  281. return;
  282. }
  283. //printf("hall %d, %d\n", state_now, state_prev);
  284. //{0xFFFF, 257/*1*/, 36/*2*/, 344/*3*/, 159/*4*/, 222/*5*/, 88/*6*/, 0xFFFF};
  285. float delta_theta = 360.0f;
  286. switch (state_now) {
  287. case STATE_1:
  288. if (state_prev == STATE_5) {
  289. _hall.direction = POSITIVE;
  290. delta_theta = theta_now - theta_prev;
  291. }else if (state_prev == STATE_3) {
  292. _hall.direction = NEGATIVE;
  293. delta_theta = 360 - theta_now + theta_prev;
  294. }
  295. break;
  296. case STATE_2:
  297. if (state_prev == STATE_3) {
  298. _hall.direction = POSITIVE;
  299. delta_theta = theta_now - theta_prev;
  300. }else if (state_prev == STATE_6) {
  301. _hall.direction = NEGATIVE;
  302. delta_theta = theta_prev - theta_now;
  303. }
  304. break;
  305. case STATE_3:
  306. if (state_prev == STATE_1) {
  307. _hall.direction = POSITIVE;
  308. delta_theta = 360 - theta_prev + theta_now;
  309. }else if (state_prev == STATE_2) {
  310. _hall.direction = NEGATIVE;
  311. delta_theta = theta_prev - theta_now;
  312. }
  313. break;
  314. case STATE_4:
  315. if (state_prev == STATE_6) {
  316. _hall.direction = POSITIVE;
  317. delta_theta = theta_now - theta_prev;
  318. }else if (state_prev == STATE_5) {
  319. _hall.direction = NEGATIVE;
  320. delta_theta = theta_prev - theta_now;
  321. }
  322. break;
  323. case STATE_5:
  324. if (state_prev == STATE_4) {
  325. _hall.direction = POSITIVE;
  326. delta_theta = theta_now - theta_prev;
  327. }else if (state_prev == STATE_1) {
  328. _hall.direction = NEGATIVE;
  329. delta_theta = theta_prev - theta_now;
  330. }
  331. break;
  332. case STATE_6:
  333. if (state_prev == STATE_2) {
  334. _hall.direction = POSITIVE;
  335. delta_theta = theta_now - theta_prev;
  336. }else if (state_prev == STATE_4) {
  337. _hall.direction = NEGATIVE;
  338. delta_theta = theta_prev - theta_now;
  339. }
  340. break;
  341. default:
  342. break;
  343. }
  344. if (delta_theta == 360.0f) { //no vilid hall
  345. return;
  346. }
  347. float delta_time = tick_2_s(delta_ticks(_hall.ticks));
  348. if (delta_time == 0.0f) { //may be errors ???
  349. return;
  350. }
  351. delta_theta = 60.0f;
  352. _hall_put_sample(delta_theta, delta_ticks(_hall.ticks));
  353. if (!h_samples.full) {
  354. _hall.degree_per_s = delta_theta / delta_time;
  355. }else {
  356. _hall.degree_per_s = _hall_avg_speed();
  357. _hall.e_filted_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
  358. }
  359. _hall.ticks = task_ticks_abs();
  360. _hall.theta += delta_theta;
  361. _hall.state = state_now;
  362. _hall.e_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
  363. //printf("speed :%.4f - %.4f - %.4f - %d\n", _hall.degree_per_s, delta_theta, delta_time, (int)_hall.e_rpm);
  364. }
  365. #endif