PMSM_FOC_Core.c 45 KB

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