encoder.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef _Encoder_H__
  2. #define _Encoder_H__
  3. #include "bsp/enc_intf.h"
  4. #include "foc/core/PI_Controller.h"
  5. typedef struct {
  6. bool b_index_found; //I 对齐
  7. float enc_offset;
  8. float pwm_angle;
  9. u32 pwm_count;
  10. float abi_angle;
  11. float interpolation;
  12. float pll_bandwidth_shadow;
  13. float pll_bandwidth;
  14. PLL_t est_pll;
  15. bool b_timer_ov;
  16. u32 cpr;
  17. u32 last_cnt;
  18. u32 b_index_cnt;
  19. u32 last_us;
  20. s8 enc_dir; //encoder count dir UP or DOWN
  21. s8 direction; //motor running dir, 逆时针 正,顺时针负
  22. u16 p_dir_cnt;
  23. u16 n_dir_cnt;
  24. float est_vel_counts; //每秒多少个计数
  25. float est_angle_counts;
  26. float rpm;
  27. }encoder_t;
  28. #define ENC_MAX_RES 4096
  29. /*min. 490 Hz, max 603 Hz*/
  30. #define ENC_PWM_Max_P (1.0f/490.0f)
  31. #define ENC_PWM_Min_P (1.0f/603.0f)
  32. #define ENC_PWM_MAX_RES 4119.0F
  33. #define ENC_PWM_INIT_WIDTH 12.0F //PWM 起始宽度
  34. #define ENC_PWM_ERROR_WIDTH 4.0f //PWM 指示错误的宽度
  35. #define ENC_PWM_END_WIDTH 0.0F
  36. #define ENC_PWM_Error_P (ENC_PWM_INIT_WIDTH/ENC_PWM_MAX_RES)
  37. #define ENC_PWM_MIN_duty ((ENC_PWM_INIT_WIDTH + ENC_PWM_ERROR_WIDTH + ENC_PWM_END_WIDTH)/ENC_PWM_MAX_RES)
  38. #define ENC_Duty_2_Pluse_Nr(duty) (duty * ENC_PWM_MAX_RES - (ENC_PWM_INIT_WIDTH + ENC_PWM_ERROR_WIDTH + ENC_PWM_END_WIDTH)) //通过占空比计算有几个脉冲
  39. #define ENC_Pluse_Nr_2_angle(Nr) (360.0f/(float)ENC_MAX_RES * (Nr))
  40. void encoder_init(void);
  41. void encoder_init_clear(s8 direction);
  42. float encoder_get_theta(void);
  43. float encoder_get_speed(void);
  44. void encoder_detect_offset(float angle);
  45. void encoder_set_direction(s8 direction);
  46. #endif /* _Encoder_H__ */