hall_sensor.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. for (int i = 0;i < 1000;i++) {
  121. foc_set_dq_command((float)i * voltage / 1000.0f, 0.0f);
  122. task_udelay(1000);
  123. }
  124. float sin_hall[8];
  125. float cos_hall[8];
  126. int hall_iterations[8];
  127. memset(sin_hall, 0, sizeof(sin_hall));
  128. memset(cos_hall, 0, sizeof(cos_hall));
  129. memset(hall_iterations, 0, sizeof(hall_iterations));
  130. task_udelay(50 * 1000);
  131. // Forwards
  132. for (int i = 0;i < 3;i++) {
  133. for (int j = 0;j < 360;j++) {
  134. hall_sensor_set_theta(true, j);
  135. task_udelay(10 * 1000);
  136. int hall = get_hall_stat(7);
  137. float s, c;
  138. normal_sincosf(degree_2_pi(j), &s, &c);
  139. sin_hall[hall] += s;
  140. cos_hall[hall] += c;
  141. hall_iterations[hall]++;
  142. }
  143. }
  144. // Reverse
  145. for (int i = 0;i < 3;i++) {
  146. for (int j = 360;j >= 0;j--) {
  147. hall_sensor_set_theta(true, j);
  148. task_udelay(10 * 1000);
  149. int hall = get_hall_stat(7);
  150. float s, c;
  151. normal_sincosf(degree_2_pi(j), &s, &c);
  152. sin_hall[hall] += s;
  153. cos_hall[hall] += c;
  154. hall_iterations[hall]++;
  155. }
  156. }
  157. foc_pwm_start(false);
  158. hall_sensor_set_theta(false, 0.0f);
  159. foc_set_dq_command(0.0f, 0.0f);
  160. int fails = 0;
  161. for(int i = 0;i < 8;i++) {
  162. if (hall_iterations[i] > 30) {
  163. float ang = pi_2_degree(atan2f(sin_hall[i], cos_hall[i]));
  164. fast_norm_angle(&ang);
  165. hall_table[i] = (u16)ang;
  166. } else {
  167. hall_table[i] = 0xFFFF;
  168. fails++;
  169. }
  170. }
  171. return fails == 2;
  172. }
  173. #ifdef HALL_PLACE_OFFSET
  174. void hall_sensor_handler(void) {
  175. u8 state_now = get_hall_stat(HALL_READ_TIMES);
  176. float theta_now = _hall_table[state_now];
  177. u8 state_prev = _hall.state;
  178. if (!_hall.working) {
  179. if(theta_now != 0xFFFF) {
  180. _hall.working = true;
  181. _hall.state = state_now;
  182. _hall.theta = theta_now;
  183. _hall.ticks = task_ticks_abs();
  184. }
  185. return;
  186. }
  187. switch (state_now) {
  188. case STATE_1:
  189. if (state_prev == STATE_5) {
  190. _hall.direction = POSITIVE;
  191. theta_now = _hall.phase_offset + PHASE_60_DEGREE;
  192. }else if (state_prev == STATE_3) {
  193. _hall.direction = NEGATIVE;
  194. theta_now = _hall.phase_offset + PHASE_120_DEGREE;
  195. }
  196. break;
  197. case STATE_2:
  198. if (state_prev == STATE_3) {
  199. _hall.direction = POSITIVE;
  200. theta_now = _hall.phase_offset + PHASE_180_DEGREE;
  201. }else if (state_prev == STATE_6) {
  202. _hall.direction = NEGATIVE;
  203. theta_now = _hall.phase_offset + PHASE_240_DEGREE;
  204. }
  205. break;
  206. case STATE_3:
  207. if (state_prev == STATE_1) {
  208. _hall.direction = POSITIVE;
  209. theta_now = _hall.phase_offset + PHASE_120_DEGREE;
  210. }else if (state_prev == STATE_2) {
  211. _hall.direction = NEGATIVE;
  212. theta_now = _hall.phase_offset + PHASE_180_DEGREE;
  213. }
  214. break;
  215. case STATE_4:
  216. if (state_prev == STATE_6) {
  217. _hall.direction = POSITIVE;
  218. theta_now = _hall.phase_offset + PHASE_300_DEGREE;
  219. }else if (state_prev == STATE_5) {
  220. _hall.direction = NEGATIVE;
  221. theta_now = _hall.phase_offset;
  222. }
  223. break;
  224. case STATE_5:
  225. if (state_prev == STATE_4) {
  226. _hall.direction = POSITIVE;
  227. theta_now = _hall.phase_offset;
  228. }else if (state_prev == STATE_1) {
  229. _hall.direction = NEGATIVE;
  230. theta_now = _hall.phase_offset + PHASE_60_DEGREE;
  231. }
  232. break;
  233. case STATE_6:
  234. if (state_prev == STATE_2) {
  235. _hall.direction = POSITIVE;
  236. theta_now = _hall.phase_offset + PHASE_240_DEGREE;
  237. }else if (state_prev == STATE_4) {
  238. _hall.direction = NEGATIVE;
  239. theta_now = _hall.phase_offset + PHASE_300_DEGREE;
  240. }
  241. break;
  242. default:
  243. return;
  244. }
  245. float delta_time = tick_2_s(delta_ticks(_hall.ticks));
  246. if (delta_time == 0.0f) { //may be errors ???
  247. return;
  248. }
  249. float delta_theta = (_hall.direction == POSITIVE)?60.0f : -60.0f;
  250. _hall_put_sample(delta_theta, delta_ticks(_hall.ticks));
  251. if (!h_samples.full) {
  252. _hall.degree_per_s = delta_theta / delta_time;
  253. }else {
  254. _hall.degree_per_s = _hall_avg_speed();
  255. _hall.e_filted_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
  256. }
  257. _hall.ticks = task_ticks_abs();
  258. _hall.second = get_seconds();
  259. _hall.theta = theta_now;
  260. _hall.state = state_now;
  261. _hall.e_rpm = _hall.degree_per_s / 360.0f * 60.0f;
  262. //printf("speed :%.4f - %.4f - %.4f - %d\n", _hall.degree_per_s, delta_theta, delta_time, (int)_hall.e_rpm);
  263. }
  264. #else
  265. void hall_sensor_handler1(void) {
  266. u8 state_now = get_hall_stat(HALL_READ_TIMES);
  267. float theta_now = _hall_table[state_now];
  268. u8 state_prev = _hall.state;
  269. float theta_prev = _hall.theta;
  270. if (!_hall.working) {
  271. if(theta_now != 0xFFFF) {
  272. _hall.working = true;
  273. _hall.state = state_now;
  274. _hall.theta = theta_now;
  275. _hall.ticks = task_ticks_abs();
  276. }
  277. return;
  278. }
  279. //printf("hall %d, %d\n", state_now, state_prev);
  280. //{0xFFFF, 257/*1*/, 36/*2*/, 344/*3*/, 159/*4*/, 222/*5*/, 88/*6*/, 0xFFFF};
  281. float delta_theta = 360.0f;
  282. switch (state_now) {
  283. case STATE_1:
  284. if (state_prev == STATE_5) {
  285. _hall.direction = POSITIVE;
  286. delta_theta = theta_now - theta_prev;
  287. }else if (state_prev == STATE_3) {
  288. _hall.direction = NEGATIVE;
  289. delta_theta = 360 - theta_now + theta_prev;
  290. }
  291. break;
  292. case STATE_2:
  293. if (state_prev == STATE_3) {
  294. _hall.direction = POSITIVE;
  295. delta_theta = theta_now - theta_prev;
  296. }else if (state_prev == STATE_6) {
  297. _hall.direction = NEGATIVE;
  298. delta_theta = theta_prev - theta_now;
  299. }
  300. break;
  301. case STATE_3:
  302. if (state_prev == STATE_1) {
  303. _hall.direction = POSITIVE;
  304. delta_theta = 360 - theta_prev + theta_now;
  305. }else if (state_prev == STATE_2) {
  306. _hall.direction = NEGATIVE;
  307. delta_theta = theta_prev - theta_now;
  308. }
  309. break;
  310. case STATE_4:
  311. if (state_prev == STATE_6) {
  312. _hall.direction = POSITIVE;
  313. delta_theta = theta_now - theta_prev;
  314. }else if (state_prev == STATE_5) {
  315. _hall.direction = NEGATIVE;
  316. delta_theta = theta_prev - theta_now;
  317. }
  318. break;
  319. case STATE_5:
  320. if (state_prev == STATE_4) {
  321. _hall.direction = POSITIVE;
  322. delta_theta = theta_now - theta_prev;
  323. }else if (state_prev == STATE_1) {
  324. _hall.direction = NEGATIVE;
  325. delta_theta = theta_prev - theta_now;
  326. }
  327. break;
  328. case STATE_6:
  329. if (state_prev == STATE_2) {
  330. _hall.direction = POSITIVE;
  331. delta_theta = theta_now - theta_prev;
  332. }else if (state_prev == STATE_4) {
  333. _hall.direction = NEGATIVE;
  334. delta_theta = theta_prev - theta_now;
  335. }
  336. break;
  337. default:
  338. break;
  339. }
  340. if (delta_theta == 360.0f) { //no vilid hall
  341. return;
  342. }
  343. float delta_time = tick_2_s(delta_ticks(_hall.ticks));
  344. if (delta_time == 0.0f) { //may be errors ???
  345. return;
  346. }
  347. delta_theta = 60.0f;
  348. _hall_put_sample(delta_theta, delta_ticks(_hall.ticks));
  349. if (!h_samples.full) {
  350. _hall.degree_per_s = delta_theta / delta_time;
  351. }else {
  352. _hall.degree_per_s = _hall_avg_speed();
  353. _hall.e_filted_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
  354. }
  355. _hall.ticks = task_ticks_abs();
  356. _hall.theta += delta_theta;
  357. _hall.state = state_now;
  358. _hall.e_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
  359. //printf("speed :%.4f - %.4f - %.4f - %d\n", _hall.degree_per_s, delta_theta, delta_time, (int)_hall.e_rpm);
  360. }
  361. #endif