motor.h 3.4 KB

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