hall_sensor.c 9.7 KB

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