hall_sensor.c 7.5 KB

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