encoder.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #define ANGLE_OFFSET (-50.0f)//133.0f
  9. /* 磁编码器使用一对极的磁铁,所以编码器获取的角度和机械角度相同需要转为电角度*/
  10. #define DIR_ADJUGE_MAX_CNT 10
  11. #define PLL_BANDWIDTH 200
  12. static encoder_t g_encoder;
  13. #define USE_PWM_ONLY
  14. static __INLINE void encoder_pll_update_gain(void) {
  15. if (g_encoder.pll_bandwidth_shadow != g_encoder.pll_bandwidth) {
  16. g_encoder.pll_bandwidth = g_encoder.pll_bandwidth_shadow;
  17. g_encoder.est_pll.kp = 2.0f * g_encoder.pll_bandwidth;
  18. g_encoder.est_pll.ki = 0.25f * g_encoder.est_pll.kp * g_encoder.est_pll.kp;
  19. }
  20. }
  21. static void _init_pll(void) {
  22. g_encoder.est_pll.DT = FOC_CTRL_US;
  23. g_encoder.est_pll.max_wp = g_encoder.cpr;
  24. g_encoder.pll_bandwidth = 0;
  25. g_encoder.pll_bandwidth_shadow = nv_get_motor_params()->est_pll_band;
  26. encoder_pll_update_gain();
  27. PLL_Reset(&g_encoder.est_pll);
  28. }
  29. void encoder_init(void) {
  30. encoder_init_clear(POSITIVE);
  31. enc_intf_init(ENC_MAX_RES);
  32. }
  33. void encoder_set_direction(s8 direction) {
  34. encoder_init_clear(direction);
  35. }
  36. void encoder_set_bandwidth(float bandwidth) {
  37. g_encoder.pll_bandwidth_shadow = bandwidth;
  38. }
  39. void encoder_init_clear(s8 diretcion) {
  40. _init_pll();
  41. g_encoder.cpr = ENC_MAX_RES;
  42. g_encoder.enc_offset = nv_get_motor_params()->encoder_offset;
  43. g_encoder.motor_poles = nv_get_motor_params()->poles;
  44. g_encoder.b_index_found = false;
  45. g_encoder.direction = diretcion;
  46. g_encoder.abi_angle = 0.0f;
  47. g_encoder.pwm_angle = 0.0f;
  48. g_encoder.est_angle_counts = 0;
  49. g_encoder.est_vel_counts = 0;
  50. g_encoder.interpolation = 0.0f;
  51. }
  52. void encoder_lock_position(bool enable) {
  53. if (g_encoder.b_lock_pos != enable) {
  54. g_encoder.b_lock_pos = enable;
  55. if (enable) {
  56. encoder_set_bandwidth(nv_get_motor_params()->pos_lock_pll_band);
  57. }else {
  58. encoder_set_bandwidth(nv_get_motor_params()->est_pll_band);
  59. }
  60. }
  61. }
  62. static __INLINE float _pll_over_comp(void) {
  63. u8 dir = ENC_DIR_DOWN;
  64. #ifdef ENCODER_CC_INVERT
  65. dir = ENC_DIR_UP;
  66. #endif
  67. if(ENC_Direction() == dir){
  68. return -((float)g_encoder.cpr);
  69. }
  70. return (float)g_encoder.cpr;
  71. }
  72. static __INLINE void encoder_run_pll(float cnt) {
  73. float pll_comp = 0.0f;
  74. if (g_encoder.b_timer_ov) {
  75. pll_comp = _pll_over_comp();
  76. g_encoder.b_timer_ov = false;
  77. }
  78. encoder_pll_update_gain();
  79. g_encoder.est_vel_counts = PLL_run(&g_encoder.est_pll, cnt, pll_comp);
  80. g_encoder.est_angle_counts = g_encoder.est_pll.observer;
  81. }
  82. static __INLINE u32 _abi_count(void) {
  83. #ifdef ENCODER_CC_INVERT
  84. return (g_encoder.cpr - ENC_COUNT);
  85. #else
  86. return ENC_COUNT;
  87. #endif
  88. }
  89. float encoder_get_theta(void) {
  90. if (!g_encoder.b_index_found) {
  91. return g_encoder.pwm_angle;
  92. }
  93. u32 cnt = _abi_count();
  94. __NOP();__NOP();__NOP();__NOP();
  95. if (ENC_OverFlow()) {
  96. cnt = _abi_count();
  97. g_encoder.b_timer_ov = true;
  98. ENC_ClearUpFlags();
  99. }
  100. if (cnt == g_encoder.last_cnt) {
  101. g_encoder.interpolation += g_encoder.est_vel_counts * FOC_CTRL_US;
  102. if (g_encoder.interpolation > 1.0f) {
  103. g_encoder.interpolation = 1.0f;
  104. }else if (g_encoder.interpolation < -1.0f) {
  105. g_encoder.interpolation = -1.0f;
  106. }
  107. }else {
  108. g_encoder.interpolation = 0.0f;
  109. }
  110. g_encoder.abi_angle = ENC_Pluse_Nr_2_angle((float)cnt + g_encoder.interpolation) * g_encoder.motor_poles + g_encoder.enc_offset;
  111. rand_angle(g_encoder.abi_angle);
  112. encoder_run_pll((float)(cnt));
  113. g_encoder.last_cnt = cnt;
  114. g_encoder.last_us = timer_count32_get();
  115. return g_encoder.abi_angle;
  116. }
  117. float encoder_get_speed(void) {
  118. return (g_encoder.est_vel_counts/g_encoder.cpr) * 60.0f * g_encoder.motor_poles;
  119. }
  120. void encoder_detect_offset(float angle){
  121. //plot_3data16(angle, g_encoder.pwm_angle, g_encoder.abi_angle);
  122. }
  123. float encoder_get_vel_count(void) {
  124. return g_encoder.est_vel_counts;
  125. }
  126. static void encoder_sync_pwm_abs(void) {
  127. ENC_COUNT = g_encoder.pwm_count;
  128. g_encoder.last_cnt = g_encoder.pwm_count;
  129. g_encoder.est_pll.observer = (float)g_encoder.pwm_count;
  130. g_encoder.abi_angle = g_encoder.pwm_angle;
  131. g_encoder.b_index_found = true;
  132. }
  133. /*I 信号的中断处理,一圈一个中断*/
  134. void ENC_ABI_IRQHandler(void) {
  135. g_encoder.b_index_cnt = ENC_COUNT;
  136. if (!g_encoder.b_index_found){
  137. encoder_sync_pwm_abs();
  138. }
  139. }
  140. /* 编码器AB信号读书溢出处理 */
  141. void ENC_TIMER_Overflow(void) {
  142. //g_encoder.b_timer_ov = true;
  143. }
  144. /*PWM 信号捕获一个周期的处理 */
  145. void ENC_PWM_Duty_Handler(float t, float d) {
  146. float duty = d/t;
  147. if (duty < ENC_PWM_Min_P || duty > 1.0f) {
  148. return;
  149. }
  150. float Nr = ENC_Duty_2_Pluse_Nr(duty);
  151. if (Nr < 0) {
  152. return;
  153. }
  154. g_encoder.pwm_count = (u32)Nr;
  155. g_encoder.pwm_angle = ENC_Pluse_Nr_2_angle(Nr) * g_encoder.motor_poles + g_encoder.enc_offset;
  156. rand_angle(g_encoder.pwm_angle);
  157. if (!g_encoder.b_index_found) {
  158. encoder_sync_pwm_abs();
  159. }
  160. }
  161. float encoder_get_pwm_angle(void) {
  162. return g_encoder.pwm_angle;
  163. }