PMSM_FOC_Core.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. #include "arm_math.h"
  2. #include "PMSM_FOC_Core.h"
  3. #include "foc/foc_config.h"
  4. #include "foc/motor/motor_param.h"
  5. #include "foc/core/e_ctrl.h"
  6. #include "math/fix_math.h"
  7. #include "math/fast_math.h"
  8. #include "foc/motor/current.h"
  9. #include "foc/motor/motor.h"
  10. #include "foc/core/svpwm.h"
  11. #include "foc/core/thro_torque.h"
  12. #include "foc/core/foc_observer.h"
  13. #include "foc/core/F_Calc.h"
  14. #include "foc/samples.h"
  15. #include "foc/limit.h"
  16. #include "app/nv_storage.h"
  17. #include "bsp/bsp_driver.h"
  18. #include "libs/logger.h"
  19. #include "math/fir.h"
  20. #define _DEBUG(fmt, args...) sys_debug(fmt, ##args)
  21. PMSM_FOC_Ctrl gFoc_Ctrl;
  22. static bool g_focinit = false;
  23. static u32 PMSM_FOC_Debug_Task(void *p);
  24. static __INLINE void RevPark(DQ_t *dq, float angle, AB_t *alpha_beta) {
  25. float c,s;
  26. #if 0
  27. SinCos_Lut(angle, &s, &c);
  28. #else
  29. s = gFoc_Ctrl.out.sin;
  30. c = gFoc_Ctrl.out.cos;
  31. #endif
  32. alpha_beta->a = dq->d * c - dq->q * s;
  33. alpha_beta->b = dq->d * s + dq->q * c;
  34. }
  35. static __INLINE void Clark(float A, float B, float C, AB_t *alpha_beta){
  36. alpha_beta->a = A;
  37. alpha_beta->b = ONE_BY_SQRT3 * (B - C);
  38. }
  39. static __INLINE void Park(AB_t *alpha_beta, float angle, DQ_t *dq) {
  40. float c,s;
  41. #if 0
  42. SinCos_Lut(angle, &s, &c);
  43. #else
  44. s = gFoc_Ctrl.out.sin;
  45. c = gFoc_Ctrl.out.cos;
  46. #endif
  47. dq->d = alpha_beta->a * c + alpha_beta->b * s;
  48. dq->q = -alpha_beta->a * s + alpha_beta->b * c;
  49. }
  50. #if 0
  51. #define VD_PRIO_HIGH
  52. static __INLINE float Circle_Limitation(DQ_t *vdq, float vDC, float module, DQ_t *out) {
  53. float sq_vdq = vdq->d * vdq->d + vdq->q * vdq->q;
  54. float vDC_m = vDC * module * SQRT3_BY_2;
  55. float sq_vDC = vDC_m * vDC_m;
  56. if (sq_vdq > sq_vDC) {
  57. #ifdef VD_PRIO_HIGH
  58. out->d = vdq->d;
  59. out->q = sqrtf(sq_vDC - out->d*out->d);
  60. #else
  61. float r = sqrtf(sq_vDC / sq_vdq);
  62. out->d = vdq->d * r;
  63. out->q = vdq->q * r;
  64. #endif
  65. }else {
  66. out->d = vdq->d;
  67. out->q = vdq->q;
  68. }
  69. return sqrtf(sq_vdq/sq_vDC);
  70. }
  71. #endif
  72. static __INLINE void FOC_Set_DqRamp(dq_Rctrl *c, float target, int time) {
  73. float cp = c->s_Cp;
  74. c->s_FinalTgt = target;
  75. c->s_Step = (c->s_FinalTgt - cp) / (float)time;
  76. }
  77. static __INLINE float FOC_Get_DqRamp(dq_Rctrl *c) {
  78. if (++c->n_StepCount == c->n_CtrlCount) {
  79. c->s_Cp += c->s_Step;
  80. if (c->s_Step < 0) {
  81. if (c->s_Cp < c->s_FinalTgt) {
  82. c->s_Cp = c->s_FinalTgt;
  83. }
  84. }else {
  85. if (c->s_Cp > c->s_FinalTgt) {
  86. c->s_Cp = c->s_FinalTgt;
  87. }
  88. }
  89. c->n_StepCount = 0;
  90. }
  91. return c->s_Cp;
  92. }
  93. static __INLINE void FOC_DqRamp_init(dq_Rctrl *c, int count) {
  94. c->n_CtrlCount = count;
  95. c->n_StepCount = 0;
  96. c->s_Cp = 0;
  97. c->s_FinalTgt = 0;
  98. c->s_Step = 0;
  99. }
  100. static __INLINE void FOC_Set_iDqRamp(dq_Rctrl *c, float target) {
  101. FOC_Set_DqRamp(c, target, (/*CONFIG_IDQ_CTRL_TS/CONFIG_SPD_CTRL_TS - 1*/CURRENT_LOOP_RAMP_COUNT));
  102. }
  103. static __INLINE void FOC_Set_vDqRamp(dq_Rctrl *c, float target) {
  104. FOC_Set_DqRamp(c, target, (CONFIG_FOC_VDQ_RAMP_FINAL_TIME/1000*((CONFIG_IDQ_CTRL_TS/CONFIG_FOC_VDQ_RAMP_TS))));
  105. }
  106. static void PMSM_FOC_Reset_PID(void) {
  107. PI_Controller_Reset(&gFoc_Ctrl.pi_id, 0);
  108. PI_Controller_Reset(&gFoc_Ctrl.pi_iq, 0);
  109. PI_Controller_Reset(&gFoc_Ctrl.pi_lock, 0);
  110. PI_Controller_Reset(&gFoc_Ctrl.pi_power, 0);
  111. #ifdef CONFIG_SPEED_LADRC
  112. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, 0, 0);
  113. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, 0);
  114. #else
  115. PI_Controller_Reset(&gFoc_Ctrl.pi_speed, 0);
  116. PI_Controller_Reset(&gFoc_Ctrl.pi_torque, 0);
  117. #endif
  118. }
  119. static void PMSM_FOC_Conf_PID(void) {
  120. float slow_ctrl_ts = (1.0f/(float)CONFIG_SPD_CTRL_TS);
  121. gFoc_Ctrl.pi_id.kp = nv_get_foc_params()->pid_conf[PID_D_id].kp;
  122. gFoc_Ctrl.pi_id.ki = nv_get_foc_params()->pid_conf[PID_D_id].ki;
  123. gFoc_Ctrl.pi_id.kd = nv_get_foc_params()->pid_conf[PID_D_id].kd;
  124. gFoc_Ctrl.pi_id.DT = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
  125. gFoc_Ctrl.pi_iq.kp = nv_get_foc_params()->pid_conf[PID_Q_id].kp;
  126. gFoc_Ctrl.pi_iq.ki = nv_get_foc_params()->pid_conf[PID_Q_id].ki;
  127. gFoc_Ctrl.pi_iq.kd = nv_get_foc_params()->pid_conf[PID_Q_id].kd;
  128. gFoc_Ctrl.pi_iq.DT = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
  129. gFoc_Ctrl.pi_power.kp = nv_get_foc_params()->pid_conf[PID_Pow_id].kp;
  130. gFoc_Ctrl.pi_power.ki = nv_get_foc_params()->pid_conf[PID_Pow_id].ki;
  131. gFoc_Ctrl.pi_power.kd = nv_get_foc_params()->pid_conf[PID_Pow_id].kd;
  132. gFoc_Ctrl.pi_power.DT = slow_ctrl_ts;
  133. gFoc_Ctrl.pi_lock.kp = nv_get_foc_params()->pid_conf[PID_Lock_id].kp;
  134. gFoc_Ctrl.pi_lock.ki = nv_get_foc_params()->pid_conf[PID_Lock_id].ki;
  135. gFoc_Ctrl.pi_lock.kd = nv_get_foc_params()->pid_conf[PID_Lock_id].kd;
  136. gFoc_Ctrl.pi_lock.DT = slow_ctrl_ts;
  137. #ifdef CONFIG_SPEED_LADRC
  138. ladrc_init(&gFoc_Ctrl.vel_lim_adrc, slow_ctrl_ts, nv_get_foc_params()->f_adrc_vel_lim_Wo, nv_get_foc_params()->f_adrc_vel_lim_Wcv, nv_get_foc_params()->f_adrc_vel_lim_B0);
  139. ladrc_init(&gFoc_Ctrl.vel_adrc, slow_ctrl_ts, nv_get_foc_params()->f_adrc_vel_Wo, nv_get_foc_params()->f_adrc_vel_Wcv, nv_get_foc_params()->f_adrc_vel_B0);
  140. #else
  141. gFoc_Ctrl.pi_torque.kp = nv_get_foc_params()->pid_conf[PID_TRQ_id].kp;
  142. gFoc_Ctrl.pi_torque.ki = nv_get_foc_params()->pid_conf[PID_TRQ_id].ki;
  143. gFoc_Ctrl.pi_torque.kd = nv_get_foc_params()->pid_conf[PID_TRQ_id].kd;
  144. gFoc_Ctrl.pi_torque.DT = slow_ctrl_ts;
  145. gFoc_Ctrl.pi_speed.kp = nv_get_foc_params()->pid_conf[PID_Spd_id].kp;
  146. gFoc_Ctrl.pi_speed.ki = nv_get_foc_params()->pid_conf[PID_Spd_id].ki;
  147. gFoc_Ctrl.pi_speed.kd = nv_get_foc_params()->pid_conf[PID_Spd_id].kd;
  148. gFoc_Ctrl.pi_speed.DT = slow_ctrl_ts;
  149. #endif
  150. }
  151. static void PMSM_FOC_UserInit(void) {
  152. memset(&gFoc_Ctrl.userLim, 0, sizeof(gFoc_Ctrl.userLim));
  153. gFoc_Ctrl.userLim.s_iDCLim = min(nv_get_foc_params()->s_LimitiDC, gFoc_Ctrl.hwLim.s_iDCMax);
  154. gFoc_Ctrl.userLim.s_motRPMLim = min(nv_get_foc_params()->s_maxRPM, gFoc_Ctrl.hwLim.s_motRPMMax);
  155. gFoc_Ctrl.userLim.s_torqueLim = nv_get_foc_params()->s_maxTorque;//MAX_TORQUE;
  156. gFoc_Ctrl.userLim.s_PhaseCurrLim = min(nv_get_foc_params()->s_PhaseCurrLim, gFoc_Ctrl.hwLim.s_PhaseCurrMax);
  157. gFoc_Ctrl.userLim.s_vDCMaxLim = nv_get_foc_params()->s_maxDCVol;
  158. gFoc_Ctrl.userLim.s_vDCMinLim = nv_get_foc_params()->s_minDCVol;
  159. gFoc_Ctrl.userLim.s_iDCeBrkLim = nv_get_foc_params()->s_iDCeBrkLim;
  160. gFoc_Ctrl.userLim.s_TorqueBrkLim = nv_get_foc_params()->s_TorqueBrkLim;
  161. gFoc_Ctrl.userLim.s_PhaseVoleBrkLim = gFoc_Ctrl.hwLim.s_PhaseVolMax;
  162. }
  163. void PMSM_FOC_RT_LimInit(void) {
  164. gFoc_Ctrl.protLim.s_iDCLim = HW_LIMIT_NONE;
  165. gFoc_Ctrl.protLim.s_TorqueLim = HW_LIMIT_NONE;
  166. eRamp_init_target2(&gFoc_Ctrl.rtLim.rpmLimRamp, gFoc_Ctrl.userLim.s_motRPMLim, CONFIG_LIMIT_RAMP_TIME);
  167. eRamp_init_target2(&gFoc_Ctrl.rtLim.torqueLimRamp, gFoc_Ctrl.userLim.s_torqueLim, CONFIG_LIMIT_RAMP_TIME);
  168. eRamp_init_target2(&gFoc_Ctrl.rtLim.DCCurrLimRamp, gFoc_Ctrl.userLim.s_iDCLim, CONFIG_LIMIT_RAMP_TIME);
  169. }
  170. void PMSM_FOC_CoreInit(void) {
  171. PMSM_FOC_Conf_PID();
  172. memset(&gFoc_Ctrl.in, 0, sizeof(gFoc_Ctrl.in));
  173. memset(&gFoc_Ctrl.out, 0, sizeof(gFoc_Ctrl.out));
  174. gFoc_Ctrl.hwLim.s_iDCMax = CONFIG_MAX_VBUS_CURRENT;
  175. gFoc_Ctrl.hwLim.s_motRPMMax = CONFIG_MAX_MOT_RPM;
  176. gFoc_Ctrl.hwLim.s_PhaseCurrMax = CONFIG_MAX_PHASE_CURR;
  177. gFoc_Ctrl.hwLim.s_PhaseVolMax = CONFIG_MAX_PHASE_VOL;
  178. gFoc_Ctrl.hwLim.s_vDCMax = CONFIG_MAX_DC_VOL;
  179. gFoc_Ctrl.hwLim.s_torqueMax = CONFIG_MAX_MOTOR_TORQUE;
  180. gFoc_Ctrl.hwLim.s_FWDCurrMax = CONFIG_MAX_FW_D_CURR;
  181. if (!g_focinit) {
  182. PMSM_FOC_UserInit();
  183. PMSM_FOC_RT_LimInit();
  184. shark_task_create(PMSM_FOC_Debug_Task, NULL);
  185. g_focinit = true;
  186. //_DEBUG("User Limit:\n");
  187. //_DEBUG("dc %f, rpm %f, torque %f, phase %f, vDCmax %f, vDCmin %f, ebrk %f\n", gFoc_Ctrl.userLim.s_iDCLim, gFoc_Ctrl.userLim.s_motRPMLim, gFoc_Ctrl.userLim.s_torqueLim,
  188. // gFoc_Ctrl.userLim.s_PhaseCurrLim, gFoc_Ctrl.userLim.s_vDCMaxLim, gFoc_Ctrl.userLim.s_vDCMinLim, gFoc_Ctrl.userLim.s_TorqueBrkLim);
  189. //_DEBUG("Hw Limit:\n");
  190. //_DEBUG("dc %f, rpm %f, torque %f, phase %f\n", gFoc_Ctrl.hwLim.s_iDCMax, gFoc_Ctrl.hwLim.s_motRPMMax, gFoc_Ctrl.hwLim.s_torqueMax, gFoc_Ctrl.hwLim.s_PhaseCurrMax);
  191. }
  192. gFoc_Ctrl.params.n_modulation = CONFIG_SVM_MODULATION;//SVM_Modulation;
  193. gFoc_Ctrl.params.n_PhaseFilterCeof = CONFIG_CURR_LP_CEOF;
  194. gFoc_Ctrl.params.n_poles = nv_get_motor_params()->poles;//MOTOR_POLES;
  195. gFoc_Ctrl.params.lq = nv_get_motor_params()->lq;
  196. gFoc_Ctrl.params.ld = nv_get_motor_params()->ld;
  197. gFoc_Ctrl.params.flux = nv_get_motor_params()->flux_linkage;
  198. gFoc_Ctrl.in.s_manualAngle = INVALID_ANGLE;
  199. gFoc_Ctrl.in.s_dqAngle = INVALID_ANGLE;
  200. gFoc_Ctrl.in.b_fwEnable = nv_get_foc_params()->n_FwEnable;
  201. gFoc_Ctrl.in.s_vDC = nv_get_foc_params()->s_maxDCVol;//(CONFIG_RATED_DC_VOL);
  202. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
  203. gFoc_Ctrl.out.f_vdqRation = 0;
  204. eRamp_init_target2(&gFoc_Ctrl.in.cruiseRpmRamp, 0, CONFIG_CRUISE_RAMP_TIME);
  205. FOC_DqRamp_init(&gFoc_Ctrl.idq_ctl[0], 1);
  206. FOC_DqRamp_init(&gFoc_Ctrl.idq_ctl[1], 1);
  207. FOC_DqRamp_init(&gFoc_Ctrl.vdq_ctl[0], (CONFIG_FOC_VDQ_RAMP_TS));
  208. FOC_DqRamp_init(&gFoc_Ctrl.vdq_ctl[1], (CONFIG_FOC_VDQ_RAMP_TS));
  209. PMSM_FOC_Reset_PID();
  210. foc_observer_init();
  211. gFoc_Ctrl.plot_type = Plot_None;
  212. }
  213. /* 通过三相电流重构母线电流,和单电阻采样正好相反,原理一致 */
  214. static __INLINE void PMSM_FOC_Calc_iDC_Fast(void) {
  215. float deadtime = (float)(NS_2_TCLK(PWM_DEAD_TIME_NS + HW_DEAD_TIME_NS))/(float)FOC_PWM_Half_Period;
  216. float duty_pu[3];
  217. duty_pu[0] = (float)gFoc_Ctrl.out.n_Duty[0] / (float)FOC_PWM_Half_Period;
  218. duty_pu[1] = (float)gFoc_Ctrl.out.n_Duty[1] / (float)FOC_PWM_Half_Period;
  219. duty_pu[2] = (float)gFoc_Ctrl.out.n_Duty[2] / (float)FOC_PWM_Half_Period;
  220. float *iABC = gFoc_Ctrl.in.s_iABC;
  221. float iDC;
  222. if ((duty_pu[0] >= duty_pu[1]) && (duty_pu[1] >= duty_pu[2])) {
  223. iDC = iABC[0] * MAX(duty_pu[0] - duty_pu[1] - deadtime, 0) + (iABC[0] + iABC[1]) * MAX(duty_pu[1] - duty_pu[2] - deadtime, 0);
  224. if (iABC[0] < 0) {
  225. iDC = iDC + iABC[0] * deadtime;
  226. }
  227. if (iABC[1] > 0) {
  228. iDC = iDC + iABC[0] * deadtime;
  229. }else {
  230. iDC = iDC + (iABC[0] + iABC[1]) * deadtime;
  231. }
  232. if (iABC[2] > 0) {
  233. iDC = iDC + (iABC[0] + iABC[1]) * deadtime;
  234. }
  235. }else if ((duty_pu[0] >= duty_pu[2]) && (duty_pu[2] >= duty_pu[1])) {
  236. iDC = iABC[0] * MAX(duty_pu[0] - duty_pu[2] - deadtime, 0) + (iABC[0] + iABC[2]) * MAX(duty_pu[2] - duty_pu[1] - deadtime, 0);
  237. if (iABC[0] < 0) {
  238. iDC = iDC + iABC[0] * deadtime;
  239. }
  240. if (iABC[2] > 0) {
  241. iDC = iDC + iABC[0] * deadtime;
  242. }else {
  243. iDC = iDC + (iABC[0] + iABC[2]) * deadtime;
  244. }
  245. if (iABC[1] > 0) {
  246. iDC = iDC + (iABC[0] + iABC[2]) * deadtime;
  247. }
  248. }else if ((duty_pu[1] >= duty_pu[0]) && (duty_pu[0] >= duty_pu[2])) {
  249. iDC = iABC[1] * MAX(duty_pu[1] - duty_pu[0] - deadtime, 0) + (iABC[1] + iABC[0]) * MAX(duty_pu[0] - duty_pu[2] - deadtime, 0);
  250. if (iABC[1] < 0) {
  251. iDC = iDC + iABC[1] * deadtime;
  252. }
  253. if (iABC[0] > 0) {
  254. iDC = iDC + iABC[1] * deadtime;
  255. }else {
  256. iDC = iDC + (iABC[1] + iABC[0]) * deadtime;
  257. }
  258. if (iABC[2] > 0) {
  259. iDC = iDC + (iABC[1] + iABC[0]) * deadtime;
  260. }
  261. }else if ((duty_pu[1] >= duty_pu[2]) && (duty_pu[2] >= duty_pu[0])) {
  262. iDC = iABC[1] * MAX(duty_pu[1] - duty_pu[2] - deadtime, 0) + (iABC[1] + iABC[2]) * MAX(duty_pu[2] - duty_pu[0] - deadtime, 0);
  263. if (iABC[1] < 0) {
  264. iDC = iDC + iABC[1] * deadtime;
  265. }
  266. if (iABC[2] > 0) {
  267. iDC = iDC + iABC[1] * deadtime;
  268. }else {
  269. iDC = iDC + (iABC[1] + iABC[2]) * deadtime;
  270. }
  271. if (iABC[0] > 0) {
  272. iDC = iDC + (iABC[1] + iABC[2]) * deadtime;
  273. }
  274. }else if ((duty_pu[2] >= duty_pu[0]) && (duty_pu[0] >= duty_pu[1])) {
  275. iDC = iABC[2] * MAX(duty_pu[2] - duty_pu[0] - deadtime, 0) + (iABC[2] + iABC[0]) * MAX(duty_pu[0] - duty_pu[1] - deadtime, 0);
  276. if (iABC[2] < 0) {
  277. iDC = iDC + iABC[2] * deadtime;
  278. }
  279. if (iABC[0] > 0) {
  280. iDC = iDC + iABC[2] * deadtime;
  281. }else {
  282. iDC = iDC + (iABC[2] + iABC[0]) * deadtime;
  283. }
  284. if (iABC[1] > 0) {
  285. iDC = iDC + (iABC[2] + iABC[0]) * deadtime;
  286. }
  287. }else { // duty_pu[2] >= duty_pu[1] && duty_pu[1] >= duty_pu[0]
  288. iDC = iABC[2] * MAX(duty_pu[2] - duty_pu[1] - deadtime, 0) + (iABC[2] + iABC[1]) * MAX(duty_pu[1] - duty_pu[0] - deadtime, 0);
  289. if (iABC[2] < 0) {
  290. iDC = iDC + iABC[2] * deadtime;
  291. }
  292. if (iABC[1] > 0) {
  293. iDC = iDC + iABC[2] * deadtime;
  294. }else {
  295. iDC = iDC + (iABC[2] + iABC[1]) * deadtime;
  296. }
  297. if (iABC[0] > 0) {
  298. iDC = iDC + (iABC[2] + iABC[1]) * deadtime;
  299. }
  300. }
  301. LowPass_Filter(gFoc_Ctrl.out.s_CalciDC2, iDC, 0.005f);
  302. }
  303. //#define UPDATE_Lq_By_iq /* Q轴电感 通过Iq电流补偿 */
  304. //#define Volvec_Delay_Comp /* 电压矢量角度补偿 */
  305. static __INLINE float PMSM_FOC_Update_Input(void) {
  306. AB_t iAB;
  307. bool count_down = PWM_Direction_Down();
  308. float *iabc = gFoc_Ctrl.in.s_iABC;
  309. phase_current_get(iabc);
  310. PMSM_FOC_Calc_iDC_Fast();
  311. Clark(iabc[0], iabc[1], iabc[2], &iAB);
  312. foc_observer_update(gFoc_Ctrl.out.s_OutVAB.a * TWO_BY_THREE, gFoc_Ctrl.out.s_OutVAB.b * TWO_BY_THREE, iAB.a, iAB.b);
  313. if (!count_down) {
  314. return false;
  315. }
  316. float enc_angle = motor_encoder_get_angle();
  317. float enc_vel = motor_encoder_get_speed();
  318. if (!foc_observer_diagnostic(enc_angle, enc_vel)){
  319. /* deltect encoder angle error, do something here */
  320. enc_angle = foc_observer_sensorless_angle();
  321. enc_vel = foc_observer_sensorless_speed();
  322. }
  323. if (!gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_manualAngle != INVALID_ANGLE)) {
  324. gFoc_Ctrl.in.s_motAngle = gFoc_Ctrl.in.s_manualAngle;
  325. }else {
  326. gFoc_Ctrl.in.s_motAngle = enc_angle;
  327. }
  328. gFoc_Ctrl.in.s_motVelocity = enc_vel;
  329. LowPass_Filter(gFoc_Ctrl.in.s_motRPMFilted, gFoc_Ctrl.in.s_motVelocity, 0.005f);
  330. gFoc_Ctrl.in.s_motVelDegreePers = gFoc_Ctrl.in.s_motVelocity / 30.0f * PI * gFoc_Ctrl.params.n_poles;
  331. #ifdef CONFIG_DQ_STEP_RESPONSE
  332. gFoc_Ctrl.in.s_motAngle = 0;
  333. #endif
  334. gFoc_Ctrl.in.s_vDC = get_vbus_float();
  335. get_phase_vols(gFoc_Ctrl.in.s_vABC);
  336. SinCos_Lut(gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
  337. Park(&iAB, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_RealIdq);
  338. LowPass_Filter(gFoc_Ctrl.out.s_FilterIdq.d, gFoc_Ctrl.out.s_RealIdq.d, 0.004f);
  339. LowPass_Filter(gFoc_Ctrl.out.s_FilterIdq.q, gFoc_Ctrl.out.s_RealIdq.q, 0.004f);
  340. #ifdef Volvec_Delay_Comp
  341. float next_angle = gFoc_Ctrl.in.s_motAngle + gFoc_Ctrl.in.s_motVelDegreePers / PI * 180.0f * (FOC_CTRL_US - 2e-6f);
  342. rand_angle(next_angle);
  343. SinCos_Lut(next_angle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
  344. #endif
  345. return true;
  346. }
  347. #ifdef CONFIG_DQ_STEP_RESPONSE
  348. float target_d = 0.0f;
  349. float target_q = 0.0f;
  350. #endif
  351. static u32 PMSM_FOC_Debug_Task(void *p) {
  352. if (gFoc_Ctrl.in.b_motEnable) {
  353. #ifdef CONFIG_DQ_STEP_RESPONSE
  354. if (gFoc_Ctrl.plot_type == Plot_D_Step) {
  355. plot_2data16(FtoS16x10(target_d), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d));
  356. }if (gFoc_Ctrl.plot_type == Plot_Q_Step) {
  357. plot_2data16(FtoS16x10(target_q), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q));
  358. }
  359. #else
  360. if (gFoc_Ctrl.plot_type == Plot_D_flow) {
  361. plot_2data16(FtoS16x10(gFoc_Ctrl.idq_ctl[0].s_Cp), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d));
  362. }else if (gFoc_Ctrl.plot_type == Plot_Q_flow) {
  363. plot_2data16(FtoS16x10(gFoc_Ctrl.idq_ctl[1].s_Cp), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q));
  364. }else if (gFoc_Ctrl.plot_type == Plot_DQ_Curr) {
  365. plot_3data16(FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q), FtoS16x10(gFoc_Ctrl.out.s_FilteriDC));
  366. }else if (gFoc_Ctrl.plot_type == Plot_Spd_flow) {
  367. plot_2data16(gFoc_Ctrl.in.s_targetRPM, gFoc_Ctrl.in.s_motVelocity);
  368. }
  369. #endif
  370. }
  371. return 1;
  372. }
  373. static __INLINE float id_feedforward(float eW) {
  374. #ifdef CONFIG_CURRENT_LOOP_DECOUPE
  375. return -(gFoc_Ctrl.params.lq * gFoc_Ctrl.out.s_RealIdq.q * eW);
  376. //gFoc_Ctrl.in.s_targetVdq.d += -(gFoc_Ctrl.params.lq * gFoc_Ctrl.out.s_RealIdq.q * eW);
  377. //gFoc_Ctrl.in.s_targetVdq.d = fclamp(gFoc_Ctrl.in.s_targetVdq.d, gFoc_Ctrl.pi_id.min, gFoc_Ctrl.pi_id.max);
  378. #else
  379. return 0;
  380. #endif
  381. }
  382. static __INLINE float iq_feedforward(float eW) {
  383. #ifdef CONFIG_CURRENT_LOOP_DECOUPE
  384. return (gFoc_Ctrl.params.ld * gFoc_Ctrl.out.s_RealIdq.d + gFoc_Ctrl.params.flux) * eW;
  385. //gFoc_Ctrl.in.s_targetVdq.q += (gFoc_Ctrl.params.ld * gFoc_Ctrl.out.s_RealIdq.d + gFoc_Ctrl.params.flux) * eW;
  386. //gFoc_Ctrl.in.s_targetVdq.q = fclamp(gFoc_Ctrl.in.s_targetVdq.q, gFoc_Ctrl.pi_iq.min, gFoc_Ctrl.pi_iq.max);
  387. #else
  388. return 0;
  389. #endif
  390. }
  391. void PMSM_FOC_Schedule(void) {
  392. gFoc_Ctrl.ctrl_count++;
  393. if (!PMSM_FOC_Update_Input()){
  394. return;
  395. }
  396. if (gFoc_Ctrl.out.n_RunMode != CTRL_MODE_OPEN) {
  397. float max_Vdc = gFoc_Ctrl.in.s_vDC * CONFIG_SVM_MODULATION;
  398. float max_vd = max_Vdc * SQRT3_BY_2;
  399. /* limiter Vd output for PI controller */
  400. gFoc_Ctrl.pi_id.max = max_vd;
  401. gFoc_Ctrl.pi_id.min = -max_vd;
  402. #ifndef CONFIG_DQ_STEP_RESPONSE
  403. float target_d = FOC_Get_DqRamp(&gFoc_Ctrl.idq_ctl[0]);
  404. #endif
  405. float err = target_d - gFoc_Ctrl.out.s_RealIdq.d;
  406. float id_ff = id_feedforward(gFoc_Ctrl.in.s_motVelDegreePers);
  407. gFoc_Ctrl.in.s_targetVdq.d = PI_Controller_Current(&gFoc_Ctrl.pi_id, err, id_ff);
  408. #ifdef UPDATE_Lq_By_iq
  409. /* update kp&ki from lq for iq PI controller */
  410. float lq = motor_get_lq_from_iq((s16)gFoc_Ctrl.out.s_FilterIdq.q);
  411. LowPass_Filter(gFoc_Ctrl.params.lq, lq, 0.01f);
  412. gFoc_Ctrl.pi_iq.kp = ((float)nv_get_foc_params()->n_currentBand * gFoc_Ctrl.params.lq);
  413. gFoc_Ctrl.pi_iq.ki = (nv_get_motor_params()->r/gFoc_Ctrl.params.lq);
  414. #endif
  415. /* limiter Vq output for PI controller */
  416. float max_vq = sqrtf(SQ(max_vd) - SQ(gFoc_Ctrl.in.s_targetVdq.d));
  417. gFoc_Ctrl.pi_iq.max = max_vq;
  418. gFoc_Ctrl.pi_iq.min = -max_vq;
  419. #ifndef CONFIG_DQ_STEP_RESPONSE
  420. float target_q = FOC_Get_DqRamp(&gFoc_Ctrl.idq_ctl[1]);
  421. #endif
  422. err = target_q - gFoc_Ctrl.out.s_RealIdq.q;
  423. float iq_ff = iq_feedforward(gFoc_Ctrl.in.s_motVelDegreePers);
  424. gFoc_Ctrl.in.s_targetVdq.q = PI_Controller_Current(&gFoc_Ctrl.pi_iq, err, iq_ff);
  425. }else {
  426. float max_Vdc = gFoc_Ctrl.in.s_vDC * CONFIG_SVM_MODULATION;
  427. float max_vd = max_Vdc * SQRT3_BY_2;
  428. float vd_ref = FOC_Get_DqRamp(&gFoc_Ctrl.vdq_ctl[0]);
  429. gFoc_Ctrl.in.s_targetVdq.d = fclamp(vd_ref, -max_vd, max_vd);
  430. float max_vq = sqrtf(SQ(max_vd) - SQ(gFoc_Ctrl.in.s_targetVdq.d));
  431. float vq_ref = FOC_Get_DqRamp(&gFoc_Ctrl.vdq_ctl[1]);
  432. gFoc_Ctrl.in.s_targetVdq.q = fclamp(vq_ref, -max_vq, max_vq);
  433. }
  434. #if 0
  435. gFoc_Ctrl.out.f_vdqRation = Circle_Limitation(&gFoc_Ctrl.in.s_targetVdq, gFoc_Ctrl.in.s_vDC, gFoc_Ctrl.params.n_modulation, &gFoc_Ctrl.out.s_OutVdq);
  436. gFoc_Ctrl.out.s_OutVdq.d *= SQRT3_BY_2;
  437. gFoc_Ctrl.out.s_OutVdq.q *= SQRT3_BY_2;
  438. #else
  439. gFoc_Ctrl.out.s_OutVdq.d = gFoc_Ctrl.in.s_targetVdq.d;
  440. gFoc_Ctrl.out.s_OutVdq.q = gFoc_Ctrl.in.s_targetVdq.q;
  441. #endif
  442. RevPark(&gFoc_Ctrl.out.s_OutVdq, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_OutVAB);
  443. SVM_Duty_Fix(&gFoc_Ctrl.out.s_OutVAB, gFoc_Ctrl.in.s_vDC, FOC_PWM_Half_Period, &gFoc_Ctrl.out);
  444. phase_current_point(&gFoc_Ctrl.out);
  445. pwm_update_duty(gFoc_Ctrl.out.n_Duty[0], gFoc_Ctrl.out.n_Duty[1], gFoc_Ctrl.out.n_Duty[2]);
  446. pwm_update_sample(gFoc_Ctrl.out.n_Sample1, gFoc_Ctrl.out.n_Sample2, gFoc_Ctrl.out.n_CPhases);
  447. }
  448. void PMSM_FOC_LogDebug(void) {
  449. sys_debug("DC curr %f --- %f, - %f\n", gFoc_Ctrl.out.s_CalciDC, gFoc_Ctrl.out.s_CalciDC2, gFoc_Ctrl.hwLim.s_iDCMax);
  450. sys_debug("%s\n", gFoc_Ctrl.out.empty_load?"NoLoad Running":"Load Runing");
  451. sys_debug("cruise vel: %d\n", (s16)gFoc_Ctrl.in.s_cruiseRPM);
  452. }
  453. /*called in media task */
  454. u8 PMSM_FOC_CtrlMode(void) {
  455. u8 preMode = gFoc_Ctrl.out.n_RunMode;
  456. if (!gFoc_Ctrl.in.b_motEnable) {
  457. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
  458. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_OPEN) {
  459. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
  460. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_SPD || gFoc_Ctrl.in.b_cruiseEna){
  461. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_SPD;
  462. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_CURRENT) {
  463. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_CURRENT;
  464. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_EBRAKE) {
  465. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_EBRAKE;
  466. }else {
  467. if (!gFoc_Ctrl.in.b_cruiseEna) {
  468. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_TRQ;
  469. }
  470. }
  471. if (preMode != gFoc_Ctrl.out.n_RunMode) {
  472. if ((preMode == CTRL_MODE_SPD) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ)) {
  473. #ifdef CONFIG_SPEED_LADRC
  474. //ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
  475. ladrc_copy(&gFoc_Ctrl.vel_lim_adrc, &gFoc_Ctrl.vel_adrc);
  476. #else
  477. PI_Controller_Reset(&gFoc_Ctrl.pi_torque, gFoc_Ctrl.in.s_targetTorque);
  478. #endif
  479. }else if ((preMode == CTRL_MODE_TRQ) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD)) {
  480. #ifdef CONFIG_SPEED_LADRC
  481. ladrc_copy(&gFoc_Ctrl.vel_adrc, &gFoc_Ctrl.vel_lim_adrc);
  482. #else
  483. float target_troque = gFoc_Ctrl.in.s_targetTorque;
  484. if (gFoc_Ctrl.pi_id.is_sat || gFoc_Ctrl.pi_iq.is_sat) {
  485. target_troque = PMSM_FOC_Get_Real_dqVector();
  486. }
  487. PI_Controller_Reset(&gFoc_Ctrl.pi_speed, target_troque);
  488. #endif
  489. }else if ((preMode == CTRL_MODE_CURRENT) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ)) {
  490. #ifdef CONFIG_SPEED_LADRC
  491. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
  492. #else
  493. PI_Controller_Reset(&gFoc_Ctrl.pi_torque, gFoc_Ctrl.in.s_targetTorque);
  494. #endif
  495. }else if ((preMode != gFoc_Ctrl.out.n_RunMode) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
  496. eCtrl_reset_Torque(gFoc_Ctrl.in.s_targetTorque);
  497. eCtrl_set_TgtTorque(motor_get_ebreak_toruqe(gFoc_Ctrl.in.s_motVelocity));
  498. }else if ((preMode == CTRL_MODE_EBRAKE) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD)) {
  499. #ifdef CONFIG_SPEED_LADRC
  500. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, F_get_air());
  501. #else
  502. PI_Controller_Reset(&gFoc_Ctrl.pi_torque, F_get_air());
  503. #endif
  504. }
  505. }
  506. return gFoc_Ctrl.out.n_RunMode;
  507. }
  508. #define RAMPE_1 CONFIG_RAMP_FIRST_TARGET
  509. static void crosszero_step_towards(float *value, float target) {
  510. static float no_cro_step = 2.0f;
  511. float v_now = *value;
  512. bool cross_zero = false;
  513. float high_ramp_torque = CONFIG_RAMP_SECOND_TARGET;
  514. if (target > 0) {
  515. if (v_now < -RAMPE_1) {
  516. step_towards(value, -RAMPE_1, 2.0f);
  517. cross_zero = true;
  518. }else if (v_now >= -RAMPE_1 && v_now <= high_ramp_torque) {
  519. step_towards(value, target, 0.03f);
  520. cross_zero = true;
  521. }
  522. }else if (target == 0) {
  523. if (v_now > high_ramp_torque) {
  524. step_towards(value, high_ramp_torque, 1.0f);
  525. cross_zero = true;
  526. }else if (v_now >= RAMPE_1 && v_now <= high_ramp_torque) {
  527. step_towards(value, target, 0.03f);
  528. cross_zero = true;
  529. }
  530. }else {
  531. if (v_now > high_ramp_torque) {
  532. step_towards(value, high_ramp_torque, 20.0f);
  533. cross_zero = true;
  534. }else if (v_now >= -RAMPE_1 && v_now <= high_ramp_torque) {
  535. step_towards(value, target, 0.03f);
  536. cross_zero = true;
  537. }
  538. }
  539. if (!cross_zero) {
  540. step_towards(&no_cro_step, 3.0f, 0.1f);
  541. step_towards(value, target, no_cro_step);
  542. }else {
  543. no_cro_step = 0.5f;
  544. }
  545. }
  546. /* MPTA, 弱磁, 功率限制,主要是分配DQ轴电流 */
  547. #define CHANGE_MAX_CNT 3
  548. static __INLINE void PMSM_FOC_VelCtrl_Decide(void) {
  549. static int change_cnt = 0;
  550. static bool change_done = false;
  551. static u32 change_time = 0xFFFFFFFF;
  552. float f_te = F_get_Te();
  553. float f_accl = F_get_accl();
  554. if (mc_is_epm()) {
  555. change_cnt = 0;
  556. change_time = 0xFFFFFFFF;
  557. change_done = false;
  558. gFoc_Ctrl.out.empty_load = false;
  559. return;
  560. }
  561. if (gFoc_Ctrl.in.s_motVelocity == 0.0f || gFoc_Ctrl.out.n_RunMode == CTRL_MODE_OPEN) {
  562. change_cnt = 0;
  563. change_time = 0xFFFFFFFF;
  564. change_done = false;
  565. gFoc_Ctrl.out.empty_load = false;
  566. return;
  567. }
  568. if (f_te <= 0.0f) {
  569. change_cnt = 0;
  570. change_time = 0xFFFFFFFF;
  571. return;
  572. }
  573. f_te = ABS(f_te);
  574. if (change_done) {
  575. /* 误判空转,发现电机给定的N大于空气阻力,说明不是空转 */
  576. if (gFoc_Ctrl.out.empty_load) {
  577. float f_air = F_get_air();
  578. if ((f_accl > 1.0f) && (f_te >= (f_air + f_accl))) {
  579. change_cnt ++;
  580. }else {
  581. change_cnt = 0;
  582. }
  583. if (change_cnt >= 500) {
  584. gFoc_Ctrl.out.empty_load = false;
  585. #ifdef CONFIG_SPEED_LADRC
  586. PMSM_FOC_Change_TrqLoop_Params(nv_get_foc_params()->f_adrc_vel_lim_Wcv, nv_get_foc_params()->f_adrc_vel_lim_B0);
  587. PMSM_FOC_Change_VelLoop_Params(nv_get_foc_params()->f_adrc_vel_Wcv, nv_get_foc_params()->f_adrc_vel_B0);
  588. #endif
  589. }
  590. }
  591. return;
  592. }
  593. if (change_time == 0xFFFFFFFF) {
  594. change_time = get_tick_ms();
  595. }else { //起步3s内检测是否空转
  596. if (get_delta_ms(change_time) > 3000) {
  597. return;
  598. }
  599. }
  600. if ((f_accl > 200.0f) && (f_accl/f_te > 3.0f )) {
  601. change_cnt++;
  602. }else if ((F_get_MotAccl() >= 10.0f) && (f_accl/f_te > 1.2f )) {
  603. change_cnt = CHANGE_MAX_CNT;
  604. }
  605. else {
  606. if ((f_te > 50) && (f_accl > 0) && (f_te > f_accl)) {
  607. change_cnt --;
  608. }else {
  609. change_cnt = 0;
  610. }
  611. }
  612. if (!change_done && (change_cnt >= CHANGE_MAX_CNT)) {
  613. change_done = true;
  614. change_cnt = 0;
  615. gFoc_Ctrl.out.empty_load = change_done;
  616. #ifdef CONFIG_SPEED_LADRC
  617. PMSM_FOC_Change_TrqLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
  618. PMSM_FOC_Change_VelLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
  619. #endif
  620. }else if (!change_done && (change_cnt <= -200)) {
  621. change_done = true;
  622. change_cnt = 0;
  623. gFoc_Ctrl.out.empty_load = false;
  624. }
  625. }
  626. static __INLINE float PMSM_FOC_Limit_iDC(float maxTrq) {
  627. #if 1
  628. gFoc_Ctrl.pi_power.max = maxTrq;
  629. float errRef = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.DCCurrLimRamp) - (gFoc_Ctrl.out.s_FilteriDC);
  630. return PI_Controller_Run(&gFoc_Ctrl.pi_power, errRef);
  631. #else
  632. return maxTrq;
  633. #endif
  634. }
  635. static __INLINE float PMSM_FOC_Limit_Speed(float maxTrq) {
  636. #ifdef CONFIG_SPEED_LADRC
  637. float lim = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp);
  638. ladrc_set_range(&gFoc_Ctrl.vel_lim_adrc, 0, maxTrq);
  639. return ladrc_run(&gFoc_Ctrl.vel_lim_adrc, lim, gFoc_Ctrl.in.s_motVelocity);
  640. #else
  641. #if 1
  642. gFoc_Ctrl.pi_torque.max = maxTrq;
  643. gFoc_Ctrl.pi_torque.min = 0;
  644. float err = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp) - gFoc_Ctrl.in.s_motVelocity;
  645. return PI_Controller_RunLimit(&gFoc_Ctrl.pi_torque, err);
  646. #else
  647. return maxTrq;
  648. #endif
  649. #endif
  650. }
  651. static __INLINE void PMSM_FOC_idq_Assign(void) {
  652. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
  653. if (gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_dqAngle != INVALID_ANGLE)) {
  654. float s, c;
  655. normal_sincosf(degree_2_pi(gFoc_Ctrl.in.s_dqAngle + 90.0f), &s, &c);
  656. gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.in.s_targetCurrent * c;
  657. if (gFoc_Ctrl.in.s_targetIdq.d > gFoc_Ctrl.hwLim.s_FWDCurrMax) {
  658. gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.hwLim.s_FWDCurrMax;
  659. }else if (gFoc_Ctrl.in.s_targetIdq.d < -gFoc_Ctrl.hwLim.s_FWDCurrMax) {
  660. gFoc_Ctrl.in.s_targetIdq.d = -gFoc_Ctrl.hwLim.s_FWDCurrMax;
  661. }
  662. gFoc_Ctrl.in.s_targetIdq.q = sqrtf(SQ(gFoc_Ctrl.in.s_targetCurrent) - SQ(gFoc_Ctrl.in.s_targetIdq.d));
  663. if (s < 0) {
  664. gFoc_Ctrl.in.s_targetIdq.q = -gFoc_Ctrl.in.s_targetIdq.q;
  665. }
  666. }else {
  667. gFoc_Ctrl.in.s_targetIdq.d = 0;
  668. gFoc_Ctrl.in.s_targetIdq.q = gFoc_Ctrl.in.s_targetCurrent;
  669. }
  670. }else if ((gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) || (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) ||
  671. (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
  672. motor_mpta_fw_lookup(gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque, &gFoc_Ctrl.in.s_targetIdq);
  673. }
  674. u32 mask = cpu_enter_critical();
  675. FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[0], gFoc_Ctrl.in.s_targetIdq.d);
  676. FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[1], gFoc_Ctrl.in.s_targetIdq.q);
  677. cpu_exit_critical(mask);
  678. }
  679. /*called in media task */
  680. void PMSM_FOC_idqCalc(void) {
  681. if (gFoc_Ctrl.in.b_AutoHold) {
  682. gFoc_Ctrl.pi_lock.max = CONFIG_MAX_LOCK_TORQUE;
  683. gFoc_Ctrl.pi_lock.min = -CONFIG_MAX_LOCK_TORQUE;
  684. float vel_count = motor_encoder_get_vel_count();
  685. float errRef = 0 - vel_count;
  686. gFoc_Ctrl.in.s_targetTorque = PI_Controller_Run(&gFoc_Ctrl.pi_lock ,errRef);
  687. PMSM_FOC_idq_Assign();
  688. return;
  689. }
  690. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
  691. gFoc_Ctrl.in.s_targetCurrent = eCtrl_get_RefCurrent();
  692. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE) {
  693. float maxTrq = eCtrl_get_RefTorque();
  694. if (eCtrl_get_FinalTorque() < 0.0001f && gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  695. maxTrq = 0;
  696. }
  697. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  698. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
  699. float refTorque = min(eCtrl_get_RefTorque(), eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
  700. float maxTrq = PMSM_FOC_Limit_Speed(refTorque);
  701. maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
  702. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  703. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD){
  704. float maxSpeed = eCtrl_get_FinalSpeed();
  705. float refSpeed = eCtrl_get_RefSpeed();
  706. if (gFoc_Ctrl.in.b_cruiseEna) {
  707. maxSpeed = eRamp_get_target(&gFoc_Ctrl.in.cruiseRpmRamp);
  708. refSpeed = eRamp_get_intepolation(&gFoc_Ctrl.in.cruiseRpmRamp);//gFoc_Ctrl.in.s_cruiseRPM;
  709. }
  710. #ifdef CONFIG_SPEED_LADRC
  711. if (maxSpeed >= 0) {
  712. ladrc_set_range(&gFoc_Ctrl.vel_adrc, -CONFIG_MAX_NEG_TORQUE, eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
  713. }else if (maxSpeed < 0) {
  714. ladrc_set_range(&gFoc_Ctrl.vel_adrc, -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp), CONFIG_MAX_NEG_TORQUE);
  715. }
  716. if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  717. ladrc_set_range(&gFoc_Ctrl.vel_adrc, 0, 0);
  718. }
  719. gFoc_Ctrl.in.s_targetRPM = refSpeed;
  720. float maxTrq = ladrc_run(&gFoc_Ctrl.vel_adrc, refSpeed, gFoc_Ctrl.in.s_motVelocity);
  721. #else
  722. if (maxSpeed >= 0) {
  723. gFoc_Ctrl.pi_speed.max = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);//gFoc_Ctrl.userLim.s_PhaseCurrLim;
  724. gFoc_Ctrl.pi_speed.min = -CONFIG_MAX_NEG_TORQUE;
  725. }else if (maxSpeed < 0) {
  726. gFoc_Ctrl.pi_speed.min = -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);//gFoc_Ctrl.userLim.s_PhaseCurrLim;
  727. gFoc_Ctrl.pi_speed.max = CONFIG_MAX_NEG_TORQUE;
  728. }
  729. if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  730. gFoc_Ctrl.pi_speed.max = 0;
  731. gFoc_Ctrl.pi_speed.min = 0; //防止倒转
  732. }
  733. gFoc_Ctrl.in.s_targetRPM = refSpeed;
  734. float errRef = refSpeed - gFoc_Ctrl.in.s_motVelocity;
  735. float maxTrq = PI_Controller_Run(&gFoc_Ctrl.pi_speed, errRef);
  736. #endif
  737. maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
  738. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  739. }
  740. PMSM_FOC_idq_Assign();
  741. }
  742. u8 PMSM_FOC_RunTime_Limit(void) {
  743. u8 changed = FOC_LIM_NO_CHANGE;
  744. float dc_lim = (float)vbus_under_vol_limit();
  745. float torque_lim = (float)min(mos_temp_high_limit(), motor_temp_high_limit());
  746. if (gFoc_Ctrl.protLim.s_iDCLim != dc_lim || gFoc_Ctrl.protLim.s_TorqueLim != torque_lim) {
  747. if ((dc_lim > gFoc_Ctrl.protLim.s_iDCLim) || (torque_lim > gFoc_Ctrl.protLim.s_TorqueLim)) {
  748. changed = FOC_LIM_CHANGE_H;
  749. }else {
  750. changed = FOC_LIM_CHANGE_L;
  751. }
  752. gFoc_Ctrl.protLim.s_iDCLim = dc_lim;
  753. gFoc_Ctrl.protLim.s_TorqueLim = torque_lim;
  754. }
  755. return changed;
  756. }
  757. bool PMSM_FOC_iDC_is_Limited(void) {
  758. return (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE);
  759. }
  760. bool PMSM_FOC_Torque_is_Limited(void) {
  761. return (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE);
  762. }
  763. void PMSM_FOC_Slow_Task(void) {
  764. eRamp_running(&gFoc_Ctrl.rtLim.torqueLimRamp);
  765. eRamp_running(&gFoc_Ctrl.rtLim.DCCurrLimRamp);
  766. eRamp_running(&gFoc_Ctrl.rtLim.rpmLimRamp);
  767. eRamp_running(&gFoc_Ctrl.in.cruiseRpmRamp);
  768. PMSM_FOC_VelCtrl_Decide();
  769. PMSM_FOC_idqCalc();
  770. }
  771. void PMSM_FOC_Change_VelLoop_Params(float wcv, float b0) {
  772. ladrc_change_b0(&gFoc_Ctrl.vel_adrc, b0);
  773. ladrc_change_K(&gFoc_Ctrl.vel_adrc, wcv);
  774. }
  775. void PMSM_FOC_Change_TrqLoop_Params(float wcv, float b0) {
  776. ladrc_change_b0(&gFoc_Ctrl.vel_lim_adrc, b0);
  777. ladrc_change_K(&gFoc_Ctrl.vel_lim_adrc, wcv);
  778. }
  779. float PMSM_FOC_Get_Real_dqVector(void) {
  780. if (gFoc_Ctrl.out.s_RealCurrentFiltered == 0) {
  781. gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
  782. }
  783. return gFoc_Ctrl.out.s_RealCurrentFiltered;
  784. }
  785. PMSM_FOC_Ctrl *PMSM_FOC_Get(void) {
  786. return &gFoc_Ctrl;
  787. }
  788. void PMSM_FOC_Start(u8 nCtrlMode) {
  789. if (gFoc_Ctrl.in.b_motEnable) {
  790. return;
  791. }
  792. PMSM_FOC_CoreInit();
  793. eCtrl_Reset();
  794. gFoc_Ctrl.in.n_ctlMode = nCtrlMode;
  795. gFoc_Ctrl.in.b_motEnable = true;
  796. }
  797. void PMSM_FOC_Stop(void) {
  798. if (!gFoc_Ctrl.in.b_motEnable) {
  799. return;
  800. }
  801. PMSM_FOC_CoreInit();
  802. gFoc_Ctrl.in.b_motEnable = false;
  803. }
  804. bool PMSM_FOC_Is_Start(void) {
  805. return gFoc_Ctrl.in.b_motEnable;
  806. }
  807. void PMSM_FOC_DCCurrLimit(float ibusLimit) {
  808. if (ibusLimit > gFoc_Ctrl.hwLim.s_iDCMax) {
  809. ibusLimit = gFoc_Ctrl.hwLim.s_iDCMax;
  810. }
  811. if (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE) {
  812. ibusLimit = min(ibusLimit, gFoc_Ctrl.protLim.s_iDCLim);
  813. }
  814. gFoc_Ctrl.userLim.s_iDCLim = ibusLimit;
  815. if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
  816. eRamp_reset_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit);
  817. }else {
  818. eRamp_set_step_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit, CONFIG_eCTRL_STEP_TS);
  819. }
  820. }
  821. float PMSM_FOC_GetDCCurrLimit(void) {
  822. return gFoc_Ctrl.userLim.s_iDCLim;
  823. }
  824. void PMSM_FOC_SpeedRampLimit(float speedLimit, float speed) {
  825. if (speedLimit > gFoc_Ctrl.hwLim.s_motRPMMax) {
  826. speedLimit = gFoc_Ctrl.hwLim.s_motRPMMax;
  827. }
  828. gFoc_Ctrl.userLim.s_motRPMLim = (speedLimit);
  829. if (ABS(speed) <= CONFIG_ZERO_SPEED_RPM) {
  830. eRamp_reset_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit);
  831. }else {
  832. eRamp_set_step_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS);
  833. }
  834. }
  835. void PMSM_FOC_SpeedLimit(float speedLimit) {
  836. PMSM_FOC_SpeedRampLimit(speedLimit, gFoc_Ctrl.in.s_motVelocity);
  837. }
  838. void PMSM_FOC_SpeedDirectLimit(float limit) {
  839. PMSM_FOC_SpeedRampLimit(limit, 0);
  840. }
  841. float PMSM_FOC_GetSpeedLimit(void) {
  842. return gFoc_Ctrl.userLim.s_motRPMLim;
  843. }
  844. void PMSM_FOC_TorqueLimit(float torqueLimit) {
  845. if (torqueLimit > gFoc_Ctrl.hwLim.s_torqueMax) {
  846. torqueLimit = gFoc_Ctrl.hwLim.s_torqueMax;
  847. }
  848. if (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE) {
  849. torqueLimit = min(torqueLimit, gFoc_Ctrl.protLim.s_TorqueLim);
  850. }
  851. gFoc_Ctrl.userLim.s_torqueLim = torqueLimit;
  852. if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
  853. eRamp_reset_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit);
  854. }else {
  855. eRamp_set_step_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit, CONFIG_eCTRL_STEP_TS);
  856. }
  857. }
  858. float PMSM_FOC_GetTorqueLimit(void) {
  859. return gFoc_Ctrl.userLim.s_torqueLim;
  860. }
  861. void PMSM_FOC_SetEbrkTorque(float phase_curr, float dc_curr) {
  862. gFoc_Ctrl.userLim.s_TorqueBrkLim = fclamp(phase_curr, 0, nv_get_foc_params()->s_TorqueBrkLim);
  863. gFoc_Ctrl.userLim.s_iDCeBrkLim = fclamp(dc_curr, 0, nv_get_foc_params()->s_iDCeBrkLim);
  864. }
  865. float PMSM_FOC_GetEbrkTorque(void) {
  866. return gFoc_Ctrl.userLim.s_TorqueBrkLim;
  867. }
  868. float PMSM_FOC_GetVbusVoltage(void) {
  869. return gFoc_Ctrl.in.s_vDC;
  870. }
  871. float PMSM_FOC_GetVbusCurrent(void) {
  872. return gFoc_Ctrl.out.s_FilteriDC;
  873. }
  874. DQ_t* PMSM_FOC_GetDQCurrent(void) {
  875. return &gFoc_Ctrl.out.s_RealIdq;
  876. }
  877. bool PMSM_FOC_SetCtrlMode(u8 mode) {
  878. if (mode > CTRL_MODE_EBRAKE) {
  879. PMSM_FOC_SetErrCode(FOC_Param_Err);
  880. return false;
  881. }
  882. gFoc_Ctrl.in.n_ctlMode = mode;
  883. return true;
  884. }
  885. u8 PMSM_FOC_GetCtrlMode(void) {
  886. return gFoc_Ctrl.in.n_ctlMode;
  887. }
  888. void PMSM_FOC_PhaseCurrLim(float lim) {
  889. if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
  890. lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
  891. }
  892. gFoc_Ctrl.userLim.s_PhaseCurrLim = lim;
  893. }
  894. void PMSM_FOC_RT_PhaseCurrLim(float lim) {
  895. if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
  896. lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
  897. }
  898. eRamp_init_target2(&gFoc_Ctrl.rtLim.torqueLimRamp, lim, CONFIG_LIMIT_RAMP_TIME);
  899. }
  900. float PMSM_FOC_GetPhaseCurrLim(void) {
  901. return gFoc_Ctrl.userLim.s_PhaseCurrLim;
  902. }
  903. void PMSM_FOC_SetOpenVdq(float vd, float vq) {
  904. FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[0], vd);
  905. FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[1], vq);
  906. }
  907. bool PMSM_FOC_EnableCruise(bool enable) {
  908. if (enable != gFoc_Ctrl.in.b_cruiseEna) {
  909. float motSpd = PMSM_FOC_GetSpeed();
  910. if (enable && (motSpd < CONFIG_MIN_CRUISE_RPM)) { //
  911. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  912. return false;
  913. }
  914. eRamp_reset_target(&gFoc_Ctrl.in.cruiseRpmRamp, motSpd);
  915. gFoc_Ctrl.in.s_cruiseRPM = motSpd;
  916. gFoc_Ctrl.in.b_cruiseEna = enable;
  917. }
  918. return true;
  919. }
  920. bool PMSM_FOC_PauseCruise(void) {
  921. gFoc_Ctrl.in.b_cruiseEna = false;
  922. return true;
  923. }
  924. bool PMSM_FOC_ResumeCruise(void) {
  925. gFoc_Ctrl.in.b_cruiseEna = true;
  926. eRamp_init_target2(&gFoc_Ctrl.in.cruiseRpmRamp, PMSM_FOC_GetSpeed(), CONFIG_CRUISE_RAMP_TIME);
  927. eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
  928. return true;
  929. }
  930. bool PMSM_FOC_Is_CruiseEnabled(void) {
  931. return (gFoc_Ctrl.in.b_cruiseEna && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD));
  932. }
  933. bool PMSM_FOC_Set_Speed(float rpm) {
  934. if (gFoc_Ctrl.in.b_cruiseEna) {
  935. return false;
  936. }
  937. eCtrl_set_TgtSpeed(min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm));
  938. return true;
  939. }
  940. bool PMSM_FOC_Set_Current(float is) {
  941. if (is > gFoc_Ctrl.userLim.s_PhaseCurrLim) {
  942. is = gFoc_Ctrl.userLim.s_PhaseCurrLim;
  943. }else if (is < -gFoc_Ctrl.userLim.s_PhaseCurrLim) {
  944. is = -gFoc_Ctrl.userLim.s_PhaseCurrLim;
  945. }
  946. eCtrl_set_TgtCurrent(is);
  947. return true;
  948. }
  949. bool PMSM_FOC_Set_Torque(float trq) {
  950. if (trq > gFoc_Ctrl.userLim.s_torqueLim) {
  951. trq = gFoc_Ctrl.userLim.s_torqueLim;
  952. }else if (trq < -gFoc_Ctrl.userLim.s_torqueLim) {
  953. trq = -gFoc_Ctrl.userLim.s_torqueLim;
  954. }
  955. eCtrl_set_TgtTorque(trq);
  956. return true;
  957. }
  958. void PMSM_FOC_Reset_Torque(void) {
  959. float real_trq = PMSM_FOC_Get_Real_dqVector();
  960. eCtrl_reset_Torque(real_trq);
  961. }
  962. bool PMSM_FOC_Set_CruiseSpeed(float rpm) {
  963. if (PMSM_FOC_Is_CruiseEnabled()) {
  964. if (rpm < CONFIG_MIN_CRUISE_RPM + 20) {
  965. rpm = CONFIG_MIN_CRUISE_RPM + 20;
  966. }
  967. gFoc_Ctrl.in.s_cruiseRPM = min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm);
  968. eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
  969. return true;
  970. }
  971. PMSM_FOC_SetErrCode(FOC_NotCruiseMode);
  972. return false;
  973. }
  974. void PMSM_FOC_MTPA_Calibrate(bool enable) {
  975. if (enable) {
  976. gFoc_Ctrl.in.b_MTPA_calibrate = true;
  977. gFoc_Ctrl.in.s_dqAngle = 0;
  978. }else {
  979. gFoc_Ctrl.in.s_dqAngle = INVALID_ANGLE;
  980. gFoc_Ctrl.in.b_MTPA_calibrate = false;
  981. }
  982. }
  983. void PMSM_FOC_Set_MotAngle(float angle) {
  984. gFoc_Ctrl.in.s_manualAngle = (angle);
  985. }
  986. void PMSM_FOC_Set_Dq_Angle(float angle) {
  987. gFoc_Ctrl.in.s_dqAngle = (angle);
  988. }
  989. void PMSM_FOC_Get_TgtIDQ(DQ_t * dq) {
  990. dq->d = gFoc_Ctrl.in.s_targetIdq.d;
  991. dq->q = gFoc_Ctrl.in.s_targetIdq.q;
  992. }
  993. float PMSM_FOC_GetSpeed(void) {
  994. return gFoc_Ctrl.in.s_motVelocity;
  995. }
  996. void PMSM_FOC_AutoHold(bool lock) {
  997. if (gFoc_Ctrl.in.b_AutoHold != lock) {
  998. motor_encoder_lock_pos(lock);
  999. PI_Controller_Reset(&gFoc_Ctrl.pi_lock, 0);
  1000. if (!lock) {
  1001. float hold_torque = gFoc_Ctrl.in.s_targetTorque * 1.1f;
  1002. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
  1003. #ifdef CONFIG_SPEED_LADRC
  1004. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, 0, hold_torque);
  1005. #else
  1006. PI_Controller_Reset(&gFoc_Ctrl.pi_torque, hold_torque);
  1007. #endif
  1008. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) {
  1009. #ifdef CONFIG_SPEED_LADRC
  1010. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, hold_torque);
  1011. #else
  1012. PI_Controller_Reset(&gFoc_Ctrl.pi_speed, hold_torque);
  1013. #endif
  1014. }
  1015. eCtrl_reset_Torque(hold_torque);
  1016. gFoc_Ctrl.out.f_autohold_trq = hold_torque;
  1017. }else {
  1018. gFoc_Ctrl.out.f_autohold_trq = 0;
  1019. }
  1020. gFoc_Ctrl.in.b_AutoHold = lock;
  1021. }
  1022. }
  1023. bool PMSM_FOC_AutoHoldding(void) {
  1024. return gFoc_Ctrl.in.b_AutoHold;
  1025. }
  1026. static PI_Controller *_pid(u8 id) {
  1027. PI_Controller *pi = NULL;
  1028. if (id == PID_D_id) {
  1029. pi = &gFoc_Ctrl.pi_id;
  1030. }else if (id == PID_Q_id) {
  1031. pi = &gFoc_Ctrl.pi_iq;
  1032. }else if (id == PID_TRQ_id) {
  1033. #ifndef CONFIG_SPEED_LADRC
  1034. pi = &gFoc_Ctrl.pi_torque;
  1035. #endif
  1036. }else if (id == PID_Spd_id) {
  1037. #ifndef CONFIG_SPEED_LADRC
  1038. pi = &gFoc_Ctrl.pi_speed;
  1039. #endif
  1040. }
  1041. return pi;
  1042. }
  1043. void PMSM_FOC_SetPid(u8 id, float kp, float ki, float kd) {
  1044. if (id > PID_Max_id) {
  1045. return;
  1046. }
  1047. PI_Controller *pi = _pid(id);
  1048. if (pi != NULL) {
  1049. pi->kp = kp;
  1050. pi->ki = ki;
  1051. pi->kd = kd;
  1052. }
  1053. }
  1054. void PMSM_FOC_GetPid(u8 id, float *kp, float *ki, float *kd) {
  1055. if (id > PID_Max_id) {
  1056. return;
  1057. }
  1058. PI_Controller *pi = _pid(id);
  1059. if (pi != NULL) {
  1060. *kp = pi->kp;
  1061. *ki = pi->ki;
  1062. *kd = pi->kd;
  1063. }
  1064. }
  1065. void PMSM_FOC_SetErrCode(u8 error) {
  1066. if (gFoc_Ctrl.out.n_Error != error) {
  1067. gFoc_Ctrl.out.n_Error = error;
  1068. }
  1069. }
  1070. u8 PMSM_FOC_GetErrCode(void) {
  1071. return gFoc_Ctrl.out.n_Error;
  1072. }
  1073. void PMSM_FOC_Set_PlotType(Plot_t t) {
  1074. gFoc_Ctrl.plot_type = t;
  1075. }
  1076. //获取母线电流和实际输出电流矢量大小
  1077. void PMSM_FOC_Calc_Current(void) {
  1078. float vd = gFoc_Ctrl.out.s_OutVdq.d;
  1079. float vq = gFoc_Ctrl.out.s_OutVdq.q;
  1080. float id = gFoc_Ctrl.out.s_FilterIdq.d;
  1081. float iq = gFoc_Ctrl.out.s_FilterIdq.q;
  1082. /*
  1083. 根据公式(等幅值变换,功率不等):
  1084. iDC x vDC = 2/3(iq x vq + id x vd);
  1085. */
  1086. float m_pow = (vd * id + vq * iq); //s32q10
  1087. float raw_idc = m_pow / get_vbus_float();// * 1.5f * 0.66f; //s16q5
  1088. LowPass_Filter(gFoc_Ctrl.out.s_CalciDC, raw_idc, 0.1f);
  1089. raw_idc = get_vbus_current();
  1090. LowPass_Filter(gFoc_Ctrl.out.s_FilteriDC, raw_idc, 0.05f);
  1091. gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
  1092. }
  1093. void PMSM_FOC_Brake(bool brake) {
  1094. gFoc_Ctrl.in.b_eBrake = brake;
  1095. if (gFoc_Ctrl.in.b_eBrake & gFoc_Ctrl.in.b_cruiseEna) {
  1096. gFoc_Ctrl.in.b_cruiseEna = false;
  1097. }
  1098. eCtrl_brake_signal(brake);
  1099. }