hall_sensor.c 10 KB

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