hall_sensor.c 9.1 KB

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