PMSM_FOC_Core.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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. LowPass_Filter(gFoc_Ctrl.in.s_iABCFilter[0], gFoc_Ctrl.in.s_iABC[0], lowpass);
  323. LowPass_Filter(gFoc_Ctrl.in.s_iABCFilter[1], gFoc_Ctrl.in.s_iABC[1], lowpass);
  324. gFoc_Ctrl.in.s_iABCFilter[2] = -(gFoc_Ctrl.in.s_iABCFilter[0] + gFoc_Ctrl.in.s_iABCFilter[1]);
  325. if ((gFoc_Ctrl.in.s_angleLast == INVALID_ANGLE) || (gFoc_Ctrl.in.s_motVelDegreePers < 100)) {
  326. gFoc_Ctrl.in.s_angleLast = gFoc_Ctrl.in.s_motAngle;
  327. a_max = b_max = c_max = 0;
  328. _unbalance_cnt = 0;
  329. _cycle_cnt = 0;
  330. _last_mod_cnt = 0;
  331. phase_unbalance_r = 0;
  332. return;
  333. }
  334. float delta_angle = gFoc_Ctrl.in.s_motAngle - gFoc_Ctrl.in.s_angleLast;
  335. if (delta_angle > 200 || delta_angle < -200) { //one cycle
  336. _cycle_cnt ++;
  337. }
  338. gFoc_Ctrl.in.s_angleLast = gFoc_Ctrl.in.s_motAngle;
  339. u32 mod_cnt = _cycle_cnt % CONFIG_PEAK_CNT;
  340. bool trigger = false;
  341. if ((mod_cnt == 0) && (_last_mod_cnt != mod_cnt)) {
  342. trigger = true;
  343. }
  344. _last_mod_cnt = mod_cnt;
  345. a_max = MAX(a_max, gFoc_Ctrl.in.s_iABCFilter[0] * (1.0f / 0.707f));
  346. b_max = MAX(b_max, gFoc_Ctrl.in.s_iABCFilter[1] * (1.0f / 0.707f));
  347. c_max = MAX(c_max, gFoc_Ctrl.in.s_iABCFilter[2] * (1.0f / 0.707f));
  348. if (trigger) { //经过CONFIG_PEAK_CNT个周期,已经得到peak值
  349. float i_min = 1000.0f, i_max = 0;
  350. if (a_max > i_max) {
  351. i_max = a_max;
  352. }
  353. if (a_max < i_min) {
  354. i_min = a_max;
  355. }
  356. if (b_max > i_max) {
  357. i_max = b_max;
  358. }
  359. if (b_max < i_min) {
  360. i_min = b_max;
  361. }
  362. if (c_max > i_max) {
  363. i_max = c_max;
  364. }
  365. if (c_max < i_min) {
  366. i_min = c_max;
  367. }
  368. float unbalance_r = (i_max - i_min - CONFIG_PHASE_UNBALANCE_THROLD)/(i_max + 1e-8f);
  369. if (unbalance_r >= CONFIG_PHASE_UNBALANCE_R) {
  370. if (_unbalance_cnt++ >= 500) {
  371. if (mc_set_critical_error(FOC_CRIT_PHASE_UNBalance_Err)) {
  372. mc_crit_err_add(FOC_CRIT_PHASE_UNBalance_Err, (s16)i_max, (s16)i_min);
  373. }
  374. }
  375. }else {
  376. _unbalance_cnt = 0;
  377. }
  378. phase_unbalance_r = unbalance_r;
  379. phase_a_max = a_max;
  380. phase_b_max = b_max;
  381. phase_c_max = c_max;
  382. a_max = b_max = c_max = 0;
  383. }
  384. }
  385. /* 死区补偿 */
  386. static __INLINE void PMSM_FOC_DeadTime_Compensate(s32 PWM_Half_Period) {
  387. #ifdef CONFIG_START_LINE_DTC_CURRENT
  388. float deadTime = (float)CONFIG_HW_DeadTime/2.0f;
  389. s32 dutyDTCA = 0;
  390. s32 dutyDTCB = 0;
  391. s32 dutyDTCC = 0;
  392. float r, delta;
  393. float iabs = ABS(gFoc_Ctrl.in.s_iABC_DT[0]);
  394. if (iabs > CONFIG_START_LINE_DTC_CURRENT) {
  395. delta = iabs - CONFIG_START_LINE_DTC_CURRENT;
  396. r = delta / (COMFIG_END_LINE_DTC_CURRENT - CONFIG_START_LINE_DTC_CURRENT);
  397. if (r > 1.0f) {
  398. r = 1.0f;
  399. }
  400. if (gFoc_Ctrl.in.s_iABC_DT[0] < 0) {
  401. r = -r;
  402. }
  403. dutyDTCA = (s32)(r * deadTime);
  404. }
  405. iabs = ABS(gFoc_Ctrl.in.s_iABC_DT[1]);
  406. if (iabs > CONFIG_START_LINE_DTC_CURRENT) {
  407. delta = iabs - CONFIG_START_LINE_DTC_CURRENT;
  408. r = delta / (COMFIG_END_LINE_DTC_CURRENT - CONFIG_START_LINE_DTC_CURRENT);
  409. if (r > 1.0f) {
  410. r = 1.0f;
  411. }
  412. if (gFoc_Ctrl.in.s_iABC_DT[1] < 0) {
  413. r = -r;
  414. }
  415. dutyDTCB = (s32)(r * deadTime);
  416. }
  417. iabs = ABS(gFoc_Ctrl.in.s_iABC_DT[2]);
  418. if (iabs > CONFIG_START_LINE_DTC_CURRENT) {
  419. delta = iabs - CONFIG_START_LINE_DTC_CURRENT;
  420. r = delta / (COMFIG_END_LINE_DTC_CURRENT - CONFIG_START_LINE_DTC_CURRENT);
  421. if (r > 1.0f) {
  422. r = 1.0f;
  423. }
  424. if (gFoc_Ctrl.in.s_iABC_DT[2] < 0) {
  425. r = -r;
  426. }
  427. dutyDTCC = (s32)(r * deadTime);
  428. }
  429. s32 dutyA = (s32)gFoc_Ctrl.out.n_Duty[0] + dutyDTCA;
  430. s32 dutyB = (s32)gFoc_Ctrl.out.n_Duty[1] + dutyDTCB;
  431. s32 dutyC = (s32)gFoc_Ctrl.out.n_Duty[2] + dutyDTCC;
  432. gFoc_Ctrl.out.n_Duty[0] = sclamp(dutyA, 0, PWM_Half_Period);
  433. gFoc_Ctrl.out.n_Duty[1] = sclamp(dutyB, 0, PWM_Half_Period);
  434. gFoc_Ctrl.out.n_Duty[2] = sclamp(dutyC, 0, PWM_Half_Period);
  435. #endif
  436. }
  437. //#define UPDATE_Lq_By_iq /* Q轴电感 通过Iq电流补偿 */
  438. #define CONFIG_Volvec_Delay_Comp /* 电压矢量角度补偿 */
  439. #define CONFIG_Volvec_Delay_Comp_Start_Vel 500 // rpm
  440. static __INLINE bool PMSM_FOC_Update_Input(void) {
  441. AB_t iAB;
  442. float *iabc = gFoc_Ctrl.in.s_iABC;
  443. phase_current_get(iabc);
  444. PMSM_FOC_Calc_iDC_Fast();
  445. Clark(iabc[0], iabc[1], iabc[2], &iAB);
  446. 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);
  447. float enc_angle = motor_encoder_get_angle();
  448. float enc_vel = motor_encoder_get_speed();
  449. if (!foc_observer_diagnostic(enc_angle, enc_vel)){
  450. /* detect encoder angle error, do something here */
  451. if (!foc_observer_sensorless_stable()) {
  452. gFoc_Ctrl.in.s_motVelocity = 0;
  453. return false;
  454. }
  455. enc_angle = foc_observer_sensorless_angle();
  456. enc_vel = foc_observer_sensorless_speed();
  457. }
  458. if (!gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_manualAngle != INVALID_ANGLE)) {
  459. gFoc_Ctrl.in.s_motAngle = gFoc_Ctrl.in.s_manualAngle;
  460. }else {
  461. gFoc_Ctrl.in.s_motAngle = enc_angle;
  462. }
  463. gFoc_Ctrl.in.s_motVelocity = enc_vel;
  464. LowPass_Filter(gFoc_Ctrl.in.s_motVelocityFiltered, gFoc_Ctrl.in.s_motVelocity, 0.01f);
  465. gFoc_Ctrl.in.s_motVelDegreePers = gFoc_Ctrl.in.s_motVelocityFiltered / 30.0f * PI * gFoc_Ctrl.params.n_poles;
  466. PMSM_FOC_Phase_Unbalance();
  467. #ifdef CONFIG_DQ_STEP_RESPONSE
  468. gFoc_Ctrl.in.s_motAngle = 0;
  469. #endif
  470. gFoc_Ctrl.in.s_vDC = get_vbus_float();
  471. get_phase_vols(gFoc_Ctrl.in.s_vABC);
  472. SinCos_Lut(gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
  473. Park(&iAB, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_RealIdq);
  474. float lowpass = gFoc_Ctrl.in.s_motVelDegreePers * FOC_CTRL_US * 2.0f;
  475. LowPass_Filter(gFoc_Ctrl.out.s_FilterIdq.d, gFoc_Ctrl.out.s_RealIdq.d, lowpass);
  476. LowPass_Filter(gFoc_Ctrl.out.s_FilterIdq.q, gFoc_Ctrl.out.s_RealIdq.q, lowpass);
  477. /* 使用低通后的dq电流重新变换得到abc电流 */
  478. RevPark(&gFoc_Ctrl.out.s_FilterIdq, gFoc_Ctrl.in.s_motAngle, &iAB);
  479. RevClark(&iAB, gFoc_Ctrl.in.s_iABC_DT);
  480. #ifdef CONFIG_Volvec_Delay_Comp
  481. if (gFoc_Ctrl.in.s_motVelocityFiltered >= CONFIG_Volvec_Delay_Comp_Start_Vel) {
  482. float next_angle = gFoc_Ctrl.in.s_motAngle + gFoc_Ctrl.in.s_motVelDegreePers / PI * 180.0f * (FOC_CTRL_US * 0.8f);
  483. rand_angle(next_angle);
  484. SinCos_Lut(next_angle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
  485. }
  486. #endif
  487. return true;
  488. }
  489. #ifdef CONFIG_DQ_STEP_RESPONSE
  490. float target_d = 0.0f;
  491. float target_q = 0.0f;
  492. #endif
  493. static u32 PMSM_FOC_Debug_Task(void *p) {
  494. if (gFoc_Ctrl.in.b_motEnable) {
  495. #ifdef CONFIG_DQ_STEP_RESPONSE
  496. if (gFoc_Ctrl.plot_type == Plot_D_Step) {
  497. plot_2data16(FtoS16x10(target_d), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d));
  498. }if (gFoc_Ctrl.plot_type == Plot_Q_Step) {
  499. plot_2data16(FtoS16x10(target_q), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q));
  500. }
  501. #else
  502. if (gFoc_Ctrl.plot_type == Plot_D_flow) {
  503. plot_2data16(FtoS16x10(gFoc_Ctrl.idq_ctl[0].s_Cp), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d));
  504. }else if (gFoc_Ctrl.plot_type == Plot_Q_flow) {
  505. plot_2data16(FtoS16x10(gFoc_Ctrl.idq_ctl[1].s_Cp), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q));
  506. }else if (gFoc_Ctrl.plot_type == Plot_DQ_Curr) {
  507. plot_3data16(FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q), FtoS16x10(gFoc_Ctrl.out.s_FilteriDC));
  508. }else if (gFoc_Ctrl.plot_type == Plot_Spd_flow) {
  509. plot_2data16(gFoc_Ctrl.in.s_targetRPM, gFoc_Ctrl.in.s_motVelocity);
  510. }
  511. #endif
  512. }
  513. return 1;
  514. }
  515. static __INLINE float id_feedforward(float eW) {
  516. #ifdef CONFIG_CURRENT_LOOP_DECOUPE
  517. return -(gFoc_Ctrl.params.lq * gFoc_Ctrl.out.s_RealIdq.q * eW);
  518. #else
  519. return 0;
  520. #endif
  521. }
  522. static __INLINE float iq_feedforward(float eW) {
  523. #ifdef CONFIG_CURRENT_LOOP_DECOUPE
  524. return (gFoc_Ctrl.params.ld * gFoc_Ctrl.out.s_RealIdq.d + gFoc_Ctrl.params.flux) * eW;
  525. #else
  526. return 0;
  527. #endif
  528. }
  529. bool PMSM_FOC_Schedule(void) {
  530. gFoc_Ctrl.ctrl_count++;
  531. if (!PMSM_FOC_Update_Input()){
  532. return false;
  533. }
  534. if (gFoc_Ctrl.out.n_RunMode != CTRL_MODE_OPEN) {
  535. float max_Vdc = gFoc_Ctrl.in.s_vDC * CONFIG_SVM_MODULATION;
  536. float max_vd = max_Vdc * SQRT3_BY_2;
  537. /* limiter Vd output for PI controller */
  538. gFoc_Ctrl.pi_id.max = max_vd;
  539. gFoc_Ctrl.pi_id.min = -max_vd;
  540. #ifndef CONFIG_DQ_STEP_RESPONSE
  541. float target_d = FOC_Get_DqRamp(&gFoc_Ctrl.idq_ctl[0]);
  542. #endif
  543. float err = target_d - gFoc_Ctrl.out.s_RealIdq.d;
  544. float id_ff = id_feedforward(gFoc_Ctrl.in.s_motVelDegreePers);
  545. gFoc_Ctrl.in.s_targetVdq.d = PI_Controller_Current(&gFoc_Ctrl.pi_id, err, id_ff);
  546. #ifdef UPDATE_Lq_By_iq
  547. /* update kp&ki from lq for iq PI controller */
  548. float lq = motor_get_lq_from_iq((s16)gFoc_Ctrl.out.s_FilterIdq.q);
  549. LowPass_Filter(gFoc_Ctrl.params.lq, lq, 0.01f);
  550. gFoc_Ctrl.pi_iq.kp = ((float)nv_get_foc_params()->n_currentBand * gFoc_Ctrl.params.lq);
  551. gFoc_Ctrl.pi_iq.ki = (nv_get_motor_params()->r/gFoc_Ctrl.params.lq);
  552. #endif
  553. /* limiter Vq output for PI controller */
  554. float max_vq = sqrtf(SQ(max_vd) - SQ(gFoc_Ctrl.in.s_targetVdq.d));
  555. gFoc_Ctrl.pi_iq.max = max_vq;
  556. gFoc_Ctrl.pi_iq.min = -max_vq;
  557. #ifndef CONFIG_DQ_STEP_RESPONSE
  558. float target_q = FOC_Get_DqRamp(&gFoc_Ctrl.idq_ctl[1]);
  559. #endif
  560. err = target_q - gFoc_Ctrl.out.s_RealIdq.q;
  561. float iq_ff = iq_feedforward(gFoc_Ctrl.in.s_motVelDegreePers);
  562. gFoc_Ctrl.in.s_targetVdq.q = PI_Controller_Current(&gFoc_Ctrl.pi_iq, err, iq_ff);
  563. }else {
  564. float max_Vdc = gFoc_Ctrl.in.s_vDC * CONFIG_SVM_MODULATION;
  565. float max_vd = max_Vdc * SQRT3_BY_2;
  566. float vd_ref = FOC_Get_DqRamp(&gFoc_Ctrl.vdq_ctl[0]);
  567. gFoc_Ctrl.in.s_targetVdq.d = fclamp(vd_ref, -max_vd, max_vd);
  568. float max_vq = sqrtf(SQ(max_vd) - SQ(gFoc_Ctrl.in.s_targetVdq.d));
  569. float vq_ref = FOC_Get_DqRamp(&gFoc_Ctrl.vdq_ctl[1]);
  570. gFoc_Ctrl.in.s_targetVdq.q = fclamp(vq_ref, -max_vq, max_vq);
  571. }
  572. #if 0
  573. 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);
  574. gFoc_Ctrl.out.s_OutVdq.d *= SQRT3_BY_2;
  575. gFoc_Ctrl.out.s_OutVdq.q *= SQRT3_BY_2;
  576. #else
  577. gFoc_Ctrl.out.s_OutVdq.d = gFoc_Ctrl.in.s_targetVdq.d;
  578. gFoc_Ctrl.out.s_OutVdq.q = gFoc_Ctrl.in.s_targetVdq.q;
  579. #endif
  580. RevPark(&gFoc_Ctrl.out.s_OutVdq, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_OutVAB);
  581. SVM_Duty_Fix(&gFoc_Ctrl.out.s_OutVAB, gFoc_Ctrl.in.s_vDC, FOC_PWM_Half_Period, &gFoc_Ctrl.out);
  582. PMSM_FOC_DeadTime_Compensate((s32)FOC_PWM_Half_Period);
  583. phase_current_point(&gFoc_Ctrl.out);
  584. pwm_update_duty(gFoc_Ctrl.out.n_Duty[0], gFoc_Ctrl.out.n_Duty[1], gFoc_Ctrl.out.n_Duty[2]);
  585. pwm_update_sample(gFoc_Ctrl.out.n_Sample1, gFoc_Ctrl.out.n_Sample2, gFoc_Ctrl.out.n_CPhases);
  586. return true;
  587. }
  588. void PMSM_FOC_LogDebug(void) {
  589. sys_debug("DC curr %f --- %f, - %f\n", gFoc_Ctrl.out.s_CalciDC, gFoc_Ctrl.out.s_CalciDC2, gFoc_Ctrl.hwLim.s_iDCMax);
  590. sys_debug("%s\n", gFoc_Ctrl.out.empty_load?"NoLoad Running":"Load Runing");
  591. sys_debug("unbalance: %f, %f, %f, %f\n", phase_unbalance_r, phase_a_max, phase_b_max, phase_c_max);
  592. }
  593. /*called in media task */
  594. u8 PMSM_FOC_CtrlMode(void) {
  595. u8 preMode = gFoc_Ctrl.out.n_RunMode;
  596. if (!gFoc_Ctrl.in.b_motEnable) {
  597. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
  598. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_OPEN) {
  599. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
  600. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_SPD || gFoc_Ctrl.in.b_cruiseEna){
  601. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_SPD;
  602. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_CURRENT) {
  603. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_CURRENT;
  604. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_EBRAKE) {
  605. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_EBRAKE;
  606. }else {
  607. if (!gFoc_Ctrl.in.b_cruiseEna) {
  608. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_TRQ;
  609. }
  610. }
  611. if (preMode != gFoc_Ctrl.out.n_RunMode) {
  612. if ((preMode == CTRL_MODE_SPD) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ)) {
  613. #ifdef CONFIG_SPEED_LADRC
  614. //ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
  615. ladrc_copy(&gFoc_Ctrl.vel_lim_adrc, &gFoc_Ctrl.vel_adrc);
  616. #else
  617. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, gFoc_Ctrl.in.s_targetTorque);
  618. #endif
  619. }else if ((preMode == CTRL_MODE_TRQ) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD)) {
  620. #ifdef CONFIG_SPEED_LADRC
  621. ladrc_copy(&gFoc_Ctrl.vel_adrc, &gFoc_Ctrl.vel_lim_adrc);
  622. #else
  623. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, gFoc_Ctrl.in.s_targetTorque);
  624. #endif
  625. }else if ((preMode == CTRL_MODE_CURRENT) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ)) {
  626. #ifdef CONFIG_SPEED_LADRC
  627. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
  628. #else
  629. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, gFoc_Ctrl.in.s_targetTorque);
  630. #endif
  631. }else if ((preMode != gFoc_Ctrl.out.n_RunMode) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
  632. eCtrl_reset_Torque(gFoc_Ctrl.in.s_targetTorque);
  633. eCtrl_set_TgtTorque(motor_get_ebreak_toruqe(gFoc_Ctrl.in.s_motVelocity));
  634. }else if ((preMode == CTRL_MODE_EBRAKE) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD)) {
  635. #ifdef CONFIG_SPEED_LADRC
  636. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, F_get_air());
  637. #else
  638. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, F_get_air());
  639. #endif
  640. }
  641. }
  642. return gFoc_Ctrl.out.n_RunMode;
  643. }
  644. #define RAMPE_1 CONFIG_RAMP_FIRST_TARGET
  645. static void crosszero_step_towards(float *value, float target) {
  646. static float no_cro_step = 2.0f;
  647. float v_now = *value;
  648. bool cross_zero = false;
  649. float high_ramp_torque = CONFIG_RAMP_SECOND_TARGET;
  650. if (target > 0) {
  651. if (v_now < -RAMPE_1) {
  652. step_towards(value, -RAMPE_1, 2.0f);
  653. cross_zero = true;
  654. }else if (v_now >= -RAMPE_1 && v_now <= high_ramp_torque) {
  655. step_towards(value, target, 0.03f);
  656. cross_zero = true;
  657. }
  658. }else if (target == 0) {
  659. if (v_now > high_ramp_torque) {
  660. step_towards(value, high_ramp_torque, 1.0f);
  661. cross_zero = true;
  662. }else if (v_now >= RAMPE_1 && v_now <= high_ramp_torque) {
  663. step_towards(value, target, 0.03f);
  664. cross_zero = true;
  665. }
  666. }else {
  667. if (v_now > high_ramp_torque) {
  668. step_towards(value, high_ramp_torque, 20.0f);
  669. cross_zero = true;
  670. }else if (v_now >= -RAMPE_1 && v_now <= high_ramp_torque) {
  671. step_towards(value, target, 0.03f);
  672. cross_zero = true;
  673. }
  674. }
  675. if (!cross_zero) {
  676. step_towards(&no_cro_step, 3.0f, 0.1f);
  677. step_towards(value, target, no_cro_step);
  678. }else {
  679. no_cro_step = 0.5f;
  680. }
  681. }
  682. /* MPTA, 弱磁, 功率限制,主要是分配DQ轴电流 */
  683. #define CHANGE_MAX_CNT 3
  684. static __INLINE void PMSM_FOC_VelCtrl_Decide(void) {
  685. #if 0
  686. static int change_cnt = 0;
  687. static bool change_done = false;
  688. static u32 change_time = 0xFFFFFFFF;
  689. float f_te = F_get_Te();
  690. float f_accl = F_get_accl();
  691. if (mc_is_epm()) {
  692. change_cnt = 0;
  693. change_time = 0xFFFFFFFF;
  694. change_done = false;
  695. gFoc_Ctrl.out.empty_load = false;
  696. return;
  697. }
  698. if (gFoc_Ctrl.in.s_motVelocity == 0.0f || gFoc_Ctrl.out.n_RunMode == CTRL_MODE_OPEN) {
  699. change_cnt = 0;
  700. change_time = 0xFFFFFFFF;
  701. change_done = false;
  702. gFoc_Ctrl.out.empty_load = false;
  703. return;
  704. }
  705. if (f_te <= 0.0f) {
  706. change_cnt = 0;
  707. change_time = 0xFFFFFFFF;
  708. return;
  709. }
  710. if (change_done) {
  711. /* 误判空转,发现电机给定的N大于空气阻力,说明不是空转 */
  712. if (gFoc_Ctrl.out.empty_load) {
  713. float f_air = F_get_air();
  714. if ((f_accl > 1.0f) && (f_te >= (f_air + f_accl))) {
  715. change_cnt ++;
  716. }else {
  717. change_cnt = 0;
  718. }
  719. if (change_cnt >= 500) {
  720. gFoc_Ctrl.out.empty_load = false;
  721. #ifdef CONFIG_SPEED_LADRC
  722. PMSM_FOC_Change_TrqLoop_Params(nv_get_foc_params()->f_adrc_vel_lim_Wcv, nv_get_foc_params()->f_adrc_vel_lim_B0);
  723. PMSM_FOC_Change_VelLoop_Params(nv_get_foc_params()->f_adrc_vel_Wcv, nv_get_foc_params()->f_adrc_vel_B0);
  724. #endif
  725. }
  726. }
  727. return;
  728. }
  729. if (change_time == 0xFFFFFFFF) {
  730. change_time = get_tick_ms();
  731. }else { //起步3s内检测是否空转
  732. if (get_delta_ms(change_time) > 3000) {
  733. return;
  734. }
  735. }
  736. if ((f_accl > 200.0f) && (f_accl/f_te > 3.0f )) {
  737. change_cnt++;
  738. }else if ((F_get_MotAccl() >= 10.0f) && (f_accl/f_te > 1.2f )) {
  739. change_cnt = CHANGE_MAX_CNT;
  740. }
  741. else {
  742. if ((f_te > 50) && (f_accl > 0) && (f_te > f_accl)) {
  743. change_cnt --;
  744. }else {
  745. change_cnt = 0;
  746. }
  747. }
  748. if (!change_done && (change_cnt >= CHANGE_MAX_CNT)) {
  749. change_done = true;
  750. change_cnt = 0;
  751. gFoc_Ctrl.out.empty_load = change_done;
  752. #ifdef CONFIG_SPEED_LADRC
  753. PMSM_FOC_Change_TrqLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
  754. PMSM_FOC_Change_VelLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
  755. #endif
  756. }else if (!change_done && (change_cnt <= -200)) {
  757. change_done = true;
  758. change_cnt = 0;
  759. gFoc_Ctrl.out.empty_load = false;
  760. }
  761. #endif
  762. }
  763. static __INLINE float PMSM_FOC_Limit_iDC(float maxTrq) {
  764. #if 1
  765. gFoc_Ctrl.pi_power.max = maxTrq;
  766. float errRef = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.DCCurrLimRamp) - (gFoc_Ctrl.out.s_FilteriDC);
  767. return PI_Controller_Run(&gFoc_Ctrl.pi_power, errRef);
  768. #else
  769. return maxTrq;
  770. #endif
  771. }
  772. static __INLINE float PMSM_FOC_Limit_Speed(float maxTrq) {
  773. #ifdef CONFIG_SPEED_LADRC
  774. float lim = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp);
  775. ladrc_set_range(&gFoc_Ctrl.vel_lim_adrc, 0, maxTrq);
  776. return ladrc_run(&gFoc_Ctrl.vel_lim_adrc, lim, gFoc_Ctrl.in.s_motVelocity);
  777. #else
  778. gFoc_Ctrl.pi_vel_lim.max = maxTrq;
  779. gFoc_Ctrl.pi_vel_lim.min = 0;
  780. float err = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp) - gFoc_Ctrl.in.s_motVelocity;
  781. return PI_Controller_RunVel(&gFoc_Ctrl.pi_vel_lim, err);
  782. #endif
  783. }
  784. static __INLINE void PMSM_FOC_idq_Assign(void) {
  785. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
  786. if (gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_dqAngle != INVALID_ANGLE)) {
  787. float s, c;
  788. normal_sincosf(degree_2_pi(gFoc_Ctrl.in.s_dqAngle + 90.0f), &s, &c);
  789. gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.in.s_targetCurrent * c;
  790. if (gFoc_Ctrl.in.s_targetIdq.d > gFoc_Ctrl.hwLim.s_FWDCurrMax) {
  791. gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.hwLim.s_FWDCurrMax;
  792. }else if (gFoc_Ctrl.in.s_targetIdq.d < -gFoc_Ctrl.hwLim.s_FWDCurrMax) {
  793. gFoc_Ctrl.in.s_targetIdq.d = -gFoc_Ctrl.hwLim.s_FWDCurrMax;
  794. }
  795. gFoc_Ctrl.in.s_targetIdq.q = sqrtf(SQ(gFoc_Ctrl.in.s_targetCurrent) - SQ(gFoc_Ctrl.in.s_targetIdq.d));
  796. if (s < 0) {
  797. gFoc_Ctrl.in.s_targetIdq.q = -gFoc_Ctrl.in.s_targetIdq.q;
  798. }
  799. }else {
  800. gFoc_Ctrl.in.s_targetIdq.d = 0;
  801. gFoc_Ctrl.in.s_targetIdq.q = gFoc_Ctrl.in.s_targetCurrent;
  802. }
  803. }else if ((gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) || (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) ||
  804. (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
  805. motor_mpta_fw_lookup(gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque, &gFoc_Ctrl.in.s_targetIdq);
  806. }
  807. u32 mask = cpu_enter_critical();
  808. FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[0], gFoc_Ctrl.in.s_targetIdq.d);
  809. FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[1], gFoc_Ctrl.in.s_targetIdq.q);
  810. cpu_exit_critical(mask);
  811. }
  812. /*called in media task */
  813. void PMSM_FOC_idqCalc(void) {
  814. if (gFoc_Ctrl.in.b_AutoHold) {
  815. gFoc_Ctrl.pi_lock.max = CONFIG_MAX_LOCK_TORQUE;
  816. gFoc_Ctrl.pi_lock.min = -CONFIG_MAX_LOCK_TORQUE;
  817. float vel_count = motor_encoder_get_vel_count();
  818. float errRef = 0 - vel_count;
  819. gFoc_Ctrl.in.s_targetTorque = PI_Controller_Run(&gFoc_Ctrl.pi_lock ,errRef);
  820. PMSM_FOC_idq_Assign();
  821. return;
  822. }
  823. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
  824. gFoc_Ctrl.in.s_targetCurrent = eCtrl_get_RefCurrent();
  825. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE) {
  826. float maxTrq = eCtrl_get_RefTorque();
  827. if (eCtrl_get_FinalTorque() < 0.0001f && gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  828. maxTrq = 0;
  829. }
  830. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  831. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
  832. float refTorque = min(eCtrl_get_RefTorque(), eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
  833. float maxTrq = PMSM_FOC_Limit_Speed(refTorque);
  834. maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
  835. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  836. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD){
  837. float maxSpeed = eCtrl_get_FinalSpeed();
  838. float refSpeed = eCtrl_get_RefSpeed();
  839. if (gFoc_Ctrl.in.b_cruiseEna) {
  840. maxSpeed = eRamp_get_target(&gFoc_Ctrl.in.cruiseRpmRamp);
  841. refSpeed = eRamp_get_intepolation(&gFoc_Ctrl.in.cruiseRpmRamp);//gFoc_Ctrl.in.s_cruiseRPM;
  842. }
  843. #ifdef CONFIG_SPEED_LADRC
  844. if (maxSpeed >= 0) {
  845. ladrc_set_range(&gFoc_Ctrl.vel_adrc, -CONFIG_MAX_NEG_TORQUE, eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
  846. }else if (maxSpeed < 0) {
  847. ladrc_set_range(&gFoc_Ctrl.vel_adrc, -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp), CONFIG_MAX_NEG_TORQUE);
  848. }
  849. if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  850. ladrc_set_range(&gFoc_Ctrl.vel_adrc, 0, 0);
  851. }
  852. gFoc_Ctrl.in.s_targetRPM = refSpeed;
  853. float maxTrq = ladrc_run(&gFoc_Ctrl.vel_adrc, refSpeed, gFoc_Ctrl.in.s_motVelocity);
  854. #else
  855. if (maxSpeed >= 0) {
  856. gFoc_Ctrl.pi_vel.max = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);
  857. gFoc_Ctrl.pi_vel.min = -CONFIG_MAX_NEG_TORQUE;
  858. }else if (maxSpeed < 0) {
  859. gFoc_Ctrl.pi_vel.min = -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);
  860. gFoc_Ctrl.pi_vel.max = CONFIG_MAX_NEG_TORQUE;
  861. }
  862. if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  863. gFoc_Ctrl.pi_vel.max = 0;
  864. gFoc_Ctrl.pi_vel.min = 0; //防止倒转
  865. }
  866. gFoc_Ctrl.in.s_targetRPM = refSpeed;
  867. float errRef = refSpeed - gFoc_Ctrl.in.s_motVelocity;
  868. float maxTrq = PI_Controller_RunVel(&gFoc_Ctrl.pi_vel, errRef);
  869. #endif
  870. maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
  871. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  872. }
  873. PMSM_FOC_idq_Assign();
  874. }
  875. u8 PMSM_FOC_RunTime_Limit(void) {
  876. u8 changed = FOC_LIM_NO_CHANGE;
  877. float dc_lim = (float)vbus_under_vol_limit();
  878. float torque_lim = (float)min(mos_temp_high_limit(), motor_temp_high_limit());
  879. if (gFoc_Ctrl.protLim.s_iDCLim != dc_lim || gFoc_Ctrl.protLim.s_TorqueLim != torque_lim) {
  880. if ((dc_lim > gFoc_Ctrl.protLim.s_iDCLim) || (torque_lim > gFoc_Ctrl.protLim.s_TorqueLim)) {
  881. changed = FOC_LIM_CHANGE_H;
  882. }else {
  883. changed = FOC_LIM_CHANGE_L;
  884. }
  885. gFoc_Ctrl.protLim.s_iDCLim = dc_lim;
  886. gFoc_Ctrl.protLim.s_TorqueLim = torque_lim;
  887. }
  888. return changed;
  889. }
  890. bool PMSM_FOC_iDC_is_Limited(void) {
  891. return (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE);
  892. }
  893. bool PMSM_FOC_Torque_is_Limited(void) {
  894. return (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE);
  895. }
  896. void PMSM_FOC_Slow_Task(void) {
  897. eRamp_running(&gFoc_Ctrl.rtLim.torqueLimRamp);
  898. eRamp_running(&gFoc_Ctrl.rtLim.DCCurrLimRamp);
  899. eRamp_running(&gFoc_Ctrl.rtLim.rpmLimRamp);
  900. eRamp_running(&gFoc_Ctrl.in.cruiseRpmRamp);
  901. PMSM_FOC_VelCtrl_Decide();
  902. PMSM_FOC_idqCalc();
  903. }
  904. void PMSM_FOC_Change_VelLoop_Params(float wcv, float b0) {
  905. #ifdef CONFIG_SPEED_LADRC
  906. ladrc_change_b0(&gFoc_Ctrl.vel_adrc, b0);
  907. ladrc_change_K(&gFoc_Ctrl.vel_adrc, wcv);
  908. #else
  909. PI_Controller_Change_Kpi(&gFoc_Ctrl.pi_vel, wcv, b0);
  910. #endif
  911. }
  912. void PMSM_FOC_Change_TrqLoop_Params(float wcv, float b0) {
  913. #ifdef CONFIG_SPEED_LADRC
  914. ladrc_change_b0(&gFoc_Ctrl.vel_lim_adrc, b0);
  915. ladrc_change_K(&gFoc_Ctrl.vel_lim_adrc, wcv);
  916. #else
  917. PI_Controller_Change_Kpi(&gFoc_Ctrl.pi_vel_lim, wcv, b0);
  918. #endif
  919. }
  920. float PMSM_FOC_Get_Real_dqVector(void) {
  921. if (gFoc_Ctrl.out.s_RealCurrentFiltered == 0) {
  922. gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
  923. }
  924. return gFoc_Ctrl.out.s_RealCurrentFiltered;
  925. }
  926. PMSM_FOC_Ctrl *PMSM_FOC_Get(void) {
  927. return &gFoc_Ctrl;
  928. }
  929. void PMSM_FOC_Start(u8 nCtrlMode) {
  930. if (gFoc_Ctrl.in.b_motEnable) {
  931. return;
  932. }
  933. PMSM_FOC_CoreInit();
  934. eCtrl_Reset();
  935. gFoc_Ctrl.in.n_ctlMode = nCtrlMode;
  936. gFoc_Ctrl.in.b_motEnable = true;
  937. }
  938. void PMSM_FOC_Stop(void) {
  939. if (!gFoc_Ctrl.in.b_motEnable) {
  940. return;
  941. }
  942. PMSM_FOC_CoreInit();
  943. gFoc_Ctrl.in.b_motEnable = false;
  944. }
  945. bool PMSM_FOC_Is_Start(void) {
  946. return gFoc_Ctrl.in.b_motEnable;
  947. }
  948. void PMSM_FOC_DCCurrLimit(float ibusLimit) {
  949. if (ibusLimit > gFoc_Ctrl.hwLim.s_iDCMax) {
  950. ibusLimit = gFoc_Ctrl.hwLim.s_iDCMax;
  951. }
  952. if (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE) {
  953. ibusLimit = min(ibusLimit, gFoc_Ctrl.protLim.s_iDCLim);
  954. }
  955. gFoc_Ctrl.userLim.s_iDCLim = ibusLimit;
  956. if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
  957. eRamp_reset_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit);
  958. }else {
  959. eRamp_set_step_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit, CONFIG_eCTRL_STEP_TS);
  960. }
  961. }
  962. float PMSM_FOC_GetDCCurrLimit(void) {
  963. return gFoc_Ctrl.userLim.s_iDCLim;
  964. }
  965. void PMSM_FOC_SpeedRampLimit(float speedLimit, float speed) {
  966. if (speedLimit > gFoc_Ctrl.hwLim.s_motRPMMax) {
  967. speedLimit = gFoc_Ctrl.hwLim.s_motRPMMax;
  968. }
  969. gFoc_Ctrl.userLim.s_motRPMLim = (speedLimit);
  970. if (ABS(speed) <= CONFIG_ZERO_SPEED_RPM) {
  971. eRamp_reset_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit);
  972. }else {
  973. eRamp_set_step_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS);
  974. }
  975. }
  976. void PMSM_FOC_SpeedLimit(float speedLimit) {
  977. PMSM_FOC_SpeedRampLimit(speedLimit, gFoc_Ctrl.in.s_motVelocity);
  978. }
  979. void PMSM_FOC_SpeedDirectLimit(float limit) {
  980. PMSM_FOC_SpeedRampLimit(limit, 0);
  981. }
  982. float PMSM_FOC_GetSpeedLimit(void) {
  983. return gFoc_Ctrl.userLim.s_motRPMLim;
  984. }
  985. void PMSM_FOC_TorqueLimit(float torqueLimit) {
  986. if (torqueLimit > gFoc_Ctrl.hwLim.s_torqueMax) {
  987. torqueLimit = gFoc_Ctrl.hwLim.s_torqueMax;
  988. }
  989. if (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE) {
  990. torqueLimit = min(torqueLimit, gFoc_Ctrl.protLim.s_TorqueLim);
  991. }
  992. gFoc_Ctrl.userLim.s_torqueLim = torqueLimit;
  993. if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
  994. eRamp_reset_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit);
  995. }else {
  996. eRamp_set_step_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit, CONFIG_eCTRL_STEP_TS);
  997. }
  998. }
  999. float PMSM_FOC_GetTorqueLimit(void) {
  1000. return gFoc_Ctrl.userLim.s_torqueLim;
  1001. }
  1002. void PMSM_FOC_SetEbrkTorque(s16 torque) {
  1003. gFoc_Ctrl.userLim.s_TorqueBrkLim = (float)torque;
  1004. //gFoc_Ctrl.userLim.s_iDCeBrkLim = fclamp(dc_curr, 0, nv_get_foc_params()->s_iDCeBrkLim);
  1005. }
  1006. float PMSM_FOC_GetEbrkTorque(void) {
  1007. if (!foc_observer_is_encoder()) {
  1008. return 0; //无感运行关闭能量回收
  1009. }
  1010. return gFoc_Ctrl.userLim.s_TorqueBrkLim;
  1011. }
  1012. float PMSM_FOC_GetVbusVoltage(void) {
  1013. return gFoc_Ctrl.in.s_vDC;
  1014. }
  1015. float PMSM_FOC_GetVbusCurrent(void) {
  1016. return gFoc_Ctrl.out.s_FilteriDC;
  1017. }
  1018. DQ_t* PMSM_FOC_GetDQCurrent(void) {
  1019. return &gFoc_Ctrl.out.s_RealIdq;
  1020. }
  1021. bool PMSM_FOC_SetCtrlMode(u8 mode) {
  1022. if (mode > CTRL_MODE_EBRAKE) {
  1023. PMSM_FOC_SetErrCode(FOC_Param_Err);
  1024. return false;
  1025. }
  1026. gFoc_Ctrl.in.n_ctlMode = mode;
  1027. return true;
  1028. }
  1029. u8 PMSM_FOC_GetCtrlMode(void) {
  1030. return gFoc_Ctrl.in.n_ctlMode;
  1031. }
  1032. void PMSM_FOC_PhaseCurrLim(float lim) {
  1033. if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
  1034. lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
  1035. }
  1036. gFoc_Ctrl.userLim.s_PhaseCurrLim = lim;
  1037. }
  1038. void PMSM_FOC_RT_PhaseCurrLim(float lim) {
  1039. if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
  1040. lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
  1041. }
  1042. eRamp_init_target2(&gFoc_Ctrl.rtLim.torqueLimRamp, lim, CONFIG_LIMIT_RAMP_TIME);
  1043. }
  1044. float PMSM_FOC_GetPhaseCurrLim(void) {
  1045. return gFoc_Ctrl.userLim.s_PhaseCurrLim;
  1046. }
  1047. void PMSM_FOC_SetOpenVdq(float vd, float vq) {
  1048. FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[0], vd);
  1049. FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[1], vq);
  1050. }
  1051. bool PMSM_FOC_EnableCruise(bool enable) {
  1052. if (enable != gFoc_Ctrl.in.b_cruiseEna) {
  1053. float motSpd = PMSM_FOC_GetSpeed();
  1054. if (enable && (motSpd < CONFIG_MIN_CRUISE_RPM)) { //
  1055. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  1056. return false;
  1057. }
  1058. eRamp_reset_target(&gFoc_Ctrl.in.cruiseRpmRamp, motSpd);
  1059. gFoc_Ctrl.in.s_cruiseRPM = motSpd;
  1060. gFoc_Ctrl.in.b_cruiseEna = enable;
  1061. }
  1062. return true;
  1063. }
  1064. bool PMSM_FOC_PauseCruise(void) {
  1065. gFoc_Ctrl.in.b_cruiseEna = false;
  1066. return true;
  1067. }
  1068. bool PMSM_FOC_ResumeCruise(void) {
  1069. gFoc_Ctrl.in.b_cruiseEna = true;
  1070. eRamp_init_target2(&gFoc_Ctrl.in.cruiseRpmRamp, PMSM_FOC_GetSpeed(), CONFIG_CRUISE_RAMP_TIME);
  1071. eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
  1072. return true;
  1073. }
  1074. bool PMSM_FOC_Is_CruiseEnabled(void) {
  1075. return (gFoc_Ctrl.in.b_cruiseEna && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD));
  1076. }
  1077. bool PMSM_FOC_Set_Speed(float rpm) {
  1078. if (gFoc_Ctrl.in.b_cruiseEna) {
  1079. return false;
  1080. }
  1081. eCtrl_set_TgtSpeed(min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm));
  1082. return true;
  1083. }
  1084. bool PMSM_FOC_Set_Current(float is) {
  1085. if (is > gFoc_Ctrl.userLim.s_PhaseCurrLim) {
  1086. is = gFoc_Ctrl.userLim.s_PhaseCurrLim;
  1087. }else if (is < -gFoc_Ctrl.userLim.s_PhaseCurrLim) {
  1088. is = -gFoc_Ctrl.userLim.s_PhaseCurrLim;
  1089. }
  1090. eCtrl_set_TgtCurrent(is);
  1091. return true;
  1092. }
  1093. bool PMSM_FOC_Set_Torque(float trq) {
  1094. if (trq > gFoc_Ctrl.userLim.s_torqueLim) {
  1095. trq = gFoc_Ctrl.userLim.s_torqueLim;
  1096. }else if (trq < -gFoc_Ctrl.userLim.s_torqueLim) {
  1097. trq = -gFoc_Ctrl.userLim.s_torqueLim;
  1098. }
  1099. eCtrl_set_TgtTorque(trq);
  1100. return true;
  1101. }
  1102. void PMSM_FOC_Reset_Torque(void) {
  1103. float real_trq = PMSM_FOC_Get_Real_dqVector();
  1104. eCtrl_reset_Torque(real_trq);
  1105. }
  1106. bool PMSM_FOC_Set_CruiseSpeed(float rpm) {
  1107. if (PMSM_FOC_Is_CruiseEnabled()) {
  1108. if (rpm < CONFIG_MIN_CRUISE_RPM) {
  1109. rpm = CONFIG_MIN_CRUISE_RPM;
  1110. }
  1111. gFoc_Ctrl.in.s_cruiseRPM = min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm);
  1112. eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
  1113. return true;
  1114. }
  1115. PMSM_FOC_SetErrCode(FOC_NotCruiseMode);
  1116. return false;
  1117. }
  1118. void PMSM_FOC_MTPA_Calibrate(bool enable) {
  1119. if (enable) {
  1120. gFoc_Ctrl.in.b_MTPA_calibrate = true;
  1121. gFoc_Ctrl.in.s_dqAngle = 0;
  1122. }else {
  1123. gFoc_Ctrl.in.s_dqAngle = INVALID_ANGLE;
  1124. gFoc_Ctrl.in.b_MTPA_calibrate = false;
  1125. }
  1126. }
  1127. void PMSM_FOC_Set_MotAngle(float angle) {
  1128. gFoc_Ctrl.in.s_manualAngle = (angle);
  1129. }
  1130. void PMSM_FOC_Set_Dq_Angle(float angle) {
  1131. gFoc_Ctrl.in.s_dqAngle = (angle);
  1132. }
  1133. void PMSM_FOC_Get_TgtIDQ(DQ_t * dq) {
  1134. dq->d = gFoc_Ctrl.in.s_targetIdq.d;
  1135. dq->q = gFoc_Ctrl.in.s_targetIdq.q;
  1136. }
  1137. float PMSM_FOC_GetSpeed(void) {
  1138. float speed = gFoc_Ctrl.in.s_motVelocity;
  1139. if (!gFoc_Ctrl.in.b_motEnable || foc_observer_is_encoder()) {
  1140. speed = motor_encoder_get_speed();
  1141. }else {
  1142. if (foc_observer_sensorless_stable()) {
  1143. speed = foc_observer_sensorless_speed();
  1144. }else {
  1145. speed = 0;
  1146. }
  1147. }
  1148. return speed;
  1149. }
  1150. void PMSM_FOC_AutoHold(bool lock) {
  1151. if (gFoc_Ctrl.in.b_AutoHold != lock) {
  1152. motor_encoder_lock_pos(lock);
  1153. PI_Controller_Reset(&gFoc_Ctrl.pi_lock, 0);
  1154. if (!lock) {
  1155. float hold_torque = gFoc_Ctrl.in.s_targetTorque * 1.1f;
  1156. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
  1157. #ifdef CONFIG_SPEED_LADRC
  1158. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, 0, hold_torque);
  1159. #else
  1160. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, hold_torque);
  1161. #endif
  1162. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) {
  1163. #ifdef CONFIG_SPEED_LADRC
  1164. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, hold_torque);
  1165. #else
  1166. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, hold_torque);
  1167. #endif
  1168. }
  1169. eCtrl_reset_Torque(hold_torque);
  1170. gFoc_Ctrl.out.f_autohold_trq = hold_torque;
  1171. }else {
  1172. gFoc_Ctrl.out.f_autohold_trq = 0;
  1173. }
  1174. gFoc_Ctrl.in.b_AutoHold = lock;
  1175. }
  1176. }
  1177. bool PMSM_FOC_AutoHoldding(void) {
  1178. return gFoc_Ctrl.in.b_AutoHold;
  1179. }
  1180. static PI_Controller *_pid(u8 id) {
  1181. PI_Controller *pi = NULL;
  1182. if (id == PID_D_id) {
  1183. pi = &gFoc_Ctrl.pi_id;
  1184. }else if (id == PID_Q_id) {
  1185. pi = &gFoc_Ctrl.pi_iq;
  1186. }else if (id == PID_TRQ_id) {
  1187. #ifndef CONFIG_SPEED_LADRC
  1188. pi = &gFoc_Ctrl.pi_vel_lim;
  1189. #endif
  1190. }else if (id == PID_Spd_id) {
  1191. #ifndef CONFIG_SPEED_LADRC
  1192. pi = &gFoc_Ctrl.pi_vel;
  1193. #endif
  1194. }
  1195. return pi;
  1196. }
  1197. void PMSM_FOC_SetPid(u8 id, float kp, float ki, float kd) {
  1198. if (id > PID_Max_id) {
  1199. return;
  1200. }
  1201. PI_Controller *pi = _pid(id);
  1202. if (pi != NULL) {
  1203. pi->kp = kp;
  1204. pi->ki = ki;
  1205. pi->kd = kd;
  1206. }
  1207. }
  1208. void PMSM_FOC_GetPid(u8 id, float *kp, float *ki, float *kd) {
  1209. if (id > PID_Max_id) {
  1210. return;
  1211. }
  1212. PI_Controller *pi = _pid(id);
  1213. if (pi != NULL) {
  1214. *kp = pi->kp;
  1215. *ki = pi->ki;
  1216. *kd = pi->kd;
  1217. }
  1218. }
  1219. void PMSM_FOC_SetErrCode(u8 error) {
  1220. if (gFoc_Ctrl.out.n_Error != error) {
  1221. gFoc_Ctrl.out.n_Error = error;
  1222. }
  1223. }
  1224. u8 PMSM_FOC_GetErrCode(void) {
  1225. return gFoc_Ctrl.out.n_Error;
  1226. }
  1227. void PMSM_FOC_Set_PlotType(Plot_t t) {
  1228. gFoc_Ctrl.plot_type = t;
  1229. }
  1230. //获取母线电流和实际输出电流矢量大小
  1231. void PMSM_FOC_Calc_Current(void) {
  1232. float vd = gFoc_Ctrl.out.s_OutVdq.d;
  1233. float vq = gFoc_Ctrl.out.s_OutVdq.q;
  1234. float id = gFoc_Ctrl.out.s_FilterIdq.d;
  1235. float iq = gFoc_Ctrl.out.s_FilterIdq.q;
  1236. /*
  1237. 根据公式(等幅值变换,功率不等):
  1238. iDC x vDC = 2/3(iq x vq + id x vd);
  1239. */
  1240. float m_pow = (vd * id + vq * iq); //s32q10
  1241. float raw_idc = m_pow / get_vbus_float();// * 1.5f * 0.66f; //s16q5
  1242. LowPass_Filter(gFoc_Ctrl.out.s_CalciDC, raw_idc, 0.1f);
  1243. raw_idc = get_vbus_current();
  1244. LowPass_Filter(gFoc_Ctrl.out.s_FilteriDC, raw_idc, 0.05f);
  1245. gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
  1246. }
  1247. void PMSM_FOC_Brake(bool brake) {
  1248. gFoc_Ctrl.in.b_eBrake = brake;
  1249. if (gFoc_Ctrl.in.b_eBrake & gFoc_Ctrl.in.b_cruiseEna) {
  1250. gFoc_Ctrl.in.b_cruiseEna = false;
  1251. }
  1252. eCtrl_brake_signal(brake);
  1253. }