motor.h 3.6 KB

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