motor.h 5.7 KB

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