motor.h 3.7 KB

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