motor.h 4.7 KB

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