controller.h 9.4 KB

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