motor.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #ifndef _MOTOR_H__
  2. #define _MOTOR_H__
  3. #include "os/os_types.h"
  4. #include "foc/core/PMSM_FOC_Core.h"
  5. #include "foc/motor/hall.h"
  6. #include "foc/motor/encoder.h"
  7. typedef struct {
  8. bool b_start;
  9. float throttle;
  10. bool b_ignor_throttle;
  11. bool b_calibrate;
  12. bool b_runStall; //是否堵转
  13. bool b_epm;
  14. EPM_Dir_t epm_dir;
  15. bool b_epm_cmd_move;
  16. float lim_rpm;
  17. float lim_phase_curr;
  18. u32 runStall_time;
  19. s16 s_testAngle;
  20. s32 s_targetFix;
  21. s8 s_direction;
  22. u32 n_brake_errors;
  23. u8 mode;
  24. bool b_updated;
  25. }motor_t;
  26. motor_t * mc_params(void);
  27. void mc_init(void);
  28. bool mc_start(u8 mode);
  29. bool mc_stop(void);
  30. void mc_encoder_off_calibrate(s16 vd);
  31. bool mc_throttle_released(void);
  32. bool mc_lock_motor(bool lock);
  33. void mc_set_spd_torque(s32 target);
  34. void mc_use_throttle(void);
  35. bool mc_current_sensor_calibrate(float current);
  36. bool mc_encoder_zero_calibrate(s16 vd);
  37. bool mc_set_foc_mode(u8 mode);
  38. bool mc_is_epm(void);
  39. bool mc_start_epm(bool epm);
  40. bool mc_start_epm_move(EPM_Dir_t dir, bool is_command);
  41. void mc_get_running_status(u8 *data);
  42. bool mc_command_epm_move(EPM_Dir_t dir);
  43. bool mc_throttle_epm_move(EPM_Dir_t dir);
  44. void mc_need_update(void);
  45. static __INLINE float motor_encoder_get_angle(void) {
  46. #ifdef USE_ENCODER_HALL
  47. return hall_sensor_get_theta();
  48. #elif defined (USE_ENCODER_ABI)
  49. return encoder_get_theta();
  50. #else
  51. #error "Postion sensor ERROR"
  52. #endif
  53. }
  54. static __INLINE void motor_encoder_update(void) {
  55. #ifdef USE_ENCODER_HALL
  56. hall_sensor_get_theta();
  57. #elif defined (USE_ENCODER_ABI)
  58. encoder_get_theta();
  59. #else
  60. #error "Postion sensor ERROR"
  61. #endif
  62. }
  63. static __INLINE float motor_encoder_get_speed(void) {
  64. #ifdef USE_ENCODER_HALL
  65. return hall_sensor_get_speed();
  66. #elif defined (USE_ENCODER_ABI)
  67. return encoder_get_speed();
  68. #else
  69. #error "Postion sensor ERROR"
  70. #endif
  71. }
  72. static __INLINE float motor_encoder_get_vel_count(void) {
  73. #ifdef USE_ENCODER_HALL
  74. return 0;
  75. #elif defined (USE_ENCODER_ABI)
  76. return encoder_get_vel_count();
  77. #else
  78. #error "Postion sensor ERROR"
  79. #endif
  80. }
  81. static __INLINE void motor_encoder_init(void) {
  82. #ifdef USE_ENCODER_HALL
  83. hall_sensor_init();
  84. #elif defined (USE_ENCODER_ABI)
  85. encoder_init();
  86. #else
  87. #error "Postion sensor ERROR"
  88. #endif
  89. }
  90. static __INLINE void motor_encoder_start(s8 direction) {
  91. #ifdef USE_ENCODER_HALL
  92. hall_sensor_clear(direction);
  93. #elif defined (USE_ENCODER_ABI)
  94. encoder_init_clear(direction);
  95. #else
  96. #error "Postion sensor ERROR"
  97. #endif
  98. }
  99. static __INLINE void motor_encoder_offset(float angle) {
  100. #ifdef USE_ENCODER_HALL
  101. hall_detect_offset(angle);
  102. #elif defined (USE_ENCODER_ABI)
  103. encoder_detect_offset(angle);
  104. #else
  105. #error "Postion sensor ERROR"
  106. #endif
  107. }
  108. static __INLINE void motor_encoder_offset_finish(void) {
  109. #ifdef USE_ENCODER_HALL
  110. hall_detect_offset_finish();
  111. #elif defined (USE_ENCODER_ABI)
  112. encoder_detect_finish();
  113. #else
  114. #error "Postion sensor ERROR"
  115. #endif
  116. }
  117. static __INLINE bool motor_encoder_offset_is_finish(void) {
  118. #ifdef USE_ENCODER_HALL
  119. return false;
  120. #elif defined (USE_ENCODER_ABI)
  121. return encoder_detect_finish();
  122. #else
  123. #error "Postion sensor ERROR"
  124. #endif
  125. }
  126. static __INLINE float motor_encoder_zero_phase_detect(void){
  127. #ifdef USE_ENCODER_HALL
  128. return 0.0f;
  129. #elif defined (USE_ENCODER_ABI)
  130. return encoder_zero_phase_detect();
  131. #else
  132. #error "Postion sensor ERROR"
  133. #endif
  134. }
  135. static __INLINE void motor_encoder_set_direction(s8 dir) {
  136. #ifdef USE_ENCODER_HALL
  137. hall_set_direction(dir);
  138. #elif defined (USE_ENCODER_ABI)
  139. encoder_set_direction(dir);
  140. #else
  141. #error "Postion sensor ERROR"
  142. #endif
  143. }
  144. static __INLINE void motor_encoder_lock_pos(bool lock) {
  145. #ifdef USE_ENCODER_HALL
  146. #elif defined (USE_ENCODER_ABI)
  147. encoder_lock_position(lock);
  148. #else
  149. #error "Postion sensor ERROR"
  150. #endif
  151. }
  152. #endif /* _MOTOR_H__ */