controller.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #ifndef _CONTROLLER_H__
  2. #define _CONTROLLER_H__
  3. #include "math/fix_math.h"
  4. #include "foc/core/PI_Controller.h"
  5. #include "foc/core/foc.h"
  6. #include "foc/core/etcs.h"
  7. #include "foc/limit.h"
  8. #define CTRL_MODE_OPEN ((u8)0U)
  9. #define CTRL_MODE_SPD ((u8)1U)
  10. #define CTRL_MODE_TRQ ((u8)2U)
  11. #define CTRL_MODE_CURRENT ((u8)3U)
  12. #define CTRL_MODE_EBRAKE ((u8)4U)
  13. #define FOC_LIM_NO_CHANGE 0
  14. #define FOC_LIM_CHANGE_H 1
  15. #define FOC_LIM_CHANGE_L 2
  16. #define SECTOR_1 0u
  17. #define SECTOR_2 1u
  18. #define SECTOR_3 2u
  19. #define SECTOR_4 3u
  20. #define SECTOR_5 4u
  21. #define SECTOR_6 5u
  22. #define SECTOR_UKNOW 0xFF
  23. typedef enum {
  24. EPM_Dir_None,
  25. EPM_Dir_Back,
  26. EPM_Dir_Forward,
  27. }epm_dir_t;
  28. typedef enum {
  29. FOC_Success = 0,
  30. FOC_NotAllowed = 1,
  31. FOC_Have_CritiCal_Err,
  32. FOC_Throttle_Err, //ready的时候检测到转把信号
  33. FOC_NowAllowed_With_Speed,
  34. FOC_Speed_TooLow,
  35. FOC_NotCruiseMode,
  36. FOC_Param_Err,
  37. FOC_MEM_Err,
  38. FOC_CRC_Err,
  39. FOC_Unknow_Cmd,
  40. }error_code_t;
  41. typedef enum {
  42. FOC_CRIT_OV_Vol_Err,
  43. FOC_CRIT_UN_Vol_Err,
  44. FOC_CRIT_ACC_OV_Err,
  45. FOC_CRIT_ACC_Un_Err,
  46. FOC_CRIT_Phase_Err,
  47. FOC_CRIT_Encoder_Err, /* 编码器错误,可能还是可以骑行,取决无感是否稳定 */
  48. FOC_CRIT_Angle_Err, /* FOC 角度错误,一般发生在编码器错误,同时无感没有稳定的情况下,必须要停机 */
  49. FOC_CRIT_CURR_OFF_Err,
  50. FOC_CRIT_H_MOS_Err,
  51. FOC_CRIT_L_MOS_Err,
  52. FOC_CRIT_Phase_Conn_Err,
  53. FOC_CRIT_MOTOR_TEMP_Lim,
  54. FOC_CRIT_MOS_TEMP_Lim,
  55. FOC_CRIT_Fan_Err,
  56. FOC_CRIT_IDC_OV,
  57. FOC_CRIT_THRO_Err,
  58. FOC_CRIT_ENC_AB_Err,
  59. FOC_CRIT_Vol_HW_Err, //17
  60. FOC_CRIT_PHASE_UNBalance_Err, /* 三相不平衡错误,比如相线螺丝松了 */
  61. FOC_CRIT_THRO2_Err,
  62. FOC_CRIT_MOT_TEMP_Sensor,
  63. FOC_CRIT_MOS_TEMP_Sensor, //21
  64. FOC_CRIT_BRK_Err,
  65. FOC_CRIT_Err_Max = 32,
  66. }mctrl_critical_ebit_t;
  67. #define FOC_Cri_Err_Mask(err) (1<<(err))
  68. typedef enum {
  69. FOC_EV_MOT_Limit_L=FOC_CRIT_Err_Max + 1,
  70. FOC_EV_MOS_Limit_L,
  71. FOC_EV_THRO_START_V,
  72. FOC_START_Err_Code,
  73. FOC_Reset_Reson,
  74. }ctrl_event_r_t;
  75. typedef struct {
  76. float mot_vel;
  77. float torque;
  78. float dc_curr;
  79. float phase_curr; //最大相电流
  80. float ebrk_dc_curr; //最大母线回收电流
  81. float ebrk_torque;
  82. float dc_vol_min;
  83. float dc_vol_max;
  84. }user_limit;
  85. typedef struct {
  86. lineramp_t vel;
  87. lineramp_t torque;
  88. lineramp_t dc_curr;
  89. }rt_lim_ramper;
  90. typedef struct {
  91. float mot_vel;
  92. float phase_curr;
  93. float phase_vol;
  94. float fw_id; //D轴最大退磁电流
  95. float dc_curr;
  96. float dc_vol;
  97. float torque;
  98. }hw_limit;
  99. typedef struct {
  100. float dc_curr;
  101. float torque;
  102. }prot_limit;
  103. typedef struct{
  104. bool b_start;
  105. bool b_ebrk_running;
  106. bool b_hw_braker;
  107. bool b_mtpa_calibrate;
  108. bool b_cruiseEna;
  109. bool b_AutoHold;
  110. float adv_angle; //dq 分配超前角
  111. float force_angle;
  112. float angle_last;
  113. float target_torque;
  114. float target_torque_raw; //扭矩过零点处理前的扭矩
  115. dq_t target_idq;
  116. dq_t out_idq_filterd;
  117. float out_current_vec;
  118. u32 ebrk_ramp_time;
  119. u32 torque_acc_time;
  120. u32 torque_dec_time;
  121. float dc_curr_filted;
  122. float dc_curr_calc;
  123. float phase_curr_filted[3];
  124. float autohold_torque;
  125. u8 mode_req;
  126. u8 mode_running;
  127. u8 error;
  128. foc_t foc;
  129. etcs_t etcs;
  130. PI_Controller pi_lock;
  131. PI_Controller pi_power;
  132. PI_Controller pi_vel_lim;
  133. PI_Controller pi_vel;
  134. user_limit userlim;
  135. hw_limit hwlim;
  136. prot_limit protlim;
  137. lineramp_t target_current;
  138. lineramp_t cruise_vel;
  139. lineramp_t target_vd;
  140. lineramp_t target_vq;
  141. lineramp_t target_vel;
  142. lineramp_t vel_lim;
  143. lineramp_t torque_lim;
  144. lineramp_t dc_curr_lim;
  145. lineramp_t input_torque;
  146. float phase_a_max;
  147. float phase_b_max;
  148. float phase_c_max;
  149. u32 phase_unbalance_cnt;
  150. }mot_contrl_t;
  151. void mot_contrl_init(mot_contrl_t *ctrl);
  152. bool mot_contrl_enable(mot_contrl_t *ctrl, bool start);
  153. bool mot_contrl_update(mot_contrl_t *ctrl);
  154. u8 mot_contrl_mode(mot_contrl_t *ctrl);
  155. bool mot_contrl_request_mode(mot_contrl_t *ctrl, u8 mode);
  156. void mot_contrl_slow_task(mot_contrl_t *ctrl);
  157. u8 mot_contrl_protect(mot_contrl_t *ctrl);
  158. float mot_contrl_get_speed(mot_contrl_t *ctrl);
  159. void mot_contrl_velloop_params(mot_contrl_t *ctrl, float wcv, float b0);
  160. void mot_contrl_trqloop_params(mot_contrl_t *ctrl, float wcv, float b0);
  161. void mot_contrl_set_dccurr_limit(mot_contrl_t *ctrl, float ibusLimit);
  162. void mot_contrl_set_vel_limit(mot_contrl_t *ctrl, float vel);
  163. void mot_contrl_set_torque_limit(mot_contrl_t *ctrl, float torque);
  164. float mot_contrl_get_ebrk_torque(mot_contrl_t *ctrl);
  165. void mot_contrl_set_ebrk_time(mot_contrl_t *ctrl, u32 time);
  166. void mot_contrl_set_vdq(mot_contrl_t *ctrl, float vd, float vq);
  167. void mot_contrl_set_vdq_immediate(mot_contrl_t *ctrl, float vd, float vq);
  168. bool mot_contrl_set_cruise(mot_contrl_t *ctrl, bool enable);
  169. bool mot_contrl_resume_cruise(mot_contrl_t *ctrl);
  170. bool mot_contrl_set_cruise_speed(mot_contrl_t *ctrl, float rpm);
  171. bool mot_contrl_set_current(mot_contrl_t *ctrl, float is);
  172. void mot_contrl_set_torque_ramp_time(mot_contrl_t *ctrl, u32 acc, u32 dec);
  173. void mot_contrl_set_torque_acc_time(mot_contrl_t *ctrl, u32 acc);
  174. bool mot_contrl_set_torque(mot_contrl_t *ctrl, float torque);
  175. void mot_contrl_mtpa_calibrate(mot_contrl_t *ctrl, bool enable);
  176. void mot_contrl_set_autohold(mot_contrl_t *ctrl, bool lock);
  177. bool mot_contrl_energy_recovery(mot_contrl_t *ctrl, bool start);
  178. void mot_contrl_set_hw_brake(mot_contrl_t *ctrl, bool hw_brake);
  179. void mot_contrl_calc_current(mot_contrl_t *ctrl);
  180. void mot_contrl_get_pid(mot_contrl_t *ctrl, u8 id, float *kp, float *ki, float *kd);
  181. void mot_contrl_set_pid(mot_contrl_t *ctrl, u8 id, float kp, float ki, float kd);
  182. void mot_contrl_set_torque_limit_rttime(mot_contrl_t *ctrl, u32 time);
  183. void mot_contrl_set_vel_limit_rttime(mot_contrl_t *ctrl, u32 time);
  184. bool mot_contrl_set_force_torque(mot_contrl_t *ctrl, float torque);
  185. static __INLINE bool mot_contrl_start(mot_contrl_t *ctrl, u8 mode) {
  186. mot_contrl_enable(ctrl, true);
  187. return mot_contrl_request_mode(ctrl, mode);
  188. }
  189. static __INLINE bool mot_contrl_stop(mot_contrl_t *ctrl) {
  190. return mot_contrl_enable(ctrl, false);
  191. }
  192. static __INLINE bool mot_contrl_is_start(mot_contrl_t *ctrl) {
  193. return ctrl->b_start;
  194. }
  195. static __INLINE bool mot_contrl_dccurr_is_protected(mot_contrl_t *ctrl) {
  196. return (ctrl->protlim.dc_curr != HW_LIMIT_NONE);
  197. }
  198. static __INLINE bool mot_contrl_torque_is_protected(mot_contrl_t *ctrl) {
  199. return (ctrl->protlim.torque != HW_LIMIT_NONE);
  200. }
  201. static __INLINE void mot_contrl_set_ebrk_torquer(mot_contrl_t *ctrl, s16 torque) {
  202. ctrl->userlim.ebrk_torque = torque;
  203. }
  204. static __INLINE void mot_contrl_set_error(mot_contrl_t *ctrl, u8 error) {
  205. ctrl->error = error;
  206. }
  207. static __INLINE u8 mot_contrl_get_errcode(mot_contrl_t *ctrl) {
  208. return ctrl->error;
  209. }
  210. static __INLINE void mot_contrl_pause_cruise(mot_contrl_t *ctrl) {
  211. ctrl->b_cruiseEna = false;
  212. }
  213. static __INLINE bool mot_contrl_is_cruise_enabled(mot_contrl_t *ctrl) {
  214. return (ctrl->b_cruiseEna && (ctrl->mode_running == CTRL_MODE_SPD));
  215. }
  216. static __INLINE bool mot_contrl_set_target_vel(mot_contrl_t *ctrl, float vel) {
  217. if (ctrl->b_cruiseEna) {
  218. return false;
  219. }
  220. line_ramp_set_target(&ctrl->target_vel ,min(ABS(vel), ctrl->userlim.mot_vel)*SIGN(vel));
  221. return true;
  222. }
  223. static __INLINE void mot_contrl_set_vel_rttime(mot_contrl_t *ctrl, u32 time) {
  224. line_ramp_set_time(&ctrl->target_vel, (float)time);
  225. line_ramp_update(&ctrl->target_vel);
  226. }
  227. static __INLINE void mot_contrl_set_angle(mot_contrl_t *ctrl, float angle) {
  228. ctrl->force_angle = (angle);
  229. }
  230. static __INLINE void mot_contrl_set_adv_angle(mot_contrl_t *ctrl, float angle) {
  231. ctrl->adv_angle = (angle);
  232. }
  233. static __INLINE bool mot_contrl_is_auto_holdding(mot_contrl_t *ctrl) {
  234. return ctrl->b_AutoHold;
  235. }
  236. static __INLINE float mot_contrl_get_current_vector(mot_contrl_t *ctrl) {
  237. return ctrl->out_current_vec;
  238. }
  239. static __INLINE float mot_contrl_get_dc_current(mot_contrl_t *ctrl) {
  240. return ctrl->dc_curr_filted;
  241. }
  242. static __INLINE bool mot_contrl_ebrk_is_enabled(mot_contrl_t *ctrl) {
  243. return mot_contrl_get_ebrk_torque(ctrl) != 0;
  244. }
  245. static __INLINE bool mot_contrl_ebrk_is_running(mot_contrl_t *ctrl) {
  246. return ctrl->mode_running == CTRL_MODE_EBRAKE;
  247. }
  248. static __INLINE float mot_contrl_get_final_torque(mot_contrl_t *ctrl) {
  249. return ctrl->input_torque.target;
  250. }
  251. static __INLINE u32 mot_contrl_get_torque_acc_time(mot_contrl_t *ctrl) {
  252. return ctrl->input_torque.time;
  253. }
  254. #endif /* _CONTROLLER_H__ */