PMSM_FOC_Core.c 44 KB

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