motor.h 3.8 KB

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