motor.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. motor_t * mc_params(void);
  47. void mc_init(void);
  48. bool mc_start(u8 mode);
  49. bool mc_stop(void);
  50. void mc_encoder_off_calibrate(s16 vd);
  51. bool mc_throttle_released(void);
  52. bool mc_lock_motor(bool lock);
  53. bool mc_auto_hold(bool hold);
  54. void mc_set_throttle_r(u8 r);
  55. void mc_use_throttle(void);
  56. bool mc_current_sensor_calibrate(float current);
  57. bool mc_encoder_zero_calibrate(s16 vd);
  58. bool mc_set_foc_mode(u8 mode);
  59. bool mc_is_epm(void);
  60. bool mc_start_epm(bool epm);
  61. bool mc_start_epm_move(EPM_Dir_t dir, bool is_command);
  62. void mc_get_running_status(u8 *data);
  63. bool mc_command_epm_move(EPM_Dir_t dir);
  64. bool mc_throttle_epm_move(EPM_Dir_t dir);
  65. void mc_need_update(void);
  66. bool mc_is_start(void);
  67. bool mc_set_gear(u8 gear);
  68. u8 mc_get_gear(void);
  69. void mc_set_critical_error(u8 err);
  70. void mc_clr_critical_error(u8 err);
  71. u32 mc_get_critical_error(void);
  72. void mc_set_fan_duty(u8 duty);
  73. void mc_force_run_open(s16 vd, s16 vq);
  74. u16 mc_get_running_status2(void);
  75. bool mc_enable_cruise(bool enable);
  76. bool mc_is_cruise_enabled(void);
  77. bool mc_set_cruise_speed(bool rpm_abs, float target_rpm);
  78. static __INLINE float motor_encoder_get_angle(void) {
  79. #ifdef USE_ENCODER_HALL
  80. return hall_sensor_get_theta();
  81. #elif defined (USE_ENCODER_ABI)
  82. return encoder_get_theta();
  83. #else
  84. #error "Postion sensor ERROR"
  85. #endif
  86. }
  87. static __INLINE void motor_encoder_update(void) {
  88. #ifdef USE_ENCODER_HALL
  89. hall_sensor_get_theta();
  90. #elif defined (USE_ENCODER_ABI)
  91. encoder_get_theta();
  92. #else
  93. #error "Postion sensor ERROR"
  94. #endif
  95. }
  96. static __INLINE float motor_encoder_get_speed(void) {
  97. #ifdef USE_ENCODER_HALL
  98. return hall_sensor_get_speed();
  99. #elif defined (USE_ENCODER_ABI)
  100. return encoder_get_speed();
  101. #else
  102. #error "Postion sensor ERROR"
  103. #endif
  104. }
  105. static __INLINE float motor_encoder_get_vel_count(void) {
  106. #ifdef USE_ENCODER_HALL
  107. return 0;
  108. #elif defined (USE_ENCODER_ABI)
  109. return encoder_get_vel_count();
  110. #else
  111. #error "Postion sensor ERROR"
  112. #endif
  113. }
  114. static __INLINE float motor_encoder_get_position(void) {
  115. #ifdef USE_ENCODER_HALL
  116. return 0;
  117. #elif defined (USE_ENCODER_ABI)
  118. return encoder_get_position();
  119. #else
  120. #error "Postion sensor ERROR"
  121. #endif
  122. }
  123. static __INLINE void motor_encoder_init(void) {
  124. #ifdef USE_ENCODER_HALL
  125. hall_sensor_init();
  126. #elif defined (USE_ENCODER_ABI)
  127. encoder_init();
  128. #else
  129. #error "Postion sensor ERROR"
  130. #endif
  131. }
  132. static __INLINE void motor_encoder_start(bool start) {
  133. #ifdef USE_ENCODER_HALL
  134. hall_sensor_clear(direction);
  135. #elif defined (USE_ENCODER_ABI)
  136. encoder_init_clear();
  137. #else
  138. #error "Postion sensor ERROR"
  139. #endif
  140. }
  141. static __INLINE void motor_encoder_offset(float angle) {
  142. #ifdef USE_ENCODER_HALL
  143. hall_detect_offset(angle);
  144. #elif defined (USE_ENCODER_ABI)
  145. encoder_detect_offset(angle);
  146. #else
  147. #error "Postion sensor ERROR"
  148. #endif
  149. }
  150. static __INLINE void motor_encoder_offset_finish(void) {
  151. #ifdef USE_ENCODER_HALL
  152. hall_detect_offset_finish();
  153. #elif defined (USE_ENCODER_ABI)
  154. encoder_detect_finish();
  155. #else
  156. #error "Postion sensor ERROR"
  157. #endif
  158. }
  159. static __INLINE bool motor_encoder_offset_is_finish(void) {
  160. #ifdef USE_ENCODER_HALL
  161. return false;
  162. #elif defined (USE_ENCODER_ABI)
  163. return encoder_detect_finish();
  164. #else
  165. #error "Postion sensor ERROR"
  166. #endif
  167. }
  168. static __INLINE void motor_encoder_data_upload(void) {
  169. #ifdef USE_ENCODER_HALL
  170. #elif defined (USE_ENCODER_ABI)
  171. encoder_detect_upload();
  172. #else
  173. #error "Postion sensor ERROR"
  174. #endif
  175. }
  176. static __INLINE float motor_encoder_zero_phase_detect(float *enc_off){
  177. #ifdef USE_ENCODER_HALL
  178. return 0.0f;
  179. #elif defined (USE_ENCODER_ABI)
  180. return encoder_zero_phase_detect(enc_off);
  181. #else
  182. #error "Postion sensor ERROR"
  183. #endif
  184. }
  185. static __INLINE void motor_encoder_set_direction(s8 dir) {
  186. #ifdef USE_ENCODER_HALL
  187. hall_set_direction(dir);
  188. #elif defined (USE_ENCODER_ABI)
  189. encoder_set_direction(dir);
  190. #else
  191. #error "Postion sensor ERROR"
  192. #endif
  193. }
  194. static __INLINE void motor_encoder_lock_pos(bool lock) {
  195. #ifdef USE_ENCODER_HALL
  196. #elif defined (USE_ENCODER_ABI)
  197. encoder_lock_position(lock);
  198. #else
  199. #error "Postion sensor ERROR"
  200. #endif
  201. }
  202. #endif /* _MOTOR_H__ */