motor.h 2.4 KB

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