hall_sensor.c 8.9 KB

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