controller.c 26 KB

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