motor.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. typedef struct {
  9. bool running;
  10. u32 start_ts;
  11. u32 det_ts;
  12. u32 rpm;
  13. bool error;
  14. }fan_t;
  15. typedef struct {
  16. bool b_start;
  17. float throttle;
  18. bool b_ignor_throttle;
  19. bool b_calibrate;
  20. bool b_force_run;
  21. bool b_runStall; //是否堵转
  22. bool b_lock_motor;
  23. bool b_auto_hold;
  24. bool b_break;
  25. bool b_epm;
  26. bool b_cruise;
  27. u32 cruise_time;
  28. float cruise_torque;
  29. EPM_Dir_t epm_dir;
  30. bool b_epm_cmd_move;
  31. u32 runStall_time;
  32. float runStall_pos;
  33. u8 u_throttle_ration;
  34. s8 s_direction;
  35. u32 n_brake_errors;
  36. u32 n_CritiCalErrMask;
  37. u8 mode;
  38. bool b_is96Mode;
  39. u8 n_gear;
  40. mc_gear_t *gear_cfg;
  41. u32 n_autohold_time;
  42. bool b_wait_brk_release;
  43. bool b_updated;
  44. fan_t fan[2];
  45. }motor_t;
  46. typedef struct {
  47. s16 vbus_x10;
  48. s16 ibus_x10;
  49. s16 id_x10;
  50. s16 iq_x10;
  51. s16 vd_x10;
  52. s16 vq_x10;
  53. s16 id_ref_x10;
  54. s16 iq_ref_x10;
  55. u8 run_mode;
  56. s16 torque_ref_x10;
  57. s16 rpm;
  58. bool b_smo_running;
  59. s16 mos_temp;
  60. s16 mot_temp;
  61. u16 enc_error;
  62. }motor_err_t;
  63. motor_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. void mc_need_update(void);
  83. bool mc_is_start(void);
  84. bool mc_set_gear(u8 gear);
  85. u8 mc_get_gear(void);
  86. void 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);
  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. mc_gear_t *mc_get_gear_config(void);
  97. mc_gear_t *mc_get_gear_config_by_gear(u8 n_gear);
  98. int mc_get_phase_errinfo(u8 *data, int dlen);
  99. static __INLINE float motor_encoder_get_angle(void) {
  100. #ifdef USE_ENCODER_HALL
  101. return hall_sensor_get_theta();
  102. #elif defined (USE_ENCODER_ABI)
  103. return encoder_get_theta();
  104. #else
  105. #error "Postion sensor ERROR"
  106. #endif
  107. }
  108. static __INLINE void motor_encoder_update(void) {
  109. #ifdef USE_ENCODER_HALL
  110. hall_sensor_get_theta();
  111. #elif defined (USE_ENCODER_ABI)
  112. encoder_get_theta();
  113. #else
  114. #error "Postion sensor ERROR"
  115. #endif
  116. }
  117. static __INLINE float motor_encoder_get_speed(void) {
  118. #ifdef USE_ENCODER_HALL
  119. return hall_sensor_get_speed();
  120. #elif defined (USE_ENCODER_ABI)
  121. return encoder_get_speed();
  122. #else
  123. #error "Postion sensor ERROR"
  124. #endif
  125. }
  126. static __INLINE float motor_encoder_get_vel_count(void) {
  127. #ifdef USE_ENCODER_HALL
  128. return 0;
  129. #elif defined (USE_ENCODER_ABI)
  130. return encoder_get_vel_count();
  131. #else
  132. #error "Postion sensor ERROR"
  133. #endif
  134. }
  135. static __INLINE float motor_encoder_get_position(void) {
  136. #ifdef USE_ENCODER_HALL
  137. return 0;
  138. #elif defined (USE_ENCODER_ABI)
  139. return encoder_get_position();
  140. #else
  141. #error "Postion sensor ERROR"
  142. #endif
  143. }
  144. static __INLINE void motor_encoder_init(void) {
  145. #ifdef USE_ENCODER_HALL
  146. hall_sensor_init();
  147. #elif defined (USE_ENCODER_ABI)
  148. encoder_init();
  149. #else
  150. #error "Postion sensor ERROR"
  151. #endif
  152. }
  153. static __INLINE void motor_encoder_start(bool start) {
  154. #ifdef USE_ENCODER_HALL
  155. hall_sensor_clear(direction);
  156. #elif defined (USE_ENCODER_ABI)
  157. encoder_init_clear();
  158. #else
  159. #error "Postion sensor ERROR"
  160. #endif
  161. }
  162. static __INLINE void motor_encoder_offset(float angle) {
  163. #ifdef USE_ENCODER_HALL
  164. hall_detect_offset(angle);
  165. #elif defined (USE_ENCODER_ABI)
  166. encoder_detect_offset(angle);
  167. #else
  168. #error "Postion sensor ERROR"
  169. #endif
  170. }
  171. static __INLINE void motor_encoder_offset_finish(void) {
  172. #ifdef USE_ENCODER_HALL
  173. hall_detect_offset_finish();
  174. #elif defined (USE_ENCODER_ABI)
  175. encoder_detect_finish();
  176. #else
  177. #error "Postion sensor ERROR"
  178. #endif
  179. }
  180. static __INLINE bool motor_encoder_offset_is_finish(void) {
  181. #ifdef USE_ENCODER_HALL
  182. return false;
  183. #elif defined (USE_ENCODER_ABI)
  184. return encoder_detect_finish();
  185. #else
  186. #error "Postion sensor ERROR"
  187. #endif
  188. }
  189. static __INLINE void motor_encoder_data_upload(void) {
  190. #ifdef USE_ENCODER_HALL
  191. #elif defined (USE_ENCODER_ABI)
  192. encoder_detect_upload();
  193. #else
  194. #error "Postion sensor ERROR"
  195. #endif
  196. }
  197. static __INLINE float motor_encoder_zero_phase_detect(float *enc_off){
  198. #ifdef USE_ENCODER_HALL
  199. return 0.0f;
  200. #elif defined (USE_ENCODER_ABI)
  201. return encoder_zero_phase_detect(enc_off);
  202. #else
  203. #error "Postion sensor ERROR"
  204. #endif
  205. }
  206. static __INLINE void motor_encoder_set_direction(s8 dir) {
  207. #ifdef USE_ENCODER_HALL
  208. hall_set_direction(dir);
  209. #elif defined (USE_ENCODER_ABI)
  210. encoder_set_direction(dir);
  211. #else
  212. #error "Postion sensor ERROR"
  213. #endif
  214. }
  215. static __INLINE void motor_encoder_lock_pos(bool lock) {
  216. #ifdef USE_ENCODER_HALL
  217. #elif defined (USE_ENCODER_ABI)
  218. encoder_lock_position(lock);
  219. #else
  220. #error "Postion sensor ERROR"
  221. #endif
  222. }
  223. #endif /* _MOTOR_H__ */