encoder.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #define ANGLE_OFFSET (-50.0f)//133.0f
  11. /* 磁编码器使用一对极的磁铁,所以编码器获取的角度和机械角度相同需要转为电角度*/
  12. #define DIR_ADJUGE_MAX_CNT 10
  13. #define PLL_BANDWIDTH 200
  14. encoder_t g_encoder;
  15. u8 encoder_off_count[1024] = {0};
  16. s16 encoder_off_map[1024] = {0};
  17. s16 encoder_off_r_map[1024] = {0};
  18. u8 encoder_off_r_count[1024] = {0};
  19. bool encoder_off_finished = false;
  20. s16 encoder_off_comp = 940.0f;
  21. float encoder_off_mul = 1.0f;
  22. int encoder_off_cn_add = 542;
  23. float encoder_off_angle_add = 0.0f;
  24. static __INLINE void encoder_pll_update_gain(void) {
  25. if (g_encoder.pll_bandwidth_shadow != g_encoder.pll_bandwidth) {
  26. g_encoder.pll_bandwidth = g_encoder.pll_bandwidth_shadow;
  27. g_encoder.est_pll.kp = 2.0f * g_encoder.pll_bandwidth;
  28. g_encoder.est_pll.ki = 0.25f * g_encoder.est_pll.kp * g_encoder.est_pll.kp;
  29. }
  30. }
  31. static void _init_pll(void) {
  32. g_encoder.est_pll.DT = FOC_CTRL_US;
  33. g_encoder.est_pll.max_wp = g_encoder.cpr;
  34. g_encoder.pll_bandwidth = 0;
  35. g_encoder.pll_bandwidth_shadow = nv_get_motor_params()->est_pll_band;
  36. encoder_pll_update_gain();
  37. PLL_Reset(&g_encoder.est_pll);
  38. }
  39. void encoder_init(void) {
  40. for (int i = i; i < 1024; i++) {
  41. encoder_off_count[i] = 0;
  42. encoder_off_map[i] = 0;
  43. encoder_off_r_map[i] = 0;
  44. encoder_off_r_count[i] = 0;
  45. }
  46. encoder_init_clear(POSITIVE);
  47. enc_intf_init(ENC_MAX_RES);
  48. }
  49. void encoder_set_direction(s8 direction) {
  50. encoder_init_clear(direction);
  51. }
  52. void encoder_set_bandwidth(float bandwidth) {
  53. g_encoder.pll_bandwidth_shadow = bandwidth;
  54. }
  55. void encoder_init_clear(s8 diretcion) {
  56. _init_pll();
  57. g_encoder.cpr = ENC_MAX_RES;
  58. g_encoder.enc_offset = nv_get_motor_params()->encoder_offset;
  59. g_encoder.motor_poles = nv_get_motor_params()->poles;
  60. g_encoder.b_index_found = false;
  61. g_encoder.direction = diretcion;
  62. g_encoder.abi_angle = 0.0f;
  63. g_encoder.pwm_angle = 0.0f;
  64. g_encoder.est_angle_counts = 0;
  65. g_encoder.est_vel_counts = 0;
  66. g_encoder.interpolation = 0.0f;
  67. }
  68. void encoder_lock_position(bool enable) {
  69. if (g_encoder.b_lock_pos != enable) {
  70. g_encoder.b_lock_pos = enable;
  71. if (enable) {
  72. encoder_set_bandwidth(nv_get_motor_params()->pos_lock_pll_band);
  73. }else {
  74. encoder_set_bandwidth(nv_get_motor_params()->est_pll_band);
  75. }
  76. }
  77. }
  78. static __INLINE float _pll_over_comp(void) {
  79. u8 dir = ENC_DIR_DOWN;
  80. #ifdef ENCODER_CC_INVERT
  81. dir = ENC_DIR_UP;
  82. #endif
  83. if(ENC_Direction() == dir){
  84. return -((float)g_encoder.cpr);
  85. }
  86. return (float)g_encoder.cpr;
  87. }
  88. static __INLINE void encoder_run_pll(float cnt) {
  89. float pll_comp = 0.0f;
  90. if (g_encoder.b_timer_ov) {
  91. pll_comp = _pll_over_comp();
  92. g_encoder.b_timer_ov = false;
  93. }
  94. encoder_pll_update_gain();
  95. g_encoder.est_vel_counts = PLL_run(&g_encoder.est_pll, cnt, pll_comp);
  96. g_encoder.est_angle_counts = g_encoder.est_pll.observer;
  97. }
  98. static __INLINE u32 _abi_count(void) {
  99. #ifdef ENCODER_CC_INVERT
  100. return (g_encoder.cpr - ENC_COUNT);
  101. #else
  102. return ENC_COUNT;
  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. encoder_run_pll((float)(cnt));
  117. bool snap_to_zero_vel = false;
  118. if (ABS(g_encoder.est_pll.out) < 0.5f * g_encoder.est_pll.DT * g_encoder.est_pll.ki) {
  119. g_encoder.est_vel_counts = g_encoder.est_pll.out = 0.0f; // align delta-sigma on zero to prevent jitter
  120. snap_to_zero_vel = true;
  121. }
  122. if (snap_to_zero_vel) {
  123. g_encoder.interpolation = 0.5f;
  124. }else {
  125. if (cnt == g_encoder.last_cnt) {
  126. g_encoder.interpolation += g_encoder.est_vel_counts * FOC_CTRL_US;
  127. if (g_encoder.interpolation > ENC_MAX_interpolation) {
  128. g_encoder.interpolation = ENC_MAX_interpolation;
  129. }else if (g_encoder.interpolation < -ENC_MAX_interpolation) {
  130. g_encoder.interpolation = -ENC_MAX_interpolation;
  131. }
  132. }else {
  133. g_encoder.interpolation = 0.0f;
  134. }
  135. }
  136. g_encoder.abi_angle = ENC_Pluse_Nr_2_angle((float)cnt + g_encoder.interpolation) * g_encoder.motor_poles + g_encoder.enc_offset;
  137. if (encoder_off_finished) {
  138. int cnt_off = (cnt + FIR_PHASE_SHIFT) % g_encoder.cpr;
  139. g_encoder.abi_angle -= ((_encoder_off_map[cnt_off])/100.0f);
  140. }
  141. rand_angle(g_encoder.abi_angle);
  142. g_encoder.last_cnt = cnt;
  143. g_encoder.last_us = timer_count32_get();
  144. return g_encoder.abi_angle;
  145. }
  146. float encoder_get_speed(void) {
  147. return (g_encoder.est_vel_counts/g_encoder.cpr) * 60.0f * g_encoder.motor_poles;
  148. }
  149. extern int jtag_plot;
  150. void encoder_detect_offset(float angle, bool r){
  151. float delta = (g_encoder.abi_angle - angle);
  152. if (delta > 200) {
  153. delta = delta - 360;
  154. }
  155. if (delta < -200) {
  156. delta = delta + 360;
  157. }
  158. int last_cn = (g_encoder.last_cnt + 0) % 1024;
  159. if (!r) {
  160. encoder_off_map[last_cn] = (s16)(delta*100.0f);
  161. encoder_off_count[last_cn] ++;
  162. }else {
  163. encoder_off_r_map[last_cn] = (s16)(delta*100.0f);
  164. encoder_off_r_count[last_cn] ++;
  165. }
  166. if (jtag_plot==1) {
  167. plot_1data16((s16)(delta*100.0f));
  168. }
  169. encoder_off_finished = false;
  170. }
  171. float encoder_detect_finish(bool r) {
  172. for (int i = 0; i < 1024; i++) {
  173. if (!r) {
  174. if (encoder_off_count[i] <= 2) {
  175. return false;
  176. }
  177. }else {
  178. if (encoder_off_r_count[i] <= 2) {
  179. return false;
  180. }
  181. }
  182. }
  183. encoder_off_finished = true;
  184. return encoder_off_finished;
  185. }
  186. void encoder_detect_off_finished(void) {
  187. for (int i = 0; i < 100; i++) {
  188. plot_3data16(0,0,1000);
  189. delay_ms(2);
  190. }
  191. for (int i = 0; i < 1024; i++) {
  192. float angle_off = (encoder_off_map[i] + encoder_off_r_map[i]) / 2;
  193. plot_3data16(0, 0 , angle_off);
  194. delay_ms(2);
  195. }
  196. for (int i = 0; i < 1024; i++) {
  197. float angle_off = (encoder_off_map[i] + encoder_off_r_map[i]) / 2;
  198. plot_3data16(0, 0 , angle_off);
  199. delay_ms(2);
  200. }
  201. for (int i = 0; i < 1024; i++) {
  202. float angle_off = (encoder_off_map[i] + encoder_off_r_map[i]) / 2;
  203. plot_3data16(0, 0 , angle_off);
  204. delay_ms(2);
  205. }
  206. }
  207. float encoder_get_vel_count(void) {
  208. return g_encoder.est_vel_counts;
  209. }
  210. static void encoder_sync_pwm_abs(void) {
  211. ENC_COUNT = g_encoder.pwm_count;
  212. g_encoder.last_cnt = g_encoder.pwm_count;
  213. g_encoder.est_pll.observer = (float)g_encoder.pwm_count;
  214. g_encoder.abi_angle = g_encoder.pwm_angle;
  215. g_encoder.b_index_found = true;
  216. }
  217. /*I 信号的中断处理,一圈一个中断*/
  218. void ENC_ABI_IRQHandler(void) {
  219. g_encoder.b_index_cnt = ENC_COUNT;
  220. if (!g_encoder.b_index_found){
  221. encoder_sync_pwm_abs();
  222. }
  223. }
  224. /* 编码器AB信号读书溢出处理 */
  225. void ENC_TIMER_Overflow(void) {
  226. //g_encoder.b_timer_ov = true;
  227. }
  228. /*PWM 信号捕获一个周期的处理 */
  229. static int pwm_count = 0;
  230. void ENC_PWM_Duty_Handler(float t, float d) {
  231. float duty = ENC_Duty(d, t);
  232. if (duty < ENC_PWM_Min_P || duty > 1.0f) {
  233. return;
  234. }
  235. float Nr = ENC_Duty_2_Pluse_Nr(duty);
  236. if (Nr < 0) {
  237. return;
  238. }
  239. u32 n_nr = (u32)Nr;
  240. if (Nr - n_nr >= 0.5f) {
  241. g_encoder.pwm_count = n_nr + 1;
  242. }else {
  243. g_encoder.pwm_count = n_nr;
  244. }
  245. g_encoder.pwm_angle = ENC_Pluse_Nr_2_angle(Nr) * g_encoder.motor_poles + g_encoder.enc_offset;
  246. rand_angle(g_encoder.pwm_angle);
  247. if (!g_encoder.b_index_found && pwm_count++ >= 10) {
  248. encoder_sync_pwm_abs();
  249. }
  250. }
  251. float encoder_get_pwm_angle(void) {
  252. return g_encoder.pwm_angle;
  253. }