controller.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. #include "controller.h"
  2. #include "foc/mc_config.h"
  3. #include "foc/foc_config.h"
  4. #include "foc/core/foc_observer.h"
  5. #include "foc/motor/motor_param.h"
  6. #include "foc/motor/motor.h"
  7. #include "foc/samples.h"
  8. #include "foc/core/f_calc.h"
  9. #include "foc/motor/current.h"
  10. #include "foc/mc_error.h"
  11. static void mot_contrl_pid(mot_contrl_t *ctrl);
  12. static void mot_contrl_ulimit(mot_contrl_t *ctrl);
  13. static void mot_contrl_rtlimit(mot_contrl_t *ctrl);
  14. void mot_contrl_init(mot_contrl_t *ctrl) {
  15. memset(ctrl, 0, sizeof(mot_contrl_t));
  16. ctrl->foc.ts = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
  17. ctrl->foc.half_period = FOC_PWM_Half_Period;
  18. ctrl->force_angle = INVALID_ANGLE;
  19. ctrl->adv_angle = INVALID_ANGLE;
  20. foc_init(&ctrl->foc);
  21. ctrl->hwlim.dc_curr = CONFIG_HW_MAX_DC_CURRENT;
  22. ctrl->hwlim.mot_vel = CONFIG_HW_MAX_MOTOR_RPM;
  23. ctrl->hwlim.phase_curr = CONFIG_HW_MAX_PHASE_CURR;
  24. ctrl->hwlim.phase_vol = CONFIG_HW_MAX_PHASE_VOL;
  25. ctrl->hwlim.dc_vol = CONFIG_HW_MAX_DC_VOLTAGE;
  26. ctrl->hwlim.torque = mc_conf()->m.max_torque; //电机的最大扭矩
  27. ctrl->hwlim.fw_id = mc_conf()->m.max_fw_id; //电池能支持的最大弱磁电流
  28. ctrl->protlim.dc_curr = HW_LIMIT_NONE;
  29. ctrl->protlim.torque = HW_LIMIT_NONE;
  30. }
  31. bool mot_contrl_enable(mot_contrl_t *ctrl, bool start) {
  32. if (ctrl->b_start == start) {
  33. return true;
  34. }
  35. if (start) {
  36. mot_contrl_pid(ctrl);
  37. mot_contrl_ulimit(ctrl);
  38. mot_contrl_rtlimit(ctrl);
  39. line_ramp_init(&ctrl->torque_lim, CONFIG_LIMIT_RAMP_TIME);
  40. line_ramp_init(&ctrl->dc_curr_lim, CONFIG_LIMIT_RAMP_TIME);
  41. line_ramp_init(&ctrl->vel_lim, CONFIG_LIMIT_RAMP_TIME);
  42. line_ramp_init(&ctrl->cruise_vel, CONFIG_CRUISE_RAMP_TIME);
  43. line_ramp_init(&ctrl->target_vd, CONFIG_FOC_VDQ_RAMP_FINAL_TIME);
  44. line_ramp_init(&ctrl->target_vq, CONFIG_FOC_VDQ_RAMP_FINAL_TIME);
  45. line_ramp_init(&ctrl->target_vel, CONFIG_CRUISE_RAMP_TIME);
  46. line_ramp_init(&ctrl->target_current, CONFIG_CURRENT_RAMP_TIME);
  47. line_ramp_init(&ctrl->input_torque, CONFIG_DEFAULT_TORQUE_RAMP_TIME);
  48. }
  49. ctrl->mode_req = ctrl->mode_running = CTRL_MODE_OPEN;
  50. ctrl->force_angle = INVALID_ANGLE;
  51. ctrl->adv_angle = INVALID_ANGLE;
  52. ctrl->angle_last = INVALID_ANGLE;
  53. ctrl->b_AutoHold = false;
  54. ctrl->b_cruiseEna = false;
  55. ctrl->b_mtpa_calibrate = false;
  56. ctrl->dc_curr_filted = 0;
  57. ctrl->phase_curr_filted[0] = ctrl->phase_curr_filted[1] = 0;
  58. ctrl->out_idq_filterd.d = ctrl->out_idq_filterd.q = 0;
  59. foc_init(&ctrl->foc);
  60. foc_observer_init();
  61. ctrl->b_start = start;
  62. return true;
  63. }
  64. bool mot_contrl_request_mode(mot_contrl_t *ctrl, u8 mode) {
  65. if (mode > CTRL_MODE_EBRAKE) {
  66. mot_contrl_set_error(ctrl, FOC_Param_Err);
  67. return false;
  68. }
  69. ctrl->mode_req = mode;
  70. return true;
  71. }
  72. u8 mot_contrl_mode(mot_contrl_t *ctrl) {
  73. u8 preMode = ctrl->mode_running;
  74. if (!ctrl->b_start) {
  75. ctrl->mode_running = CTRL_MODE_OPEN;
  76. }else if (ctrl->mode_req == CTRL_MODE_OPEN) {
  77. ctrl->mode_running = CTRL_MODE_OPEN;
  78. }else if (ctrl->mode_req == CTRL_MODE_SPD || ctrl->b_cruiseEna){
  79. ctrl->mode_running = CTRL_MODE_SPD;
  80. }else if (ctrl->mode_req == CTRL_MODE_CURRENT) {
  81. ctrl->mode_running = CTRL_MODE_CURRENT;
  82. }else if (ctrl->mode_req == CTRL_MODE_EBRAKE) {
  83. ctrl->mode_running = CTRL_MODE_EBRAKE;
  84. }else {
  85. if (!ctrl->b_cruiseEna) {
  86. ctrl->mode_running = CTRL_MODE_TRQ;
  87. }
  88. }
  89. if (preMode != ctrl->mode_running) {
  90. if ((preMode != ctrl->mode_running) && (ctrl->mode_running == CTRL_MODE_TRQ)) {
  91. line_ramp_set_acctime(&ctrl->input_torque, ctrl->torque_acc_time);
  92. line_ramp_set_dectime(&ctrl->input_torque, ctrl->torque_acc_time);
  93. line_ramp_update(&ctrl->input_torque);
  94. if (preMode == CTRL_MODE_SPD) {
  95. ctrl->target_torque_raw = ctrl->target_torque;
  96. PI_Controller_Reset(&ctrl->pi_vel_lim, ctrl->target_torque);
  97. }else if (preMode == CTRL_MODE_CURRENT) {
  98. ctrl->target_torque_raw = ctrl->target_torque;
  99. PI_Controller_Reset(&ctrl->pi_vel_lim, ctrl->target_torque);
  100. }
  101. }else if ((preMode == CTRL_MODE_TRQ) && (ctrl->mode_running == CTRL_MODE_SPD)) {
  102. PI_Controller_Reset(&ctrl->pi_vel, ctrl->target_torque);
  103. }else if ((preMode != ctrl->mode_running) && (ctrl->mode_running == CTRL_MODE_EBRAKE)) {
  104. line_ramp_reset(&ctrl->input_torque, ctrl->target_torque);
  105. line_ramp_set_time(&ctrl->input_torque, ctrl->ebrk_ramp_time);
  106. line_ramp_set_target(&ctrl->input_torque, motor_get_ebreak_toruqe(ctrl->foc.in.mot_velocity));
  107. }else if ((preMode == CTRL_MODE_EBRAKE) && (ctrl->mode_running == CTRL_MODE_SPD)) {
  108. PI_Controller_Reset(&ctrl->pi_vel, F_get_air());
  109. }
  110. }
  111. if (ctrl->mode_running == CTRL_MODE_OPEN) {
  112. line_ramp_step(&ctrl->target_vd);
  113. line_ramp_step(&ctrl->target_vq);
  114. }
  115. return ctrl->mode_running;
  116. }
  117. static __INLINE void phase_curr_unbal_check(mot_contrl_t *ctrl) {
  118. static u32 _cycle_cnt = 0, _last_mod_cnt = 0;
  119. static float a_max = 0, b_max = 0, c_max = 0;
  120. static u32 _unbalance_cnt = 0;
  121. static u32 _unbalance_time = 0;
  122. foc_t *foc = &ctrl->foc;
  123. float lowpass = foc->mot_vel_radusPers * FOC_CTRL_US / 2.0f;
  124. if (lowpass > 1.0f) {
  125. lowpass = 1.0f;
  126. }
  127. LowPass_Filter(ctrl->phase_curr_filted[0], foc->in.curr_abc[0], lowpass);
  128. LowPass_Filter(ctrl->phase_curr_filted[1], foc->in.curr_abc[1], lowpass);
  129. ctrl->phase_curr_filted[2] = -(ctrl->phase_curr_filted[0] + ctrl->phase_curr_filted[1]);
  130. if ((ctrl->angle_last == INVALID_ANGLE) || (foc->mot_vel_radusPers < 100)) {
  131. ctrl->angle_last = foc->in.mot_angle;
  132. a_max = b_max = c_max = 0;
  133. _unbalance_cnt = 0;
  134. _unbalance_time = get_tick_ms();
  135. _cycle_cnt = 0;
  136. _last_mod_cnt = 0;
  137. return;
  138. }
  139. float delta_angle = foc->in.mot_angle - ctrl->angle_last;
  140. if (delta_angle > 200 || delta_angle < -200) { //one cycle
  141. _cycle_cnt ++;
  142. }
  143. ctrl->angle_last = foc->in.mot_angle;
  144. u32 mod_cnt = _cycle_cnt % CONFIG_PHASE_UNBALANCE_PEAK_CNT;
  145. bool trigger = false;
  146. if ((mod_cnt == 0) && (_last_mod_cnt != mod_cnt)) {
  147. trigger = true;
  148. }
  149. _last_mod_cnt = mod_cnt;
  150. a_max = MAX(a_max, ctrl->phase_curr_filted[0] * (2.2f));
  151. b_max = MAX(b_max, ctrl->phase_curr_filted[1] * (2.2f));
  152. c_max = MAX(c_max, ctrl->phase_curr_filted[2] * (2.2f));
  153. if (trigger) { //经过CONFIG_PEAK_CNT个周期,已经得到peak值
  154. float i_min = 1000.0f, i_max = 0;
  155. if (a_max > i_max) {
  156. i_max = a_max;
  157. }
  158. if (a_max < i_min) {
  159. i_min = a_max;
  160. }
  161. if (b_max > i_max) {
  162. i_max = b_max;
  163. }
  164. if (b_max < i_min) {
  165. i_min = b_max;
  166. }
  167. if (c_max > i_max) {
  168. i_max = c_max;
  169. }
  170. if (c_max < i_min) {
  171. i_min = c_max;
  172. }
  173. float unbalance_r = (i_max - i_min - CONFIG_PHASE_UNBALANCE_THROLD)/(i_max + 1e-8f);
  174. if (unbalance_r >= CONFIG_PHASE_UNBALANCE_R) {
  175. if ((_unbalance_cnt++ >= 500) || (get_delta_ms(_unbalance_time) >= 1000*10)) {
  176. if (mc_set_critical_error(FOC_CRIT_PHASE_UNBalance_Err)) {
  177. mc_crit_err_add(FOC_CRIT_PHASE_UNBalance_Err, (s16)i_max, (s16)i_min);
  178. }
  179. }
  180. }else {
  181. _unbalance_cnt = 0;
  182. _unbalance_time = get_tick_ms();
  183. }
  184. a_max = b_max = c_max = 0;
  185. }
  186. }
  187. bool mot_contrl_update(mot_contrl_t *ctrl) {
  188. foc_t *foc = &ctrl->foc;
  189. phase_current_get(foc->in.curr_abc);
  190. clark(foc->in.curr_abc[0], foc->in.curr_abc[1], foc->in.curr_abc[2], &foc->in.curr_ab);
  191. foc_observer_update(foc->out.vol_albeta.a * TWO_BY_THREE, foc->out.vol_albeta.b * TWO_BY_THREE, foc->in.curr_ab.a, foc->in.curr_ab.b);
  192. float enc_angle = motor_encoder_get_angle();
  193. float enc_vel = motor_encoder_get_speed();
  194. if (!foc_observer_diagnostic(enc_angle, enc_vel)){
  195. /* detect encoder angle error, do something here */
  196. if (!foc_observer_sensorless_stable()) {
  197. foc->in.mot_velocity = 0;
  198. return false;
  199. }
  200. enc_angle = foc_observer_sensorless_angle();
  201. enc_vel = foc_observer_sensorless_speed();
  202. }
  203. if (!ctrl->b_mtpa_calibrate && (ctrl->force_angle != INVALID_ANGLE)) {
  204. foc->in.mot_angle = ctrl->force_angle;
  205. }else {
  206. foc->in.mot_angle = enc_angle;
  207. }
  208. foc->in.mot_velocity = enc_vel;
  209. foc->in.dc_vol = get_vbus_float();
  210. foc->in.b_openloop = ctrl->mode_running == CTRL_MODE_OPEN;
  211. phase_curr_unbal_check(ctrl);
  212. if (foc->in.b_openloop) {
  213. foc->in.target_vol_dq.d = ctrl->target_vd.interpolation;
  214. foc->in.target_vol_dq.q = ctrl->target_vq.interpolation;
  215. }
  216. foc_update(foc);
  217. float lowpass = foc->mot_vel_radusPers * FOC_CTRL_US * 2;
  218. LowPass_Filter(ctrl->out_idq_filterd.d, foc->out.curr_dq.d ,lowpass);
  219. LowPass_Filter(ctrl->out_idq_filterd.q, foc->out.curr_dq.q ,lowpass);
  220. return true;
  221. }
  222. static __INLINE float mot_contrl_dc_curr_limiter(mot_contrl_t *ctrl, float maxTrq) {
  223. ctrl->pi_power.max = maxTrq;
  224. float errRef = ctrl->dc_curr_lim.interpolation - ctrl->dc_curr_filted;
  225. return PI_Controller_Run(&ctrl->pi_power, errRef);
  226. }
  227. static __INLINE float mot_contrl_vel_limiter(mot_contrl_t *ctrl, float maxTrq) {
  228. ctrl->pi_vel_lim.max = maxTrq;
  229. ctrl->pi_vel_lim.min = 0;
  230. float err = ctrl->vel_lim.interpolation - ctrl->foc.in.mot_velocity;
  231. return PI_Controller_RunVel(&ctrl->pi_vel_lim, err);
  232. }
  233. /* current vector or torque to dq axis current */
  234. static void mot_contrl_dq_assign(mot_contrl_t *ctrl) {
  235. if (ctrl->mode_running == CTRL_MODE_CURRENT) {
  236. float target_current = ctrl->target_current.interpolation;
  237. if (ctrl->b_mtpa_calibrate && (ctrl->adv_angle != INVALID_ANGLE)) {
  238. float s, c;
  239. normal_sincosf(degree_2_pi(ctrl->adv_angle + 90.0f), &s, &c);
  240. ctrl->target_idq.d = target_current * c;
  241. if (ctrl->target_idq.d > ctrl->hwlim.fw_id) {
  242. ctrl->target_idq.d = ctrl->hwlim.fw_id;
  243. }else if (ctrl->target_idq.d < -ctrl->hwlim.fw_id) {
  244. ctrl->target_idq.d = -ctrl->hwlim.fw_id;
  245. }
  246. ctrl->target_idq.q = sqrtf(SQ(target_current) - SQ(ctrl->target_idq.d));
  247. if (s < 0) {
  248. ctrl->target_idq.q = -ctrl->target_idq.q;
  249. }
  250. }else {
  251. ctrl->target_idq.d = 0;
  252. ctrl->target_idq.q = target_current;
  253. }
  254. }else if ((ctrl->mode_running == CTRL_MODE_TRQ) || (ctrl->mode_running == CTRL_MODE_SPD) ||
  255. (ctrl->mode_running == CTRL_MODE_EBRAKE)) {
  256. motor_mpta_fw_lookup(ctrl->foc.in.mot_velocity, ctrl->target_torque, &ctrl->target_idq);
  257. }
  258. u32 mask = cpu_enter_critical();
  259. ctrl->foc.in.target_curr_dq.d = ctrl->target_idq.d;
  260. ctrl->foc.in.target_curr_dq.q = ctrl->target_idq.q;
  261. cpu_exit_critical(mask);
  262. }
  263. static void crosszero_step_towards(float *value, float target) {
  264. static float no_cro_step = CONFIG_CrossZero_NorStep;
  265. float v_now = *value;
  266. bool cross_zero = false;
  267. float nor_step = mc_conf()->cz.normal_step;
  268. float min_step = mc_conf()->cz.min_step;
  269. float min_ramp_torque = mc_conf()->cz.low;
  270. float high_ramp_torque = mc_conf()->cz.high;
  271. if (target > 0) {
  272. if (v_now < -min_ramp_torque) {
  273. step_towards(value, -min_ramp_torque + 0.001f, nor_step);
  274. cross_zero = true;
  275. }else if (v_now >= -min_ramp_torque && v_now <= high_ramp_torque) {
  276. step_towards(value, target, min_step);
  277. cross_zero = true;
  278. }
  279. }else if (target == 0) {
  280. if (v_now > high_ramp_torque) {
  281. step_towards(value, high_ramp_torque - 0.001f, nor_step);
  282. cross_zero = true;
  283. }else if (v_now >= min_ramp_torque && v_now <= high_ramp_torque) {
  284. step_towards(value, target, min_step);
  285. cross_zero = true;
  286. }
  287. }else {
  288. if (v_now > high_ramp_torque) {
  289. step_towards(value, high_ramp_torque - 0.001f, nor_step);
  290. cross_zero = true;
  291. }else if (v_now >= -min_ramp_torque && v_now <= high_ramp_torque) {
  292. step_towards(value, target, min_step);
  293. cross_zero = true;
  294. }
  295. }
  296. if (!cross_zero) {
  297. step_towards(&no_cro_step, nor_step, 0.1f);
  298. step_towards(value, target, no_cro_step);
  299. }else {
  300. no_cro_step = 0.5f;
  301. }
  302. }
  303. /*called in media task */
  304. void mot_contrl_dq_calc(mot_contrl_t *ctrl) {
  305. foc_t *foc = &ctrl->foc;
  306. float etcs_out = etcs_process(&ctrl->ects);
  307. if (ctrl->b_AutoHold) {
  308. float hold_torque = min(ctrl->protlim.torque, mc_conf()->c.max_autohold_torque);
  309. ctrl->pi_lock.max = hold_torque;
  310. ctrl->pi_lock.min = -hold_torque;
  311. float vel_count = motor_encoder_get_vel_count();
  312. float errRef = 0 - vel_count;
  313. ctrl->target_torque = PI_Controller_Run(&ctrl->pi_lock ,errRef);
  314. mot_contrl_dq_assign(ctrl);
  315. return;
  316. }
  317. if (ctrl->mode_running == CTRL_MODE_CURRENT) {
  318. line_ramp_step(&ctrl->target_current);
  319. }else if (ctrl->mode_running == CTRL_MODE_EBRAKE) {
  320. float maxTrq = line_ramp_step(&ctrl->input_torque);
  321. if (ctrl->input_torque.target < 0.0001f && foc->in.mot_velocity < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  322. maxTrq = 0;
  323. }
  324. crosszero_step_towards(&ctrl->target_torque, maxTrq);
  325. }else if (ctrl->mode_running == CTRL_MODE_TRQ) {
  326. float refTorque = line_ramp_step(&ctrl->input_torque);
  327. refTorque = min(refTorque, ctrl->torque_lim.interpolation) * etcs_out;
  328. float maxTrq = mot_contrl_vel_limiter(ctrl, refTorque);
  329. ctrl->target_torque_raw = mot_contrl_dc_curr_limiter(ctrl, maxTrq);
  330. crosszero_step_towards(&ctrl->target_torque, ctrl->target_torque_raw);
  331. }else if (ctrl->mode_running == CTRL_MODE_SPD){
  332. float refSpeed = line_ramp_step(&ctrl->cruise_vel);
  333. float maxSpeed = ctrl->target_vel.target;
  334. if (ctrl->b_cruiseEna) {
  335. maxSpeed = ctrl->cruise_vel.target;
  336. refSpeed = ctrl->cruise_vel.interpolation;
  337. }
  338. float max_input = ctrl->torque_lim.interpolation * etcs_out;
  339. if (maxSpeed >= 0) {
  340. ctrl->pi_vel.max = max_input;
  341. #ifdef CONFIG_SERVO_MOTOR
  342. ctrl->pi_vel.min = -max_input;
  343. #else
  344. ctrl->pi_vel.min = -CONFIG_MAX_NEG_TORQUE;
  345. #endif
  346. }else if (maxSpeed < 0) {
  347. ctrl->pi_vel.min = -max_input;
  348. #ifdef CONFIG_SERVO_MOTOR
  349. ctrl->pi_vel.max = max_input;
  350. #else
  351. ctrl->pi_vel.max = CONFIG_MAX_NEG_TORQUE;
  352. #endif
  353. }
  354. if ((maxSpeed == 0) && (ctrl->foc.in.mot_velocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  355. ctrl->pi_vel.max = 0;
  356. ctrl->pi_vel.min = 0; //防止倒转
  357. }
  358. float errRef = refSpeed - ctrl->foc.in.mot_velocity;
  359. float maxTrq = PI_Controller_RunVel(&ctrl->pi_vel, errRef);
  360. ctrl->target_torque_raw = mot_contrl_dc_curr_limiter(ctrl, maxTrq);
  361. crosszero_step_towards(&ctrl->target_torque, ctrl->target_torque_raw);
  362. }
  363. mot_contrl_dq_assign(ctrl);
  364. }
  365. static void mot_contrl_pid(mot_contrl_t *ctrl) {
  366. float slow_ctrl_ts = (1.0f/(float)CONFIG_SPD_CTRL_TS);
  367. PI_Controller_Reset(&ctrl->pi_power, 0);
  368. ctrl->pi_power.kp = mc_conf()->c.pid[PID_IDCLim_ID].kp;
  369. ctrl->pi_power.ki = mc_conf()->c.pid[PID_IDCLim_ID].ki;
  370. ctrl->pi_power.kd = mc_conf()->c.pid[PID_IDCLim_ID].kd;
  371. ctrl->pi_power.DT = slow_ctrl_ts;
  372. PI_Controller_Reset(&ctrl->pi_lock, 0);
  373. ctrl->pi_lock.kp = mc_conf()->c.pid[PID_AutoHold_ID].kp;
  374. ctrl->pi_lock.ki = mc_conf()->c.pid[PID_AutoHold_ID].ki;
  375. ctrl->pi_lock.kd = mc_conf()->c.pid[PID_AutoHold_ID].kd;
  376. ctrl->pi_lock.DT = slow_ctrl_ts;
  377. PI_Controller_Reset(&ctrl->pi_vel_lim, 0);
  378. ctrl->pi_vel_lim.kp = mc_conf()->c.pid[PID_VelLim_ID].kp;
  379. ctrl->pi_vel_lim.ki = mc_conf()->c.pid[PID_VelLim_ID].ki;
  380. ctrl->pi_vel_lim.kd = mc_conf()->c.pid[PID_VelLim_ID].kd;
  381. ctrl->pi_vel_lim.DT = slow_ctrl_ts;
  382. PI_Controller_Reset(&ctrl->pi_vel, 0);
  383. ctrl->pi_vel.kp = mc_conf()->c.pid[PID_Vel_ID].kp;
  384. ctrl->pi_vel.ki = mc_conf()->c.pid[PID_Vel_ID].ki;
  385. ctrl->pi_vel.kd = mc_conf()->c.pid[PID_Vel_ID].kd;
  386. ctrl->pi_vel.DT = slow_ctrl_ts;
  387. }
  388. static void mot_contrl_ulimit(mot_contrl_t *ctrl) {
  389. ctrl->userlim.dc_curr = min(mc_conf()->c.max_idc, ctrl->hwlim.dc_curr);
  390. ctrl->userlim.mot_vel = min(mc_conf()->c.max_rpm, ctrl->hwlim.mot_vel);
  391. ctrl->userlim.torque = mc_conf()->c.max_torque;//MAX_TORQUE;
  392. ctrl->userlim.phase_curr = min(mc_conf()->c.max_phase_curr, ctrl->hwlim.phase_curr);
  393. ctrl->userlim.dc_vol_min = mc_conf()->c.max_dc_vol;
  394. ctrl->userlim.dc_vol_max = mc_conf()->c.min_dc_vol;
  395. ctrl->userlim.ebrk_dc_curr = 0xFF;
  396. ctrl->userlim.ebrk_torque = mc_conf()->c.max_ebrk_torque;
  397. }
  398. static void mot_contrl_rtlimit(mot_contrl_t *ctrl) {
  399. line_ramp_reset(&ctrl->torque_lim, ctrl->userlim.torque);
  400. line_ramp_reset(&ctrl->dc_curr_lim, ctrl->userlim.dc_curr);
  401. line_ramp_reset(&ctrl->vel_lim, ctrl->userlim.mot_vel);
  402. }
  403. void mot_contrl_slow_task(mot_contrl_t *ctrl) {
  404. line_ramp_step(&ctrl->torque_lim);
  405. line_ramp_step(&ctrl->dc_curr_lim);
  406. line_ramp_step(&ctrl->vel_lim);
  407. mot_contrl_dq_calc(ctrl);
  408. }
  409. u8 mot_contrl_protect(mot_contrl_t *ctrl) {
  410. u8 changed = FOC_LIM_NO_CHANGE;
  411. float dc_lim = (float)vbus_under_vol_limit();
  412. float torque_lim = (float)min(mos_temp_high_limit(), motor_temp_high_limit());
  413. if (ctrl->protlim.dc_curr != dc_lim || ctrl->protlim.torque != torque_lim) {
  414. if ((dc_lim > ctrl->protlim.dc_curr) || (torque_lim > ctrl->protlim.torque)) {
  415. changed = FOC_LIM_CHANGE_H;
  416. }else {
  417. changed = FOC_LIM_CHANGE_L;
  418. }
  419. ctrl->protlim.dc_curr = dc_lim;
  420. ctrl->protlim.torque = torque_lim;
  421. }
  422. return changed;
  423. }
  424. float mot_contrl_get_speed(mot_contrl_t *ctrl) {
  425. float speed = ctrl->foc.in.mot_velocity;
  426. if (!ctrl->b_start || foc_observer_is_encoder()) {
  427. speed = motor_encoder_get_speed();
  428. }else {
  429. if (foc_observer_sensorless_stable()) {
  430. speed = foc_observer_sensorless_speed();
  431. }else {
  432. speed = 0;
  433. }
  434. }
  435. return speed;
  436. }
  437. void mot_contrl_velloop_params(mot_contrl_t *ctrl, float wcv, float b0) {
  438. #ifdef CONFIG_SPEED_LADRC
  439. ladrc_change_b0(&gFoc_Ctrl.vel_adrc, b0);
  440. ladrc_change_K(&gFoc_Ctrl.vel_adrc, wcv);
  441. #else
  442. PI_Controller_Change_Kpi(&ctrl->pi_vel, wcv, b0);
  443. #endif
  444. }
  445. void mot_contrl_trqloop_params(mot_contrl_t *ctrl, float wcv, float b0) {
  446. #ifdef CONFIG_SPEED_LADRC
  447. ladrc_change_b0(&gFoc_Ctrl.vel_lim_adrc, b0);
  448. ladrc_change_K(&gFoc_Ctrl.vel_lim_adrc, wcv);
  449. #else
  450. PI_Controller_Change_Kpi(&ctrl->pi_vel_lim, wcv, b0);
  451. #endif
  452. }
  453. void mot_contrl_set_dccurr_limit(mot_contrl_t *ctrl, float ibusLimit) {
  454. if (ibusLimit > ctrl->hwlim.dc_curr) {
  455. ibusLimit = ctrl->hwlim.dc_curr;
  456. }
  457. if (ctrl->protlim.dc_curr != HW_LIMIT_NONE) {
  458. ibusLimit = min(ibusLimit, ctrl->protlim.dc_curr);
  459. }
  460. ctrl->userlim.dc_curr = ibusLimit;
  461. if (ABS(ctrl->dc_curr_filted) <= ibusLimit){
  462. line_ramp_reset(&ctrl->dc_curr_lim, ctrl->userlim.dc_curr);
  463. }else {
  464. line_ramp_set_target(&ctrl->dc_curr_lim, ctrl->userlim.dc_curr);
  465. }
  466. }
  467. void mot_contrl_set_vel_limit(mot_contrl_t *ctrl, float vel) {
  468. if (vel > ctrl->hwlim.mot_vel) {
  469. vel = ctrl->hwlim.mot_vel;
  470. }
  471. ctrl->userlim.mot_vel = vel;
  472. if (ABS(ctrl->foc.in.mot_velocity) <= vel) {
  473. line_ramp_reset(&ctrl->vel_lim, ctrl->userlim.mot_vel);
  474. }else {
  475. line_ramp_set_target(&ctrl->vel_lim, ctrl->userlim.mot_vel);
  476. }
  477. }
  478. void mot_contrl_set_torque_limit(mot_contrl_t *ctrl, float torque) {
  479. if (torque > ctrl->hwlim.torque) {
  480. torque = ctrl->hwlim.torque;
  481. }
  482. if (ctrl->protlim.torque != HW_LIMIT_NONE) {
  483. torque = min(torque, ctrl->protlim.torque);
  484. }
  485. ctrl->userlim.torque = torque;
  486. if (ABS(ctrl->target_torque) <= torque){
  487. line_ramp_reset(&ctrl->torque_lim, ctrl->userlim.torque);
  488. }else {
  489. line_ramp_set_target(&ctrl->torque_lim, ctrl->userlim.torque);
  490. }
  491. }
  492. float mot_contrl_get_ebrk_torque(mot_contrl_t *ctrl) {
  493. if (!foc_observer_is_encoder()) {
  494. return 0; //无感运行关闭能量回收
  495. }
  496. return ctrl->userlim.ebrk_torque;
  497. }
  498. void mot_contrl_set_ebrk_time(mot_contrl_t *ctrl, u32 time) {
  499. ctrl->ebrk_ramp_time = time;
  500. if (ctrl->mode_running == CTRL_MODE_EBRAKE) {
  501. line_ramp_set_time(&ctrl->input_torque, time);
  502. line_ramp_update(&ctrl->input_torque);
  503. }
  504. }
  505. void mot_contrl_set_vdq(mot_contrl_t *ctrl, float vd, float vq) {
  506. line_ramp_set_target(&ctrl->target_vd, vd);
  507. line_ramp_set_target(&ctrl->target_vq, vq);
  508. }
  509. void mot_contrl_set_vdq_immediate(mot_contrl_t *ctrl, float vd, float vq) {
  510. line_ramp_reset(&ctrl->target_vd, vd);
  511. line_ramp_reset(&ctrl->target_vq, vq);
  512. }
  513. bool mot_contrl_set_cruise(mot_contrl_t *ctrl, bool enable) {
  514. if (enable != ctrl->b_cruiseEna) {
  515. float motSpd = mot_contrl_get_speed(ctrl);
  516. if (enable && (motSpd < CONFIG_MIN_CRUISE_RPM)) { //
  517. mot_contrl_set_error(ctrl, FOC_NowAllowed_With_Speed);
  518. return false;
  519. }
  520. line_ramp_reset(&ctrl->cruise_vel, motSpd);
  521. ctrl->b_cruiseEna = enable;
  522. }
  523. return true;
  524. }
  525. bool mot_contrl_resume_cruise(mot_contrl_t *ctrl) {
  526. ctrl->b_cruiseEna = true;
  527. line_ramp_set_time(&ctrl->cruise_vel, CONFIG_CRUISE_RAMP_TIME);
  528. return true;
  529. }
  530. bool mot_contrl_set_cruise_speed(mot_contrl_t *ctrl, float rpm) {
  531. if (ctrl->b_cruiseEna) {
  532. if (rpm < CONFIG_MIN_CRUISE_RPM) {
  533. rpm = CONFIG_MIN_CRUISE_RPM;
  534. }
  535. float vel = min(ABS(rpm),ctrl->userlim.mot_vel)*SIGN(rpm);
  536. line_ramp_set_target(&ctrl->cruise_vel, vel);
  537. return true;
  538. }
  539. mot_contrl_set_error(ctrl, FOC_NotCruiseMode);
  540. return false;
  541. }
  542. bool mot_contrl_set_current(mot_contrl_t *ctrl, float is) {
  543. is = fclamp(is, -ctrl->userlim.phase_curr, ctrl->userlim.phase_curr);
  544. line_ramp_set_target(&ctrl->target_current, is);
  545. return true;
  546. }
  547. void mot_contrl_set_torque_ramp_time(mot_contrl_t *ctrl, u32 acc, u32 dec) {
  548. ctrl->torque_acc_time = acc;
  549. ctrl->torque_dec_time = dec;
  550. if (ctrl->mode_running == CTRL_MODE_TRQ) {
  551. line_ramp_set_acctime(&ctrl->input_torque, acc);
  552. line_ramp_set_dectime(&ctrl->input_torque, dec);
  553. line_ramp_update(&ctrl->input_torque);
  554. }
  555. }
  556. void mot_contrl_set_torque_acc_time(mot_contrl_t *ctrl, u32 acc) {
  557. ctrl->torque_acc_time = acc;
  558. if (ctrl->mode_running == CTRL_MODE_TRQ) {
  559. line_ramp_set_acctime(&ctrl->input_torque, acc);
  560. line_ramp_update(&ctrl->input_torque);
  561. }
  562. }
  563. bool mot_contrl_set_torque(mot_contrl_t *ctrl, float torque) {
  564. torque = fclamp(torque, -ctrl->userlim.torque, ctrl->userlim.torque);
  565. line_ramp_set_target(&ctrl->input_torque, torque);
  566. return true;
  567. }
  568. void mot_contrl_mtpa_calibrate(mot_contrl_t *ctrl, bool enable) {
  569. if (enable) {
  570. ctrl->b_mtpa_calibrate = true;
  571. ctrl->adv_angle = 0;
  572. }else {
  573. ctrl->adv_angle = INVALID_ANGLE;
  574. ctrl->b_mtpa_calibrate = false;
  575. }
  576. }
  577. void mot_contrl_set_autohold(mot_contrl_t *ctrl, bool lock) {
  578. if (ctrl->b_AutoHold != lock) {
  579. motor_encoder_lock_pos(lock);
  580. PI_Controller_Reset(&ctrl->pi_lock, 0);
  581. if (!lock) {
  582. float hold_torque = ctrl->target_torque * 1.1f;
  583. if (ctrl->mode_running == CTRL_MODE_TRQ) {
  584. PI_Controller_Reset(&ctrl->pi_vel_lim, hold_torque);
  585. }else if (ctrl->mode_running == CTRL_MODE_SPD) {
  586. PI_Controller_Reset(&ctrl->pi_vel, hold_torque);
  587. }
  588. line_ramp_reset(&ctrl->input_torque, hold_torque);
  589. ctrl->autohold_torque = hold_torque;
  590. }else {
  591. ctrl->autohold_torque = 0;
  592. }
  593. ctrl->b_AutoHold = lock;
  594. }
  595. }
  596. static bool is_hw_brake_shutting_power(mot_contrl_t *ctrl) {
  597. return (ctrl->b_hw_braker && mc_hwbrk_can_shutpower());
  598. }
  599. bool mot_contrl_energy_recovery(mot_contrl_t *ctrl, bool start) {
  600. bool enable = ctrl->b_ebrk_running;
  601. if (mot_contrl_get_ebrk_torque(ctrl) == 0) {
  602. enable = false;
  603. }else if (start && ctrl->foc.in.mot_velocity >= CONFIG_MIN_RPM_FOR_EBRAKE){
  604. enable = true;
  605. }else if (!start && !is_hw_brake_shutting_power(ctrl)) {
  606. enable = false;
  607. }
  608. if (enable != ctrl->b_ebrk_running) {
  609. ctrl->b_ebrk_running = enable;
  610. if (enable) {
  611. ctrl->mode_req = CTRL_MODE_EBRAKE;
  612. }else {
  613. ctrl->mode_req = CTRL_MODE_TRQ;
  614. }
  615. }
  616. return enable;
  617. }
  618. void mot_contrl_set_hw_brake(mot_contrl_t *ctrl, bool hw_brake) {
  619. u32 mask = cpu_enter_critical();
  620. if (hw_brake != ctrl->b_hw_braker) {
  621. ctrl->b_hw_braker = hw_brake;
  622. }
  623. if (is_hw_brake_shutting_power(ctrl)) {
  624. if (!ctrl->b_ebrk_running && !mot_contrl_energy_recovery(ctrl, true)) {
  625. line_ramp_reset(&ctrl->input_torque, 0);
  626. }
  627. }
  628. cpu_exit_critical(mask);
  629. }
  630. static PI_Controller *_pid(mot_contrl_t *ctrl, u8 id) {
  631. PI_Controller *pi = NULL;
  632. if (id == PID_ID_ID) {
  633. pi = &ctrl->foc.daxis;
  634. }else if (id == PID_IQ_ID) {
  635. pi = &ctrl->foc.qaxis;
  636. }else if (id == PID_VelLim_ID) {
  637. pi = &ctrl->pi_vel_lim;
  638. }else if (id == PID_Vel_ID) {
  639. pi = &ctrl->pi_vel;
  640. }else if (id == PID_AutoHold_ID) {
  641. pi = &ctrl->pi_lock;
  642. }
  643. return pi;
  644. }
  645. void mot_contrl_set_pid(mot_contrl_t *ctrl, u8 id, float kp, float ki, float kd) {
  646. if (id > PID_Max_ID) {
  647. return;
  648. }
  649. PI_Controller *pi = _pid(ctrl, id);
  650. if (pi != NULL) {
  651. pi->kp = kp;
  652. pi->ki = ki;
  653. pi->kd = kd;
  654. }
  655. }
  656. void mot_contrl_get_pid(mot_contrl_t *ctrl, u8 id, float *kp, float *ki, float *kd) {
  657. if (id > PID_Max_ID) {
  658. return;
  659. }
  660. PI_Controller *pi = _pid(ctrl, id);
  661. if (pi != NULL) {
  662. *kp = pi->kp;
  663. *ki = pi->ki;
  664. *kd = pi->kd;
  665. }
  666. }
  667. void mot_contrl_calc_current(mot_contrl_t *ctrl) {
  668. float vd = ctrl->foc.out.vol_dq.d;
  669. float vq = ctrl->foc.out.vol_dq.q;
  670. float id = ctrl->out_idq_filterd.d;
  671. float iq = ctrl->out_idq_filterd.q;
  672. /*
  673. 根据公式(等幅值变换,功率不等):
  674. iDC x vDC = 3/2(iq x vq + id x vd);
  675. */
  676. float m_pow = (vd * id + vq * iq);
  677. float raw_idc = 0.0f;
  678. float v_dc = get_vbus_float();
  679. if (v_dc != 0.0f) {
  680. raw_idc = m_pow / v_dc;
  681. }
  682. LowPass_Filter(ctrl->dc_curr_calc, raw_idc, 0.02f);
  683. raw_idc = get_vbus_current();
  684. if (raw_idc != NO_VALID_CURRENT) {
  685. LowPass_Filter(ctrl->dc_curr_filted, raw_idc, 0.05f);
  686. }else {
  687. ctrl->dc_curr_filted = ctrl->dc_curr_calc;
  688. }
  689. ctrl->out_current_vec = sqrtf(SQ(id) + SQ(iq));
  690. }