motor.h 3.0 KB

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