encoder.c 9.7 KB

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