motor.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. float f_epm_vel;
  33. float f_epm_trq;
  34. u32 runStall_time;
  35. float runStall_pos;
  36. u8 u_throttle_ration;
  37. s8 s_direction;
  38. u32 n_brake_errors;
  39. u32 n_CritiCalErrMask;
  40. u8 mode;
  41. bool b_is96Mode;
  42. u8 n_gear;
  43. bool b_limit_pending;
  44. mc_gear_t *gear_cfg;
  45. u32 n_autohold_time;
  46. bool b_wait_brk_release;
  47. u8 mos_lim;
  48. u8 motor_lim;
  49. s16 idc_user_lim;
  50. fan_t fan[2];
  51. }motor_t;
  52. motor_t * mc_params(void);
  53. void mc_init(void);
  54. bool mc_start(u8 mode);
  55. bool mc_stop(void);
  56. void mc_encoder_off_calibrate(s16 vd);
  57. bool mc_throttle_released(void);
  58. bool mc_lock_motor(bool lock);
  59. bool mc_auto_hold(bool hold);
  60. void mc_set_throttle_r(bool use, u8 r);
  61. void mc_use_throttle(void);
  62. bool mc_current_sensor_calibrate(float current);
  63. bool mc_encoder_zero_calibrate(s16 vd);
  64. bool mc_set_foc_mode(u8 mode);
  65. bool mc_is_epm(void);
  66. bool mc_start_epm(bool epm);
  67. bool mc_start_epm_move(EPM_Dir_t dir, bool is_command);
  68. void mc_get_running_status(u8 *data);
  69. bool mc_command_epm_move(EPM_Dir_t dir);
  70. bool mc_throttle_epm_move(EPM_Dir_t dir);
  71. bool mc_is_start(void);
  72. bool mc_set_gear(u8 gear);
  73. u8 mc_get_gear(void);
  74. u8 mc_get_internal_gear(void);
  75. void mc_set_critical_error(u8 err);
  76. void mc_clr_critical_error(u8 err);
  77. u32 mc_get_critical_error(void);
  78. void mc_set_fan_duty(u8 duty);
  79. void mc_force_run_open(s16 vd, s16 vq);
  80. u16 mc_get_running_status2(void);
  81. bool mc_enable_cruise(bool enable);
  82. bool mc_is_cruise_enabled(void);
  83. bool mc_set_cruise_speed(bool rpm_abs, float target_rpm);
  84. void mc_set_idc_limit(s16 limit);
  85. mc_gear_t *mc_get_gear_config(void);
  86. mc_gear_t *mc_get_gear_config_by_gear(u8 n_gear);
  87. void mc_set_motor_lim_level(u8 l);
  88. void mc_set_mos_lim_level(u8 l);
  89. void motor_debug(void);
  90. int mc_get_phase_errinfo_block(u8 *data, int dlen);
  91. static __INLINE float motor_encoder_get_angle(void) {
  92. #ifdef USE_ENCODER_HALL
  93. return hall_sensor_get_theta();
  94. #elif defined (USE_ENCODER_ABI)
  95. return encoder_get_theta();
  96. #else
  97. #error "Postion sensor ERROR"
  98. #endif
  99. }
  100. static __INLINE u8 motor_encoder_may_error(void) {
  101. #ifdef USE_ENCODER_HALL
  102. return false;
  103. #elif defined (USE_ENCODER_ABI)
  104. return encoder_may_error();
  105. #else
  106. #error "Postion sensor ERROR"
  107. #endif
  108. }
  109. static __INLINE void motor_encoder_update(void) {
  110. #ifdef USE_ENCODER_HALL
  111. hall_sensor_get_theta();
  112. #elif defined (USE_ENCODER_ABI)
  113. encoder_get_theta();
  114. #else
  115. #error "Postion sensor ERROR"
  116. #endif
  117. }
  118. static __INLINE float motor_encoder_get_speed(void) {
  119. #ifdef USE_ENCODER_HALL
  120. return hall_sensor_get_speed();
  121. #elif defined (USE_ENCODER_ABI)
  122. return encoder_get_speed();
  123. #else
  124. #error "Postion sensor ERROR"
  125. #endif
  126. }
  127. static __INLINE float motor_encoder_get_vel_count(void) {
  128. #ifdef USE_ENCODER_HALL
  129. return 0;
  130. #elif defined (USE_ENCODER_ABI)
  131. return encoder_get_vel_count();
  132. #else
  133. #error "Postion sensor ERROR"
  134. #endif
  135. }
  136. static __INLINE float motor_encoder_get_position(void) {
  137. #ifdef USE_ENCODER_HALL
  138. return 0;
  139. #elif defined (USE_ENCODER_ABI)
  140. return encoder_get_position();
  141. #else
  142. #error "Postion sensor ERROR"
  143. #endif
  144. }
  145. static __INLINE void motor_encoder_init(void) {
  146. #ifdef USE_ENCODER_HALL
  147. hall_sensor_init();
  148. #elif defined (USE_ENCODER_ABI)
  149. encoder_init();
  150. #else
  151. #error "Postion sensor ERROR"
  152. #endif
  153. }
  154. static __INLINE void motor_encoder_start(bool start) {
  155. #ifdef USE_ENCODER_HALL
  156. hall_sensor_clear(direction);
  157. #elif defined (USE_ENCODER_ABI)
  158. encoder_init_clear();
  159. #else
  160. #error "Postion sensor ERROR"
  161. #endif
  162. }
  163. static __INLINE float motor_encoder_zero_phase_detect(float *enc_off){
  164. #ifdef USE_ENCODER_HALL
  165. return 0.0f;
  166. #elif defined (USE_ENCODER_ABI)
  167. return encoder_zero_phase_detect(enc_off);
  168. #else
  169. #error "Postion sensor ERROR"
  170. #endif
  171. }
  172. static __INLINE void motor_encoder_set_direction(s8 dir) {
  173. #ifdef USE_ENCODER_HALL
  174. hall_set_direction(dir);
  175. #elif defined (USE_ENCODER_ABI)
  176. encoder_set_direction(dir);
  177. #else
  178. #error "Postion sensor ERROR"
  179. #endif
  180. }
  181. static __INLINE void motor_encoder_lock_pos(bool lock) {
  182. #ifdef USE_ENCODER_HALL
  183. #elif defined (USE_ENCODER_ABI)
  184. encoder_lock_position(lock);
  185. #else
  186. #error "Postion sensor ERROR"
  187. #endif
  188. }
  189. static __INLINE void motor_encoder_band_epm(bool epm) {
  190. #ifdef USE_ENCODER_HALL
  191. #elif defined (USE_ENCODER_ABI)
  192. encoder_epm_pll_band(epm);
  193. #else
  194. #error "Postion sensor ERROR"
  195. #endif
  196. }
  197. static __INLINE void motor_encoder_produce_error(bool error) {
  198. #ifdef USE_ENCODER_HALL
  199. #elif defined (USE_ENCODER_ABI)
  200. encoder_produce_error(error);
  201. #else
  202. #error "Postion sensor ERROR"
  203. #endif
  204. }
  205. #endif /* _MOTOR_H__ */