encoder.c 4.1 KB

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