controller.h 9.8 KB

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