controller.c 34 KB

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