controller.c 31 KB

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