hall_sensor.c 4.9 KB

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