encoder.c 11 KB

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