controller.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. FOC_NV_Err,
  75. }ctrl_event_r_t;
  76. typedef struct {
  77. float elec_w_acc;
  78. float max_vel; //rpm
  79. float iq_set;
  80. float curr_vel;
  81. float curr_vel_rpm;
  82. float curr_angle;
  83. float angle_deg;
  84. float b_ena;
  85. }IF_t;
  86. typedef struct {
  87. float mot_vel;
  88. float torque;
  89. float dc_curr;
  90. float phase_curr; //最大相电流
  91. float ebrk_dc_curr; //最大母线回收电流
  92. float ebrk_torque;
  93. float dc_vol_min;
  94. float dc_vol_max;
  95. }user_limit;
  96. typedef struct {
  97. lineramp_t vel;
  98. lineramp_t torque;
  99. lineramp_t dc_curr;
  100. }rt_lim_ramper;
  101. typedef struct {
  102. float mot_vel;
  103. float phase_curr;
  104. //float phase_vol;
  105. float fw_id; //D轴最大退磁电流
  106. float dc_curr;
  107. //float dc_vol;
  108. float torque;
  109. }hw_limit;
  110. typedef struct {
  111. float dc_curr;
  112. float torque;
  113. }prot_limit;
  114. #if 0
  115. typedef struct {
  116. bool b_ena;
  117. bool b_sample;
  118. s16 v_inj;
  119. float v_alpha_inj;
  120. float v_beta_inj;
  121. float sample_prev;
  122. float sample_now;
  123. float vel_integrator;
  124. float angle_rad;
  125. float angle_deg;
  126. float elec_vel;
  127. float max_vel;
  128. float int_gain2;
  129. float int_gain;
  130. float inv_ld_lq;
  131. }hfi_t;
  132. #else
  133. typedef struct {
  134. bool b_ena;
  135. float sign;
  136. bool ipd; //初始位置检测
  137. bool hfi_ready;
  138. float v_inj;
  139. int n_samples;
  140. float v_d_inj;
  141. float v_q_inj;
  142. albt_t hi_alpha_beta;
  143. albt_t sample_prev;
  144. float rad_integrator;
  145. float angle_deg;
  146. float elec_vel;
  147. float rpm_vel;
  148. PI_Controller pi;
  149. }hfi_t;
  150. #endif
  151. typedef struct{
  152. bool b_start;
  153. bool b_ebrk_running;
  154. bool b_hw_braker;
  155. bool b_mtpa_calibrate;
  156. bool b_cruiseEna;
  157. bool b_AutoHold;
  158. float adv_angle; //dq 分配超前角
  159. float force_angle;
  160. float angle_last;
  161. float target_torque;
  162. float target_torque_raw; //扭矩过零点处理前的扭矩
  163. dq_t target_idq;
  164. dq_t out_idq_filterd;
  165. albt_t phase_v_ab; //sampled
  166. dq_t phase_v_dq;
  167. float out_current_vec;
  168. u32 ebrk_ramp_time;
  169. u32 torque_acc_time;
  170. u32 torque_dec_time;
  171. float dc_curr_filted;
  172. float dc_curr_calc;
  173. float phase_curr_filted[3];
  174. float autohold_torque;
  175. float duty_raw;
  176. float duty_filterd;
  177. u8 mode_req;
  178. u8 mode_running;
  179. float mot_param_ind_freq;
  180. u8 error;
  181. foc_t foc;
  182. etcs_t etcs;
  183. PI_Controller pi_lock;
  184. PI_Controller pi_power;
  185. PI_Controller pi_vel_lim;
  186. PI_Controller pi_vel;
  187. user_limit userlim;
  188. hw_limit hwlim;
  189. prot_limit protlim;
  190. lineramp_t ramp_target_current;
  191. lineramp_t ramp_cruise_vel;
  192. lineramp_t ramp_target_vd;
  193. lineramp_t ramp_target_vq;
  194. lineramp_t ramp_target_vel;
  195. lineramp_t ramp_vel_lim;
  196. lineramp_t ramp_torque_lim;
  197. lineramp_t ramp_dc_curr_lim;
  198. lineramp_t ramp_input_torque;
  199. lineramp_t ramp_adv_angle;
  200. float phase_a_max;
  201. float phase_b_max;
  202. float phase_c_max;
  203. u32 phase_unbalance_cnt;
  204. IF_t if_ctl;
  205. hfi_t hfi;
  206. }mot_contrl_t;
  207. void mot_contrl_init(mot_contrl_t *ctrl);
  208. bool mot_contrl_enable(mot_contrl_t *ctrl, bool start);
  209. bool mot_contrl_update(mot_contrl_t *ctrl);
  210. u8 mot_contrl_mode(mot_contrl_t *ctrl);
  211. bool mot_contrl_request_mode(mot_contrl_t *ctrl, u8 mode);
  212. void mot_contrl_slow_task(mot_contrl_t *ctrl);
  213. u8 mot_contrl_protect(mot_contrl_t *ctrl);
  214. float mot_contrl_get_speed(mot_contrl_t *ctrl);
  215. void mot_contrl_velloop_params(mot_contrl_t *ctrl, float wcv, float b0);
  216. void mot_contrl_trqloop_params(mot_contrl_t *ctrl, float wcv, float b0);
  217. void mot_contrl_set_dccurr_limit(mot_contrl_t *ctrl, float ibusLimit);
  218. void mot_contrl_set_vel_limit(mot_contrl_t *ctrl, float vel);
  219. void mot_contrl_set_torque_limit(mot_contrl_t *ctrl, float torque);
  220. float mot_contrl_get_ebrk_torque(mot_contrl_t *ctrl);
  221. void mot_contrl_set_ebrk_time(mot_contrl_t *ctrl, u32 time);
  222. void mot_contrl_set_vdq(mot_contrl_t *ctrl, float vd, float vq);
  223. void mot_contrl_set_vdq_immediate(mot_contrl_t *ctrl, float vd, float vq);
  224. bool mot_contrl_set_cruise(mot_contrl_t *ctrl, bool enable);
  225. bool mot_contrl_resume_cruise(mot_contrl_t *ctrl);
  226. bool mot_contrl_set_cruise_speed(mot_contrl_t *ctrl, float rpm);
  227. bool mot_contrl_set_current(mot_contrl_t *ctrl, float is);
  228. void mot_contrl_set_torque_ramp_time(mot_contrl_t *ctrl, u32 acc, u32 dec);
  229. void mot_contrl_set_torque_acc_time(mot_contrl_t *ctrl, u32 acc);
  230. bool mot_contrl_set_torque(mot_contrl_t *ctrl, float torque);
  231. void mot_contrl_mtpa_calibrate(mot_contrl_t *ctrl, bool enable);
  232. void mot_contrl_set_autohold(mot_contrl_t *ctrl, bool lock);
  233. bool mot_contrl_set_ebreak(mot_contrl_t *ctrl, bool start);
  234. void mot_contrl_set_hw_brake(mot_contrl_t *ctrl, bool hw_brake);
  235. void mot_contrl_calc_current(mot_contrl_t *ctrl);
  236. void mot_contrl_get_pid(mot_contrl_t *ctrl, u8 id, float *kp, float *ki, float *kd);
  237. void mot_contrl_set_pid(mot_contrl_t *ctrl, u8 id, float kp, float ki, float kd);
  238. void mot_contrl_set_torque_limit_rttime(mot_contrl_t *ctrl, u32 time);
  239. void mot_contrl_set_vel_limit_rttime(mot_contrl_t *ctrl, u32 time);
  240. bool mot_contrl_set_force_torque(mot_contrl_t *ctrl, float torque);
  241. void mot_contrl_IF_start(mot_contrl_t *ctrl, bool start, float accl, float max_vel, float iq_set);
  242. void mot_contrl_enable_hfi(mot_contrl_t *ctrl, bool enable);
  243. static __INLINE bool mot_contrl_start(mot_contrl_t *ctrl, u8 mode) {
  244. mot_contrl_enable(ctrl, true);
  245. return mot_contrl_request_mode(ctrl, mode);
  246. }
  247. static __INLINE bool mot_contrl_stop(mot_contrl_t *ctrl) {
  248. return mot_contrl_enable(ctrl, false);
  249. }
  250. static __INLINE bool mot_contrl_is_start(mot_contrl_t *ctrl) {
  251. return ctrl->b_start;
  252. }
  253. static __INLINE bool mot_contrl_dccurr_is_protected(mot_contrl_t *ctrl) {
  254. return (ctrl->protlim.dc_curr != HW_LIMIT_NONE);
  255. }
  256. static __INLINE bool mot_contrl_torque_is_protected(mot_contrl_t *ctrl) {
  257. return (ctrl->protlim.torque != HW_LIMIT_NONE);
  258. }
  259. static __INLINE void mot_contrl_set_ebrk_torquer(mot_contrl_t *ctrl, s16 torque) {
  260. ctrl->userlim.ebrk_torque = torque;
  261. }
  262. static __INLINE void mot_contrl_set_error(mot_contrl_t *ctrl, u8 error) {
  263. ctrl->error = error;
  264. }
  265. static __INLINE u8 mot_contrl_get_errcode(mot_contrl_t *ctrl) {
  266. return ctrl->error;
  267. }
  268. static __INLINE void mot_contrl_pause_cruise(mot_contrl_t *ctrl) {
  269. ctrl->b_cruiseEna = false;
  270. }
  271. static __INLINE bool mot_contrl_is_cruise_enabled(mot_contrl_t *ctrl) {
  272. return (ctrl->b_cruiseEna && (ctrl->mode_running == CTRL_MODE_SPD));
  273. }
  274. static __INLINE bool mot_contrl_set_target_vel(mot_contrl_t *ctrl, float vel) {
  275. if (ctrl->b_cruiseEna) {
  276. return false;
  277. }
  278. line_ramp_set_target(&ctrl->ramp_target_vel ,min(ABS(vel), ctrl->userlim.mot_vel)*SIGN(vel));
  279. return true;
  280. }
  281. static __INLINE void mot_contrl_set_vel_rttime(mot_contrl_t *ctrl, u32 time) {
  282. line_ramp_set_time(&ctrl->ramp_target_vel, (float)time);
  283. line_ramp_update(&ctrl->ramp_target_vel);
  284. }
  285. static __INLINE void mot_contrl_set_angle(mot_contrl_t *ctrl, float angle) {
  286. ctrl->force_angle = (angle);
  287. }
  288. static __INLINE void mot_contrl_set_adv_angle(mot_contrl_t *ctrl, float angle) {
  289. ctrl->adv_angle = (angle);
  290. line_ramp_set_target(&ctrl->ramp_adv_angle, angle);
  291. }
  292. static __INLINE bool mot_contrl_is_auto_holdding(mot_contrl_t *ctrl) {
  293. return ctrl->b_AutoHold;
  294. }
  295. static __INLINE float mot_contrl_get_current_vector(mot_contrl_t *ctrl) {
  296. return ctrl->out_current_vec;
  297. }
  298. static __INLINE float mot_contrl_get_dc_current(mot_contrl_t *ctrl) {
  299. return ctrl->dc_curr_filted;
  300. }
  301. static __INLINE bool mot_contrl_ebrk_is_enabled(mot_contrl_t *ctrl) {
  302. return mot_contrl_get_ebrk_torque(ctrl) != 0;
  303. }
  304. static __INLINE bool mot_contrl_ebrk_is_running(mot_contrl_t *ctrl) {
  305. return ctrl->mode_running == CTRL_MODE_EBRAKE;
  306. }
  307. static __INLINE float mot_contrl_get_final_torque(mot_contrl_t *ctrl) {
  308. return ctrl->ramp_input_torque.target;
  309. }
  310. static __INLINE u32 mot_contrl_get_torque_acc_time(mot_contrl_t *ctrl) {
  311. return ctrl->ramp_input_torque.time;
  312. }
  313. static __INLINE s16 mot_contrl_get_speed_abs(mot_contrl_t *ctrl) {
  314. s16 speed = (s16)mot_contrl_get_speed(ctrl);
  315. return ABS(speed);
  316. }
  317. static __INLINE void mot_ctrl_set_ind_freq(mot_contrl_t *ctrl, float freq) {
  318. ctrl->mot_param_ind_freq = freq;
  319. }
  320. static __INLINE bool mot_contrl_hfi_is_ipd(mot_contrl_t *ctrl) {
  321. return ctrl->hfi.ipd;
  322. }
  323. #endif /* _CONTROLLER_H__ */