hall_sensor.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. 100
  10. 101
  11. 001
  12. 011
  13. 010
  14. 110
  15. 4,5,1,3,2,6,4
  16. */
  17. static u16 _hall_table[] = {0xFFFF, 292/*1*/, 54/*2*/, 1/*3*/, 180/*4*/, 229/*5*/, 115/*6*/, 0xFFFF};
  18. //static u16 _hall_table[] = {0xFFFF, 292/*1*/, 65/*2*/, 7/*3*/, 185/*4*/, 250/*5*/, 116/*6*/, 0xFFFF};
  19. static hall_t _hall;
  20. static hall_sample_t h_samples;
  21. #define read_hall(h,t) {h = get_hall_stat(HALL_READ_TIMES); t = _hall_table[h];}
  22. #define tick_2_s(tick) ((float)tick / (float)SYSTEM_CLOCK)
  23. static u32 __inline delta_ticks(u32 prev) {
  24. u32 now = task_ticks_abs();
  25. if (now >= prev) {
  26. return (now - prev);
  27. }
  28. return (0xFFFFFFFFU - prev + now) + 1;
  29. }
  30. static void _hall_put_sample(float angle, u32 ticks) {
  31. hall_sample_t *s = &h_samples;
  32. s->index += 1;
  33. if (s->index >= SAMPLE_MAX_COUNT) {
  34. s->full = true;
  35. s->index = 0;
  36. }
  37. s->angle[s->index] = angle;
  38. s->ticks[s->index] = ticks;
  39. }
  40. static float __inline _hall_avg_speed(void){
  41. hall_sample_t *s = &h_samples;
  42. float t_angle = 0.0f;
  43. u32 t_ticks = 0;
  44. for (int i = 0; i < SAMPLE_MAX_COUNT; i++) {
  45. t_angle += s->angle[i];
  46. t_ticks += s->ticks[i];
  47. }
  48. if (t_ticks == 0.0f) {
  49. return 0.0f;
  50. }
  51. return (t_angle / tick_2_s(t_ticks));
  52. }
  53. void hall_sensor_init(void) {
  54. memset(&_hall, 0, sizeof(_hall));
  55. read_hall(_hall.state, _hall.theta);
  56. }
  57. float hall_sensor_get_theta(void){
  58. if (!_hall.working) {
  59. read_hall(_hall.state, _hall.theta);
  60. return _hall.theta;
  61. }
  62. _hall.est_theta = tick_2_s(delta_ticks(_hall.ticks)) * _hall.degree_per_s + _hall.theta;
  63. if (_hall.est_theta > _hall.theta + 60.0f) {
  64. _hall.est_theta = _hall.theta + 60.0f;
  65. }
  66. float angle = _hall.est_theta;
  67. fast_norm_angle(&angle);
  68. return angle;
  69. }
  70. float hall_sensor_get_speed(void) {
  71. return _hall.e_rpm;
  72. }
  73. int hall_sensor_calibrate(float current, u16 *hall_table){
  74. foc_overide_set_theta(0.0f);
  75. foc_overide_theta(true);
  76. foc_overide_set_vdq(0.0f, 0.0f);
  77. foc_overide_vdq(true);
  78. foc_pwm_start(true);
  79. HAL_ADC1_InJ_StartConvert();
  80. for (int i = 0;i < 1000;i++) {
  81. foc_overide_set_vdq((float)i * current / 1000.0f, 0.0f);
  82. task_udelay(1000);
  83. }
  84. float sin_hall[8];
  85. float cos_hall[8];
  86. int hall_iterations[8];
  87. memset(sin_hall, 0, sizeof(sin_hall));
  88. memset(cos_hall, 0, sizeof(cos_hall));
  89. memset(hall_iterations, 0, sizeof(hall_iterations));
  90. // Forwards
  91. for (int i = 0;i < 3;i++) {
  92. for (int j = 0;j < 360;j++) {
  93. foc_overide_set_theta(j);
  94. task_udelay(10 * 1000);
  95. int hall = get_hall_stat(7);
  96. float s, c;
  97. normal_sincosf(degree_2_pi(j), &s, &c);
  98. sin_hall[hall] += s;
  99. cos_hall[hall] += c;
  100. hall_iterations[hall]++;
  101. }
  102. }
  103. // Reverse
  104. for (int i = 0;i < 3;i++) {
  105. for (int j = 360;j >= 0;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. foc_pwm_start(false);
  117. foc_overide_theta(false);
  118. foc_overide_vdq(false);
  119. int fails = 0;
  120. for(int i = 0;i < 8;i++) {
  121. if (hall_iterations[i] > 30) {
  122. float ang = pi_2_degree(atan2f(sin_hall[i], cos_hall[i]));
  123. fast_norm_angle(&ang);
  124. hall_table[i] = (u16)ang;
  125. } else {
  126. hall_table[i] = 0xFFFF;
  127. fails++;
  128. }
  129. }
  130. return fails == 2;
  131. }
  132. void hall_sensor_handler(void) {
  133. u8 state_now = get_hall_stat(HALL_READ_TIMES);
  134. float theta_now = _hall_table[state_now];
  135. u8 state_prev = _hall.state;
  136. float theta_prev = _hall.theta;
  137. if (!_hall.working) {
  138. if(theta_now != 0xFFFF) {
  139. _hall.working = true;
  140. _hall.state = state_now;
  141. _hall.theta = theta_now;
  142. _hall.ticks = task_ticks_abs();
  143. }
  144. return;
  145. }
  146. //printf("hall %d, %d\n", state_now, state_prev);
  147. float delta_theta = 360.0f;
  148. switch (state_now) {
  149. case STATE_1:
  150. if (state_prev == STATE_5) {
  151. _hall.direction = POSITIVE;
  152. delta_theta = theta_now - theta_prev;
  153. }else if (state_prev == STATE_3) {
  154. _hall.direction = NEGATIVE;
  155. delta_theta = 360 - theta_now + theta_prev;
  156. }
  157. break;
  158. case STATE_2:
  159. if (state_prev == STATE_3) {
  160. _hall.direction = POSITIVE;
  161. delta_theta = theta_now - theta_prev;
  162. }else if (state_prev == STATE_6) {
  163. _hall.direction = NEGATIVE;
  164. delta_theta = theta_prev - theta_now;
  165. }
  166. break;
  167. case STATE_3:
  168. if (state_prev == STATE_1) {
  169. _hall.direction = POSITIVE;
  170. delta_theta = 360 - theta_prev + theta_now;
  171. }else if (state_prev == STATE_2) {
  172. _hall.direction = NEGATIVE;
  173. delta_theta = theta_prev - theta_now;
  174. }
  175. break;
  176. case STATE_4:
  177. if (state_prev == STATE_6) {
  178. _hall.direction = POSITIVE;
  179. delta_theta = theta_now - theta_prev;
  180. }else if (state_prev == STATE_5) {
  181. _hall.direction = NEGATIVE;
  182. delta_theta = theta_prev - theta_now;
  183. }
  184. break;
  185. case STATE_5:
  186. if (state_prev == STATE_4) {
  187. _hall.direction = POSITIVE;
  188. delta_theta = theta_now - theta_prev;
  189. }else if (state_prev == STATE_1) {
  190. _hall.direction = NEGATIVE;
  191. delta_theta = theta_prev - theta_now;
  192. }
  193. break;
  194. case STATE_6:
  195. if (state_prev == STATE_2) {
  196. _hall.direction = POSITIVE;
  197. delta_theta = theta_now - theta_prev;
  198. }else if (state_prev == STATE_4) {
  199. _hall.direction = NEGATIVE;
  200. delta_theta = theta_prev - theta_now;
  201. }
  202. break;
  203. default:
  204. break;
  205. }
  206. if (delta_theta == 360.0f) { //no vilid hall
  207. return;
  208. }
  209. float delta_time = tick_2_s(delta_ticks(_hall.ticks));
  210. if (delta_time == 0.0f) { //may be errors ???
  211. return;
  212. }
  213. delta_theta = 60.0f;
  214. _hall_put_sample(delta_theta, delta_ticks(_hall.ticks));
  215. if (!h_samples.full) {
  216. _hall.degree_per_s = delta_theta / delta_time;
  217. }else {
  218. _hall.degree_per_s = _hall_avg_speed();
  219. }
  220. _hall.ticks = task_ticks_abs();
  221. _hall.theta += delta_theta;
  222. _hall.state = state_now;
  223. _hall.e_rpm = _hall.degree_per_s / 360.0f * 60.0f;
  224. //printf("speed :%.4f - %.4f - %.4f - %d\n", _hall.degree_per_s, delta_theta, delta_time, (int)_hall.e_rpm);
  225. }