motor.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. u32 runStall_time;
  20. float runStall_pos;
  21. s16 s_testAngle;
  22. s32 s_targetFix;
  23. s8 s_direction;
  24. u32 n_brake_errors;
  25. u8 mode;
  26. u32 n_autohold_time;
  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. bool mc_is_start(void);
  50. static __INLINE float motor_encoder_get_angle(void) {
  51. #ifdef USE_ENCODER_HALL
  52. return hall_sensor_get_theta();
  53. #elif defined (USE_ENCODER_ABI)
  54. return encoder_get_theta();
  55. #else
  56. #error "Postion sensor ERROR"
  57. #endif
  58. }
  59. static __INLINE void motor_encoder_update(void) {
  60. #ifdef USE_ENCODER_HALL
  61. hall_sensor_get_theta();
  62. #elif defined (USE_ENCODER_ABI)
  63. encoder_get_theta();
  64. #else
  65. #error "Postion sensor ERROR"
  66. #endif
  67. }
  68. static __INLINE float motor_encoder_get_speed(void) {
  69. #ifdef USE_ENCODER_HALL
  70. return hall_sensor_get_speed();
  71. #elif defined (USE_ENCODER_ABI)
  72. return encoder_get_speed();
  73. #else
  74. #error "Postion sensor ERROR"
  75. #endif
  76. }
  77. static __INLINE float motor_encoder_get_vel_count(void) {
  78. #ifdef USE_ENCODER_HALL
  79. return 0;
  80. #elif defined (USE_ENCODER_ABI)
  81. return encoder_get_vel_count();
  82. #else
  83. #error "Postion sensor ERROR"
  84. #endif
  85. }
  86. static __INLINE float motor_encoder_get_position(void) {
  87. #ifdef USE_ENCODER_HALL
  88. return 0;
  89. #elif defined (USE_ENCODER_ABI)
  90. return encoder_get_position();
  91. #else
  92. #error "Postion sensor ERROR"
  93. #endif
  94. }
  95. static __INLINE void motor_encoder_init(void) {
  96. #ifdef USE_ENCODER_HALL
  97. hall_sensor_init();
  98. #elif defined (USE_ENCODER_ABI)
  99. encoder_init();
  100. #else
  101. #error "Postion sensor ERROR"
  102. #endif
  103. }
  104. static __INLINE void motor_encoder_start(s8 direction) {
  105. #ifdef USE_ENCODER_HALL
  106. hall_sensor_clear(direction);
  107. #elif defined (USE_ENCODER_ABI)
  108. encoder_init_clear(direction);
  109. #else
  110. #error "Postion sensor ERROR"
  111. #endif
  112. }
  113. static __INLINE void motor_encoder_offset(float angle) {
  114. #ifdef USE_ENCODER_HALL
  115. hall_detect_offset(angle);
  116. #elif defined (USE_ENCODER_ABI)
  117. encoder_detect_offset(angle);
  118. #else
  119. #error "Postion sensor ERROR"
  120. #endif
  121. }
  122. static __INLINE void motor_encoder_offset_finish(void) {
  123. #ifdef USE_ENCODER_HALL
  124. hall_detect_offset_finish();
  125. #elif defined (USE_ENCODER_ABI)
  126. encoder_detect_finish();
  127. #else
  128. #error "Postion sensor ERROR"
  129. #endif
  130. }
  131. static __INLINE bool motor_encoder_offset_is_finish(void) {
  132. #ifdef USE_ENCODER_HALL
  133. return false;
  134. #elif defined (USE_ENCODER_ABI)
  135. return encoder_detect_finish();
  136. #else
  137. #error "Postion sensor ERROR"
  138. #endif
  139. }
  140. static __INLINE void motor_encoder_data_upload(void) {
  141. #ifdef USE_ENCODER_HALL
  142. #elif defined (USE_ENCODER_ABI)
  143. encoder_detect_upload();
  144. #else
  145. #error "Postion sensor ERROR"
  146. #endif
  147. }
  148. static __INLINE float motor_encoder_zero_phase_detect(void){
  149. #ifdef USE_ENCODER_HALL
  150. return 0.0f;
  151. #elif defined (USE_ENCODER_ABI)
  152. return encoder_zero_phase_detect();
  153. #else
  154. #error "Postion sensor ERROR"
  155. #endif
  156. }
  157. static __INLINE void motor_encoder_set_direction(s8 dir) {
  158. #ifdef USE_ENCODER_HALL
  159. hall_set_direction(dir);
  160. #elif defined (USE_ENCODER_ABI)
  161. encoder_set_direction(dir);
  162. #else
  163. #error "Postion sensor ERROR"
  164. #endif
  165. }
  166. static __INLINE void motor_encoder_lock_pos(bool lock) {
  167. #ifdef USE_ENCODER_HALL
  168. #elif defined (USE_ENCODER_ABI)
  169. encoder_lock_position(lock);
  170. #else
  171. #error "Postion sensor ERROR"
  172. #endif
  173. }
  174. #endif /* _MOTOR_H__ */