encoder.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "bsp/bsp.h"
  2. #include "bsp/enc_intf.h"
  3. #include "bsp/timer_count32.h"
  4. #include "foc/motor/encoder.h"
  5. #include "foc/motor/motor_param.h"
  6. #include "libs/logger.h"
  7. #include "app/nv_storage.h"
  8. #include "math/fast_math.h"
  9. #include "encoder_off.h"
  10. /* 磁编码器使用一对极的磁铁,所以编码器获取的角度和机械角度相同需要转为电角度*/
  11. encoder_t g_encoder;
  12. static __INLINE void encoder_pll_update_gain(void) {
  13. if (g_encoder.pll_bandwidth_shadow != g_encoder.pll_bandwidth) {
  14. g_encoder.pll_bandwidth = g_encoder.pll_bandwidth_shadow;
  15. g_encoder.est_pll.kp = 2.0f * g_encoder.pll_bandwidth;
  16. g_encoder.est_pll.ki = 0.25f * g_encoder.est_pll.kp * g_encoder.est_pll.kp;
  17. }
  18. }
  19. static void _init_pll(void) {
  20. g_encoder.est_pll.DT = FOC_CTRL_US;
  21. g_encoder.est_pll.max_wp = g_encoder.cpr;
  22. g_encoder.pll_bandwidth = 0;
  23. g_encoder.pll_bandwidth_shadow = nv_get_motor_params()->est_pll_band;
  24. encoder_pll_update_gain();
  25. PLL_Reset(&g_encoder.est_pll);
  26. }
  27. void encoder_init(void) {
  28. encoder_init_clear(POSITIVE);
  29. enc_intf_init(ENC_MAX_RES);
  30. }
  31. void encoder_set_direction(s8 direction) {
  32. g_encoder.direction = direction;
  33. }
  34. void encoder_set_bandwidth(float bandwidth) {
  35. g_encoder.pll_bandwidth_shadow = bandwidth;
  36. }
  37. void encoder_init_clear(s8 diretcion) {
  38. _init_pll();
  39. g_encoder.cpr = ENC_MAX_RES;
  40. g_encoder.enc_offset = nv_get_motor_params()->offset;
  41. g_encoder.motor_poles = nv_get_motor_params()->poles;
  42. g_encoder.b_index_found = false;
  43. g_encoder.direction = diretcion;
  44. g_encoder.abi_angle = 0.0f;
  45. g_encoder.pwm_angle = 0.0f;
  46. g_encoder.est_angle_counts = 0;
  47. g_encoder.est_vel_counts = 0;
  48. g_encoder.interpolation = 0.0f;
  49. }
  50. void encoder_lock_position(bool enable) {
  51. if (g_encoder.b_lock_pos != enable) {
  52. g_encoder.b_lock_pos = enable;
  53. if (enable) {
  54. encoder_set_bandwidth(nv_get_motor_params()->pos_lock_pll_band);
  55. }else {
  56. encoder_set_bandwidth(nv_get_motor_params()->est_pll_band);
  57. }
  58. }
  59. }
  60. static __INLINE float _pll_over_comp(void) {
  61. u8 dir = ENC_DIR_DOWN;
  62. #ifdef ENCODER_CC_INVERT
  63. dir = ENC_DIR_UP;
  64. #endif
  65. if(ENC_Direction() == dir){
  66. return -((float)g_encoder.cpr);
  67. }
  68. return (float)g_encoder.cpr;
  69. }
  70. static __INLINE bool encoder_run_pll(float cnt) {
  71. float pll_comp = 0.0f;
  72. if (g_encoder.b_timer_ov) {
  73. pll_comp = _pll_over_comp();
  74. g_encoder.b_timer_ov = false;
  75. }
  76. encoder_pll_update_gain();
  77. g_encoder.est_vel_counts = PLL_run(&g_encoder.est_pll, cnt, pll_comp);
  78. g_encoder.est_angle_counts = g_encoder.est_pll.observer;
  79. bool snap_to_zero_vel = false;
  80. if (ABS(g_encoder.est_pll.out) < 0.5f * g_encoder.est_pll.DT * g_encoder.est_pll.ki) {
  81. g_encoder.est_vel_counts = g_encoder.est_pll.out = 0.0f; // align delta-sigma on zero to prevent jitter
  82. snap_to_zero_vel = true;
  83. }
  84. return snap_to_zero_vel;
  85. }
  86. static __INLINE u32 _abi_count(void) {
  87. #ifdef ENCODER_CC_INVERT
  88. return (g_encoder.cpr - ENC_COUNT);
  89. #else
  90. return ENC_COUNT;
  91. #endif
  92. }
  93. /* 偏心补偿 */
  94. static __INLINE float _eccentricity_compensation(int cnt) {
  95. #ifdef FIR_PHASE_SHIFT
  96. int cnt_off = (cnt + FIR_PHASE_SHIFT) % g_encoder.cpr;
  97. if (g_encoder.encoder_off_map != NULL) { //do offset calibrate, can not do encentricity compensation
  98. return 0.0f;
  99. }
  100. return -(S16Q10toF(_encoder_off_map[cnt_off]));
  101. #else
  102. return 0.0f;
  103. #endif
  104. }
  105. float encoder_get_theta(void) {
  106. if (!g_encoder.b_index_found) {
  107. return g_encoder.pwm_angle;
  108. }
  109. u32 cnt = _abi_count();
  110. __NOP();__NOP();__NOP();__NOP();
  111. if (ENC_OverFlow()) {
  112. cnt = _abi_count();
  113. g_encoder.b_timer_ov = true;
  114. ENC_ClearUpFlags();
  115. }
  116. bool snap_to_zero_vel = encoder_run_pll((float)(cnt));
  117. if (snap_to_zero_vel) {
  118. g_encoder.interpolation = 0.5f;
  119. }else {
  120. if (cnt == g_encoder.last_cnt) {
  121. g_encoder.interpolation += g_encoder.est_vel_counts * FOC_CTRL_US;
  122. if (g_encoder.interpolation > ENC_MAX_interpolation) {
  123. g_encoder.interpolation = ENC_MAX_interpolation;
  124. }else if (g_encoder.interpolation < -ENC_MAX_interpolation) {
  125. g_encoder.interpolation = -ENC_MAX_interpolation;
  126. }
  127. }else {
  128. g_encoder.interpolation = 0.0f;
  129. }
  130. }
  131. g_encoder.abi_angle = ENC_Pluse_Nr_2_angle((float)cnt + g_encoder.interpolation) * g_encoder.motor_poles + g_encoder.enc_offset;
  132. g_encoder.abi_angle += _eccentricity_compensation(cnt);
  133. rand_angle(g_encoder.abi_angle);
  134. g_encoder.last_cnt = cnt;
  135. g_encoder.last_us = timer_count32_get();
  136. return g_encoder.abi_angle;
  137. }
  138. float encoder_get_speed(void) {
  139. return (g_encoder.est_vel_counts/g_encoder.cpr) * 60.0f * g_encoder.motor_poles;
  140. }
  141. void _encoder_caliberate_init(void) {
  142. if (g_encoder.encoder_off_map != NULL) {
  143. return;
  144. }
  145. g_encoder.encoder_off_map = (s16 *)os_alloc(g_encoder.cpr * sizeof(s16));
  146. g_encoder.encoder_off_count = (u8 *)os_alloc(g_encoder.cpr);
  147. for (int i = 0; i < g_encoder.cpr; i++) {
  148. g_encoder.encoder_off_map[i] = 0;
  149. g_encoder.encoder_off_count[i] = 0;
  150. }
  151. }
  152. void _encoder_caliberate_deinit(void) {
  153. if (g_encoder.encoder_off_map != NULL) {
  154. os_free(g_encoder.encoder_off_map);
  155. os_free(g_encoder.encoder_off_count);
  156. }
  157. g_encoder.encoder_off_map = NULL;
  158. g_encoder.encoder_off_count = NULL;
  159. }
  160. #define MIN_OFF_COUNT 2
  161. void encoder_detect_offset(float angle){
  162. _encoder_caliberate_init();
  163. float delta = (g_encoder.abi_angle - angle);
  164. if (delta > 200) {
  165. delta = delta - 360;
  166. }
  167. if (delta < -200) {
  168. delta = delta + 360;
  169. }
  170. if (g_encoder.direction == POSITIVE) {
  171. if ((g_encoder.encoder_off_count[g_encoder.last_cnt] & 0xF) <= MIN_OFF_COUNT) {
  172. g_encoder.encoder_off_map[g_encoder.last_cnt] += (s16)(delta*100.0f);
  173. g_encoder.encoder_off_count[g_encoder.last_cnt] += 0x01;
  174. }
  175. }else {
  176. if (((g_encoder.encoder_off_count[g_encoder.last_cnt] >> 4) & 0xF) <= MIN_OFF_COUNT) {
  177. g_encoder.encoder_off_map[g_encoder.last_cnt] += (s16)(delta*100.0f);
  178. g_encoder.encoder_off_count[g_encoder.last_cnt] += 0x10;
  179. }
  180. }
  181. }
  182. static void _detect_off_finished(void);
  183. bool encoder_detect_finish(void) {
  184. u8 off_count = 0;
  185. for (int i = 0; i < 1024; i++) {
  186. if (g_encoder.direction == POSITIVE) {
  187. off_count = g_encoder.encoder_off_count[i] & 0xF;
  188. }else {
  189. off_count = (g_encoder.encoder_off_count[i] >> 4)& 0xF;
  190. }
  191. if (off_count <= MIN_OFF_COUNT) {
  192. return false;
  193. }
  194. }
  195. if (g_encoder.direction == NEGATIVE) {
  196. _detect_off_finished();//output data to PC tools, and use Matlab do FIR filter
  197. _encoder_caliberate_deinit();
  198. }
  199. return true;
  200. }
  201. static void _detect_off_finished(void) {
  202. for (int i = 0; i < 1024; i++) {
  203. float angle_off = g_encoder.encoder_off_map[i] / (((g_encoder.encoder_off_count[i] >> 4)&0xF) + (g_encoder.encoder_off_count[i]&0xF));
  204. plot_1data16((s16)angle_off);
  205. delay_ms(2);
  206. }
  207. for (int i = 0; i < 1024; i++) {
  208. float angle_off = g_encoder.encoder_off_map[i] / (((g_encoder.encoder_off_count[i] >> 4)&0xF) + (g_encoder.encoder_off_count[i]&0xF));
  209. plot_1data16((s16)angle_off);
  210. delay_ms(2);
  211. }
  212. for (int i = 0; i < 1024; i++) {
  213. float angle_off = g_encoder.encoder_off_map[i] / (((g_encoder.encoder_off_count[i] >> 4)&0xF) + (g_encoder.encoder_off_count[i]&0xF));
  214. plot_1data16((s16)angle_off);
  215. delay_ms(2);
  216. }
  217. }
  218. float encoder_get_vel_count(void) {
  219. return g_encoder.est_vel_counts;
  220. }
  221. float encoder_zero_phase_detect(void) {
  222. float phase = g_encoder.pwm_angle;
  223. float total_ph = phase;
  224. int count = 0;
  225. for(; count < 10; count++) {
  226. delay_ms(ENC_PWM_Min_P * 1000 + 2); //wait time for pwm
  227. if ABS(phase - g_encoder.pwm_angle > 2.0f) {
  228. return INVALID_ANGLE;
  229. }
  230. phase = g_encoder.pwm_angle;
  231. total_ph += phase;
  232. }
  233. return (total_ph/(float)count);
  234. }
  235. static void encoder_sync_pwm_abs(void) {
  236. ENC_COUNT = g_encoder.pwm_count;
  237. g_encoder.last_cnt = g_encoder.pwm_count;
  238. g_encoder.est_pll.observer = (float)g_encoder.pwm_count;
  239. g_encoder.abi_angle = g_encoder.pwm_angle;
  240. g_encoder.b_index_found = true;
  241. }
  242. /*I 信号的中断处理,一圈一个中断*/
  243. void ENC_ABI_IRQHandler(void) {
  244. g_encoder.b_index_cnt = ENC_COUNT;
  245. if (!g_encoder.b_index_found){
  246. encoder_sync_pwm_abs();
  247. }
  248. }
  249. /* 编码器AB信号读书溢出处理 */
  250. void ENC_TIMER_Overflow(void) {
  251. //g_encoder.b_timer_ov = true;
  252. }
  253. /*PWM 信号捕获一个周期的处理 */
  254. static int pwm_count = 0;
  255. void ENC_PWM_Duty_Handler(float t, float d) {
  256. float duty = ENC_Duty(d, t);
  257. if (duty < ENC_PWM_Min_P || duty > 1.0f) {
  258. return;
  259. }
  260. float Nr = ENC_Duty_2_Pluse_Nr(duty);
  261. if (Nr < 0) {
  262. return;
  263. }
  264. u32 n_nr = (u32)Nr;
  265. if (Nr - n_nr >= 0.5f) {
  266. g_encoder.pwm_count = n_nr + 1;
  267. }else {
  268. g_encoder.pwm_count = n_nr;
  269. }
  270. g_encoder.pwm_angle = ENC_Pluse_Nr_2_angle(Nr) * g_encoder.motor_poles + g_encoder.enc_offset;
  271. rand_angle(g_encoder.pwm_angle);
  272. if (!g_encoder.b_index_found && pwm_count++ >= 10) {
  273. encoder_sync_pwm_abs();
  274. }
  275. }
  276. float encoder_get_pwm_angle(void) {
  277. return g_encoder.pwm_angle;
  278. }