motor.h 3.0 KB

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