motor.h 5.8 KB

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