controller.c 24 KB

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