encoder.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_off3.h"
  10. static void encoder_do_offset_calibrate(void) ;
  11. static void _detect_off_finished(void);
  12. /* 磁编码器使用一对极的磁铁,所以编码器获取的角度和机械角度相同需要转为电角度*/
  13. encoder_t g_encoder;
  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. g_encoder.direction = direction;
  35. g_encoder.cali_angle = INVALID_ANGLE;
  36. }
  37. void encoder_set_bandwidth(float bandwidth) {
  38. g_encoder.pll_bandwidth_shadow = bandwidth;
  39. }
  40. void encoder_init_clear(s8 diretcion) {
  41. _init_pll();
  42. g_encoder.cpr = ENC_MAX_RES;
  43. g_encoder.enc_offset = nv_get_motor_params()->offset;
  44. g_encoder.motor_poles = nv_get_motor_params()->poles;
  45. g_encoder.b_index_found = false;
  46. g_encoder.direction = diretcion;
  47. g_encoder.abi_angle = 0.0f;
  48. g_encoder.pwm_angle = 0.0f;
  49. g_encoder.est_angle_counts = 0;
  50. g_encoder.est_vel_counts = 0;
  51. g_encoder.interpolation = 0.0f;
  52. g_encoder.cali_angle = INVALID_ANGLE;
  53. }
  54. void encoder_lock_position(bool enable) {
  55. if (g_encoder.b_lock_pos != enable) {
  56. g_encoder.b_lock_pos = enable;
  57. if (enable) {
  58. encoder_set_bandwidth(nv_get_motor_params()->pos_lock_pll_band);
  59. }else {
  60. encoder_set_bandwidth(nv_get_motor_params()->est_pll_band);
  61. }
  62. }
  63. }
  64. static __INLINE float _pll_over_comp(void) {
  65. u8 dir = ENC_DIR_DOWN;
  66. #ifdef ENCODER_CC_INVERT
  67. dir = ENC_DIR_UP;
  68. #endif
  69. if(ENC_Direction() == dir){
  70. return -((float)g_encoder.cpr);
  71. }
  72. return (float)g_encoder.cpr;
  73. }
  74. static __INLINE bool encoder_run_pll(float cnt) {
  75. float pll_comp = 0.0f;
  76. if (g_encoder.b_timer_ov) {
  77. pll_comp = _pll_over_comp();
  78. g_encoder.b_timer_ov = false;
  79. }
  80. encoder_pll_update_gain();
  81. g_encoder.est_vel_counts = PLL_run(&g_encoder.est_pll, cnt, pll_comp);
  82. g_encoder.est_angle_counts = g_encoder.est_pll.observer;
  83. bool snap_to_zero_vel = false;
  84. if (ABS(g_encoder.est_pll.out) < 0.5f * g_encoder.est_pll.DT * g_encoder.est_pll.ki) {
  85. g_encoder.est_vel_counts = g_encoder.est_pll.out = 0.0f; // align delta-sigma on zero to prevent jitter
  86. snap_to_zero_vel = true;
  87. }
  88. return snap_to_zero_vel;
  89. }
  90. static __INLINE u32 _abi_count(void) {
  91. #ifdef ENCODER_CC_INVERT
  92. return (g_encoder.cpr - ENC_COUNT);
  93. #else
  94. return ENC_COUNT;
  95. #endif
  96. }
  97. /* 偏心补偿 */
  98. static __INLINE float _eccentricity_compensation(int cnt) {
  99. #ifdef FIR_PHASE_SHIFT
  100. int cnt_off = (cnt + FIR_PHASE_SHIFT);//g_encoder.cpr;
  101. if (g_encoder.encoder_off_map != NULL) { //do offset calibrate, can not do encentricity compensation
  102. return 0.0f;
  103. }
  104. return -(S16Q10toF(_encoder_off_map[cnt_off]));
  105. #else
  106. return 0.0f;
  107. #endif
  108. }
  109. float encoder_get_theta(void) {
  110. if (!g_encoder.b_index_found) {
  111. return g_encoder.pwm_angle;
  112. }
  113. u32 cnt = _abi_count();
  114. __NOP();__NOP();__NOP();__NOP();
  115. if (ENC_OverFlow()) {
  116. cnt = _abi_count();
  117. g_encoder.b_timer_ov = true;
  118. ENC_ClearUpFlags();
  119. }
  120. bool snap_to_zero_vel = encoder_run_pll((float)(cnt));
  121. if (snap_to_zero_vel) {
  122. g_encoder.interpolation = 0.1f;
  123. }else {
  124. if (cnt == g_encoder.last_cnt) {
  125. g_encoder.interpolation += g_encoder.est_vel_counts * FOC_CTRL_US;
  126. if (g_encoder.interpolation > ENC_MAX_interpolation) {
  127. g_encoder.interpolation = ENC_MAX_interpolation;
  128. }else if (g_encoder.interpolation < -ENC_MAX_interpolation) {
  129. g_encoder.interpolation = -ENC_MAX_interpolation;
  130. }
  131. }else {
  132. g_encoder.interpolation = 0.0f;
  133. }
  134. }
  135. if (g_encoder.cali_angle != INVALID_ANGLE) {
  136. g_encoder.interpolation = 0.0f;
  137. }
  138. g_encoder.abi_angle = ENC_Pluse_Nr_2_angle((float)cnt + g_encoder.interpolation) * g_encoder.motor_poles + g_encoder.enc_offset;
  139. g_encoder.abi_angle += _eccentricity_compensation(cnt);
  140. rand_angle(g_encoder.abi_angle);
  141. g_encoder.last_cnt = cnt;
  142. g_encoder.last_us = timer_count32_get();
  143. if (g_encoder.cali_angle != INVALID_ANGLE) {
  144. encoder_do_offset_calibrate();
  145. }
  146. return g_encoder.abi_angle;
  147. }
  148. float encoder_get_speed(void) {
  149. return (g_encoder.est_vel_counts/g_encoder.cpr) * 60.0f;
  150. }
  151. void _encoder_caliberate_init(void) {
  152. if (g_encoder.encoder_off_map != NULL) {
  153. return;
  154. }
  155. u32 mask = cpu_enter_critical();
  156. g_encoder.encoder_off_map = (s16 *)os_alloc(g_encoder.cpr * sizeof(s16));
  157. g_encoder.encoder_off_count = (u8 *)os_alloc(g_encoder.cpr);
  158. for (int i = 0; i < g_encoder.cpr; i++) {
  159. g_encoder.encoder_off_map[i] = 0;
  160. g_encoder.encoder_off_count[i] = 0;
  161. }
  162. cpu_exit_critical(mask);
  163. }
  164. void _encoder_caliberate_deinit(void) {
  165. if (g_encoder.encoder_off_map != NULL) {
  166. os_free(g_encoder.encoder_off_map);
  167. os_free(g_encoder.encoder_off_count);
  168. }
  169. g_encoder.encoder_off_map = NULL;
  170. g_encoder.encoder_off_count = NULL;
  171. }
  172. #define MIN_OFF_COUNT 10
  173. void encoder_detect_offset(float angle){
  174. #if 1
  175. _encoder_caliberate_init();
  176. g_encoder.cali_angle = angle;
  177. #else
  178. plot_2data16((s16)angle, (s16)g_encoder.abi_angle);
  179. #endif
  180. }
  181. static void encoder_do_offset_calibrate(void) {
  182. float delta = (g_encoder.abi_angle - g_encoder.cali_angle);
  183. if (delta > 200) {
  184. delta = delta - 360;
  185. }
  186. if (delta < -200) {
  187. delta = delta + 360;
  188. }
  189. if (g_encoder.direction == POSITIVE) {
  190. if ((g_encoder.encoder_off_count[g_encoder.last_cnt] & 0xF) <= MIN_OFF_COUNT) {
  191. g_encoder.encoder_off_map[g_encoder.last_cnt] += (s16)(delta*100.0f);
  192. g_encoder.encoder_off_count[g_encoder.last_cnt] += 0x01;
  193. }
  194. }else {
  195. if (((g_encoder.encoder_off_count[g_encoder.last_cnt] >> 4) & 0xF) <= MIN_OFF_COUNT) {
  196. g_encoder.encoder_off_map[g_encoder.last_cnt] += (s16)(delta*100.0f);
  197. g_encoder.encoder_off_count[g_encoder.last_cnt] += 0x10;
  198. }
  199. }
  200. }
  201. bool encoder_detect_finish(void) {
  202. u8 off_count = 0;
  203. for (int i = 0; i < 1024; i++) {
  204. if (g_encoder.direction == POSITIVE) {
  205. off_count = g_encoder.encoder_off_count[i] & 0xF;
  206. }else {
  207. off_count = (g_encoder.encoder_off_count[i] >> 4)& 0xF;
  208. }
  209. if (off_count <= MIN_OFF_COUNT) {
  210. return false;
  211. }
  212. }
  213. if (g_encoder.direction == NEGATIVE) {
  214. g_encoder.cali_angle = INVALID_ANGLE;
  215. _detect_off_finished();//output data to PC tools, and use Matlab do FIR filter
  216. _encoder_caliberate_deinit();
  217. }
  218. return true;
  219. }
  220. static void _detect_off_finished(void) {
  221. for (int i = 0; i < 1024; i++) {
  222. float angle_off = g_encoder.encoder_off_map[i] / (((g_encoder.encoder_off_count[i] >> 4)&0xF) + (g_encoder.encoder_off_count[i]&0xF));
  223. plot_1data16((s16)angle_off);
  224. delay_ms(2);
  225. wdog_reload();
  226. }
  227. }
  228. float encoder_get_vel_count(void) {
  229. return g_encoder.est_vel_counts;
  230. }
  231. float encoder_zero_phase_detect(void) {
  232. float phase = g_encoder.pwm_angle;
  233. float total_ph = phase;
  234. int count = 0;
  235. for(; count < 10; count++) {
  236. delay_ms(ENC_PWM_Min_P * 1000 + 2); //wait time for pwm
  237. if ABS(phase - g_encoder.pwm_angle > 2.0f) {
  238. return INVALID_ANGLE;
  239. }
  240. phase = g_encoder.pwm_angle;
  241. total_ph += phase;
  242. }
  243. sys_debug("offset = %f\n", (total_ph/(float)count));
  244. return (total_ph/(float)count);
  245. }
  246. static void encoder_sync_pwm_abs(void) {
  247. ENC_COUNT = g_encoder.pwm_count;
  248. g_encoder.last_cnt = g_encoder.pwm_count;
  249. g_encoder.est_pll.observer = (float)g_encoder.pwm_count;
  250. g_encoder.abi_angle = g_encoder.pwm_angle;
  251. g_encoder.b_index_found = true;
  252. }
  253. /*I 信号的中断处理,一圈一个中断*/
  254. void ENC_ABI_IRQHandler(void) {
  255. g_encoder.b_index_cnt = ENC_COUNT;
  256. if (!g_encoder.b_index_found){
  257. encoder_sync_pwm_abs();
  258. }
  259. }
  260. /* 编码器AB信号读书溢出处理 */
  261. void ENC_TIMER_Overflow(void) {
  262. //g_encoder.b_timer_ov = true;
  263. }
  264. /*PWM 信号捕获一个周期的处理 */
  265. static int pwm_count = 0;
  266. static int pwm_check_count = 0;
  267. void ENC_PWM_Duty_Handler(float t, float d) {
  268. float duty = ENC_Duty(d, t);
  269. if (duty < ENC_PWM_Min_P || duty > 1.0f) {
  270. return;
  271. }
  272. float Nr = ENC_Duty_2_Pluse_Nr(duty);
  273. if (Nr < 0) {
  274. return;
  275. }
  276. u32 n_nr = (u32)Nr;
  277. if (Nr - n_nr >= 0.5f) {
  278. g_encoder.pwm_count = n_nr + 1;
  279. }else {
  280. g_encoder.pwm_count = n_nr;
  281. }
  282. g_encoder.pwm_angle = ENC_Pluse_Nr_2_angle(Nr) * g_encoder.motor_poles + g_encoder.enc_offset;
  283. rand_angle(g_encoder.pwm_angle);
  284. if (!g_encoder.b_index_found && pwm_count++ >= 10) {
  285. encoder_sync_pwm_abs();
  286. }
  287. pwm_check_count ++;
  288. }
  289. static u32 _check_time = 0;
  290. bool ENC_Check_error(void) {
  291. bool error = false;
  292. if (get_delta_ms(_check_time) > 1000) {
  293. if (pwm_check_count == 0) {
  294. error = true;
  295. }
  296. pwm_check_count = 0;
  297. _check_time = get_tick_ms();
  298. }
  299. return error;
  300. }
  301. float encoder_get_pwm_angle(void) {
  302. return g_encoder.pwm_angle;
  303. }