controller.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. static bool is_hw_brake_shutting_power(mot_contrl_t *ctrl);
  15. void mot_contrl_init(mot_contrl_t *ctrl) {
  16. memset(ctrl, 0, sizeof(mot_contrl_t));
  17. ctrl->foc.ts = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
  18. ctrl->foc.half_period = FOC_PWM_Half_Period;
  19. ctrl->force_angle = INVALID_ANGLE;
  20. ctrl->adv_angle = INVALID_ANGLE;
  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. ctrl->torque_acc_time = 500; //will be set after start
  31. ctrl->torque_dec_time = 500; //will be set after start
  32. ctrl->ebrk_ramp_time = 500; //will be set after start
  33. etcs_init(&ctrl->etcs);
  34. foc_init(&ctrl->foc);
  35. }
  36. bool mot_contrl_enable(mot_contrl_t *ctrl, bool start) {
  37. if (ctrl->b_start == start) {
  38. return true;
  39. }
  40. if (start) {
  41. line_ramp_init(&ctrl->ramp_torque_lim, CONFIG_LIMIT_RAMP_TIME);
  42. line_ramp_init(&ctrl->ramp_dc_curr_lim, CONFIG_LIMIT_RAMP_TIME);
  43. line_ramp_init(&ctrl->ramp_vel_lim, CONFIG_LIMIT_RAMP_TIME);
  44. line_ramp_init(&ctrl->ramp_cruise_vel, CONFIG_CRUISE_RAMP_TIME);
  45. line_ramp_init(&ctrl->ramp_target_vd, CONFIG_FOC_VDQ_RAMP_FINAL_TIME);
  46. line_ramp_init(&ctrl->ramp_target_vq, CONFIG_FOC_VDQ_RAMP_FINAL_TIME);
  47. line_ramp_init(&ctrl->ramp_target_vel, CONFIG_CRUISE_RAMP_TIME);
  48. line_ramp_init(&ctrl->ramp_target_current, CONFIG_CURRENT_RAMP_TIME);
  49. line_ramp_init(&ctrl->ramp_input_torque, CONFIG_DEFAULT_TORQUE_RAMP_TIME);
  50. line_ramp_init(&ctrl->ramp_adv_angle, CONFIG_CURRENT_RAMP_TIME);
  51. mot_contrl_ulimit(ctrl);
  52. mot_contrl_rtlimit(ctrl);
  53. }
  54. mot_contrl_pid(ctrl);
  55. ctrl->b_ebrk_running = false;
  56. ctrl->b_AutoHold = false;
  57. ctrl->b_cruiseEna = false;
  58. ctrl->b_mtpa_calibrate = false;
  59. ctrl->b_hw_braker = false;
  60. ctrl->mode_req = CTRL_MODE_OPEN;
  61. ctrl->mode_running = CTRL_MODE_OPEN;
  62. ctrl->force_angle = INVALID_ANGLE;
  63. ctrl->adv_angle = INVALID_ANGLE;
  64. ctrl->angle_last = INVALID_ANGLE;
  65. ctrl->dc_curr_filted = 0;
  66. ctrl->dc_curr_calc = 0;
  67. ctrl->phase_curr_filted[0] = ctrl->phase_curr_filted[1] = 0;
  68. ctrl->out_idq_filterd.d = ctrl->out_idq_filterd.q = 0;
  69. ctrl->out_vdq_filterd.d = ctrl->out_vdq_filterd.q = 0;
  70. ctrl->autohold_torque = 0;
  71. ctrl->out_current_vec = 0;
  72. ctrl->target_idq.d = 0;
  73. ctrl->target_idq.q = 0;
  74. ctrl->target_torque = 0;
  75. ctrl->target_torque_raw = 0;
  76. foc_init(&ctrl->foc);
  77. foc_observer_init();
  78. ctrl->b_start = start;
  79. return true;
  80. }
  81. void mot_contrl_ifctl_start(mot_contrl_t *ctrl, bool start, float accl, float max_vel, float iq_set) {
  82. if (ctrl->if_ctl.b_ena == start) {
  83. return;
  84. }
  85. if (start) {
  86. memset(&ctrl->if_ctl, 0, sizeof(if_ctl_t));
  87. ctrl->if_ctl.elec_w_acc = accl * (1.0/30.0*M_PI*mc_conf()->m.poles);
  88. ctrl->if_ctl.max_vel = max_vel * (1.0/30.0*M_PI*mc_conf()->m.poles);
  89. ctrl->if_ctl.curr_angle = 0;
  90. ctrl->if_ctl.angle_deg = pi_2_degree(ctrl->if_ctl.curr_angle);
  91. }
  92. if (start) {
  93. line_ramp_init(&ctrl->ramp_target_current, 20);
  94. ctrl->if_ctl.iq_set = iq_set;
  95. }else {
  96. ctrl->if_ctl.iq_set = 0;
  97. mot_contrl_set_current(ctrl, 0);
  98. }
  99. ctrl->if_ctl.b_ena = start;
  100. }
  101. float mot_contrl_ifctl_update(mot_contrl_t *ctrl) {
  102. if_ctl_t *i_f = &ctrl->if_ctl;
  103. i_f->curr_vel += i_f->elec_w_acc * ctrl->foc.ts; //加速度积分获取速度
  104. if (i_f->curr_vel > ctrl->if_ctl.max_vel) {
  105. i_f->curr_vel = ctrl->if_ctl.max_vel;
  106. }
  107. i_f->curr_angle += i_f->curr_vel * ctrl->foc.ts; //速度积分获取电角度
  108. norm_angle_rad(i_f->curr_angle);
  109. ctrl->if_ctl.angle_deg = pi_2_degree(ctrl->if_ctl.curr_angle);
  110. i_f->curr_vel_rpm = i_f->curr_vel/M_PI*30.0f/mc_conf()->m.poles;
  111. return ctrl->if_ctl.angle_deg;
  112. }
  113. bool mot_contrl_request_mode(mot_contrl_t *ctrl, u8 mode) {
  114. if (mode > CTRL_MODE_EBRAKE) {
  115. mot_contrl_set_error(ctrl, FOC_Param_Err);
  116. return false;
  117. }
  118. ctrl->mode_req = mode;
  119. return true;
  120. }
  121. u8 mot_contrl_mode(mot_contrl_t *ctrl) {
  122. u8 preMode = ctrl->mode_running;
  123. if (!ctrl->b_start) {
  124. ctrl->mode_running = CTRL_MODE_OPEN;
  125. }else if (ctrl->mode_req == CTRL_MODE_OPEN) {
  126. ctrl->mode_running = CTRL_MODE_OPEN;
  127. }else if (ctrl->mode_req == CTRL_MODE_SPD || ctrl->b_cruiseEna){
  128. ctrl->mode_running = CTRL_MODE_SPD;
  129. }else if (ctrl->mode_req == CTRL_MODE_CURRENT) {
  130. ctrl->mode_running = CTRL_MODE_CURRENT;
  131. }else if (ctrl->mode_req == CTRL_MODE_EBRAKE) {
  132. ctrl->mode_running = CTRL_MODE_EBRAKE;
  133. }else {
  134. if (!ctrl->b_cruiseEna) {
  135. ctrl->mode_running = CTRL_MODE_TRQ;
  136. }
  137. }
  138. if (preMode != ctrl->mode_running) {
  139. if ((preMode != ctrl->mode_running) && (ctrl->mode_running == CTRL_MODE_TRQ)) {
  140. line_ramp_set_acctime(&ctrl->ramp_input_torque, ctrl->torque_acc_time);
  141. line_ramp_set_dectime(&ctrl->ramp_input_torque, ctrl->torque_dec_time);
  142. line_ramp_update(&ctrl->ramp_input_torque);
  143. if (preMode == CTRL_MODE_SPD) {
  144. ctrl->target_torque_raw = ctrl->target_torque;
  145. PI_Controller_Reset(&ctrl->pi_vel_lim, ctrl->target_torque_raw);
  146. }else if (preMode == CTRL_MODE_CURRENT) {
  147. ctrl->target_torque_raw = line_ramp_get_interp(&ctrl->ramp_target_current);
  148. PI_Controller_Reset(&ctrl->pi_vel_lim, ctrl->target_torque_raw);
  149. }else if (preMode == CTRL_MODE_EBRAKE) {
  150. line_ramp_set_target(&ctrl->ramp_input_torque, 0);
  151. }
  152. }else if ((preMode == CTRL_MODE_TRQ) && (ctrl->mode_running == CTRL_MODE_SPD)) {
  153. PI_Controller_Reset(&ctrl->pi_vel, ctrl->target_torque);
  154. }else if ((preMode != ctrl->mode_running) && (ctrl->mode_running == CTRL_MODE_EBRAKE)) {
  155. line_ramp_reset(&ctrl->ramp_input_torque, ctrl->target_torque);
  156. line_ramp_set_time(&ctrl->ramp_input_torque, ctrl->torque_dec_time);
  157. line_ramp_set_target(&ctrl->ramp_input_torque, 0);//先把扭矩快速减小到0
  158. }else if ((preMode == CTRL_MODE_EBRAKE) && (ctrl->mode_running == CTRL_MODE_SPD)) {
  159. PI_Controller_Reset(&ctrl->pi_vel, 0);
  160. }
  161. }
  162. if (ctrl->mode_running == CTRL_MODE_OPEN) {
  163. line_ramp_step(&ctrl->ramp_target_vd);
  164. line_ramp_step(&ctrl->ramp_target_vq);
  165. }
  166. return ctrl->mode_running;
  167. }
  168. static __INLINE void phase_curr_unbal_check(mot_contrl_t *ctrl) {
  169. static u32 _cycle_cnt = 0, _last_mod_cnt = 0;
  170. static float a_max = 0, b_max = 0, c_max = 0;
  171. static u32 _unbalance_cnt = 0;
  172. static u32 _unbalance_time = 0;
  173. foc_t *foc = &ctrl->foc;
  174. float lowpass = foc->mot_vel_radusPers * FOC_CTRL_US / 2.0f;
  175. if (lowpass > 1.0f) {
  176. lowpass = 1.0f;
  177. }
  178. LowPass_Filter(ctrl->phase_curr_filted[0], foc->in.curr_abc[0], lowpass);
  179. LowPass_Filter(ctrl->phase_curr_filted[1], foc->in.curr_abc[1], lowpass);
  180. ctrl->phase_curr_filted[2] = -(ctrl->phase_curr_filted[0] + ctrl->phase_curr_filted[1]);
  181. if ((ctrl->angle_last == INVALID_ANGLE) || (foc->mot_vel_radusPers < 100) || ctrl->out_current_vec < 50) {
  182. ctrl->angle_last = foc->in.mot_angle;
  183. a_max = b_max = c_max = 0;
  184. _unbalance_cnt = 0;
  185. _unbalance_time = get_tick_ms();
  186. _cycle_cnt = 0;
  187. _last_mod_cnt = 0;
  188. return;
  189. }
  190. float delta_angle = foc->in.mot_angle - ctrl->angle_last;
  191. if (delta_angle > 200 || delta_angle < -200) { //one cycle
  192. _cycle_cnt ++;
  193. }
  194. ctrl->angle_last = foc->in.mot_angle;
  195. u32 mod_cnt = _cycle_cnt % CONFIG_PHASE_UNBALANCE_PEAK_CNT;
  196. bool trigger = false;
  197. if ((mod_cnt == 0) && (_last_mod_cnt != mod_cnt)) {
  198. trigger = true;
  199. }
  200. _last_mod_cnt = mod_cnt;
  201. a_max = MAX(a_max, ctrl->phase_curr_filted[0] * (2.2f));
  202. b_max = MAX(b_max, ctrl->phase_curr_filted[1] * (2.2f));
  203. c_max = MAX(c_max, ctrl->phase_curr_filted[2] * (2.2f));
  204. if (trigger) { //经过CONFIG_PEAK_CNT个周期,已经得到peak值
  205. float i_min = 1000.0f, i_max = 0;
  206. if (a_max > i_max) {
  207. i_max = a_max;
  208. }
  209. if (a_max < i_min) {
  210. i_min = a_max;
  211. }
  212. if (b_max > i_max) {
  213. i_max = b_max;
  214. }
  215. if (b_max < i_min) {
  216. i_min = b_max;
  217. }
  218. if (c_max > i_max) {
  219. i_max = c_max;
  220. }
  221. if (c_max < i_min) {
  222. i_min = c_max;
  223. }
  224. float unbalance_r = (i_max - i_min - CONFIG_PHASE_UNBALANCE_THROLD)/(i_max + 1e-8f);
  225. if (unbalance_r >= CONFIG_PHASE_UNBALANCE_R) {
  226. if ((_unbalance_cnt++ >= 500) || (get_delta_ms(_unbalance_time) >= 1000*10)) {
  227. if (mc_set_critical_error(FOC_CRIT_PHASE_UNBalance_Err)) {
  228. mc_crit_err_add(FOC_CRIT_PHASE_UNBalance_Err, (s16)i_max, (s16)i_min);
  229. }
  230. }
  231. }else {
  232. _unbalance_cnt = 0;
  233. _unbalance_time = get_tick_ms();
  234. }
  235. a_max = b_max = c_max = 0;
  236. }
  237. }
  238. static __INLINE void mot_contrl_update_phase_vol(mot_contrl_t *ctrl) {
  239. float phase_vol[3];
  240. get_uvw_phases_raw(phase_vol);
  241. /* 三相端电压转到alpha-beta轴的相电压 */
  242. ctrl->phase_v_ab.a = (2 * phase_vol[0] - phase_vol[1] - phase_vol[2])/3.0f;
  243. ctrl->phase_v_ab.b = (phase_vol[1] - phase_vol[2]) * ONE_BY_SQRT3;
  244. /* 当前电气频率 除于相电压低通滤波器截止频率 */
  245. float We_hz = (ctrl->foc.in.mot_velocity / 60.0f * mc_conf()->m.poles);
  246. float w_r_wc = We_hz / PHASE_VOL_LPF_BAND;
  247. if (ctrl->mot_param_ind_freq > 10.0f) {//dq轴电感识别
  248. w_r_wc = (ctrl->mot_param_ind_freq / (2.0f * M_PI)) / PHASE_VOL_LPF_BAND;
  249. }
  250. /* 计算低通滤波器的幅度补偿系数*/
  251. float mag_mul = sqrtf(1 + SQ(w_r_wc));
  252. ctrl->phase_v_ab.a *= mag_mul;
  253. ctrl->phase_v_ab.b *= mag_mul;
  254. /* 计算低通相位延时 */
  255. float angle_rad = fast_atan2(We_hz, PHASE_VOL_LPF_BAND);
  256. /* 延时半周期*/
  257. angle_rad += (We_hz * 2 * M_PI * ctrl->foc.ts / 2);
  258. norm_angle_rad(angle_rad);
  259. /* 补偿 alpha-beta的相位 */
  260. float sin,cos;
  261. arm_sin_cos(-pi_2_degree(angle_rad), &sin, &cos);
  262. float alpha = ctrl->phase_v_ab.a * cos + ctrl->phase_v_ab.b * sin;
  263. float beta = ctrl->phase_v_ab.b * cos - ctrl->phase_v_ab.a * sin;
  264. ctrl->phase_v_ab.a = alpha;
  265. ctrl->phase_v_ab.b = beta;
  266. }
  267. bool mot_contrl_update(mot_contrl_t *ctrl) {
  268. foc_t *foc = &ctrl->foc;
  269. mot_contrl_update_phase_vol(ctrl);
  270. phase_current_get(foc->in.curr_abc);
  271. clark(foc->in.curr_abc[0], foc->in.curr_abc[1], foc->in.curr_abc[2], &foc->in.curr_ab);
  272. 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);
  273. float enc_angle = motor_encoder_get_angle();
  274. float enc_vel = motor_encoder_get_speed();
  275. if (ctrl->if_ctl.b_ena) {
  276. enc_angle = mot_contrl_ifctl_update(ctrl);
  277. enc_vel = ctrl->if_ctl.curr_vel_rpm;
  278. mot_contrl_set_current(ctrl, ctrl->if_ctl.iq_set);
  279. }else if (!foc_observer_diagnostic(enc_angle, enc_vel)){
  280. /* detect encoder angle error, do something here */
  281. if (!foc_observer_sensorless_stable()) {
  282. foc->in.mot_velocity = 0;
  283. return false;
  284. }
  285. enc_angle = foc_observer_sensorless_angle();
  286. enc_vel = foc_observer_sensorless_speed();
  287. }
  288. if (!ctrl->b_mtpa_calibrate && (ctrl->force_angle != INVALID_ANGLE)) {
  289. foc->in.mot_angle = ctrl->force_angle;
  290. }else {
  291. foc->in.mot_angle = enc_angle;
  292. }
  293. #ifdef CONFIG_DQ_STEP_RESPONSE
  294. foc->in.mot_angle = 0;
  295. #endif
  296. foc->in.mot_velocity = enc_vel;
  297. foc->in.dc_vol = get_vbus_float();
  298. foc->in.b_openloop = ctrl->mode_running == CTRL_MODE_OPEN;
  299. phase_curr_unbal_check(ctrl);
  300. if (foc->in.b_openloop) {
  301. foc->in.target_vol_dq.d = line_ramp_get_interp(&ctrl->ramp_target_vd);
  302. foc->in.target_vol_dq.q = line_ramp_get_interp(&ctrl->ramp_target_vq);
  303. }
  304. foc_update(foc);
  305. park(foc, &ctrl->phase_v_ab, &ctrl->phase_v_dq);
  306. ctrl->duty_raw = NORM2_f(foc->out.vol_dq.d, foc->out.vol_dq.q)/(foc->in.dc_vol * CONFIG_SVM_MODULATION * SQRT3_BY_2);
  307. LowPass_Filter(ctrl->duty_filterd, ctrl->duty_raw, 0.01f);
  308. float lowpass = 0.001f;
  309. LowPass_Filter(ctrl->out_idq_filterd.d, foc->out.curr_dq.d ,lowpass);
  310. LowPass_Filter(ctrl->out_idq_filterd.q, foc->out.curr_dq.q ,lowpass);
  311. LowPass_Filter(ctrl->out_vdq_filterd.d, foc->out.vol_dq.d ,lowpass/1.5f);
  312. LowPass_Filter(ctrl->out_vdq_filterd.q, foc->out.vol_dq.q ,lowpass/1.5f);
  313. /* 计算母线电流 */
  314. #if (CONFIG_IBUS_CALC_SAMPLED_V)
  315. float v_alpha = ctrl->phase_v_ab.a;
  316. float v_beta = ctrl->phase_v_ab.b;
  317. float i_alpha = foc->in.curr_ab.a;
  318. float i_beta = foc->in.curr_ab.b;
  319. #else
  320. float vd = ctrl->out_vdq_filterd.d;
  321. float vq = ctrl->out_vdq_filterd.q;
  322. float id = ctrl->out_idq_filterd.d;
  323. float iq = ctrl->out_idq_filterd.q;
  324. #endif
  325. /*
  326. 根据公式(等幅值变换,功率不等):
  327. iDC x vDC = 3/2(iq x vq + id x vd) = 3/2(i_alpha * v_alpha + i_beta * v_beta);
  328. */
  329. #if (CONFIG_IBUS_CALC_SAMPLED_V)
  330. float m_pow = (v_alpha * i_alpha + v_beta * i_beta);
  331. #else
  332. float m_pow = (vd * id + vq * iq);
  333. #endif
  334. float raw_idc = 0.0f;
  335. float v_dc = get_vbus_float();
  336. if (v_dc != 0.0f) {
  337. raw_idc = m_pow / v_dc;
  338. }
  339. ctrl->dc_curr_calc = raw_idc;
  340. return true;
  341. }
  342. #ifndef CONFIG_TORQUE_LUT
  343. static __INLINE float mot_contrl_dc_curr_limiter(mot_contrl_t *ctrl, float maxTrq) {
  344. float dc_lim = line_ramp_get_interp(&ctrl->ramp_dc_curr_lim);
  345. /* 计算iq的前馈,(母线功率 - d轴功率)/vq
  346. * 加上0.0001f 避免除0
  347. */
  348. float r = ctrl->dc_curr_filted / (ctrl->dc_curr_calc + 0.0001f) + 0.0001f;
  349. r = fclamp(ABS(r), 0.5f, 1.5f);
  350. float q_axis_power = dc_lim * get_vbus_float() / r - ctrl->out_idq_filterd.d * ctrl->out_vdq_filterd.d;
  351. float vq = ctrl->out_vdq_filterd.q;
  352. if (vq < 1.0f) {
  353. vq = 1.0f;
  354. }
  355. float iq_ff = q_axis_power / vq;
  356. iq_ff = fabs(iq_ff);
  357. if (iq_ff > maxTrq) {
  358. ctrl->dc_lim_t_ff = maxTrq;
  359. }else {
  360. ctrl->dc_lim_t_ff = iq_ff;
  361. }
  362. /** 使用min(maxTrq, ctrl->dc_lim_t_ff * 1.1f)带入max的原因是为了防止积分器饱和后有过冲现象
  363. * 1.1f的系数是为了保证ctrl->dc_lim_t_ff 计算偏小的情况下功率能达到上限,这个系数太大不能
  364. * 防止过冲
  365. */
  366. ctrl->pi_power.max = min(maxTrq, ctrl->dc_lim_t_ff * 1.1f);
  367. float errRef = dc_lim - ctrl->dc_curr_filted;
  368. return PI_Controller_Parallel(&ctrl->pi_power, errRef, ctrl->dc_lim_t_ff);
  369. }
  370. #else
  371. static __INLINE float mot_contrl_dc_curr_limiter(mot_contrl_t *ctrl, float maxTrq) {
  372. float dc_lim = line_ramp_get_interp(&ctrl->ramp_dc_curr_lim);
  373. /* 计算扭矩的前馈,(母线功率 * 9.55 * 电机效率)/(电机速度)
  374. * 加上0.0001f 避免除0
  375. */
  376. float torque_ff = dc_lim * get_vbus_float() * 9.55f * 0.8f/(ctrl->foc.in.mot_velocity + 0.0001f);
  377. torque_ff = ABS(torque_ff);
  378. if (torque_ff > maxTrq) {
  379. ctrl->dc_lim_t_ff = maxTrq;
  380. }else if (torque_ff < 0) {
  381. ctrl->dc_lim_t_ff = 0;
  382. }else {
  383. ctrl->dc_lim_t_ff = torque_ff;
  384. }
  385. /** 使用min(maxTrq, ctrl->dc_lim_t_ff * 1.1f)带入max的原因是为了防止积分器饱和后有过冲现象
  386. * 1.1f的系数是为了保证ctrl->dc_lim_t_ff 计算偏小的情况下功率能达到上限,这个系数太大不能
  387. * 防止过冲
  388. */
  389. ctrl->pi_power.max = min(maxTrq, ctrl->dc_lim_t_ff * 1.1f);
  390. float errRef = dc_lim - ctrl->dc_curr_filted;
  391. return PI_Controller_Parallel(&ctrl->pi_power, errRef, ctrl->dc_lim_t_ff);
  392. }
  393. #endif
  394. static __INLINE float mot_contrl_vel_limiter(mot_contrl_t *ctrl, float maxTrq) {
  395. ctrl->pi_vel_lim.max = maxTrq;
  396. ctrl->pi_vel_lim.min = 0;
  397. float err = line_ramp_get_interp(&ctrl->ramp_vel_lim) - ctrl->foc.in.mot_velocity;
  398. return PI_Controller_Parallel(&ctrl->pi_vel_lim, err, 0);
  399. }
  400. /* current vector or torque to dq axis current */
  401. static void mot_contrl_dq_assign(mot_contrl_t *ctrl) {
  402. if (ctrl->mode_running == CTRL_MODE_CURRENT) {
  403. float target_current = line_ramp_get_interp(&ctrl->ramp_target_current);
  404. if (ctrl->b_mtpa_calibrate && (ctrl->adv_angle != INVALID_ANGLE)) {
  405. float s, c;
  406. float angle_step = line_ramp_step(&ctrl->ramp_adv_angle);
  407. arm_sin_cos(angle_step + 90.0f, &s, &c);
  408. ctrl->target_idq.d = target_current * c;
  409. if (ctrl->target_idq.d > ctrl->hwlim.fw_id) {
  410. ctrl->target_idq.d = ctrl->hwlim.fw_id;
  411. }else if (ctrl->target_idq.d < -ctrl->hwlim.fw_id) {
  412. ctrl->target_idq.d = -ctrl->hwlim.fw_id;
  413. }
  414. ctrl->target_idq.q = sqrtsub2_f(target_current, ctrl->target_idq.d);
  415. if (s < 0) {
  416. ctrl->target_idq.q = -ctrl->target_idq.q;
  417. }
  418. }else {
  419. ctrl->target_idq.d = 0;
  420. ctrl->target_idq.q = target_current;
  421. }
  422. }else if (ctrl->mode_running != CTRL_MODE_OPEN) {
  423. motor_mpta_fw_lookup(ctrl->foc.in.mot_velocity, ctrl->target_torque, &ctrl->target_idq);
  424. }
  425. u32 mask = cpu_enter_critical();
  426. foc_set_target_idq(&ctrl->foc, &ctrl->target_idq);
  427. cpu_exit_critical(mask);
  428. }
  429. static void crosszero_step_towards(float *value, float target) {
  430. static float no_cro_step = CONFIG_CrossZero_NorStep;
  431. float v_now = *value;
  432. bool cross_zero = false;
  433. float nor_step = mc_conf()->cz.normal_step;
  434. float min_step = mc_conf()->cz.min_step;
  435. float min_ramp_torque = mc_conf()->cz.low;
  436. float high_ramp_torque = mc_conf()->cz.high;
  437. if (target > 0) {
  438. if (v_now < -min_ramp_torque) {
  439. step_towards(value, -min_ramp_torque + 0.001f, nor_step);
  440. cross_zero = true;
  441. }else if (v_now >= -min_ramp_torque && v_now <= high_ramp_torque) {
  442. step_towards(value, target, min_step);
  443. cross_zero = true;
  444. }
  445. }else if (target == 0) {
  446. if (v_now > high_ramp_torque) {
  447. step_towards(value, high_ramp_torque - 0.001f, nor_step);
  448. cross_zero = true;
  449. }else if (v_now >= min_ramp_torque && v_now <= high_ramp_torque) {
  450. step_towards(value, target, min_step);
  451. cross_zero = true;
  452. }
  453. }else {
  454. if (v_now > high_ramp_torque) {
  455. step_towards(value, high_ramp_torque - 0.001f, nor_step);
  456. cross_zero = true;
  457. }else if (v_now >= -min_ramp_torque && v_now <= high_ramp_torque) {
  458. step_towards(value, target, min_step);
  459. cross_zero = true;
  460. }
  461. }
  462. if (!cross_zero) {
  463. step_towards(&no_cro_step, nor_step, 0.1f);
  464. step_towards(value, target, no_cro_step);
  465. }else {
  466. no_cro_step = 0.5f;
  467. }
  468. }
  469. /*called in media task */
  470. void mot_contrl_dq_calc(mot_contrl_t *ctrl) {
  471. foc_t *foc = &ctrl->foc;
  472. float etcs_out = etcs_process(&ctrl->etcs);
  473. if (ctrl->b_AutoHold) {
  474. float hold_torque = min(ctrl->protlim.torque, mc_conf()->c.max_autohold_torque);
  475. ctrl->pi_lock.max = hold_torque;
  476. ctrl->pi_lock.min = -hold_torque;
  477. float vel_count = motor_encoder_get_vel_count();
  478. float errRef = 0 - vel_count;
  479. ctrl->target_torque = PI_Controller_Parallel(&ctrl->pi_lock ,errRef, 0);
  480. mot_contrl_dq_assign(ctrl);
  481. return;
  482. }
  483. if (ctrl->mode_running == CTRL_MODE_CURRENT) {
  484. line_ramp_step(&ctrl->ramp_target_current);
  485. }else if (ctrl->mode_running == CTRL_MODE_EBRAKE) {
  486. float maxTrq = line_ramp_step(&ctrl->ramp_input_torque);
  487. if (line_ramp_get_target(&ctrl->ramp_input_torque) < 0.0001f && foc->in.mot_velocity < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  488. maxTrq = 0;
  489. }
  490. crosszero_step_towards(&ctrl->target_torque, maxTrq);
  491. }else if (ctrl->mode_running == CTRL_MODE_TRQ) {
  492. float refTorque = line_ramp_step(&ctrl->ramp_input_torque);
  493. refTorque = min(refTorque, line_ramp_get_interp(&ctrl->ramp_torque_lim)) * etcs_out;
  494. float maxTrq = mot_contrl_vel_limiter(ctrl, refTorque);
  495. ctrl->target_torque_raw = mot_contrl_dc_curr_limiter(ctrl, maxTrq);
  496. crosszero_step_towards(&ctrl->target_torque, ctrl->target_torque_raw);
  497. }else if (ctrl->mode_running == CTRL_MODE_SPD){
  498. float refSpeed;
  499. float maxSpeed;
  500. if (ctrl->b_cruiseEna) {
  501. refSpeed = line_ramp_step(&ctrl->ramp_cruise_vel);
  502. maxSpeed = line_ramp_get_target(&ctrl->ramp_cruise_vel);
  503. }else {
  504. refSpeed = line_ramp_step(&ctrl->ramp_target_vel);
  505. maxSpeed = line_ramp_get_target(&ctrl->ramp_target_vel);
  506. }
  507. float max_input = line_ramp_get_interp(&ctrl->ramp_torque_lim) * etcs_out;
  508. if (maxSpeed >= 0) {
  509. ctrl->pi_vel.max = max_input;
  510. #ifdef CONFIG_SERVO_MOTOR
  511. ctrl->pi_vel.min = -max_input;
  512. #else
  513. ctrl->pi_vel.min = -CONFIG_MAX_NEG_TORQUE;
  514. #endif
  515. }else if (maxSpeed < 0) {
  516. ctrl->pi_vel.min = -max_input;
  517. #ifdef CONFIG_SERVO_MOTOR
  518. ctrl->pi_vel.max = max_input;
  519. #else
  520. ctrl->pi_vel.max = CONFIG_MAX_NEG_TORQUE;
  521. #endif
  522. }
  523. if ((maxSpeed == 0) && (ctrl->foc.in.mot_velocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  524. ctrl->pi_vel.max = 0;
  525. ctrl->pi_vel.min = 0; //防止倒转
  526. }
  527. float errRef = refSpeed - ctrl->foc.in.mot_velocity;
  528. float maxTrq = PI_Controller_Parallel(&ctrl->pi_vel, errRef, 0);
  529. ctrl->target_torque_raw = mot_contrl_dc_curr_limiter(ctrl, maxTrq);
  530. crosszero_step_towards(&ctrl->target_torque, ctrl->target_torque_raw);
  531. }
  532. mot_contrl_dq_assign(ctrl);
  533. }
  534. static void mot_contrl_pid(mot_contrl_t *ctrl) {
  535. float slow_ctrl_ts = (1.0f/(float)CONFIG_SPD_CTRL_TS);
  536. PI_Controller_Reset(&ctrl->pi_power, 0);
  537. ctrl->pi_power.kp = mc_conf()->c.pid[PID_IDCLim_ID].kp;
  538. ctrl->pi_power.ki = mc_conf()->c.pid[PID_IDCLim_ID].ki;
  539. ctrl->pi_power.kd = mc_conf()->c.pid[PID_IDCLim_ID].kd;
  540. ctrl->pi_power.ts = slow_ctrl_ts;
  541. PI_Controller_Reset(&ctrl->pi_lock, 0);
  542. ctrl->pi_lock.kp = mc_conf()->c.pid[PID_AutoHold_ID].kp;
  543. ctrl->pi_lock.ki = mc_conf()->c.pid[PID_AutoHold_ID].ki;
  544. ctrl->pi_lock.kd = mc_conf()->c.pid[PID_AutoHold_ID].kd;
  545. ctrl->pi_lock.ts = slow_ctrl_ts;
  546. PI_Controller_Reset(&ctrl->pi_vel_lim, 0);
  547. ctrl->pi_vel_lim.kp = mc_conf()->c.pid[PID_VelLim_ID].kp;
  548. ctrl->pi_vel_lim.ki = mc_conf()->c.pid[PID_VelLim_ID].ki;
  549. ctrl->pi_vel_lim.kd = mc_conf()->c.pid[PID_VelLim_ID].kd;
  550. ctrl->pi_vel_lim.ts = slow_ctrl_ts;
  551. PI_Controller_Reset(&ctrl->pi_vel, 0);
  552. ctrl->pi_vel.kp = mc_conf()->c.pid[PID_Vel_ID].kp;
  553. ctrl->pi_vel.ki = mc_conf()->c.pid[PID_Vel_ID].ki;
  554. ctrl->pi_vel.kd = mc_conf()->c.pid[PID_Vel_ID].kd;
  555. ctrl->pi_vel.ts = slow_ctrl_ts;
  556. }
  557. static void mot_contrl_ulimit(mot_contrl_t *ctrl) {
  558. ctrl->userlim.dc_curr = min(mc_conf()->c.max_idc, ctrl->hwlim.dc_curr);
  559. ctrl->userlim.mot_vel = min(mc_conf()->c.max_rpm, ctrl->hwlim.mot_vel);
  560. ctrl->userlim.torque = mc_conf()->c.max_torque;//MAX_TORQUE;
  561. ctrl->userlim.phase_curr = min(mc_conf()->c.max_phase_curr, ctrl->hwlim.phase_curr);
  562. ctrl->userlim.dc_vol_min = mc_conf()->c.max_dc_vol;
  563. ctrl->userlim.dc_vol_max = mc_conf()->c.min_dc_vol;
  564. ctrl->userlim.ebrk_dc_curr = 0xFF;
  565. ctrl->userlim.ebrk_torque = mc_get_ebrk_torque();
  566. }
  567. static void mot_contrl_rtlimit(mot_contrl_t *ctrl) {
  568. line_ramp_reset(&ctrl->ramp_torque_lim, ctrl->userlim.torque);
  569. line_ramp_reset(&ctrl->ramp_dc_curr_lim, ctrl->userlim.dc_curr);
  570. line_ramp_reset(&ctrl->ramp_vel_lim, ctrl->userlim.mot_vel);
  571. }
  572. void mot_contrl_slow_task(mot_contrl_t *ctrl) {
  573. line_ramp_step(&ctrl->ramp_torque_lim);
  574. line_ramp_step(&ctrl->ramp_dc_curr_lim);
  575. line_ramp_step(&ctrl->ramp_vel_lim);
  576. mot_contrl_dq_calc(ctrl);
  577. }
  578. u8 mot_contrl_protect(mot_contrl_t *ctrl) {
  579. float torque_lim;
  580. u8 changed = FOC_LIM_NO_CHANGE;
  581. float dc_lim = (float)vbus_voltage_low_limit();
  582. float mot_lim_r = motor_temp_high_limit();
  583. float mos_lim_r = mos_temp_high_limit();
  584. float mos_torque = HW_LIMIT_NONE;
  585. float mot_torque = HW_LIMIT_NONE;
  586. float mot_idc = HW_LIMIT_NONE;
  587. if (mot_lim_r < 1.0f) {
  588. mot_torque = mot_lim_r * mc_gear_conf()->max_torque;
  589. mot_idc = mot_lim_r * mc_gear_conf()->max_idc;
  590. }
  591. if (mos_lim_r < 1.0f) {
  592. mos_torque = mos_lim_r * mc_gear_conf()->max_torque;
  593. }
  594. dc_lim = min(dc_lim, mot_idc);
  595. torque_lim = min(mos_torque, mot_torque);
  596. if (ctrl->protlim.dc_curr != dc_lim || ctrl->protlim.torque != torque_lim) {
  597. if ((dc_lim > ctrl->protlim.dc_curr) || (torque_lim > ctrl->protlim.torque)) {
  598. changed = FOC_LIM_CHANGE_H;
  599. }else {
  600. changed = FOC_LIM_CHANGE_L;
  601. }
  602. ctrl->protlim.dc_curr = dc_lim;
  603. ctrl->protlim.torque = torque_lim;
  604. }
  605. return changed;
  606. }
  607. float mot_contrl_get_speed(mot_contrl_t *ctrl) {
  608. float speed = ctrl->foc.in.mot_velocity;
  609. if (!ctrl->b_start || foc_observer_is_encoder()) {
  610. speed = motor_encoder_get_speed();
  611. }else {
  612. if (foc_observer_sensorless_stable()) {
  613. speed = foc_observer_sensorless_speed();
  614. }else {
  615. speed = 0;
  616. }
  617. }
  618. return speed;
  619. }
  620. void mot_contrl_velloop_params(mot_contrl_t *ctrl, float wcv, float b0) {
  621. #ifdef CONFIG_SPEED_LADRC
  622. ladrc_change_b0(&gFoc_Ctrl.vel_adrc, b0);
  623. ladrc_change_K(&gFoc_Ctrl.vel_adrc, wcv);
  624. #else
  625. PI_Controller_Change_Kpi(&ctrl->pi_vel, wcv, b0);
  626. #endif
  627. }
  628. void mot_contrl_trqloop_params(mot_contrl_t *ctrl, float wcv, float b0) {
  629. #ifdef CONFIG_SPEED_LADRC
  630. ladrc_change_b0(&gFoc_Ctrl.vel_lim_adrc, b0);
  631. ladrc_change_K(&gFoc_Ctrl.vel_lim_adrc, wcv);
  632. #else
  633. PI_Controller_Change_Kpi(&ctrl->pi_vel_lim, wcv, b0);
  634. #endif
  635. }
  636. void mot_contrl_set_dccurr_limit(mot_contrl_t *ctrl, float ibusLimit) {
  637. if (ibusLimit > ctrl->hwlim.dc_curr) {
  638. ibusLimit = ctrl->hwlim.dc_curr;
  639. }
  640. if (ctrl->protlim.dc_curr != HW_LIMIT_NONE) {
  641. ibusLimit = min(ibusLimit, ctrl->protlim.dc_curr);
  642. }
  643. ctrl->userlim.dc_curr = ibusLimit;
  644. /*
  645. * 这个地方不能像限制扭矩和速度那样从当前实际母线电流开始做ramp
  646. * 因为用了功率环前馈,会导致扭矩突然下降
  647. */
  648. line_ramp_set_target(&ctrl->ramp_dc_curr_lim, ctrl->userlim.dc_curr);
  649. }
  650. void mot_contrl_set_vel_limit(mot_contrl_t *ctrl, float vel) {
  651. if (vel > ctrl->hwlim.mot_vel) {
  652. vel = ctrl->hwlim.mot_vel;
  653. }
  654. ctrl->userlim.mot_vel = vel;
  655. line_ramp_reset(&ctrl->ramp_vel_lim, ctrl->foc.in.mot_velocity);//从当前实际转速做ramp
  656. line_ramp_set_target(&ctrl->ramp_vel_lim, ctrl->userlim.mot_vel);
  657. }
  658. void mot_contrl_set_vel_limit_rttime(mot_contrl_t *ctrl, u32 time) {
  659. line_ramp_set_time(&ctrl->ramp_vel_lim, (float)time);
  660. line_ramp_update(&ctrl->ramp_vel_lim);
  661. }
  662. void mot_contrl_set_torque_limit(mot_contrl_t *ctrl, float torque) {
  663. if (torque > ctrl->hwlim.torque) {
  664. torque = ctrl->hwlim.torque;
  665. }
  666. if (ctrl->protlim.torque != HW_LIMIT_NONE) {
  667. torque = min(torque, ctrl->protlim.torque);
  668. }
  669. ctrl->userlim.torque = torque;
  670. line_ramp_reset(&ctrl->ramp_torque_lim, ctrl->target_torque_raw);//从当前给定的torque做ramp
  671. line_ramp_set_target(&ctrl->ramp_torque_lim, ctrl->userlim.torque);
  672. }
  673. void mot_contrl_set_torque_limit_rttime(mot_contrl_t *ctrl, u32 time) {
  674. line_ramp_set_time(&ctrl->ramp_torque_lim, (float)time);
  675. line_ramp_update(&ctrl->ramp_torque_lim);
  676. }
  677. float mot_contrl_get_ebrk_torque(mot_contrl_t *ctrl) {
  678. if (!foc_observer_is_encoder()) {
  679. return 0; //无感运行关闭能量回收
  680. }
  681. return ctrl->userlim.ebrk_torque;
  682. }
  683. void mot_contrl_set_ebrk_time(mot_contrl_t *ctrl, u32 time) {
  684. ctrl->ebrk_ramp_time = time;
  685. if ((ctrl->mode_running == CTRL_MODE_EBRAKE) && (time != ctrl->ramp_input_torque.time_dec)) {
  686. line_ramp_set_time(&ctrl->ramp_input_torque, time);
  687. line_ramp_update(&ctrl->ramp_input_torque);
  688. }
  689. }
  690. void mot_contrl_set_vdq(mot_contrl_t *ctrl, float vd, float vq) {
  691. line_ramp_set_target(&ctrl->ramp_target_vd, vd);
  692. line_ramp_set_target(&ctrl->ramp_target_vq, vq);
  693. }
  694. void mot_contrl_set_vdq_immediate(mot_contrl_t *ctrl, float vd, float vq) {
  695. line_ramp_reset(&ctrl->ramp_target_vd, vd);
  696. line_ramp_reset(&ctrl->ramp_target_vq, vq);
  697. }
  698. bool mot_contrl_set_cruise(mot_contrl_t *ctrl, bool enable) {
  699. if (enable != ctrl->b_cruiseEna) {
  700. float motSpd = mot_contrl_get_speed(ctrl);
  701. if (enable && (motSpd < CONFIG_MIN_CRUISE_RPM)) { //
  702. mot_contrl_set_error(ctrl, FOC_NowAllowed_With_Speed);
  703. return false;
  704. }
  705. line_ramp_reset(&ctrl->ramp_cruise_vel, motSpd);
  706. ctrl->b_cruiseEna = enable;
  707. }
  708. return true;
  709. }
  710. bool mot_contrl_resume_cruise(mot_contrl_t *ctrl) {
  711. ctrl->b_cruiseEna = true;
  712. line_ramp_set_time(&ctrl->ramp_cruise_vel, CONFIG_CRUISE_RAMP_TIME);
  713. return true;
  714. }
  715. bool mot_contrl_set_cruise_speed(mot_contrl_t *ctrl, float rpm) {
  716. if (ctrl->b_cruiseEna) {
  717. if (rpm < CONFIG_MIN_CRUISE_RPM) {
  718. rpm = CONFIG_MIN_CRUISE_RPM;
  719. }
  720. float vel = min(ABS(rpm),ctrl->userlim.mot_vel)*SIGN(rpm);
  721. line_ramp_set_target(&ctrl->ramp_cruise_vel, vel);
  722. return true;
  723. }
  724. mot_contrl_set_error(ctrl, FOC_NotCruiseMode);
  725. return false;
  726. }
  727. bool mot_contrl_set_current(mot_contrl_t *ctrl, float is) {
  728. is = fclamp(is, -ctrl->userlim.phase_curr, ctrl->userlim.phase_curr);
  729. line_ramp_set_target(&ctrl->ramp_target_current, is);
  730. return true;
  731. }
  732. void mot_contrl_set_torque_ramp_time(mot_contrl_t *ctrl, u32 acc, u32 dec) {
  733. ctrl->torque_acc_time = acc;
  734. ctrl->torque_dec_time = dec;
  735. if (ctrl->mode_running == CTRL_MODE_TRQ) {
  736. line_ramp_set_acctime(&ctrl->ramp_input_torque, acc);
  737. line_ramp_set_dectime(&ctrl->ramp_input_torque, dec);
  738. line_ramp_update(&ctrl->ramp_input_torque);
  739. }
  740. }
  741. void mot_contrl_set_torque_acc_time(mot_contrl_t *ctrl, u32 acc) {
  742. ctrl->torque_acc_time = acc;
  743. if (ctrl->mode_running == CTRL_MODE_TRQ) {
  744. line_ramp_set_acctime(&ctrl->ramp_input_torque, acc);
  745. line_ramp_update(&ctrl->ramp_input_torque);
  746. }
  747. }
  748. bool mot_contrl_set_torque(mot_contrl_t *ctrl, float torque) {
  749. if (is_hw_brake_shutting_power(ctrl) && !ctrl->b_ebrk_running){
  750. return false;
  751. }
  752. float torque_min = 0;
  753. float torque_max = ctrl->userlim.torque;
  754. if (ctrl->mode_running == CTRL_MODE_EBRAKE) {
  755. torque_min = -ctrl->userlim.ebrk_torque;
  756. torque_max = 0;
  757. }
  758. torque = fclamp(torque, torque_min, torque_max);
  759. line_ramp_set_target(&ctrl->ramp_input_torque, torque);
  760. return true;
  761. }
  762. /* 这个接口只在上位机直接设置扭矩的时候调试,其他情况一律不能使用,扭矩请求可以未负 */
  763. bool mot_contrl_set_force_torque(mot_contrl_t *ctrl, float torque) {
  764. if (is_hw_brake_shutting_power(ctrl) && !ctrl->b_ebrk_running){
  765. return false;
  766. }
  767. float torque_min = -ctrl->userlim.torque;
  768. float torque_max = ctrl->userlim.torque;
  769. if (ctrl->mode_running == CTRL_MODE_EBRAKE) {
  770. torque_min = -ctrl->userlim.torque;
  771. torque_max = 0;
  772. }
  773. torque = fclamp(torque, torque_min, torque_max);
  774. line_ramp_set_target(&ctrl->ramp_input_torque, torque);
  775. return true;
  776. }
  777. void mot_contrl_mtpa_calibrate(mot_contrl_t *ctrl, bool enable) {
  778. if (enable) {
  779. line_ramp_reset(&ctrl->ramp_adv_angle, 0);
  780. ctrl->b_mtpa_calibrate = true;
  781. ctrl->adv_angle = 0;
  782. }else {
  783. ctrl->adv_angle = INVALID_ANGLE;
  784. ctrl->b_mtpa_calibrate = false;
  785. }
  786. }
  787. void mot_contrl_set_autohold(mot_contrl_t *ctrl, bool lock) {
  788. if (ctrl->b_AutoHold != lock) {
  789. motor_encoder_lock_pos(lock);
  790. PI_Controller_Reset(&ctrl->pi_lock, 0);
  791. if (!lock) {
  792. float hold_torque = ctrl->target_torque * 1.1f;
  793. if (ctrl->mode_running == CTRL_MODE_TRQ) {
  794. PI_Controller_Reset(&ctrl->pi_vel_lim, hold_torque);
  795. }else if (ctrl->mode_running == CTRL_MODE_SPD) {
  796. PI_Controller_Reset(&ctrl->pi_vel, hold_torque);
  797. }
  798. line_ramp_reset(&ctrl->ramp_input_torque, hold_torque);
  799. ctrl->autohold_torque = hold_torque;
  800. }else {
  801. ctrl->autohold_torque = 0;
  802. }
  803. ctrl->b_AutoHold = lock;
  804. }
  805. }
  806. static bool is_hw_brake_shutting_power(mot_contrl_t *ctrl) {
  807. return (ctrl->b_hw_braker && mc_hwbrk_can_shutpower());
  808. }
  809. bool mot_contrl_set_ebreak(mot_contrl_t *ctrl, bool start) {
  810. bool enable = ctrl->b_ebrk_running;
  811. if (mot_contrl_get_ebrk_torque(ctrl) == 0) {
  812. enable = false;
  813. }else if (start && ctrl->foc.in.mot_velocity >= CONFIG_MIN_RPM_FOR_EBRAKE){
  814. enable = true;
  815. }else if (!start && !is_hw_brake_shutting_power(ctrl)) {
  816. enable = false;
  817. }
  818. if (enable != ctrl->b_ebrk_running) {
  819. ctrl->b_ebrk_running = enable;
  820. if (enable) {
  821. ctrl->mode_req = CTRL_MODE_EBRAKE;
  822. }else {
  823. ctrl->mode_req = CTRL_MODE_TRQ;
  824. }
  825. }
  826. return enable;
  827. }
  828. void mot_contrl_set_hw_brake(mot_contrl_t *ctrl, bool hw_brake) {
  829. u32 mask = cpu_enter_critical();
  830. if (hw_brake != ctrl->b_hw_braker) {
  831. ctrl->b_hw_braker = hw_brake;
  832. }
  833. if (is_hw_brake_shutting_power(ctrl)) {
  834. if (!ctrl->b_ebrk_running && !mot_contrl_set_ebreak(ctrl, true)) {
  835. line_ramp_reset(&ctrl->ramp_input_torque, 0);
  836. }
  837. }
  838. cpu_exit_critical(mask);
  839. }
  840. static PI_Controller *_pid(mot_contrl_t *ctrl, u8 id) {
  841. PI_Controller *pi = NULL;
  842. if (id == PID_ID_ID) {
  843. pi = &ctrl->foc.daxis;
  844. }else if (id == PID_IQ_ID) {
  845. pi = &ctrl->foc.qaxis;
  846. }else if (id == PID_VelLim_ID) {
  847. pi = &ctrl->pi_vel_lim;
  848. }else if (id == PID_Vel_ID) {
  849. pi = &ctrl->pi_vel;
  850. }else if (id == PID_AutoHold_ID) {
  851. pi = &ctrl->pi_lock;
  852. }
  853. return pi;
  854. }
  855. void mot_contrl_set_pid(mot_contrl_t *ctrl, u8 id, float kp, float ki, float kd) {
  856. if (id > PID_Max_ID) {
  857. return;
  858. }
  859. PI_Controller *pi = _pid(ctrl, id);
  860. if (pi != NULL) {
  861. u32 mask = cpu_enter_critical();
  862. pi->kp = kp;
  863. pi->ki = ki;
  864. pi->kd = kd;
  865. cpu_exit_critical(mask);
  866. }
  867. }
  868. void mot_contrl_get_pid(mot_contrl_t *ctrl, u8 id, float *kp, float *ki, float *kd) {
  869. if (id > PID_Max_ID) {
  870. return;
  871. }
  872. PI_Controller *pi = _pid(ctrl, id);
  873. if (pi != NULL) {
  874. *kp = pi->kp;
  875. *ki = pi->ki;
  876. *kd = pi->kd;
  877. }
  878. }
  879. void mot_contrl_calc_current(mot_contrl_t *ctrl) {
  880. float id = ctrl->out_idq_filterd.d;
  881. float iq = ctrl->out_idq_filterd.q;
  882. float raw_idc = get_vbus_current();
  883. if (raw_idc != NO_VALID_CURRENT) {
  884. LowPass_Filter(ctrl->dc_curr_filted, raw_idc, 0.04f);
  885. }else {
  886. ctrl->dc_curr_filted = ctrl->dc_curr_calc;
  887. }
  888. ctrl->out_current_vec = NORM2_f(id, iq);
  889. }