motor.h 3.1 KB

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