controller.c 29 KB

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