motor.h 5.3 KB

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