motor.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #ifndef _MOTOR_H__
  2. #define _MOTOR_H__
  3. #include "os/os_types.h"
  4. #include "foc/core/controller.h"
  5. #include "foc/motor/hall.h"
  6. #include "foc/motor/encoder.h"
  7. #include "foc/mc_config.h"
  8. #include "foc/foc_config.h"
  9. #define IDC_USER_LIMIT_NONE ((s16)0x3fff)
  10. #define RPM_USER_LIMIT_NONE ((s16)0x3fff)
  11. #define TURBO_GEAR 3
  12. #define TURBO_GEAR_AUTO_EXIT_TIME 0//30*1000 //30S
  13. #define TURBO_MIN_MOTOR_TEMP 90
  14. #define TURBO_MIN_MOS_TEMP 80
  15. typedef struct {
  16. bool running;
  17. u32 start_ts;
  18. u32 det_ts;
  19. u32 rpm;
  20. bool error;
  21. }fan_t;
  22. typedef struct {
  23. s16 idc_lim;
  24. s16 rpm_lim;
  25. u8 ebrk_lvl;
  26. u8 n_brkShutPower;
  27. u8 b_tcs;
  28. }user_rt_set;
  29. typedef struct {
  30. bool b_start;
  31. float throttle;
  32. bool b_ignor_throttle;
  33. bool b_ind_start;
  34. bool b_calibrate;
  35. bool b_force_run;
  36. bool b_runStall; //是否堵转
  37. bool b_lock_motor;
  38. bool b_auto_hold;
  39. bool b_break;
  40. bool b_epm;
  41. bool b_cruise;
  42. u32 cruise_time;
  43. float cruise_torque;
  44. epm_dir_t epm_dir;
  45. bool b_epm_cmd_move;
  46. float f_epm_vel;
  47. float f_epm_trq;
  48. u32 runStall_time;
  49. float runStall_pos;
  50. u8 u_throttle_ration;
  51. s8 s_direction;
  52. u32 n_brake_errors;
  53. u32 n_CritiCalErrMask;
  54. u8 mode;
  55. bool b_high_vol_mode;
  56. u8 n_gear;
  57. bool b_limit_pending;
  58. gear_t *gear_cfg;
  59. u32 n_autohold_time;
  60. bool b_wait_brk_release;
  61. u8 mos_lim;
  62. u8 motor_lim;
  63. s16 s_vbus_hw_min;
  64. s16 s_vbus_hw_max;
  65. u16 vbus_le_cnt;
  66. u16 vbus_he_cnt;
  67. s16 s_target_speed;
  68. s16 s_force_torque;
  69. u8 gear_last;
  70. u32 turbo_time;
  71. u8 turbo_remain_sec;
  72. mot_contrl_t controller;
  73. user_rt_set u_set; //用户运行时设置
  74. fan_t fan[2];
  75. s16 if_acc;
  76. s16 if_max_vel;
  77. s16 if_iq_set;
  78. float force_open_angle;
  79. s32 force_open_wait;
  80. //for debug
  81. int foc_stop_cnt;
  82. int foc_start_cnt;
  83. }motor_t;
  84. extern motor_t motor;
  85. motor_t * get_motor(void);
  86. void mc_init(void);
  87. bool mc_start(u8 mode);
  88. bool mc_stop(void);
  89. void mc_encoder_off_calibrate(s16 vd);
  90. bool mc_throttle_released(void);
  91. bool mc_lock_motor(bool lock);
  92. bool mc_auto_hold(bool hold);
  93. void mc_set_throttle_r(bool use, u8 r);
  94. void mc_use_throttle(void);
  95. bool mc_current_sensor_calibrate(float current);
  96. bool mc_encoder_zero_calibrate(s16 vd);
  97. bool mc_set_ctrl_mode(u8 mode);
  98. bool mc_is_epm(void);
  99. bool mc_start_epm(bool epm);
  100. bool mc_start_epm_move(epm_dir_t dir, bool is_command);
  101. void mc_get_running_status(u8 *data);
  102. bool mc_command_epm_move(epm_dir_t dir);
  103. bool mc_throttle_epm_move(epm_dir_t dir);
  104. bool mc_is_start(void);
  105. bool mc_set_gear(u8 gear);
  106. u8 mc_get_gear(void);
  107. u8 mc_get_internal_gear(void);
  108. bool mc_set_critical_error(u8 err);
  109. void mc_clr_critical_error(u8 err);
  110. u32 mc_get_critical_error(void);
  111. void mc_set_fan_duty(u8 duty);
  112. void mc_force_run_open(s16 vd, s16 vq, bool align);
  113. u16 mc_get_running_status2(void);
  114. bool mc_enable_cruise(bool enable);
  115. bool mc_is_cruise_enabled(void);
  116. bool mc_set_cruise_speed(bool rpm_abs, float target_rpm);
  117. void mc_set_idc_limit(s16 limit);
  118. void mc_set_rpm_limit(s16 limit);
  119. gear_t *mc_gear_conf(void);
  120. gear_t *mc_gear_conf_by_gear(u8 n_gear);
  121. void mc_set_motor_lim_level(u8 l);
  122. void mc_set_mos_lim_level(u8 l);
  123. void motor_debug(void);
  124. int mc_get_phase_errinfo_block(u8 *data, int dlen);
  125. bool mc_set_ebrk_level(u8 level);
  126. s16 mc_get_ebrk_torque(void);
  127. u16 mc_get_ebrk_time(void);
  128. bool mc_critical_err_is_set(u8 err);
  129. bool mc_hwbrk_can_shutpower(void);
  130. void mc_start_current_rec(bool is_i, bool rec);
  131. bool mc_ind_motor_start(bool start);
  132. void mc_enable_brkshutpower(u8 shut);
  133. void mc_enable_tcs(bool enable);
  134. bool mc_set_target_vel(s16 vel);
  135. float mc_gear_max_torque(s16 vel, u8 gear);
  136. bool mc_set_force_torque(s16 torque);
  137. bool mc_tcs_is_enabled(void);
  138. float mc_get_max_torque_now(void);
  139. bool mc_start_ifctl_mode(bool start, s16 accl, s16 max_vel, s16 align_vd, s16 iq_set);
  140. static __INLINE mot_contrl_t *mot_contrl(void) {
  141. return &motor.controller;
  142. }
  143. static __INLINE foc_t *foc(void) {
  144. return &motor.controller.foc;
  145. }
  146. static __INLINE float motor_encoder_get_angle(void) {
  147. #ifdef USE_ENCODER_HALL
  148. return hall_update_elec_angle();
  149. #elif defined (USE_ENCODER_ABI)
  150. return encoder_get_theta(true);
  151. #else
  152. #error "Postion sensor ERROR"
  153. #endif
  154. }
  155. static __INLINE u8 motor_encoder_may_error(void) {
  156. #ifdef USE_ENCODER_HALL
  157. return false;
  158. #elif defined (USE_ENCODER_ABI)
  159. return encoder_may_error();
  160. #else
  161. #error "Postion sensor ERROR"
  162. #endif
  163. }
  164. static __INLINE void motor_encoder_update(bool detect_err) {
  165. #ifdef USE_ENCODER_HALL
  166. hall_update_elec_angle();
  167. #elif defined (USE_ENCODER_ABI)
  168. encoder_get_theta(detect_err);
  169. #else
  170. #error "Postion sensor ERROR"
  171. #endif
  172. }
  173. static __INLINE float motor_encoder_get_speed(void) {
  174. #ifdef USE_ENCODER_HALL
  175. return hall_get_velocity();
  176. #elif defined (USE_ENCODER_ABI)
  177. return encoder_get_speed();
  178. #else
  179. #error "Postion sensor ERROR"
  180. #endif
  181. }
  182. static __INLINE float motor_encoder_get_vel_count(void) {
  183. #ifdef USE_ENCODER_HALL
  184. return hall_get_vel_counts();
  185. #elif defined (USE_ENCODER_ABI)
  186. return encoder_get_vel_count();
  187. #else
  188. #error "Postion sensor ERROR"
  189. #endif
  190. }
  191. static __INLINE float motor_encoder_get_position(void) {
  192. #ifdef USE_ENCODER_HALL
  193. return hall_get_position();
  194. #elif defined (USE_ENCODER_ABI)
  195. return encoder_get_position();
  196. #else
  197. #error "Postion sensor ERROR"
  198. #endif
  199. }
  200. static __INLINE void motor_encoder_init(void) {
  201. #ifdef USE_ENCODER_HALL
  202. hall_init();
  203. #elif defined (USE_ENCODER_ABI)
  204. encoder_init();
  205. #else
  206. #error "Postion sensor ERROR"
  207. #endif
  208. }
  209. static __INLINE void motor_encoder_start(bool start) {
  210. #ifdef USE_ENCODER_HALL
  211. hall_init();
  212. #elif defined (USE_ENCODER_ABI)
  213. encoder_init_clear();
  214. #else
  215. #error "Postion sensor ERROR"
  216. #endif
  217. }
  218. static __INLINE void motor_encoder_reinit(void) {
  219. #ifdef USE_ENCODER_HALL
  220. hall_init();
  221. #elif defined (USE_ENCODER_ABI)
  222. //do nothing
  223. #else
  224. #error "Postion sensor ERROR"
  225. #endif
  226. }
  227. static __INLINE float motor_encoder_zero_phase_detect(float *enc_off){
  228. #ifdef USE_ENCODER_HALL
  229. return 0.0f;
  230. #elif defined (USE_ENCODER_ABI)
  231. return encoder_zero_phase_detect(enc_off);
  232. #else
  233. #error "Postion sensor ERROR"
  234. #endif
  235. }
  236. static __INLINE void motor_encoder_set_direction(s8 dir) {
  237. #ifdef USE_ENCODER_HALL
  238. hall_set_direction(dir);
  239. #elif defined (USE_ENCODER_ABI)
  240. //encoder_set_direction(dir);
  241. #else
  242. #error "Postion sensor ERROR"
  243. #endif
  244. }
  245. static __INLINE void motor_encoder_lock_pos(bool lock) {
  246. #ifdef USE_ENCODER_HALL
  247. #elif defined (USE_ENCODER_ABI)
  248. encoder_lock_position(lock);
  249. #else
  250. #error "Postion sensor ERROR"
  251. #endif
  252. }
  253. static __INLINE void motor_encoder_band_epm(bool epm) {
  254. #ifdef USE_ENCODER_HALL
  255. #elif defined (USE_ENCODER_ABI)
  256. encoder_epm_pll_band(epm);
  257. #else
  258. #error "Postion sensor ERROR"
  259. #endif
  260. }
  261. static __INLINE void motor_encoder_produce_error(bool error) {
  262. #ifdef USE_ENCODER_HALL
  263. #elif defined (USE_ENCODER_ABI)
  264. encoder_produce_error(error);
  265. #else
  266. #error "Postion sensor ERROR"
  267. #endif
  268. }
  269. #endif /* _MOTOR_H__ */