PMSM_FOC_Core.c 36 KB

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