hall_sensor.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. #include <string.h>
  2. #include "bsp/bsp.h"
  3. #include "bsp/mc_hall_gpio.h"
  4. #include "os/co_task.h"
  5. #include "os/timer.h"
  6. #include "libs/utils.h"
  7. #include "math/fast_math.h"
  8. #include "hall_sensor.h"
  9. #include "foc/foc_api.h"
  10. #include "app/nv_storage.h"
  11. #include "bsp/timer_count32.h"
  12. #define HALL_READ_TIMES 3
  13. static void _hall_timeout_handler(timer_t *timer);
  14. /*
  15. * 测量HALL_PLACE_OFFSET通用方式就是ST推荐的通过外力带动电机,
  16. * 测量电机U相反电动势和hall 1的上升沿之间的差值
  17. * 这里使用的是先通过hall_sensor_calibrate测量hall1,2,3,4,5,6
  18. * 对应的角度(偏差比较大),然后启动电机,让HALL_PLACE_OFFSET
  19. * 从0开始增加,每增加1度观察电机电流(看直流电源),
  20. * 找到一个电机平稳转动并且电流最小的角度作为HALL_PLACE_OFFSET
  21. */
  22. #define HALL_PLACE_OFFSET 210.0f
  23. /*
  24. 100
  25. 101
  26. 001
  27. 011
  28. 010
  29. 110
  30. 4,5,1,3,2,6,4
  31. */
  32. //static u16 _hall_table[] = {0xFFFF, 292/*1*/, 54/*2*/, 1/*3*/, 180/*4*/, 229/*5*/, 115/*6*/, 0xFFFF};
  33. static u16 _hall_table[] = {0xFFFF, 121/*1*/, 240/*2*/, 190/*3*/, 13/*4*/, 58/*5*/, 306/*6*/, 0xFFFF};
  34. static hall_t _hall;
  35. static hall_sample_t h_samples;
  36. static timer_t _hall_detect_timer = TIMER_INIT(_hall_detect_timer, _hall_timeout_handler);
  37. #define read_hall(h,t) {h = get_hall_stat(HALL_READ_TIMES); t = _hall_table[h];}
  38. #define us_2_s(tick) ((float)tick / 1000000.0f)
  39. static float __inline _delta_seconds(u32 prev) {
  40. return (float)timer_count32_delta_us(prev, NULL)/1000000.0f;
  41. }
  42. static u32 __inline get_seconds(void) {
  43. return ticks_2_ms(co_task_sys64_ticks()/1000);
  44. }
  45. static void _hall_put_sample(float angle, u32 ticks) {
  46. hall_sample_t *s = &h_samples;
  47. s->index += 1;
  48. if (s->index >= SAMPLE_MAX_COUNT) {
  49. s->full = true;
  50. s->index = 0;
  51. }
  52. s->angle[s->index] = angle;
  53. s->ticks[s->index] = ticks;
  54. }
  55. static float __inline _hall_avg_speed(void){
  56. hall_sample_t *s = &h_samples;
  57. float t_angle = 0.0f;
  58. u32 t_ticks = 0;
  59. for (int i = 0; i < SAMPLE_MAX_COUNT; i++) {
  60. t_angle += s->angle[i];
  61. t_ticks += s->ticks[i];
  62. }
  63. if (t_ticks == 0.0f) {
  64. return 0.0f;
  65. }
  66. return (t_angle / us_2_s(t_ticks));
  67. }
  68. void hall_sensor_init(void) {
  69. mc_hall_init();
  70. memset(&_hall, 0, sizeof(_hall));
  71. read_hall(_hall.state, _hall.theta);
  72. _hall.phase_offset = HALL_PLACE_OFFSET;//mc_config_get()->hall_offset;
  73. }
  74. void _hall_timeout_handler(timer_t *timer){
  75. _hall.degree_per_s = 0;
  76. _hall.e_rpm = _hall.e_filted_rpm = 0;
  77. h_samples.index = 0;
  78. }
  79. u16 *hall_phase_angle(void) {
  80. return _hall_table;
  81. }
  82. float hall_sensor_get_theta(void){
  83. if (_hall.is_override_theta) {
  84. return _hall.override_theta;
  85. }
  86. if (!_hall.working) {
  87. read_hall(_hall.state, _hall.theta);
  88. return _hall.theta;
  89. }
  90. _hall.est_theta = _delta_seconds(_hall.ticks) * _hall.degree_per_s + _hall.theta;
  91. float est_delta = _hall.est_theta - _hall.theta;
  92. if (est_delta > PHASE_60_DEGREE) {
  93. _hall.est_theta = _hall.theta + PHASE_60_DEGREE;
  94. if (_hall.est_theta >= PHASE_360_DEGREE){
  95. _hall.est_theta -= PHASE_360_DEGREE;
  96. }
  97. }else if (est_delta < -PHASE_60_DEGREE){
  98. _hall.est_theta = _hall.theta - PHASE_60_DEGREE;
  99. if (_hall.est_theta <= -PHASE_360_DEGREE) {
  100. _hall.est_theta += PHASE_360_DEGREE;
  101. }
  102. }
  103. return _hall.est_theta;
  104. }
  105. void hall_sensor_set_theta(bool override, float theta){
  106. _hall.is_override_theta = override;
  107. _hall.override_theta = theta;
  108. }
  109. float hall_sensor_get_speed(void) {
  110. return _hall.e_rpm;
  111. }
  112. float hall_sensor_avg_speed(void) {
  113. return _hall.e_filted_rpm;
  114. }
  115. int hall_offset_increase(int inc) {
  116. if (_hall.phase_offset + inc >= 360.0f) {
  117. _hall.phase_offset = _hall.phase_offset + inc - 360.0f;
  118. }else {
  119. _hall.phase_offset += inc;
  120. }
  121. return _hall.phase_offset;
  122. }
  123. int hall_sensor_calibrate(float voltage, u16 *hall_table){
  124. if (hall_table == NULL) {
  125. hall_table = _hall_table;
  126. }
  127. foc_set_controller_mode(FOC_MODE_OPEN_LOOP);
  128. hall_sensor_set_theta(true, 0.0f);
  129. foc_set_dq_command(0.0f, 0.0f);
  130. foc_pwm_start(true);
  131. for (int i = 0;i < 1000;i++) {
  132. foc_set_dq_command((float)i * voltage / 1000.0f, 0.0f);
  133. delay_ms(1);
  134. }
  135. float sin_hall[8];
  136. float cos_hall[8];
  137. int hall_iterations[8];
  138. memset(sin_hall, 0, sizeof(sin_hall));
  139. memset(cos_hall, 0, sizeof(cos_hall));
  140. memset(hall_iterations, 0, sizeof(hall_iterations));
  141. delay_ms(2 * 1000);
  142. // Forwards
  143. for (int i = 0;i < 5;i++) {
  144. for (int j = 0;j < 360;j++) {
  145. hall_sensor_set_theta(true, j);
  146. delay_ms(5);
  147. int hall = get_hall_stat(7);
  148. float s, c;
  149. normal_sincosf(degree_2_pi(j), &s, &c);
  150. sin_hall[hall] += s;
  151. cos_hall[hall] += c;
  152. hall_iterations[hall]++;
  153. }
  154. }
  155. delay_ms(2 * 1000);
  156. // Reverse
  157. for (int i = 0;i < 5;i++) {
  158. for (int j = 360;j >= 0;j--) {
  159. hall_sensor_set_theta(true, j);
  160. delay_ms(5);
  161. int hall = get_hall_stat(7);
  162. float s, c;
  163. normal_sincosf(degree_2_pi(j), &s, &c);
  164. sin_hall[hall] += s;
  165. cos_hall[hall] += c;
  166. hall_iterations[hall]++;
  167. }
  168. }
  169. foc_pwm_start(false);
  170. hall_sensor_set_theta(false, 0.0f);
  171. foc_set_dq_command(0.0f, 0.0f);
  172. int fails = 0;
  173. for(int i = 0;i < 8;i++) {
  174. if (hall_iterations[i] > 30) {
  175. float ang = pi_2_degree(atan2f(sin_hall[i], cos_hall[i]));
  176. fast_norm_angle(&ang);
  177. hall_table[i] = (u16)ang;
  178. } else {
  179. hall_table[i] = 0xFFFF;
  180. fails++;
  181. }
  182. }
  183. return fails == 2;
  184. }
  185. void hall_sensor_handler(void) {
  186. u8 state_now = get_hall_stat(HALL_READ_TIMES);
  187. float theta_now = _hall_table[state_now];
  188. u8 state_prev = _hall.state;
  189. if (!_hall.working) {
  190. if(theta_now != 0xFFFF) {
  191. _hall.working = true;
  192. _hall.state = state_now;
  193. _hall.theta = theta_now;
  194. _hall.ticks = timer_count32_get();
  195. }
  196. return;
  197. }
  198. switch (state_now) {
  199. case STATE_1:
  200. if (state_prev == STATE_5) {
  201. _hall.direction = POSITIVE;
  202. theta_now = _hall.phase_offset + PHASE_60_DEGREE;
  203. }else if (state_prev == STATE_3) {
  204. _hall.direction = NEGATIVE;
  205. theta_now = _hall.phase_offset + PHASE_120_DEGREE;
  206. }
  207. break;
  208. case STATE_2:
  209. if (state_prev == STATE_3) {
  210. _hall.direction = POSITIVE;
  211. theta_now = _hall.phase_offset + PHASE_180_DEGREE;
  212. }else if (state_prev == STATE_6) {
  213. _hall.direction = NEGATIVE;
  214. theta_now = _hall.phase_offset + PHASE_240_DEGREE;
  215. }
  216. break;
  217. case STATE_3:
  218. if (state_prev == STATE_1) {
  219. _hall.direction = POSITIVE;
  220. theta_now = _hall.phase_offset + PHASE_120_DEGREE;
  221. }else if (state_prev == STATE_2) {
  222. _hall.direction = NEGATIVE;
  223. theta_now = _hall.phase_offset + PHASE_180_DEGREE;
  224. }
  225. break;
  226. case STATE_4:
  227. if (state_prev == STATE_6) {
  228. _hall.direction = POSITIVE;
  229. theta_now = _hall.phase_offset + PHASE_300_DEGREE;
  230. }else if (state_prev == STATE_5) {
  231. _hall.direction = NEGATIVE;
  232. theta_now = _hall.phase_offset;
  233. }
  234. break;
  235. case STATE_5:
  236. if (state_prev == STATE_4) {
  237. _hall.direction = POSITIVE;
  238. theta_now = _hall.phase_offset;
  239. }else if (state_prev == STATE_1) {
  240. _hall.direction = NEGATIVE;
  241. theta_now = _hall.phase_offset + PHASE_60_DEGREE;
  242. }
  243. break;
  244. case STATE_6:
  245. if (state_prev == STATE_2) {
  246. _hall.direction = POSITIVE;
  247. theta_now = _hall.phase_offset + PHASE_240_DEGREE;
  248. }else if (state_prev == STATE_4) {
  249. _hall.direction = NEGATIVE;
  250. theta_now = _hall.phase_offset + PHASE_300_DEGREE;
  251. }
  252. break;
  253. default:
  254. return;
  255. }
  256. float delta_time = _delta_seconds(_hall.ticks);
  257. if (delta_time == 0.0f) { //may be errors ???
  258. return;
  259. }
  260. float delta_theta = (_hall.direction == POSITIVE)?60.0f : -60.0f;
  261. _hall_put_sample(delta_theta, timer_count32_delta_us(_hall.ticks, NULL));
  262. if (!h_samples.full) {
  263. _hall.degree_per_s = delta_theta / delta_time;
  264. }else {
  265. _hall.degree_per_s = _hall_avg_speed();
  266. _hall.e_filted_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
  267. }
  268. _hall.ticks = timer_count32_get();
  269. _hall.second = get_seconds();
  270. _hall.theta = theta_now;
  271. _hall.state = state_now;
  272. _hall.e_rpm = _hall.degree_per_s / 360.0f * 60.0f;
  273. timer_post(&_hall_detect_timer, 2000);
  274. //printf("speed :%.4f - %.4f - %.4f - %d\n", _hall.degree_per_s, delta_theta, delta_time, (int)_hall.e_rpm);
  275. }