motor.h 3.0 KB

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