motor.h 2.5 KB

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