motor.h 3.4 KB

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