encoder.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_off2.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.1f;
  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;
  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. #if 1
  163. _encoder_caliberate_init();
  164. float delta = (g_encoder.abi_angle - angle);
  165. if (delta > 200) {
  166. delta = delta - 360;
  167. }
  168. if (delta < -200) {
  169. delta = delta + 360;
  170. }
  171. if (g_encoder.direction == POSITIVE) {
  172. if ((g_encoder.encoder_off_count[g_encoder.last_cnt] & 0xF) <= MIN_OFF_COUNT) {
  173. g_encoder.encoder_off_map[g_encoder.last_cnt] += (s16)(delta*100.0f);
  174. g_encoder.encoder_off_count[g_encoder.last_cnt] += 0x01;
  175. }
  176. }else {
  177. if (((g_encoder.encoder_off_count[g_encoder.last_cnt] >> 4) & 0xF) <= MIN_OFF_COUNT) {
  178. g_encoder.encoder_off_map[g_encoder.last_cnt] += (s16)(delta*100.0f);
  179. g_encoder.encoder_off_count[g_encoder.last_cnt] += 0x10;
  180. }
  181. }
  182. #else
  183. plot_2data16((s16)angle, (s16)g_encoder.abi_angle);
  184. #endif
  185. }
  186. static void _detect_off_finished(void);
  187. bool encoder_detect_finish(void) {
  188. u8 off_count = 0;
  189. for (int i = 0; i < 1024; i++) {
  190. if (g_encoder.direction == POSITIVE) {
  191. off_count = g_encoder.encoder_off_count[i] & 0xF;
  192. }else {
  193. off_count = (g_encoder.encoder_off_count[i] >> 4)& 0xF;
  194. }
  195. if (off_count <= MIN_OFF_COUNT) {
  196. return false;
  197. }
  198. }
  199. if (g_encoder.direction == NEGATIVE) {
  200. _detect_off_finished();//output data to PC tools, and use Matlab do FIR filter
  201. _encoder_caliberate_deinit();
  202. }
  203. return true;
  204. }
  205. static void _detect_off_finished(void) {
  206. for (int i = 0; i < 1024; i++) {
  207. float angle_off = g_encoder.encoder_off_map[i] / (((g_encoder.encoder_off_count[i] >> 4)&0xF) + (g_encoder.encoder_off_count[i]&0xF));
  208. plot_1data16((s16)angle_off);
  209. delay_ms(10);
  210. wdog_reload();
  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(10);
  216. wdog_reload();
  217. }
  218. for (int i = 0; i < 1024; i++) {
  219. float angle_off = g_encoder.encoder_off_map[i] / (((g_encoder.encoder_off_count[i] >> 4)&0xF) + (g_encoder.encoder_off_count[i]&0xF));
  220. plot_1data16((s16)angle_off);
  221. delay_ms(10);
  222. wdog_reload();
  223. }
  224. }
  225. float encoder_get_vel_count(void) {
  226. return g_encoder.est_vel_counts;
  227. }
  228. float encoder_zero_phase_detect(void) {
  229. float phase = g_encoder.pwm_angle;
  230. float total_ph = phase;
  231. int count = 0;
  232. for(; count < 10; count++) {
  233. delay_ms(ENC_PWM_Min_P * 1000 + 2); //wait time for pwm
  234. if ABS(phase - g_encoder.pwm_angle > 2.0f) {
  235. return INVALID_ANGLE;
  236. }
  237. phase = g_encoder.pwm_angle;
  238. total_ph += phase;
  239. }
  240. sys_debug("offset = %f\n", (total_ph/(float)count));
  241. return (total_ph/(float)count);
  242. }
  243. static void encoder_sync_pwm_abs(void) {
  244. ENC_COUNT = g_encoder.pwm_count;
  245. g_encoder.last_cnt = g_encoder.pwm_count;
  246. g_encoder.est_pll.observer = (float)g_encoder.pwm_count;
  247. g_encoder.abi_angle = g_encoder.pwm_angle;
  248. g_encoder.b_index_found = true;
  249. }
  250. /*I 信号的中断处理,一圈一个中断*/
  251. void ENC_ABI_IRQHandler(void) {
  252. g_encoder.b_index_cnt = ENC_COUNT;
  253. if (!g_encoder.b_index_found){
  254. encoder_sync_pwm_abs();
  255. }
  256. }
  257. /* 编码器AB信号读书溢出处理 */
  258. void ENC_TIMER_Overflow(void) {
  259. //g_encoder.b_timer_ov = true;
  260. }
  261. /*PWM 信号捕获一个周期的处理 */
  262. static int pwm_count = 0;
  263. static int pwm_check_count = 0;
  264. void ENC_PWM_Duty_Handler(float t, float d) {
  265. float duty = ENC_Duty(d, t);
  266. if (duty < ENC_PWM_Min_P || duty > 1.0f) {
  267. return;
  268. }
  269. float Nr = ENC_Duty_2_Pluse_Nr(duty);
  270. if (Nr < 0) {
  271. return;
  272. }
  273. u32 n_nr = (u32)Nr;
  274. if (Nr - n_nr >= 0.5f) {
  275. g_encoder.pwm_count = n_nr + 1;
  276. }else {
  277. g_encoder.pwm_count = n_nr;
  278. }
  279. g_encoder.pwm_angle = ENC_Pluse_Nr_2_angle(Nr) * g_encoder.motor_poles + g_encoder.enc_offset;
  280. rand_angle(g_encoder.pwm_angle);
  281. if (!g_encoder.b_index_found && pwm_count++ >= 10) {
  282. encoder_sync_pwm_abs();
  283. }
  284. pwm_check_count ++;
  285. }
  286. static u32 _check_time = 0;
  287. bool ENC_Check_error(void) {
  288. bool error = false;
  289. if (get_delta_ms(_check_time) > 1000) {
  290. if (pwm_check_count == 0) {
  291. error = true;
  292. }
  293. pwm_check_count = 0;
  294. _check_time = get_tick_ms();
  295. }
  296. return error;
  297. }
  298. float encoder_get_pwm_angle(void) {
  299. return g_encoder.pwm_angle;
  300. }