PMSM_FOC_Core.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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. if (change_done) {
  574. /* 误判空转,发现电机给定的N大于空气阻力,说明不是空转 */
  575. if (gFoc_Ctrl.out.empty_load) {
  576. float f_air = F_get_air();
  577. if ((f_accl > 1.0f) && (f_te >= (f_air + f_accl))) {
  578. change_cnt ++;
  579. }else {
  580. change_cnt = 0;
  581. }
  582. if (change_cnt >= 500) {
  583. gFoc_Ctrl.out.empty_load = false;
  584. #ifdef CONFIG_SPEED_LADRC
  585. PMSM_FOC_Change_TrqLoop_Params(nv_get_foc_params()->f_adrc_vel_lim_Wcv, nv_get_foc_params()->f_adrc_vel_lim_B0);
  586. PMSM_FOC_Change_VelLoop_Params(nv_get_foc_params()->f_adrc_vel_Wcv, nv_get_foc_params()->f_adrc_vel_B0);
  587. #endif
  588. }
  589. }
  590. return;
  591. }
  592. if (change_time == 0xFFFFFFFF) {
  593. change_time = get_tick_ms();
  594. }else { //起步3s内检测是否空转
  595. if (get_delta_ms(change_time) > 3000) {
  596. return;
  597. }
  598. }
  599. if ((f_accl > 200.0f) && (f_accl/f_te > 3.0f )) {
  600. change_cnt++;
  601. }else if ((F_get_MotAccl() >= 10.0f) && (f_accl/f_te > 1.2f )) {
  602. change_cnt = CHANGE_MAX_CNT;
  603. }
  604. else {
  605. if ((f_te > 50) && (f_accl > 0) && (f_te > f_accl)) {
  606. change_cnt --;
  607. }else {
  608. change_cnt = 0;
  609. }
  610. }
  611. if (!change_done && (change_cnt >= CHANGE_MAX_CNT)) {
  612. change_done = true;
  613. change_cnt = 0;
  614. gFoc_Ctrl.out.empty_load = change_done;
  615. #ifdef CONFIG_SPEED_LADRC
  616. PMSM_FOC_Change_TrqLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
  617. PMSM_FOC_Change_VelLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
  618. #endif
  619. }else if (!change_done && (change_cnt <= -200)) {
  620. change_done = true;
  621. change_cnt = 0;
  622. gFoc_Ctrl.out.empty_load = false;
  623. }
  624. }
  625. static __INLINE float PMSM_FOC_Limit_iDC(float maxTrq) {
  626. #if 1
  627. gFoc_Ctrl.pi_power.max = maxTrq;
  628. float errRef = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.DCCurrLimRamp) - (gFoc_Ctrl.out.s_FilteriDC);
  629. return PI_Controller_Run(&gFoc_Ctrl.pi_power, errRef);
  630. #else
  631. return maxTrq;
  632. #endif
  633. }
  634. static __INLINE float PMSM_FOC_Limit_Speed(float maxTrq) {
  635. #ifdef CONFIG_SPEED_LADRC
  636. float lim = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp);
  637. ladrc_set_range(&gFoc_Ctrl.vel_lim_adrc, 0, maxTrq);
  638. return ladrc_run(&gFoc_Ctrl.vel_lim_adrc, lim, gFoc_Ctrl.in.s_motVelocity);
  639. #else
  640. #if 1
  641. gFoc_Ctrl.pi_torque.max = maxTrq;
  642. gFoc_Ctrl.pi_torque.min = 0;
  643. float err = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp) - gFoc_Ctrl.in.s_motVelocity;
  644. return PI_Controller_RunLimit(&gFoc_Ctrl.pi_torque, err);
  645. #else
  646. return maxTrq;
  647. #endif
  648. #endif
  649. }
  650. static __INLINE void PMSM_FOC_idq_Assign(void) {
  651. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
  652. if (gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_dqAngle != INVALID_ANGLE)) {
  653. float s, c;
  654. normal_sincosf(degree_2_pi(gFoc_Ctrl.in.s_dqAngle + 90.0f), &s, &c);
  655. gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.in.s_targetCurrent * c;
  656. if (gFoc_Ctrl.in.s_targetIdq.d > gFoc_Ctrl.hwLim.s_FWDCurrMax) {
  657. gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.hwLim.s_FWDCurrMax;
  658. }else if (gFoc_Ctrl.in.s_targetIdq.d < -gFoc_Ctrl.hwLim.s_FWDCurrMax) {
  659. gFoc_Ctrl.in.s_targetIdq.d = -gFoc_Ctrl.hwLim.s_FWDCurrMax;
  660. }
  661. gFoc_Ctrl.in.s_targetIdq.q = sqrtf(SQ(gFoc_Ctrl.in.s_targetCurrent) - SQ(gFoc_Ctrl.in.s_targetIdq.d));
  662. if (s < 0) {
  663. gFoc_Ctrl.in.s_targetIdq.q = -gFoc_Ctrl.in.s_targetIdq.q;
  664. }
  665. }else {
  666. gFoc_Ctrl.in.s_targetIdq.d = 0;
  667. gFoc_Ctrl.in.s_targetIdq.q = gFoc_Ctrl.in.s_targetCurrent;
  668. }
  669. }else if ((gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) || (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) ||
  670. (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
  671. motor_mpta_fw_lookup(gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque, &gFoc_Ctrl.in.s_targetIdq);
  672. }
  673. u32 mask = cpu_enter_critical();
  674. FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[0], gFoc_Ctrl.in.s_targetIdq.d);
  675. FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[1], gFoc_Ctrl.in.s_targetIdq.q);
  676. cpu_exit_critical(mask);
  677. }
  678. /*called in media task */
  679. void PMSM_FOC_idqCalc(void) {
  680. if (gFoc_Ctrl.in.b_AutoHold) {
  681. gFoc_Ctrl.pi_lock.max = CONFIG_MAX_LOCK_TORQUE;
  682. gFoc_Ctrl.pi_lock.min = -CONFIG_MAX_LOCK_TORQUE;
  683. float vel_count = motor_encoder_get_vel_count();
  684. float errRef = 0 - vel_count;
  685. gFoc_Ctrl.in.s_targetTorque = PI_Controller_Run(&gFoc_Ctrl.pi_lock ,errRef);
  686. PMSM_FOC_idq_Assign();
  687. return;
  688. }
  689. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
  690. gFoc_Ctrl.in.s_targetCurrent = eCtrl_get_RefCurrent();
  691. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE) {
  692. float maxTrq = eCtrl_get_RefTorque();
  693. if (eCtrl_get_FinalTorque() < 0.0001f && gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  694. maxTrq = 0;
  695. }
  696. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  697. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
  698. float refTorque = min(eCtrl_get_RefTorque(), eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
  699. float maxTrq = PMSM_FOC_Limit_Speed(refTorque);
  700. maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
  701. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  702. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD){
  703. float maxSpeed = eCtrl_get_FinalSpeed();
  704. float refSpeed = eCtrl_get_RefSpeed();
  705. if (gFoc_Ctrl.in.b_cruiseEna) {
  706. maxSpeed = eRamp_get_target(&gFoc_Ctrl.in.cruiseRpmRamp);
  707. refSpeed = eRamp_get_intepolation(&gFoc_Ctrl.in.cruiseRpmRamp);//gFoc_Ctrl.in.s_cruiseRPM;
  708. }
  709. #ifdef CONFIG_SPEED_LADRC
  710. if (maxSpeed >= 0) {
  711. ladrc_set_range(&gFoc_Ctrl.vel_adrc, -CONFIG_MAX_NEG_TORQUE, eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
  712. }else if (maxSpeed < 0) {
  713. ladrc_set_range(&gFoc_Ctrl.vel_adrc, -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp), CONFIG_MAX_NEG_TORQUE);
  714. }
  715. if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  716. ladrc_set_range(&gFoc_Ctrl.vel_adrc, 0, 0);
  717. }
  718. gFoc_Ctrl.in.s_targetRPM = refSpeed;
  719. float maxTrq = ladrc_run(&gFoc_Ctrl.vel_adrc, refSpeed, gFoc_Ctrl.in.s_motVelocity);
  720. #else
  721. if (maxSpeed >= 0) {
  722. gFoc_Ctrl.pi_speed.max = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);//gFoc_Ctrl.userLim.s_PhaseCurrLim;
  723. gFoc_Ctrl.pi_speed.min = -CONFIG_MAX_NEG_TORQUE;
  724. }else if (maxSpeed < 0) {
  725. gFoc_Ctrl.pi_speed.min = -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);//gFoc_Ctrl.userLim.s_PhaseCurrLim;
  726. gFoc_Ctrl.pi_speed.max = CONFIG_MAX_NEG_TORQUE;
  727. }
  728. if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  729. gFoc_Ctrl.pi_speed.max = 0;
  730. gFoc_Ctrl.pi_speed.min = 0; //防止倒转
  731. }
  732. gFoc_Ctrl.in.s_targetRPM = refSpeed;
  733. float errRef = refSpeed - gFoc_Ctrl.in.s_motVelocity;
  734. float maxTrq = PI_Controller_Run(&gFoc_Ctrl.pi_speed, errRef);
  735. #endif
  736. maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
  737. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  738. }
  739. PMSM_FOC_idq_Assign();
  740. }
  741. u8 PMSM_FOC_RunTime_Limit(void) {
  742. u8 changed = FOC_LIM_NO_CHANGE;
  743. float dc_lim = (float)vbus_under_vol_limit();
  744. float torque_lim = (float)min(mos_temp_high_limit(), motor_temp_high_limit());
  745. if (gFoc_Ctrl.protLim.s_iDCLim != dc_lim || gFoc_Ctrl.protLim.s_TorqueLim != torque_lim) {
  746. if ((dc_lim > gFoc_Ctrl.protLim.s_iDCLim) || (torque_lim > gFoc_Ctrl.protLim.s_TorqueLim)) {
  747. changed = FOC_LIM_CHANGE_H;
  748. }else {
  749. changed = FOC_LIM_CHANGE_L;
  750. }
  751. gFoc_Ctrl.protLim.s_iDCLim = dc_lim;
  752. gFoc_Ctrl.protLim.s_TorqueLim = torque_lim;
  753. }
  754. return changed;
  755. }
  756. bool PMSM_FOC_iDC_is_Limited(void) {
  757. return (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE);
  758. }
  759. bool PMSM_FOC_Torque_is_Limited(void) {
  760. return (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE);
  761. }
  762. void PMSM_FOC_Slow_Task(void) {
  763. eRamp_running(&gFoc_Ctrl.rtLim.torqueLimRamp);
  764. eRamp_running(&gFoc_Ctrl.rtLim.DCCurrLimRamp);
  765. eRamp_running(&gFoc_Ctrl.rtLim.rpmLimRamp);
  766. eRamp_running(&gFoc_Ctrl.in.cruiseRpmRamp);
  767. PMSM_FOC_VelCtrl_Decide();
  768. PMSM_FOC_idqCalc();
  769. }
  770. void PMSM_FOC_Change_VelLoop_Params(float wcv, float b0) {
  771. ladrc_change_b0(&gFoc_Ctrl.vel_adrc, b0);
  772. ladrc_change_K(&gFoc_Ctrl.vel_adrc, wcv);
  773. }
  774. void PMSM_FOC_Change_TrqLoop_Params(float wcv, float b0) {
  775. ladrc_change_b0(&gFoc_Ctrl.vel_lim_adrc, b0);
  776. ladrc_change_K(&gFoc_Ctrl.vel_lim_adrc, wcv);
  777. }
  778. float PMSM_FOC_Get_Real_dqVector(void) {
  779. if (gFoc_Ctrl.out.s_RealCurrentFiltered == 0) {
  780. gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
  781. }
  782. return gFoc_Ctrl.out.s_RealCurrentFiltered;
  783. }
  784. PMSM_FOC_Ctrl *PMSM_FOC_Get(void) {
  785. return &gFoc_Ctrl;
  786. }
  787. void PMSM_FOC_Start(u8 nCtrlMode) {
  788. if (gFoc_Ctrl.in.b_motEnable) {
  789. return;
  790. }
  791. PMSM_FOC_CoreInit();
  792. eCtrl_Reset();
  793. gFoc_Ctrl.in.n_ctlMode = nCtrlMode;
  794. gFoc_Ctrl.in.b_motEnable = true;
  795. }
  796. void PMSM_FOC_Stop(void) {
  797. if (!gFoc_Ctrl.in.b_motEnable) {
  798. return;
  799. }
  800. PMSM_FOC_CoreInit();
  801. gFoc_Ctrl.in.b_motEnable = false;
  802. }
  803. bool PMSM_FOC_Is_Start(void) {
  804. return gFoc_Ctrl.in.b_motEnable;
  805. }
  806. void PMSM_FOC_DCCurrLimit(float ibusLimit) {
  807. if (ibusLimit > gFoc_Ctrl.hwLim.s_iDCMax) {
  808. ibusLimit = gFoc_Ctrl.hwLim.s_iDCMax;
  809. }
  810. if (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE) {
  811. ibusLimit = min(ibusLimit, gFoc_Ctrl.protLim.s_iDCLim);
  812. }
  813. gFoc_Ctrl.userLim.s_iDCLim = ibusLimit;
  814. if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
  815. eRamp_reset_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit);
  816. }else {
  817. eRamp_set_step_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit, CONFIG_eCTRL_STEP_TS);
  818. }
  819. }
  820. float PMSM_FOC_GetDCCurrLimit(void) {
  821. return gFoc_Ctrl.userLim.s_iDCLim;
  822. }
  823. void PMSM_FOC_SpeedRampLimit(float speedLimit, float speed) {
  824. if (speedLimit > gFoc_Ctrl.hwLim.s_motRPMMax) {
  825. speedLimit = gFoc_Ctrl.hwLim.s_motRPMMax;
  826. }
  827. gFoc_Ctrl.userLim.s_motRPMLim = (speedLimit);
  828. if (ABS(speed) <= CONFIG_ZERO_SPEED_RPM) {
  829. eRamp_reset_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit);
  830. }else {
  831. eRamp_set_step_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS);
  832. }
  833. }
  834. void PMSM_FOC_SpeedLimit(float speedLimit) {
  835. PMSM_FOC_SpeedRampLimit(speedLimit, gFoc_Ctrl.in.s_motVelocity);
  836. }
  837. void PMSM_FOC_SpeedDirectLimit(float limit) {
  838. PMSM_FOC_SpeedRampLimit(limit, 0);
  839. }
  840. float PMSM_FOC_GetSpeedLimit(void) {
  841. return gFoc_Ctrl.userLim.s_motRPMLim;
  842. }
  843. void PMSM_FOC_TorqueLimit(float torqueLimit) {
  844. if (torqueLimit > gFoc_Ctrl.hwLim.s_torqueMax) {
  845. torqueLimit = gFoc_Ctrl.hwLim.s_torqueMax;
  846. }
  847. if (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE) {
  848. torqueLimit = min(torqueLimit, gFoc_Ctrl.protLim.s_TorqueLim);
  849. }
  850. gFoc_Ctrl.userLim.s_torqueLim = torqueLimit;
  851. if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
  852. eRamp_reset_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit);
  853. }else {
  854. eRamp_set_step_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit, CONFIG_eCTRL_STEP_TS);
  855. }
  856. }
  857. float PMSM_FOC_GetTorqueLimit(void) {
  858. return gFoc_Ctrl.userLim.s_torqueLim;
  859. }
  860. void PMSM_FOC_SetEbrkTorque(float phase_curr, float dc_curr) {
  861. gFoc_Ctrl.userLim.s_TorqueBrkLim = fclamp(phase_curr, 0, nv_get_foc_params()->s_TorqueBrkLim);
  862. gFoc_Ctrl.userLim.s_iDCeBrkLim = fclamp(dc_curr, 0, nv_get_foc_params()->s_iDCeBrkLim);
  863. }
  864. float PMSM_FOC_GetEbrkTorque(void) {
  865. return gFoc_Ctrl.userLim.s_TorqueBrkLim;
  866. }
  867. float PMSM_FOC_GetVbusVoltage(void) {
  868. return gFoc_Ctrl.in.s_vDC;
  869. }
  870. float PMSM_FOC_GetVbusCurrent(void) {
  871. return gFoc_Ctrl.out.s_FilteriDC;
  872. }
  873. DQ_t* PMSM_FOC_GetDQCurrent(void) {
  874. return &gFoc_Ctrl.out.s_RealIdq;
  875. }
  876. bool PMSM_FOC_SetCtrlMode(u8 mode) {
  877. if (mode > CTRL_MODE_EBRAKE) {
  878. PMSM_FOC_SetErrCode(FOC_Param_Err);
  879. return false;
  880. }
  881. gFoc_Ctrl.in.n_ctlMode = mode;
  882. return true;
  883. }
  884. u8 PMSM_FOC_GetCtrlMode(void) {
  885. return gFoc_Ctrl.in.n_ctlMode;
  886. }
  887. void PMSM_FOC_PhaseCurrLim(float lim) {
  888. if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
  889. lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
  890. }
  891. gFoc_Ctrl.userLim.s_PhaseCurrLim = lim;
  892. }
  893. void PMSM_FOC_RT_PhaseCurrLim(float lim) {
  894. if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
  895. lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
  896. }
  897. eRamp_init_target2(&gFoc_Ctrl.rtLim.torqueLimRamp, lim, CONFIG_LIMIT_RAMP_TIME);
  898. }
  899. float PMSM_FOC_GetPhaseCurrLim(void) {
  900. return gFoc_Ctrl.userLim.s_PhaseCurrLim;
  901. }
  902. void PMSM_FOC_SetOpenVdq(float vd, float vq) {
  903. FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[0], vd);
  904. FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[1], vq);
  905. }
  906. bool PMSM_FOC_EnableCruise(bool enable) {
  907. if (enable != gFoc_Ctrl.in.b_cruiseEna) {
  908. float motSpd = PMSM_FOC_GetSpeed();
  909. if (enable && (motSpd < CONFIG_MIN_CRUISE_RPM)) { //
  910. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  911. return false;
  912. }
  913. eRamp_reset_target(&gFoc_Ctrl.in.cruiseRpmRamp, motSpd);
  914. gFoc_Ctrl.in.s_cruiseRPM = motSpd;
  915. gFoc_Ctrl.in.b_cruiseEna = enable;
  916. }
  917. return true;
  918. }
  919. bool PMSM_FOC_PauseCruise(void) {
  920. gFoc_Ctrl.in.b_cruiseEna = false;
  921. return true;
  922. }
  923. bool PMSM_FOC_ResumeCruise(void) {
  924. gFoc_Ctrl.in.b_cruiseEna = true;
  925. eRamp_init_target2(&gFoc_Ctrl.in.cruiseRpmRamp, PMSM_FOC_GetSpeed(), CONFIG_CRUISE_RAMP_TIME);
  926. eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
  927. return true;
  928. }
  929. bool PMSM_FOC_Is_CruiseEnabled(void) {
  930. return (gFoc_Ctrl.in.b_cruiseEna && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD));
  931. }
  932. bool PMSM_FOC_Set_Speed(float rpm) {
  933. if (gFoc_Ctrl.in.b_cruiseEna) {
  934. return false;
  935. }
  936. eCtrl_set_TgtSpeed(min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm));
  937. return true;
  938. }
  939. bool PMSM_FOC_Set_Current(float is) {
  940. if (is > gFoc_Ctrl.userLim.s_PhaseCurrLim) {
  941. is = gFoc_Ctrl.userLim.s_PhaseCurrLim;
  942. }else if (is < -gFoc_Ctrl.userLim.s_PhaseCurrLim) {
  943. is = -gFoc_Ctrl.userLim.s_PhaseCurrLim;
  944. }
  945. eCtrl_set_TgtCurrent(is);
  946. return true;
  947. }
  948. bool PMSM_FOC_Set_Torque(float trq) {
  949. if (trq > gFoc_Ctrl.userLim.s_torqueLim) {
  950. trq = gFoc_Ctrl.userLim.s_torqueLim;
  951. }else if (trq < -gFoc_Ctrl.userLim.s_torqueLim) {
  952. trq = -gFoc_Ctrl.userLim.s_torqueLim;
  953. }
  954. eCtrl_set_TgtTorque(trq);
  955. return true;
  956. }
  957. void PMSM_FOC_Reset_Torque(void) {
  958. float real_trq = PMSM_FOC_Get_Real_dqVector();
  959. eCtrl_reset_Torque(real_trq);
  960. }
  961. bool PMSM_FOC_Set_CruiseSpeed(float rpm) {
  962. if (PMSM_FOC_Is_CruiseEnabled()) {
  963. if (rpm < CONFIG_MIN_CRUISE_RPM + 20) {
  964. rpm = CONFIG_MIN_CRUISE_RPM + 20;
  965. }
  966. gFoc_Ctrl.in.s_cruiseRPM = min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm);
  967. eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
  968. return true;
  969. }
  970. PMSM_FOC_SetErrCode(FOC_NotCruiseMode);
  971. return false;
  972. }
  973. void PMSM_FOC_MTPA_Calibrate(bool enable) {
  974. if (enable) {
  975. gFoc_Ctrl.in.b_MTPA_calibrate = true;
  976. gFoc_Ctrl.in.s_dqAngle = 0;
  977. }else {
  978. gFoc_Ctrl.in.s_dqAngle = INVALID_ANGLE;
  979. gFoc_Ctrl.in.b_MTPA_calibrate = false;
  980. }
  981. }
  982. void PMSM_FOC_Set_MotAngle(float angle) {
  983. gFoc_Ctrl.in.s_manualAngle = (angle);
  984. }
  985. void PMSM_FOC_Set_Dq_Angle(float angle) {
  986. gFoc_Ctrl.in.s_dqAngle = (angle);
  987. }
  988. void PMSM_FOC_Get_TgtIDQ(DQ_t * dq) {
  989. dq->d = gFoc_Ctrl.in.s_targetIdq.d;
  990. dq->q = gFoc_Ctrl.in.s_targetIdq.q;
  991. }
  992. float PMSM_FOC_GetSpeed(void) {
  993. return gFoc_Ctrl.in.s_motVelocity;
  994. }
  995. void PMSM_FOC_AutoHold(bool lock) {
  996. if (gFoc_Ctrl.in.b_AutoHold != lock) {
  997. motor_encoder_lock_pos(lock);
  998. PI_Controller_Reset(&gFoc_Ctrl.pi_lock, 0);
  999. if (!lock) {
  1000. float hold_torque = gFoc_Ctrl.in.s_targetTorque * 1.1f;
  1001. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
  1002. #ifdef CONFIG_SPEED_LADRC
  1003. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, 0, hold_torque);
  1004. #else
  1005. PI_Controller_Reset(&gFoc_Ctrl.pi_torque, hold_torque);
  1006. #endif
  1007. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) {
  1008. #ifdef CONFIG_SPEED_LADRC
  1009. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, hold_torque);
  1010. #else
  1011. PI_Controller_Reset(&gFoc_Ctrl.pi_speed, hold_torque);
  1012. #endif
  1013. }
  1014. eCtrl_reset_Torque(hold_torque);
  1015. gFoc_Ctrl.out.f_autohold_trq = hold_torque;
  1016. }else {
  1017. gFoc_Ctrl.out.f_autohold_trq = 0;
  1018. }
  1019. gFoc_Ctrl.in.b_AutoHold = lock;
  1020. }
  1021. }
  1022. bool PMSM_FOC_AutoHoldding(void) {
  1023. return gFoc_Ctrl.in.b_AutoHold;
  1024. }
  1025. static PI_Controller *_pid(u8 id) {
  1026. PI_Controller *pi = NULL;
  1027. if (id == PID_D_id) {
  1028. pi = &gFoc_Ctrl.pi_id;
  1029. }else if (id == PID_Q_id) {
  1030. pi = &gFoc_Ctrl.pi_iq;
  1031. }else if (id == PID_TRQ_id) {
  1032. #ifndef CONFIG_SPEED_LADRC
  1033. pi = &gFoc_Ctrl.pi_torque;
  1034. #endif
  1035. }else if (id == PID_Spd_id) {
  1036. #ifndef CONFIG_SPEED_LADRC
  1037. pi = &gFoc_Ctrl.pi_speed;
  1038. #endif
  1039. }
  1040. return pi;
  1041. }
  1042. void PMSM_FOC_SetPid(u8 id, float kp, float ki, float kd) {
  1043. if (id > PID_Max_id) {
  1044. return;
  1045. }
  1046. PI_Controller *pi = _pid(id);
  1047. if (pi != NULL) {
  1048. pi->kp = kp;
  1049. pi->ki = ki;
  1050. pi->kd = kd;
  1051. }
  1052. }
  1053. void PMSM_FOC_GetPid(u8 id, float *kp, float *ki, float *kd) {
  1054. if (id > PID_Max_id) {
  1055. return;
  1056. }
  1057. PI_Controller *pi = _pid(id);
  1058. if (pi != NULL) {
  1059. *kp = pi->kp;
  1060. *ki = pi->ki;
  1061. *kd = pi->kd;
  1062. }
  1063. }
  1064. void PMSM_FOC_SetErrCode(u8 error) {
  1065. if (gFoc_Ctrl.out.n_Error != error) {
  1066. gFoc_Ctrl.out.n_Error = error;
  1067. }
  1068. }
  1069. u8 PMSM_FOC_GetErrCode(void) {
  1070. return gFoc_Ctrl.out.n_Error;
  1071. }
  1072. void PMSM_FOC_Set_PlotType(Plot_t t) {
  1073. gFoc_Ctrl.plot_type = t;
  1074. }
  1075. //获取母线电流和实际输出电流矢量大小
  1076. void PMSM_FOC_Calc_Current(void) {
  1077. float vd = gFoc_Ctrl.out.s_OutVdq.d;
  1078. float vq = gFoc_Ctrl.out.s_OutVdq.q;
  1079. float id = gFoc_Ctrl.out.s_FilterIdq.d;
  1080. float iq = gFoc_Ctrl.out.s_FilterIdq.q;
  1081. /*
  1082. 根据公式(等幅值变换,功率不等):
  1083. iDC x vDC = 2/3(iq x vq + id x vd);
  1084. */
  1085. float m_pow = (vd * id + vq * iq); //s32q10
  1086. float raw_idc = m_pow / get_vbus_float();// * 1.5f * 0.66f; //s16q5
  1087. LowPass_Filter(gFoc_Ctrl.out.s_CalciDC, raw_idc, 0.1f);
  1088. raw_idc = get_vbus_current();
  1089. LowPass_Filter(gFoc_Ctrl.out.s_FilteriDC, raw_idc, 0.05f);
  1090. gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
  1091. }
  1092. void PMSM_FOC_Brake(bool brake) {
  1093. gFoc_Ctrl.in.b_eBrake = brake;
  1094. if (gFoc_Ctrl.in.b_eBrake & gFoc_Ctrl.in.b_cruiseEna) {
  1095. gFoc_Ctrl.in.b_cruiseEna = false;
  1096. }
  1097. eCtrl_brake_signal(brake);
  1098. }