hall_sensor.c 9.9 KB

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