encoder.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 void encoder_run_pll(float cnt) {
  63. float pll_comp = 0.0f;
  64. if (g_encoder.b_timer_ov) {
  65. if(ENC_Direction() == ENC_DIR_DOWN){
  66. pll_comp = -((float)g_encoder.cpr);
  67. }else {
  68. pll_comp = g_encoder.cpr;
  69. }
  70. g_encoder.b_timer_ov = false;
  71. }
  72. encoder_pll_update_gain();
  73. g_encoder.est_vel_counts = PLL_run(&g_encoder.est_pll, cnt, pll_comp);
  74. g_encoder.est_angle_counts = g_encoder.est_pll.observer;
  75. }
  76. float encoder_get_theta(void) {
  77. if (!g_encoder.b_index_found) {
  78. return g_encoder.pwm_angle;
  79. }
  80. u32 cnt = ENC_COUNT;
  81. __NOP();__NOP();__NOP();__NOP();
  82. if (ENC_OverFlow()) {
  83. cnt = ENC_COUNT;
  84. g_encoder.b_timer_ov = true;
  85. ENC_ClearUpFlags();
  86. }
  87. if (cnt == g_encoder.last_cnt) {
  88. g_encoder.interpolation += g_encoder.est_vel_counts * FOC_CTRL_US;
  89. if (g_encoder.interpolation > 1.0f) {
  90. g_encoder.interpolation = 1.0f;
  91. }else if (g_encoder.interpolation < -1.0f) {
  92. g_encoder.interpolation = -1.0f;
  93. }
  94. }else {
  95. g_encoder.interpolation = 0.0f;
  96. }
  97. g_encoder.abi_angle = ENC_Pluse_Nr_2_angle((float)cnt + g_encoder.interpolation) * g_encoder.motor_poles + g_encoder.enc_offset;
  98. rand_angle(g_encoder.abi_angle);
  99. encoder_run_pll((float)(cnt));
  100. g_encoder.last_cnt = cnt;
  101. g_encoder.last_us = timer_count32_get();
  102. return g_encoder.abi_angle;
  103. }
  104. float encoder_get_speed(void) {
  105. return (g_encoder.est_vel_counts/g_encoder.cpr) * 60.0f * g_encoder.motor_poles;
  106. }
  107. void encoder_detect_offset(float angle){
  108. //plot_3data16(angle, g_encoder.pwm_angle, g_encoder.abi_angle);
  109. }
  110. float encoder_get_vel_count(void) {
  111. return g_encoder.est_vel_counts;
  112. }
  113. static void encoder_sync_pwm_abs(void) {
  114. ENC_COUNT = g_encoder.pwm_count;
  115. g_encoder.last_cnt = g_encoder.pwm_count;
  116. g_encoder.est_pll.observer = (float)g_encoder.pwm_count;
  117. g_encoder.abi_angle = g_encoder.pwm_angle;
  118. g_encoder.b_index_found = true;
  119. }
  120. /*I 信号的中断处理,一圈一个中断*/
  121. void ENC_ABI_IRQHandler(void) {
  122. g_encoder.b_index_cnt = ENC_COUNT;
  123. if (!g_encoder.b_index_found){
  124. encoder_sync_pwm_abs();
  125. }
  126. }
  127. /* 编码器AB信号读书溢出处理 */
  128. void ENC_TIMER_Overflow(void) {
  129. //g_encoder.b_timer_ov = true;
  130. }
  131. /*PWM 信号捕获一个周期的处理 */
  132. void ENC_PWM_Duty_Handler(float t, float d) {
  133. float duty = d/t;
  134. if (duty < ENC_PWM_Min_P || duty > 1.0f) {
  135. return;
  136. }
  137. float Nr = ENC_Duty_2_Pluse_Nr(duty);
  138. if (Nr < 0) {
  139. return;
  140. }
  141. g_encoder.pwm_count = (u32)Nr;
  142. g_encoder.pwm_angle = ENC_Pluse_Nr_2_angle(Nr) * g_encoder.motor_poles + g_encoder.enc_offset;
  143. rand_angle(g_encoder.pwm_angle);
  144. if (!g_encoder.b_index_found) {
  145. encoder_sync_pwm_abs();
  146. }
  147. }
  148. float encoder_get_pwm_angle(void) {
  149. return g_encoder.pwm_angle;
  150. }