motor.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #include "app/nv_storage.h"
  8. typedef struct {
  9. bool b_start;
  10. float throttle;
  11. bool b_ignor_throttle;
  12. bool b_calibrate;
  13. bool b_runStall; //是否堵转
  14. bool b_lock_motor;
  15. bool b_auto_hold;
  16. bool b_break;
  17. bool b_epm;
  18. EPM_Dir_t epm_dir;
  19. bool b_epm_cmd_move;
  20. u32 runStall_time;
  21. float runStall_pos;
  22. s16 s_testAngle;
  23. s32 s_targetFix;
  24. s8 s_direction;
  25. u32 n_brake_errors;
  26. u8 mode;
  27. bool b_is96Mode;
  28. u8 n_gear;
  29. mc_gear_t *gear_cfg;
  30. u32 n_autohold_time;
  31. bool b_wait_brk_release;
  32. bool b_updated;
  33. }motor_t;
  34. motor_t * mc_params(void);
  35. void mc_init(void);
  36. bool mc_start(u8 mode);
  37. bool mc_stop(void);
  38. void mc_encoder_off_calibrate(s16 vd);
  39. bool mc_throttle_released(void);
  40. bool mc_lock_motor(bool lock);
  41. bool mc_auto_hold(bool hold);
  42. void mc_set_spd_torque(s32 target);
  43. void mc_use_throttle(void);
  44. bool mc_current_sensor_calibrate(float current);
  45. bool mc_encoder_zero_calibrate(s16 vd);
  46. bool mc_set_foc_mode(u8 mode);
  47. bool mc_is_epm(void);
  48. bool mc_start_epm(bool epm);
  49. bool mc_start_epm_move(EPM_Dir_t dir, bool is_command);
  50. void mc_get_running_status(u8 *data);
  51. bool mc_command_epm_move(EPM_Dir_t dir);
  52. bool mc_throttle_epm_move(EPM_Dir_t dir);
  53. void mc_need_update(void);
  54. bool mc_is_start(void);
  55. bool mc_set_gear(u8 gear);
  56. u8 mc_get_gear(void);
  57. static __INLINE float motor_encoder_get_angle(void) {
  58. #ifdef USE_ENCODER_HALL
  59. return hall_sensor_get_theta();
  60. #elif defined (USE_ENCODER_ABI)
  61. return encoder_get_theta();
  62. #else
  63. #error "Postion sensor ERROR"
  64. #endif
  65. }
  66. static __INLINE void motor_encoder_update(void) {
  67. #ifdef USE_ENCODER_HALL
  68. hall_sensor_get_theta();
  69. #elif defined (USE_ENCODER_ABI)
  70. encoder_get_theta();
  71. #else
  72. #error "Postion sensor ERROR"
  73. #endif
  74. }
  75. static __INLINE float motor_encoder_get_speed(void) {
  76. #ifdef USE_ENCODER_HALL
  77. return hall_sensor_get_speed();
  78. #elif defined (USE_ENCODER_ABI)
  79. return encoder_get_speed();
  80. #else
  81. #error "Postion sensor ERROR"
  82. #endif
  83. }
  84. static __INLINE float motor_encoder_get_vel_count(void) {
  85. #ifdef USE_ENCODER_HALL
  86. return 0;
  87. #elif defined (USE_ENCODER_ABI)
  88. return encoder_get_vel_count();
  89. #else
  90. #error "Postion sensor ERROR"
  91. #endif
  92. }
  93. static __INLINE float motor_encoder_get_position(void) {
  94. #ifdef USE_ENCODER_HALL
  95. return 0;
  96. #elif defined (USE_ENCODER_ABI)
  97. return encoder_get_position();
  98. #else
  99. #error "Postion sensor ERROR"
  100. #endif
  101. }
  102. static __INLINE void motor_encoder_init(void) {
  103. #ifdef USE_ENCODER_HALL
  104. hall_sensor_init();
  105. #elif defined (USE_ENCODER_ABI)
  106. encoder_init();
  107. #else
  108. #error "Postion sensor ERROR"
  109. #endif
  110. }
  111. static __INLINE void motor_encoder_start(bool start) {
  112. #ifdef USE_ENCODER_HALL
  113. hall_sensor_clear(direction);
  114. #elif defined (USE_ENCODER_ABI)
  115. encoder_init_clear();
  116. #else
  117. #error "Postion sensor ERROR"
  118. #endif
  119. }
  120. static __INLINE void motor_encoder_offset(float angle) {
  121. #ifdef USE_ENCODER_HALL
  122. hall_detect_offset(angle);
  123. #elif defined (USE_ENCODER_ABI)
  124. encoder_detect_offset(angle);
  125. #else
  126. #error "Postion sensor ERROR"
  127. #endif
  128. }
  129. static __INLINE void motor_encoder_offset_finish(void) {
  130. #ifdef USE_ENCODER_HALL
  131. hall_detect_offset_finish();
  132. #elif defined (USE_ENCODER_ABI)
  133. encoder_detect_finish();
  134. #else
  135. #error "Postion sensor ERROR"
  136. #endif
  137. }
  138. static __INLINE bool motor_encoder_offset_is_finish(void) {
  139. #ifdef USE_ENCODER_HALL
  140. return false;
  141. #elif defined (USE_ENCODER_ABI)
  142. return encoder_detect_finish();
  143. #else
  144. #error "Postion sensor ERROR"
  145. #endif
  146. }
  147. static __INLINE void motor_encoder_data_upload(void) {
  148. #ifdef USE_ENCODER_HALL
  149. #elif defined (USE_ENCODER_ABI)
  150. encoder_detect_upload();
  151. #else
  152. #error "Postion sensor ERROR"
  153. #endif
  154. }
  155. static __INLINE float motor_encoder_zero_phase_detect(void){
  156. #ifdef USE_ENCODER_HALL
  157. return 0.0f;
  158. #elif defined (USE_ENCODER_ABI)
  159. return encoder_zero_phase_detect();
  160. #else
  161. #error "Postion sensor ERROR"
  162. #endif
  163. }
  164. static __INLINE void motor_encoder_set_direction(s8 dir) {
  165. #ifdef USE_ENCODER_HALL
  166. hall_set_direction(dir);
  167. #elif defined (USE_ENCODER_ABI)
  168. encoder_set_direction(dir);
  169. #else
  170. #error "Postion sensor ERROR"
  171. #endif
  172. }
  173. static __INLINE void motor_encoder_lock_pos(bool lock) {
  174. #ifdef USE_ENCODER_HALL
  175. #elif defined (USE_ENCODER_ABI)
  176. encoder_lock_position(lock);
  177. #else
  178. #error "Postion sensor ERROR"
  179. #endif
  180. }
  181. #endif /* _MOTOR_H__ */