motor.h 2.4 KB

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