encoder.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #if CONFIG_MOT_TYPE==MOTOR_BLUESHARK_OLD
  10. #include "encoder_off2.h"
  11. #elif CONFIG_MOT_TYPE==MOTOR_BLUESHARK_NEW1
  12. #include "encoder_off3.h"
  13. #elif CONFIG_MOT_TYPE==MOTOR_BLUESHARK_NEW2
  14. #if ENCODER_TYPE==ENCODER_MPS
  15. #include "encoder_off4.h"
  16. #endif
  17. #elif CONFIG_MOT_TYPE==MOTOR_BLUESHARK_ZD_100
  18. #if ENCODER_TYPE==ENCODER_MPS
  19. #include "encoder_off5.h"
  20. #endif
  21. #endif
  22. static void encoder_do_offset_calibrate(void) ;
  23. static void _detect_off_finished(void);
  24. /* 磁编码器使用一对极的磁铁,所以编码器获取的角度和机械角度相同需要转为电角度*/
  25. encoder_t g_encoder;
  26. static __INLINE void encoder_pll_update_gain(void) {
  27. if (g_encoder.pll_bandwidth_shadow != g_encoder.pll_bandwidth) {
  28. g_encoder.pll_bandwidth = g_encoder.pll_bandwidth_shadow;
  29. g_encoder.est_pll.kp = 2.0f * g_encoder.pll_bandwidth;
  30. g_encoder.est_pll.ki = 0.25f * g_encoder.est_pll.kp * g_encoder.est_pll.kp;
  31. }
  32. }
  33. static void _init_pll(void) {
  34. g_encoder.est_pll.DT = FOC_CTRL_US;
  35. g_encoder.est_pll.max_wp = g_encoder.cpr;
  36. g_encoder.pll_bandwidth = 0;
  37. g_encoder.pll_bandwidth_shadow = nv_get_motor_params()->est_pll_band;
  38. encoder_pll_update_gain();
  39. PLL_Reset(&g_encoder.est_pll);
  40. }
  41. void encoder_init(void) {
  42. encoder_init_clear();
  43. enc_intf_init(ENC_MAX_RES);
  44. }
  45. void encoder_set_direction(s8 direction) {
  46. g_encoder.direction = direction;
  47. g_encoder.cali_angle = INVALID_ANGLE;
  48. }
  49. void encoder_set_bandwidth(float bandwidth) {
  50. g_encoder.pll_bandwidth_shadow = bandwidth;
  51. }
  52. void encoder_init_clear(void) {
  53. _init_pll();
  54. g_encoder.cpr = ENC_MAX_RES;
  55. g_encoder.enc_offset = nv_get_motor_params()->offset;
  56. g_encoder.motor_poles = nv_get_motor_params()->poles;
  57. g_encoder.b_index_found = false;
  58. g_encoder.direction = POSITIVE;
  59. g_encoder.abi_angle = 0.0f;
  60. g_encoder.pwm_angle = 0.0f;
  61. g_encoder.est_angle_counts = 0;
  62. g_encoder.est_vel_counts = 0;
  63. g_encoder.position = 0.0f;
  64. g_encoder.interpolation = 0.0f;
  65. g_encoder.cali_angle = INVALID_ANGLE;
  66. g_encoder.enc_count_off = 0xFFFFFFFF;
  67. g_encoder.b_cali_err = false;
  68. }
  69. void encoder_lock_position(bool enable) {
  70. if (g_encoder.b_lock_pos != enable) {
  71. g_encoder.b_lock_pos = enable;
  72. if (enable) {
  73. encoder_set_bandwidth(nv_get_motor_params()->pos_lock_pll_band);
  74. }else {
  75. encoder_set_bandwidth(nv_get_motor_params()->est_pll_band);
  76. }
  77. }
  78. }
  79. static __INLINE float _pll_over_comp(void) {
  80. u8 dir = ENC_DIR_DOWN;
  81. #ifdef ENCODER_CC_INVERT
  82. dir = ENC_DIR_UP;
  83. #endif
  84. if(ENC_Direction() == dir){
  85. return -((float)g_encoder.cpr);
  86. }
  87. return (float)g_encoder.cpr;
  88. }
  89. static __INLINE bool encoder_run_pll(float cnt) {
  90. float pll_comp = 0.0f;
  91. if (g_encoder.b_timer_ov) {
  92. pll_comp = _pll_over_comp();
  93. g_encoder.b_timer_ov = false;
  94. }
  95. encoder_pll_update_gain();
  96. g_encoder.est_vel_counts = PLL_run(&g_encoder.est_pll, cnt, pll_comp);
  97. g_encoder.est_angle_counts = g_encoder.est_pll.observer;
  98. bool snap_to_zero_vel = false;
  99. if (ABS(g_encoder.est_pll.out) < 0.5f * g_encoder.est_pll.DT * g_encoder.est_pll.ki) {
  100. g_encoder.est_vel_counts = g_encoder.est_pll.out = 0.0f; // align delta-sigma on zero to prevent jitter
  101. snap_to_zero_vel = true;
  102. }
  103. return snap_to_zero_vel;
  104. }
  105. static __INLINE u32 _abi_count(void) {
  106. #ifdef ENCODER_CC_INVERT
  107. return (g_encoder.cpr - ENC_COUNT);
  108. #else
  109. return ENC_COUNT;
  110. #endif
  111. }
  112. /* 偏心补偿 */
  113. static __INLINE float _eccentricity_compensation(int cnt) {
  114. #ifdef FIR_PHASE_SHIFT
  115. int cnt_off = (cnt + FIR_PHASE_SHIFT);//g_encoder.cpr;
  116. if (g_encoder.encoder_off_map != NULL) { //do offset calibrate, can not do encentricity compensation
  117. return 0.0f;
  118. }
  119. return -(S16Q10toF(_encoder_off_map[cnt_off]));
  120. #else
  121. return 0.0f;
  122. #endif
  123. }
  124. float encoder_get_theta(void) {
  125. if (!g_encoder.b_index_found) {
  126. return g_encoder.pwm_angle;
  127. }
  128. u32 cnt = _abi_count();
  129. __NOP();__NOP();__NOP();__NOP();
  130. if (ENC_OverFlow()) {
  131. cnt = _abi_count();
  132. g_encoder.b_timer_ov = true;
  133. ENC_ClearUpFlags();
  134. }
  135. bool snap_to_zero_vel = encoder_run_pll((float)(cnt));
  136. if (snap_to_zero_vel) {
  137. g_encoder.interpolation = 0.1f;
  138. }else {
  139. if (cnt == g_encoder.last_cnt) {
  140. g_encoder.interpolation += g_encoder.est_vel_counts * FOC_CTRL_US;
  141. if (g_encoder.interpolation > ENC_MAX_interpolation) {
  142. g_encoder.interpolation = ENC_MAX_interpolation;
  143. }else if (g_encoder.interpolation < -ENC_MAX_interpolation) {
  144. g_encoder.interpolation = -ENC_MAX_interpolation;
  145. }
  146. }else {
  147. g_encoder.interpolation = 0.0f;
  148. }
  149. }
  150. if (g_encoder.cali_angle != INVALID_ANGLE) {
  151. g_encoder.interpolation = 0.0f;
  152. }
  153. g_encoder.abi_angle = ENC_Pluse_Nr_2_angle((float)cnt + g_encoder.interpolation) * g_encoder.motor_poles + g_encoder.enc_offset;
  154. g_encoder.abi_angle += _eccentricity_compensation(cnt);
  155. rand_angle(g_encoder.abi_angle);
  156. g_encoder.last_cnt = cnt;
  157. g_encoder.last_us = timer_count32_get();
  158. if (g_encoder.cali_angle != INVALID_ANGLE) {
  159. encoder_do_offset_calibrate();
  160. }
  161. g_encoder.position += (g_encoder.est_vel_counts/g_encoder.cpr) * FOC_CTRL_US;
  162. return g_encoder.abi_angle;
  163. }
  164. float encoder_get_speed(void) {
  165. return (g_encoder.est_vel_counts/g_encoder.cpr) * 60.0f;
  166. }
  167. void _encoder_caliberate_init(void) {
  168. if (g_encoder.encoder_off_map != NULL) {
  169. return;
  170. }
  171. u32 mask = cpu_enter_critical();
  172. g_encoder.encoder_off_map = (s16 *)os_alloc(g_encoder.cpr * sizeof(s16));
  173. g_encoder.encoder_off_count = (u8 *)os_alloc(g_encoder.cpr);
  174. for (int i = 0; i < g_encoder.cpr; i++) {
  175. g_encoder.encoder_off_map[i] = 0;
  176. g_encoder.encoder_off_count[i] = 0;
  177. }
  178. cpu_exit_critical(mask);
  179. }
  180. void _encoder_caliberate_deinit(void) {
  181. if (g_encoder.encoder_off_map != NULL) {
  182. os_free(g_encoder.encoder_off_map);
  183. os_free(g_encoder.encoder_off_count);
  184. }
  185. g_encoder.encoder_off_map = NULL;
  186. g_encoder.encoder_off_count = NULL;
  187. }
  188. #define MIN_OFF_COUNT 5
  189. void encoder_detect_offset(float angle){
  190. #if 1
  191. _encoder_caliberate_init();
  192. g_encoder.cali_angle = angle;
  193. #else
  194. plot_2data16((s16)angle, (s16)g_encoder.abi_angle);
  195. #endif
  196. }
  197. static void encoder_do_offset_calibrate(void) {
  198. float delta = (g_encoder.abi_angle - g_encoder.cali_angle);
  199. if (delta > 200) {
  200. delta = delta - 360;
  201. }
  202. if (delta < -200) {
  203. delta = delta + 360;
  204. }
  205. if (g_encoder.direction == POSITIVE) {
  206. if ((g_encoder.encoder_off_count[g_encoder.last_cnt] & 0xF) <= MIN_OFF_COUNT) {
  207. g_encoder.encoder_off_map[g_encoder.last_cnt] += (s16)(delta*100.0f);
  208. g_encoder.encoder_off_count[g_encoder.last_cnt] += 0x01;
  209. }
  210. }else {
  211. if (((g_encoder.encoder_off_count[g_encoder.last_cnt] >> 4) & 0xF) <= MIN_OFF_COUNT) {
  212. g_encoder.encoder_off_map[g_encoder.last_cnt] += (s16)(delta*100.0f);
  213. g_encoder.encoder_off_count[g_encoder.last_cnt] += 0x10;
  214. }
  215. }
  216. }
  217. bool encoder_detect_finish(void) {
  218. u8 off_count = 0;
  219. for (int i = 0; i < 1024; i++) {
  220. if (g_encoder.direction == POSITIVE) {
  221. off_count = g_encoder.encoder_off_count[i] & 0xF;
  222. }else {
  223. off_count = (g_encoder.encoder_off_count[i] >> 4)& 0xF;
  224. }
  225. if (off_count <= MIN_OFF_COUNT) {
  226. return false;
  227. }
  228. }
  229. if (g_encoder.direction == NEGATIVE) {
  230. g_encoder.cali_angle = INVALID_ANGLE;
  231. }
  232. return true;
  233. }
  234. void encoder_detect_upload(void) {
  235. _detect_off_finished();//output data to PC tools, and use Matlab do FIR filter
  236. _encoder_caliberate_deinit();
  237. }
  238. static void _detect_off_finished(void) {
  239. for (int i = 0; i < 1024; i++) {
  240. float angle_off = g_encoder.encoder_off_map[i] / (((g_encoder.encoder_off_count[i] >> 4)&0xF) + (g_encoder.encoder_off_count[i]&0xF));
  241. plot_1data16((s16)angle_off);
  242. delay_ms(30);
  243. wdog_reload();
  244. }
  245. }
  246. float encoder_get_vel_count(void) {
  247. return g_encoder.est_vel_counts;
  248. }
  249. float encoder_get_position(void) {
  250. return g_encoder.position;
  251. }
  252. float encoder_zero_phase_detect(float *enc_off) {
  253. g_encoder.enc_offset = 0;
  254. delay_ms(5);
  255. float total_enc_off = g_encoder.pwm_count;
  256. float prev_offset = g_encoder.enc_offset;
  257. float phase = g_encoder.pwm_angle;
  258. float total_ph = phase;
  259. int count = 0;
  260. for(; count < 10; count++) {
  261. delay_ms(5); //wait time for pwm
  262. float angle_now = g_encoder.pwm_angle;
  263. if (ABS(phase - angle_now) > 2.0f) {
  264. g_encoder.enc_offset = prev_offset;
  265. g_encoder.enc_count_off = 0xFFFFFFFF;
  266. g_encoder.b_cali_err = true;
  267. sys_debug("err %f, %f, %d\n", phase, angle_now, count);
  268. return INVALID_ANGLE;
  269. }
  270. phase = angle_now;
  271. total_ph += phase;
  272. total_enc_off += g_encoder.pwm_count;
  273. }
  274. sys_debug("count = %d\n", count);
  275. float offset_now = total_ph/(float)(count + 1);
  276. g_encoder.enc_offset = offset_now;
  277. g_encoder.enc_count_off = (u32)(total_enc_off/(float)(count + 1));
  278. if (enc_off) {
  279. *enc_off = (float)g_encoder.enc_count_off;
  280. sys_debug("encoder off %f\n", *enc_off);
  281. }
  282. sys_debug("encoder ph off = %f\n", offset_now);
  283. return offset_now;
  284. }
  285. void encoder_clear_cnt_offset(void) {
  286. g_encoder.b_cali_err = false;
  287. g_encoder.enc_count_off = 0xFFFFFFFF;
  288. }
  289. u32 encoder_get_cnt_offset(void) {
  290. return g_encoder.enc_count_off;
  291. }
  292. bool encoder_get_cali_error(void) {
  293. return g_encoder.b_cali_err;
  294. }
  295. static void encoder_sync_pwm_abs(void) {
  296. ENC_COUNT = g_encoder.pwm_count;
  297. g_encoder.last_cnt = g_encoder.pwm_count;
  298. g_encoder.est_pll.observer = (float)g_encoder.pwm_count;
  299. g_encoder.abi_angle = g_encoder.pwm_angle;
  300. g_encoder.b_index_found = true;
  301. }
  302. /*I 信号的中断处理,一圈一个中断*/
  303. static int abi_I_delta = 0;
  304. void ENC_ABI_IRQHandler(void) {
  305. g_encoder.b_index_cnt = ENC_COUNT;
  306. if (!g_encoder.b_index_found){
  307. encoder_sync_pwm_abs();
  308. }
  309. if (g_encoder.b_index_cnt > 10 && g_encoder.b_index_cnt < (g_encoder.cpr - 10)) {
  310. abi_I_delta = g_encoder.b_index_cnt;
  311. }
  312. }
  313. /* 编码器AB信号读书溢出处理 */
  314. void ENC_TIMER_Overflow(void) {
  315. //g_encoder.b_timer_ov = true;
  316. }
  317. /*PWM 信号捕获一个周期的处理 */
  318. static int pwm_count = 0;
  319. static int pwm_check_count = 0;
  320. void ENC_PWM_Duty_Handler(float t, float d) {
  321. float duty = ENC_Duty(d, t);
  322. if (duty < ENC_PWM_Min_P || duty > ENC_PWM_Max_P) {
  323. return;
  324. }
  325. float Nr = ENC_Duty_2_Pluse_Nr(duty);
  326. if (Nr < 0) {
  327. Nr = 0;
  328. }else if (Nr > ENC_MAX_RES) {
  329. Nr = ENC_MAX_RES;
  330. }
  331. u32 n_nr = (u32)Nr;
  332. if (Nr - n_nr >= 0.5f) {
  333. g_encoder.pwm_count = n_nr + 1;
  334. }else {
  335. g_encoder.pwm_count = n_nr;
  336. }
  337. g_encoder.pwm_angle = ENC_Pluse_Nr_2_angle(Nr) * g_encoder.motor_poles + g_encoder.enc_offset;
  338. rand_angle(g_encoder.pwm_angle);
  339. if (!g_encoder.b_index_found && pwm_count++ >= 10) {
  340. encoder_sync_pwm_abs();
  341. }
  342. pwm_check_count ++;
  343. }
  344. static u32 _check_time = 0;
  345. bool ENC_Check_error(void) {
  346. bool error = false;
  347. if (get_delta_ms(_check_time) > 1000) {
  348. if (pwm_check_count == 0) {
  349. error = true;
  350. }
  351. pwm_check_count = 0;
  352. _check_time = get_tick_ms();
  353. }
  354. return error;
  355. }
  356. float encoder_get_pwm_angle(void) {
  357. #ifdef ENCODER_CC_INVERT
  358. g_encoder.pwm_angle = 360.0f - (g_encoder.pwm_angle - g_encoder.enc_offset) + g_encoder.enc_offset;
  359. rand_angle(g_encoder.pwm_angle);
  360. #endif
  361. return g_encoder.pwm_angle;
  362. }
  363. float encoder_get_abi_angle(void) {
  364. u32 cnt = _abi_count();
  365. float angle = ENC_Pluse_Nr_2_angle((float)cnt) * g_encoder.motor_poles + g_encoder.enc_offset;
  366. rand_angle(angle);
  367. return angle;
  368. }
  369. void encoder_log(void) {
  370. sys_debug("pwm %f, abi %f\n", encoder_get_pwm_angle(), encoder_get_abi_angle());
  371. sys_debug("pwm count %d, I count %d\n", g_encoder.pwm_count, abi_I_delta);
  372. }