motor.h 4.5 KB

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