controller.c 25 KB

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