motor.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "foc/mc_config.h"
  8. #define IDC_USER_LIMIT_NONE ((s16)0x3fff)
  9. #define RPM_USER_LIMIT_NONE ((s16)0x3fff)
  10. typedef struct {
  11. bool running;
  12. u32 start_ts;
  13. u32 det_ts;
  14. u32 rpm;
  15. bool error;
  16. }fan_t;
  17. typedef struct {
  18. s16 idc_lim;
  19. s16 rpm_lim;
  20. u8 ebrk_lvl;
  21. u8 n_brkShutPower;
  22. bool b_tcs;
  23. }user_rt_set;
  24. typedef struct {
  25. bool b_start;
  26. float throttle;
  27. bool b_ignor_throttle;
  28. bool b_ind_start;
  29. bool b_calibrate;
  30. bool b_force_run;
  31. bool b_runStall; //是否堵转
  32. bool b_lock_motor;
  33. bool b_auto_hold;
  34. bool b_break;
  35. bool b_epm;
  36. bool b_cruise;
  37. u32 cruise_time;
  38. float cruise_torque;
  39. EPM_Dir_t epm_dir;
  40. bool b_epm_cmd_move;
  41. float f_epm_vel;
  42. float f_epm_trq;
  43. u32 runStall_time;
  44. float runStall_pos;
  45. u8 u_throttle_ration;
  46. s8 s_direction;
  47. u32 n_brake_errors;
  48. u32 n_CritiCalErrMask;
  49. u8 mode;
  50. bool b_is96Mode;
  51. u8 n_gear;
  52. bool b_limit_pending;
  53. gear_t *gear_cfg;
  54. u32 n_autohold_time;
  55. bool b_wait_brk_release;
  56. u8 mos_lim;
  57. u8 motor_lim;
  58. s16 s_vbus_hw_min;
  59. s16 s_target_speed;
  60. user_rt_set u_set; //用户运行时设置
  61. fan_t fan[2];
  62. }m_contrl_t;
  63. m_contrl_t * mc_params(void);
  64. void mc_init(void);
  65. bool mc_start(u8 mode);
  66. bool mc_stop(void);
  67. void mc_encoder_off_calibrate(s16 vd);
  68. bool mc_throttle_released(void);
  69. bool mc_lock_motor(bool lock);
  70. bool mc_auto_hold(bool hold);
  71. void mc_set_throttle_r(bool use, u8 r);
  72. void mc_use_throttle(void);
  73. bool mc_current_sensor_calibrate(float current);
  74. bool mc_encoder_zero_calibrate(s16 vd);
  75. bool mc_set_foc_mode(u8 mode);
  76. bool mc_is_epm(void);
  77. bool mc_start_epm(bool epm);
  78. bool mc_start_epm_move(EPM_Dir_t dir, bool is_command);
  79. void mc_get_running_status(u8 *data);
  80. bool mc_command_epm_move(EPM_Dir_t dir);
  81. bool mc_throttle_epm_move(EPM_Dir_t dir);
  82. bool mc_is_start(void);
  83. bool mc_set_gear(u8 gear);
  84. u8 mc_get_gear(void);
  85. u8 mc_get_internal_gear(void);
  86. bool mc_set_critical_error(u8 err);
  87. void mc_clr_critical_error(u8 err);
  88. u32 mc_get_critical_error(void);
  89. void mc_set_fan_duty(u8 duty);
  90. void mc_force_run_open(s16 vd, s16 vq, bool align);
  91. u16 mc_get_running_status2(void);
  92. bool mc_enable_cruise(bool enable);
  93. bool mc_is_cruise_enabled(void);
  94. bool mc_set_cruise_speed(bool rpm_abs, float target_rpm);
  95. void mc_set_idc_limit(s16 limit);
  96. void mc_set_rpm_limit(s16 limit);
  97. gear_t *mc_get_gear_config(void);
  98. gear_t *mc_get_gear_config_by_gear(u8 n_gear);
  99. void mc_set_motor_lim_level(u8 l);
  100. void mc_set_mos_lim_level(u8 l);
  101. void motor_debug(void);
  102. int mc_get_phase_errinfo_block(u8 *data, int dlen);
  103. bool mc_set_ebrk_level(u8 level);
  104. s16 mc_get_ebrk_torque(void);
  105. u16 mc_get_ebrk_time(void);
  106. bool mc_critical_err_is_set(u8 err);
  107. bool mc_hwbrk_can_shutpower(void);
  108. void mc_start_current_rec(bool rec);
  109. bool mc_ind_motor_start(bool start);
  110. void mc_enable_brkshutpower(u8 shut);
  111. void mc_enable_tcs(bool enable);
  112. bool mc_set_target_vel(s16 vel);
  113. static __INLINE float motor_encoder_get_angle(void) {
  114. #ifdef USE_ENCODER_HALL
  115. return hall_sensor_get_theta(true);
  116. #elif defined (USE_ENCODER_ABI)
  117. return encoder_get_theta(true);
  118. #else
  119. #error "Postion sensor ERROR"
  120. #endif
  121. }
  122. static __INLINE u8 motor_encoder_may_error(void) {
  123. #ifdef USE_ENCODER_HALL
  124. return false;
  125. #elif defined (USE_ENCODER_ABI)
  126. return encoder_may_error();
  127. #else
  128. #error "Postion sensor ERROR"
  129. #endif
  130. }
  131. static __INLINE void motor_encoder_update(bool detect_err) {
  132. #ifdef USE_ENCODER_HALL
  133. hall_sensor_get_theta(detect_err);
  134. #elif defined (USE_ENCODER_ABI)
  135. encoder_get_theta(detect_err);
  136. #else
  137. #error "Postion sensor ERROR"
  138. #endif
  139. }
  140. static __INLINE float motor_encoder_get_speed(void) {
  141. #ifdef USE_ENCODER_HALL
  142. return hall_sensor_get_speed();
  143. #elif defined (USE_ENCODER_ABI)
  144. return encoder_get_speed();
  145. #else
  146. #error "Postion sensor ERROR"
  147. #endif
  148. }
  149. static __INLINE float motor_encoder_get_vel_count(void) {
  150. #ifdef USE_ENCODER_HALL
  151. return 0;
  152. #elif defined (USE_ENCODER_ABI)
  153. return encoder_get_vel_count();
  154. #else
  155. #error "Postion sensor ERROR"
  156. #endif
  157. }
  158. static __INLINE float motor_encoder_get_position(void) {
  159. #ifdef USE_ENCODER_HALL
  160. return 0;
  161. #elif defined (USE_ENCODER_ABI)
  162. return encoder_get_position();
  163. #else
  164. #error "Postion sensor ERROR"
  165. #endif
  166. }
  167. static __INLINE void motor_encoder_init(void) {
  168. #ifdef USE_ENCODER_HALL
  169. hall_sensor_init();
  170. #elif defined (USE_ENCODER_ABI)
  171. encoder_init();
  172. #else
  173. #error "Postion sensor ERROR"
  174. #endif
  175. }
  176. static __INLINE void motor_encoder_start(bool start) {
  177. #ifdef USE_ENCODER_HALL
  178. hall_sensor_clear(direction);
  179. #elif defined (USE_ENCODER_ABI)
  180. encoder_init_clear();
  181. #else
  182. #error "Postion sensor ERROR"
  183. #endif
  184. }
  185. static __INLINE float motor_encoder_zero_phase_detect(float *enc_off){
  186. #ifdef USE_ENCODER_HALL
  187. return 0.0f;
  188. #elif defined (USE_ENCODER_ABI)
  189. return encoder_zero_phase_detect(enc_off);
  190. #else
  191. #error "Postion sensor ERROR"
  192. #endif
  193. }
  194. static __INLINE void motor_encoder_set_direction(s8 dir) {
  195. #ifdef USE_ENCODER_HALL
  196. hall_set_direction(dir);
  197. #elif defined (USE_ENCODER_ABI)
  198. encoder_set_direction(dir);
  199. #else
  200. #error "Postion sensor ERROR"
  201. #endif
  202. }
  203. static __INLINE void motor_encoder_lock_pos(bool lock) {
  204. #ifdef USE_ENCODER_HALL
  205. #elif defined (USE_ENCODER_ABI)
  206. encoder_lock_position(lock);
  207. #else
  208. #error "Postion sensor ERROR"
  209. #endif
  210. }
  211. static __INLINE void motor_encoder_band_epm(bool epm) {
  212. #ifdef USE_ENCODER_HALL
  213. #elif defined (USE_ENCODER_ABI)
  214. encoder_epm_pll_band(epm);
  215. #else
  216. #error "Postion sensor ERROR"
  217. #endif
  218. }
  219. static __INLINE void motor_encoder_produce_error(bool error) {
  220. #ifdef USE_ENCODER_HALL
  221. #elif defined (USE_ENCODER_ABI)
  222. encoder_produce_error(error);
  223. #else
  224. #error "Postion sensor ERROR"
  225. #endif
  226. }
  227. #endif /* _MOTOR_H__ */