| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471 |
- #include "arm_math.h"
- #include "PMSM_FOC_Core.h"
- #include "foc/foc_config.h"
- #include "foc/motor/motor_param.h"
- #include "foc/core/e_ctrl.h"
- #include "math/fix_math.h"
- #include "math/fast_math.h"
- #include "foc/motor/current.h"
- #include "foc/motor/motor.h"
- #include "foc/core/svpwm.h"
- #include "foc/core/thro_torque.h"
- #include "foc/core/foc_observer.h"
- #include "foc/core/F_Calc.h"
- #include "foc/samples.h"
- #include "foc/limit.h"
- #include "foc/mc_error.h"
- #include "app/nv_storage.h"
- #include "bsp/bsp_driver.h"
- #include "libs/logger.h"
- #include "math/fir.h"
- #define _DEBUG(fmt, args...) sys_debug(fmt, ##args)
- PMSM_FOC_Ctrl gFoc_Ctrl;
- static bool g_focinit = false;
- static u32 PMSM_FOC_Debug_Task(void *p);
- static __INLINE void RevPark(DQ_t *dq, float angle, AB_t *alpha_beta) {
- float c,s;
- #if 0
- SinCos_Lut(angle, &s, &c);
- #else
- s = gFoc_Ctrl.out.sin;
- c = gFoc_Ctrl.out.cos;
- #endif
- alpha_beta->a = dq->d * c - dq->q * s;
- alpha_beta->b = dq->d * s + dq->q * c;
- }
- static __INLINE void RevClark(AB_t *alpha_beta, float *ABC){
- ABC[0] = alpha_beta->a;
- ABC[1] = -alpha_beta->a * 0.5f + alpha_beta->b * SQRT3_BY_2;
- ABC[2] = -alpha_beta->a * 0.5f - alpha_beta->b * SQRT3_BY_2;
- }
- static __INLINE void Clark(float A, float B, float C, AB_t *alpha_beta){
- alpha_beta->a = A;
- alpha_beta->b = ONE_BY_SQRT3 * (B - C);
- }
- static __INLINE void Park(AB_t *alpha_beta, float angle, DQ_t *dq) {
- float c,s;
- #if 0
- SinCos_Lut(angle, &s, &c);
- #else
- s = gFoc_Ctrl.out.sin;
- c = gFoc_Ctrl.out.cos;
- #endif
- dq->d = alpha_beta->a * c + alpha_beta->b * s;
- dq->q = -alpha_beta->a * s + alpha_beta->b * c;
- }
- void PMSM_FOC_ABC2Dq(float a, float b, float c, float *d, float *q) {
- AB_t ab;
- DQ_t dq;
- Clark(a, b, c, &ab);
- Park(&ab, 0, &dq);
- *d = dq.d;
- *q = dq.q;
- }
- #if 0
- #define VD_PRIO_HIGH
- static __INLINE float Circle_Limitation(DQ_t *vdq, float vDC, float module, DQ_t *out) {
- float sq_vdq = vdq->d * vdq->d + vdq->q * vdq->q;
- float vDC_m = vDC * module * SQRT3_BY_2;
- float sq_vDC = vDC_m * vDC_m;
- if (sq_vdq > sq_vDC) {
- #ifdef VD_PRIO_HIGH
- out->d = vdq->d;
- out->q = sqrtf(sq_vDC - out->d*out->d);
- #else
- float r = sqrtf(sq_vDC / sq_vdq);
- out->d = vdq->d * r;
- out->q = vdq->q * r;
- #endif
- }else {
- out->d = vdq->d;
- out->q = vdq->q;
- }
- return sqrtf(sq_vdq/sq_vDC);
- }
- #endif
- static __INLINE void FOC_Set_DqRamp(dq_Rctrl *c, float target, int time) {
- float cp = c->s_Cp;
- c->s_FinalTgt = target;
- c->s_Step = (c->s_FinalTgt - cp) / (float)time;
- }
- static __INLINE float FOC_Get_DqRamp(dq_Rctrl *c) {
- if (++c->n_StepCount == c->n_CtrlCount) {
- c->s_Cp += c->s_Step;
- if (c->s_Step < 0) {
- if (c->s_Cp < c->s_FinalTgt) {
- c->s_Cp = c->s_FinalTgt;
- }
- }else {
- if (c->s_Cp > c->s_FinalTgt) {
- c->s_Cp = c->s_FinalTgt;
- }
- }
- c->n_StepCount = 0;
- }
- return c->s_Cp;
- }
- static __INLINE void FOC_DqRamp_init(dq_Rctrl *c, int count) {
- c->n_CtrlCount = count;
- c->n_StepCount = 0;
- c->s_Cp = 0;
- c->s_FinalTgt = 0;
- c->s_Step = 0;
- }
- static __INLINE void FOC_Set_iDqRamp(dq_Rctrl *c, float target) {
- FOC_Set_DqRamp(c, target, (/*CONFIG_IDQ_CTRL_TS/CONFIG_SPD_CTRL_TS - 1*/CURRENT_LOOP_RAMP_COUNT));
- }
- static __INLINE void FOC_Set_vDqRamp(dq_Rctrl *c, float target) {
- FOC_Set_DqRamp(c, target, (CONFIG_FOC_VDQ_RAMP_FINAL_TIME/1000*((CONFIG_IDQ_CTRL_TS/CONFIG_FOC_VDQ_RAMP_TS))));
- }
- static void PMSM_FOC_Reset_PID(void) {
- PI_Controller_Reset(&gFoc_Ctrl.pi_id, 0);
- PI_Controller_Reset(&gFoc_Ctrl.pi_iq, 0);
- PI_Controller_Reset(&gFoc_Ctrl.pi_lock, 0);
- PI_Controller_Reset(&gFoc_Ctrl.pi_power, 0);
- #ifdef CONFIG_SPEED_LADRC
- ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, 0, 0);
- ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, 0);
- #else
- PI_Controller_Reset(&gFoc_Ctrl.pi_vel, 0);
- PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, 0);
- #endif
- }
- static void PMSM_FOC_Conf_PID(void) {
- float slow_ctrl_ts = (1.0f/(float)CONFIG_SPD_CTRL_TS);
- gFoc_Ctrl.pi_id.kp = nv_get_foc_params()->pid_conf[PID_D_id].kp;
- gFoc_Ctrl.pi_id.ki = nv_get_foc_params()->pid_conf[PID_D_id].ki;
- gFoc_Ctrl.pi_id.kd = nv_get_foc_params()->pid_conf[PID_D_id].kd;
- gFoc_Ctrl.pi_id.DT = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
- gFoc_Ctrl.pi_iq.kp = nv_get_foc_params()->pid_conf[PID_Q_id].kp;
- gFoc_Ctrl.pi_iq.ki = nv_get_foc_params()->pid_conf[PID_Q_id].ki;
- gFoc_Ctrl.pi_iq.kd = nv_get_foc_params()->pid_conf[PID_Q_id].kd;
- gFoc_Ctrl.pi_iq.DT = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
- gFoc_Ctrl.pi_power.kp = nv_get_foc_params()->pid_conf[PID_Pow_id].kp;
- gFoc_Ctrl.pi_power.ki = nv_get_foc_params()->pid_conf[PID_Pow_id].ki;
- gFoc_Ctrl.pi_power.kd = nv_get_foc_params()->pid_conf[PID_Pow_id].kd;
- gFoc_Ctrl.pi_power.DT = slow_ctrl_ts;
- gFoc_Ctrl.pi_lock.kp = nv_get_foc_params()->pid_conf[PID_Lock_id].kp;
- gFoc_Ctrl.pi_lock.ki = nv_get_foc_params()->pid_conf[PID_Lock_id].ki;
- gFoc_Ctrl.pi_lock.kd = nv_get_foc_params()->pid_conf[PID_Lock_id].kd;
- gFoc_Ctrl.pi_lock.DT = slow_ctrl_ts;
- #ifdef CONFIG_SPEED_LADRC
- 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);
- 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);
- #else
- gFoc_Ctrl.pi_vel_lim.kp = nv_get_foc_params()->pid_conf[PID_TRQ_id].kp;
- gFoc_Ctrl.pi_vel_lim.ki = nv_get_foc_params()->pid_conf[PID_TRQ_id].ki;
- gFoc_Ctrl.pi_vel_lim.kd = nv_get_foc_params()->pid_conf[PID_TRQ_id].kd;
- gFoc_Ctrl.pi_vel_lim.DT = slow_ctrl_ts;
- gFoc_Ctrl.pi_vel.kp = nv_get_foc_params()->pid_conf[PID_Spd_id].kp;
- gFoc_Ctrl.pi_vel.ki = nv_get_foc_params()->pid_conf[PID_Spd_id].ki;
- gFoc_Ctrl.pi_vel.kd = nv_get_foc_params()->pid_conf[PID_Spd_id].kd;
- gFoc_Ctrl.pi_vel.DT = slow_ctrl_ts;
- #endif
- }
- static void PMSM_FOC_UserInit(void) {
- memset(&gFoc_Ctrl.userLim, 0, sizeof(gFoc_Ctrl.userLim));
- gFoc_Ctrl.userLim.s_iDCLim = min(nv_get_foc_params()->s_LimitiDC, gFoc_Ctrl.hwLim.s_iDCMax);
- gFoc_Ctrl.userLim.s_motRPMLim = min(nv_get_foc_params()->s_maxRPM, gFoc_Ctrl.hwLim.s_motRPMMax);
- gFoc_Ctrl.userLim.s_torqueLim = nv_get_foc_params()->s_maxTorque;//MAX_TORQUE;
- gFoc_Ctrl.userLim.s_PhaseCurrLim = min(nv_get_foc_params()->s_PhaseCurrLim, gFoc_Ctrl.hwLim.s_PhaseCurrMax);
- gFoc_Ctrl.userLim.s_vDCMaxLim = nv_get_foc_params()->s_maxDCVol;
- gFoc_Ctrl.userLim.s_vDCMinLim = nv_get_foc_params()->s_minDCVol;
- gFoc_Ctrl.userLim.s_iDCeBrkLim = nv_get_foc_params()->s_iDCeBrkLim;
- gFoc_Ctrl.userLim.s_PhaseVoleBrkLim = gFoc_Ctrl.hwLim.s_PhaseVolMax;
- }
- void PMSM_FOC_RT_LimInit(void) {
- gFoc_Ctrl.protLim.s_iDCLim = HW_LIMIT_NONE;
- gFoc_Ctrl.protLim.s_TorqueLim = HW_LIMIT_NONE;
- eRamp_init_target2(&gFoc_Ctrl.rtLim.rpmLimRamp, gFoc_Ctrl.userLim.s_motRPMLim, CONFIG_LIMIT_RAMP_TIME);
- eRamp_init_target2(&gFoc_Ctrl.rtLim.torqueLimRamp, gFoc_Ctrl.userLim.s_torqueLim, CONFIG_LIMIT_RAMP_TIME);
- eRamp_init_target2(&gFoc_Ctrl.rtLim.DCCurrLimRamp, gFoc_Ctrl.userLim.s_iDCLim, CONFIG_LIMIT_RAMP_TIME);
- }
- void PMSM_FOC_CoreInit(void) {
- PMSM_FOC_Conf_PID();
-
- memset(&gFoc_Ctrl.in, 0, sizeof(FOC_InP));
- memset(&gFoc_Ctrl.out, 0, sizeof(FOC_OutP));
-
- gFoc_Ctrl.hwLim.s_iDCMax = CONFIG_MAX_VBUS_CURRENT;
- gFoc_Ctrl.hwLim.s_motRPMMax = CONFIG_MAX_MOT_RPM;
- gFoc_Ctrl.hwLim.s_PhaseCurrMax = CONFIG_MAX_PHASE_CURR;
- gFoc_Ctrl.hwLim.s_PhaseVolMax = CONFIG_MAX_PHASE_VOL;
- gFoc_Ctrl.hwLim.s_vDCMax = CONFIG_MAX_DC_VOL;
- gFoc_Ctrl.hwLim.s_torqueMax = CONFIG_MAX_MOTOR_TORQUE;
- gFoc_Ctrl.hwLim.s_FWDCurrMax = CONFIG_MAX_FW_D_CURR;
- if (!g_focinit) {
- PMSM_FOC_UserInit();
- PMSM_FOC_RT_LimInit();
- shark_task_create(PMSM_FOC_Debug_Task, NULL);
- g_focinit = true;
- //_DEBUG("User Limit:\n");
- //_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,
- // gFoc_Ctrl.userLim.s_PhaseCurrLim, gFoc_Ctrl.userLim.s_vDCMaxLim, gFoc_Ctrl.userLim.s_vDCMinLim, gFoc_Ctrl.userLim.s_TorqueBrkLim);
- //_DEBUG("Hw Limit:\n");
- //_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);
- }
- gFoc_Ctrl.userLim.s_TorqueBrkLim = mc_get_ebrk_torque();
- gFoc_Ctrl.params.n_modulation = CONFIG_SVM_MODULATION;//SVM_Modulation;
- gFoc_Ctrl.params.n_PhaseFilterCeof = CONFIG_CURR_LP_CEOF;
- gFoc_Ctrl.params.n_poles = nv_get_motor_params()->poles;//MOTOR_POLES;
- gFoc_Ctrl.params.lq = nv_get_motor_params()->lq;
- gFoc_Ctrl.params.ld = nv_get_motor_params()->ld;
- gFoc_Ctrl.params.flux = nv_get_motor_params()->flux_linkage;
- gFoc_Ctrl.in.s_manualAngle = INVALID_ANGLE;
- gFoc_Ctrl.in.s_dqAngle = INVALID_ANGLE;
- gFoc_Ctrl.in.b_fwEnable = nv_get_foc_params()->n_FwEnable;
- gFoc_Ctrl.in.s_vDC = nv_get_foc_params()->s_maxDCVol;//(CONFIG_RATED_DC_VOL);
- gFoc_Ctrl.in.s_angleLast = INVALID_ANGLE;
- gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
- gFoc_Ctrl.out.f_vdqRation = 0;
- eRamp_init_target2(&gFoc_Ctrl.in.cruiseRpmRamp, 0, CONFIG_CRUISE_RAMP_TIME);
- FOC_DqRamp_init(&gFoc_Ctrl.idq_ctl[0], 1);
- FOC_DqRamp_init(&gFoc_Ctrl.idq_ctl[1], 1);
- FOC_DqRamp_init(&gFoc_Ctrl.vdq_ctl[0], (CONFIG_FOC_VDQ_RAMP_TS));
- FOC_DqRamp_init(&gFoc_Ctrl.vdq_ctl[1], (CONFIG_FOC_VDQ_RAMP_TS));
- PMSM_FOC_Reset_PID();
- foc_observer_init();
- gFoc_Ctrl.plot_type = Plot_None;
- }
- /* 通过三相电流重构母线电流,和单电阻采样正好相反,原理一致 */
- static __INLINE void PMSM_FOC_Calc_iDC_Fast(void) {
- #if 0
- float deadtime = (float)(NS_2_TCLK(PWM_DEAD_TIME_NS + HW_DEAD_TIME_NS))/(float)FOC_PWM_Half_Period;
- float duty_pu[3];
- duty_pu[0] = (float)gFoc_Ctrl.out.n_Duty[0] / (float)FOC_PWM_Half_Period;
- duty_pu[1] = (float)gFoc_Ctrl.out.n_Duty[1] / (float)FOC_PWM_Half_Period;
- duty_pu[2] = (float)gFoc_Ctrl.out.n_Duty[2] / (float)FOC_PWM_Half_Period;
-
- float *iABC = gFoc_Ctrl.in.s_iABC;
- float iDC;
- if ((duty_pu[0] >= duty_pu[1]) && (duty_pu[1] >= duty_pu[2])) {
- 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);
- if (iABC[0] < 0) {
- iDC = iDC + iABC[0] * deadtime;
- }
- if (iABC[1] > 0) {
- iDC = iDC + iABC[0] * deadtime;
- }else {
- iDC = iDC + (iABC[0] + iABC[1]) * deadtime;
- }
- if (iABC[2] > 0) {
- iDC = iDC + (iABC[0] + iABC[1]) * deadtime;
- }
- }else if ((duty_pu[0] >= duty_pu[2]) && (duty_pu[2] >= duty_pu[1])) {
- 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);
- if (iABC[0] < 0) {
- iDC = iDC + iABC[0] * deadtime;
- }
- if (iABC[2] > 0) {
- iDC = iDC + iABC[0] * deadtime;
- }else {
- iDC = iDC + (iABC[0] + iABC[2]) * deadtime;
- }
- if (iABC[1] > 0) {
- iDC = iDC + (iABC[0] + iABC[2]) * deadtime;
- }
- }else if ((duty_pu[1] >= duty_pu[0]) && (duty_pu[0] >= duty_pu[2])) {
- 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);
- if (iABC[1] < 0) {
- iDC = iDC + iABC[1] * deadtime;
- }
- if (iABC[0] > 0) {
- iDC = iDC + iABC[1] * deadtime;
- }else {
- iDC = iDC + (iABC[1] + iABC[0]) * deadtime;
- }
- if (iABC[2] > 0) {
- iDC = iDC + (iABC[1] + iABC[0]) * deadtime;
- }
- }else if ((duty_pu[1] >= duty_pu[2]) && (duty_pu[2] >= duty_pu[0])) {
- 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);
- if (iABC[1] < 0) {
- iDC = iDC + iABC[1] * deadtime;
- }
- if (iABC[2] > 0) {
- iDC = iDC + iABC[1] * deadtime;
- }else {
- iDC = iDC + (iABC[1] + iABC[2]) * deadtime;
- }
- if (iABC[0] > 0) {
- iDC = iDC + (iABC[1] + iABC[2]) * deadtime;
- }
- }else if ((duty_pu[2] >= duty_pu[0]) && (duty_pu[0] >= duty_pu[1])) {
- 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);
- if (iABC[2] < 0) {
- iDC = iDC + iABC[2] * deadtime;
- }
- if (iABC[0] > 0) {
- iDC = iDC + iABC[2] * deadtime;
- }else {
- iDC = iDC + (iABC[2] + iABC[0]) * deadtime;
- }
- if (iABC[1] > 0) {
- iDC = iDC + (iABC[2] + iABC[0]) * deadtime;
- }
- }else { // duty_pu[2] >= duty_pu[1] && duty_pu[1] >= duty_pu[0]
- 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);
- if (iABC[2] < 0) {
- iDC = iDC + iABC[2] * deadtime;
- }
- if (iABC[1] > 0) {
- iDC = iDC + iABC[2] * deadtime;
- }else {
- iDC = iDC + (iABC[2] + iABC[1]) * deadtime;
- }
- if (iABC[0] > 0) {
- iDC = iDC + (iABC[2] + iABC[1]) * deadtime;
- }
- }
- LowPass_Filter(gFoc_Ctrl.out.s_CalciDC2, iDC, 0.005f);
- #endif
- }
- #define CONFIG_PEAK_CNT 3 //计算经过的电周期内的最大值(peak 峰值)
- #define CONFIG_PHASE_UNBALANCE_THROLD 4.0F
- #define CONFIG_PHASE_UNBALANCE_R 0.1F
- static float phase_unbalance_r = 0.0f;
- static float phase_a_max, phase_b_max, phase_c_max;
- static u32 phase_unbalance_cnt;
- static __INLINE void PMSM_FOC_Phase_Unbalance(void) {
- static u32 _cycle_cnt = 0, _last_mod_cnt = 0;
- static float a_max = 0, b_max = 0, c_max = 0;
- static u32 _unbalance_cnt = 0;
- static u32 _unbalance_time = 0;
- float lowpass = gFoc_Ctrl.in.s_motVelRadusPers * FOC_CTRL_US / 2.0f;
- if (lowpass > 1.0f) {
- lowpass = 1.0f;
- }
- LowPass_Filter(gFoc_Ctrl.in.s_iABCFilter[0], gFoc_Ctrl.in.s_iABC[0], lowpass);
- LowPass_Filter(gFoc_Ctrl.in.s_iABCFilter[1], gFoc_Ctrl.in.s_iABC[1], lowpass);
- gFoc_Ctrl.in.s_iABCFilter[2] = -(gFoc_Ctrl.in.s_iABCFilter[0] + gFoc_Ctrl.in.s_iABCFilter[1]);
- if ((gFoc_Ctrl.in.s_angleLast == INVALID_ANGLE) || (gFoc_Ctrl.in.s_motVelRadusPers < 100)) {
- gFoc_Ctrl.in.s_angleLast = gFoc_Ctrl.in.s_motAngle;
- a_max = b_max = c_max = 0;
- _unbalance_cnt = 0;
- _unbalance_time = get_tick_ms();
- _cycle_cnt = 0;
- _last_mod_cnt = 0;
- phase_unbalance_r = 0;
- return;
- }
- float delta_angle = gFoc_Ctrl.in.s_motAngle - gFoc_Ctrl.in.s_angleLast;
- if (delta_angle > 200 || delta_angle < -200) { //one cycle
- _cycle_cnt ++;
- }
- gFoc_Ctrl.in.s_angleLast = gFoc_Ctrl.in.s_motAngle;
- u32 mod_cnt = _cycle_cnt % CONFIG_PEAK_CNT;
- bool trigger = false;
- if ((mod_cnt == 0) && (_last_mod_cnt != mod_cnt)) {
- trigger = true;
- }
- _last_mod_cnt = mod_cnt;
- a_max = MAX(a_max, gFoc_Ctrl.in.s_iABCFilter[0] * (2.2f));
- b_max = MAX(b_max, gFoc_Ctrl.in.s_iABCFilter[1] * (2.2f));
- c_max = MAX(c_max, gFoc_Ctrl.in.s_iABCFilter[2] * (2.2f));
- if (trigger) { //经过CONFIG_PEAK_CNT个周期,已经得到peak值
- float i_min = 1000.0f, i_max = 0;
- if (a_max > i_max) {
- i_max = a_max;
- }
- if (a_max < i_min) {
- i_min = a_max;
- }
- if (b_max > i_max) {
- i_max = b_max;
- }
- if (b_max < i_min) {
- i_min = b_max;
- }
- if (c_max > i_max) {
- i_max = c_max;
- }
- if (c_max < i_min) {
- i_min = c_max;
- }
- float unbalance_r = (i_max - i_min - CONFIG_PHASE_UNBALANCE_THROLD)/(i_max + 1e-8f);
- if (unbalance_r >= CONFIG_PHASE_UNBALANCE_R) {
- if ((_unbalance_cnt++ >= 500) || (get_delta_ms(_unbalance_time) >= 1000*10)) {
- if (mc_set_critical_error(FOC_CRIT_PHASE_UNBalance_Err)) {
- mc_crit_err_add(FOC_CRIT_PHASE_UNBalance_Err, (s16)i_max, (s16)i_min);
- }
- }
- }else {
- _unbalance_cnt = 0;
- _unbalance_time = get_tick_ms();
- }
- phase_unbalance_r = unbalance_r;
- phase_a_max = a_max;
- phase_b_max = b_max;
- phase_c_max = c_max;
- phase_unbalance_cnt = _unbalance_cnt;
- a_max = b_max = c_max = 0;
- }
- }
- /* 死区补偿 */
- static __INLINE void PMSM_FOC_DeadTime_Compensate(s32 PWM_Half_Period) {
- #ifdef CONFIG_START_LINE_DTC_CURRENT
- float deadTime = (float)CONFIG_HW_DeadTime/2.0f;
- s32 dutyDTCA = 0;
- s32 dutyDTCB = 0;
- s32 dutyDTCC = 0;
- float r, delta;
- float iabs = ABS(gFoc_Ctrl.in.s_iABC_DT[0]);
- if (iabs > CONFIG_START_LINE_DTC_CURRENT) {
- delta = iabs - CONFIG_START_LINE_DTC_CURRENT;
- r = delta / (COMFIG_END_LINE_DTC_CURRENT - CONFIG_START_LINE_DTC_CURRENT);
- if (r > 1.0f) {
- r = 1.0f;
- }
- if (gFoc_Ctrl.in.s_iABC_DT[0] < 0) {
- r = -r;
- }
- dutyDTCA = (s32)(r * deadTime);
- }
- iabs = ABS(gFoc_Ctrl.in.s_iABC_DT[1]);
- if (iabs > CONFIG_START_LINE_DTC_CURRENT) {
- delta = iabs - CONFIG_START_LINE_DTC_CURRENT;
- r = delta / (COMFIG_END_LINE_DTC_CURRENT - CONFIG_START_LINE_DTC_CURRENT);
- if (r > 1.0f) {
- r = 1.0f;
- }
- if (gFoc_Ctrl.in.s_iABC_DT[1] < 0) {
- r = -r;
- }
- dutyDTCB = (s32)(r * deadTime);
- }
- iabs = ABS(gFoc_Ctrl.in.s_iABC_DT[2]);
- if (iabs > CONFIG_START_LINE_DTC_CURRENT) {
- delta = iabs - CONFIG_START_LINE_DTC_CURRENT;
- r = delta / (COMFIG_END_LINE_DTC_CURRENT - CONFIG_START_LINE_DTC_CURRENT);
- if (r > 1.0f) {
- r = 1.0f;
- }
- if (gFoc_Ctrl.in.s_iABC_DT[2] < 0) {
- r = -r;
- }
- dutyDTCC = (s32)(r * deadTime);
- }
- s32 dutyA = (s32)gFoc_Ctrl.out.n_Duty[0] + dutyDTCA;
- s32 dutyB = (s32)gFoc_Ctrl.out.n_Duty[1] + dutyDTCB;
- s32 dutyC = (s32)gFoc_Ctrl.out.n_Duty[2] + dutyDTCC;
- gFoc_Ctrl.out.n_Duty[0] = sclamp(dutyA, 0, PWM_Half_Period);
- gFoc_Ctrl.out.n_Duty[1] = sclamp(dutyB, 0, PWM_Half_Period);
- gFoc_Ctrl.out.n_Duty[2] = sclamp(dutyC, 0, PWM_Half_Period);
- #endif
- }
- //#define UPDATE_Lq_By_iq /* Q轴电感 通过Iq电流补偿 */
- #define CONFIG_Volvec_Delay_Comp /* 电压矢量角度补偿 */
- #define CONFIG_Volvec_Delay_Comp_Start_Vel 500 // rpm
- static float encoder_angle,obser_angle, obser_vel = 111111;
- static __INLINE bool PMSM_FOC_Update_Input(void) {
- AB_t iAB;
- float *iabc = gFoc_Ctrl.in.s_iABC;
- phase_current_get(iabc);
- PMSM_FOC_Calc_iDC_Fast();
- Clark(iabc[0], iabc[1], iabc[2], &iAB);
- 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);
- float enc_angle = motor_encoder_get_angle();
- float enc_vel = motor_encoder_get_speed();
- if (!foc_observer_diagnostic(enc_angle, enc_vel)){
- /* detect encoder angle error, do something here */
- if (!foc_observer_sensorless_stable()) {
- gFoc_Ctrl.in.s_motVelocity = 0;
- return false;
- }
- if (obser_vel == 111111) {
- obser_vel = foc_observer_sensorless_speed();
- obser_angle = foc_observer_sensorless_angle();
- encoder_angle = enc_angle;
- }
- enc_angle = foc_observer_sensorless_angle();
- enc_vel = foc_observer_sensorless_speed();
- }
-
- if (!gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_manualAngle != INVALID_ANGLE)) {
- gFoc_Ctrl.in.s_motAngle = gFoc_Ctrl.in.s_manualAngle;
- }else {
- gFoc_Ctrl.in.s_motAngle = enc_angle;
- }
- gFoc_Ctrl.in.s_motVelocity = enc_vel;
- LowPass_Filter(gFoc_Ctrl.in.s_motVelocityFiltered, gFoc_Ctrl.in.s_motVelocity, 0.01f);
- gFoc_Ctrl.in.s_motVelRadusPers = gFoc_Ctrl.in.s_motVelocityFiltered / 30.0f * PI * gFoc_Ctrl.params.n_poles;
- PMSM_FOC_Phase_Unbalance();
- #ifdef CONFIG_DQ_STEP_RESPONSE
- gFoc_Ctrl.in.s_motAngle = 0;
- #endif
- gFoc_Ctrl.in.s_vDC = get_vbus_float();
-
- get_phase_vols(gFoc_Ctrl.in.s_vABC);
- SinCos_Lut(gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
- Park(&iAB, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_RealIdq);
- float lowpass = gFoc_Ctrl.in.s_motVelRadusPers * FOC_CTRL_US * 2.0f;
- if (lowpass > 1.0f) {
- lowpass = 1.0f;
- }else if (lowpass <= 0.0001f) {
- lowpass = 0.001f;
- }
- LowPass_Filter(gFoc_Ctrl.out.s_FilterIdq.d, gFoc_Ctrl.out.s_RealIdq.d, lowpass);
- LowPass_Filter(gFoc_Ctrl.out.s_FilterIdq.q, gFoc_Ctrl.out.s_RealIdq.q, lowpass);
- /* 使用低通后的dq电流重新变换得到abc电流,给死区补偿使用 */
- RevPark(&gFoc_Ctrl.out.s_FilterIdq, gFoc_Ctrl.in.s_motAngle, &iAB);
- RevClark(&iAB, gFoc_Ctrl.in.s_iABC_DT);
- #ifdef CONFIG_START_LINE_DTC_CURRENT
- gFoc_Ctrl.out.s_OutVdqDTC.d = 0;
- gFoc_Ctrl.out.s_OutVdqDTC.q = 0;
- #else
- AB_t vAB;
- vAB.a = (1.0f / 3.0f) * (2.0f * SIGN(gFoc_Ctrl.in.s_iABC_DT[0]) - SIGN(gFoc_Ctrl.in.s_iABC_DT[1]) - SIGN(gFoc_Ctrl.in.s_iABC_DT[2]));
- vAB.b = ONE_BY_SQRT3 * (SIGN(gFoc_Ctrl.in.s_iABC_DT[1]) - SIGN(gFoc_Ctrl.in.s_iABC_DT[2]));
- float dtc = ((float)CONFIG_HW_DeadTime/(float)FOC_PWM_Half_Period) * gFoc_Ctrl.in.s_vDC;
- vAB.a = vAB.a * dtc;
- vAB.b = vAB.b * dtc;
- Park(&vAB, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_OutVdqDTC); //used for vbus current calc
- #endif
- #ifdef CONFIG_Volvec_Delay_Comp
- if (gFoc_Ctrl.in.s_motVelocityFiltered >= CONFIG_Volvec_Delay_Comp_Start_Vel) {
- float next_angle = gFoc_Ctrl.in.s_motAngle + gFoc_Ctrl.in.s_motVelRadusPers / PI * 180.0f * (FOC_CTRL_US * 0.8f);
- rand_angle(next_angle);
- SinCos_Lut(next_angle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
- }
- #endif
- return true;
- }
- #ifdef CONFIG_DQ_STEP_RESPONSE
- float target_d = 0.0f;
- float target_q = 0.0f;
- #endif
- static u32 PMSM_FOC_Debug_Task(void *p) {
- if (gFoc_Ctrl.in.b_motEnable) {
- #ifdef CONFIG_DQ_STEP_RESPONSE
- if (gFoc_Ctrl.plot_type == Plot_D_Step) {
- plot_2data16(FtoS16x10(target_d), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d));
- }if (gFoc_Ctrl.plot_type == Plot_Q_Step) {
- plot_2data16(FtoS16x10(target_q), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q));
- }
- #else
- if (gFoc_Ctrl.plot_type == Plot_D_flow) {
- plot_2data16(FtoS16x10(gFoc_Ctrl.idq_ctl[0].s_Cp), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d));
- }else if (gFoc_Ctrl.plot_type == Plot_Q_flow) {
- plot_2data16(FtoS16x10(gFoc_Ctrl.idq_ctl[1].s_Cp), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q));
- }else if (gFoc_Ctrl.plot_type == Plot_DQ_Curr) {
- plot_3data16(FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q), FtoS16x10(gFoc_Ctrl.out.s_FilteriDC));
- }else if (gFoc_Ctrl.plot_type == Plot_Spd_flow) {
- plot_2data16(gFoc_Ctrl.in.s_targetRPM, gFoc_Ctrl.in.s_motVelocity);
- }
- #endif
- }
- return 1;
- }
- static __INLINE float id_feedforward(float eW) {
- #ifdef CONFIG_CURRENT_LOOP_DECOUPE
- return -(gFoc_Ctrl.params.lq * gFoc_Ctrl.out.s_RealIdq.q * eW);
- #else
- return 0;
- #endif
- }
- static __INLINE float iq_feedforward(float eW) {
- #ifdef CONFIG_CURRENT_LOOP_DECOUPE
- return (gFoc_Ctrl.params.ld * gFoc_Ctrl.out.s_RealIdq.d + gFoc_Ctrl.params.flux) * eW;
- #else
- return 0;
- #endif
- }
- bool PMSM_FOC_Schedule(void) {
- gFoc_Ctrl.ctrl_count++;
- if (!PMSM_FOC_Update_Input()){
- return false;
- }
-
- if (gFoc_Ctrl.out.n_RunMode != CTRL_MODE_OPEN) {
- float max_Vdc = gFoc_Ctrl.in.s_vDC * CONFIG_SVM_MODULATION;
- float max_vd = max_Vdc * SQRT3_BY_2;
- /* limiter Vd output for PI controller */
- gFoc_Ctrl.pi_id.max = max_vd;
- gFoc_Ctrl.pi_id.min = -max_vd;
- #ifndef CONFIG_DQ_STEP_RESPONSE
- float target_d = FOC_Get_DqRamp(&gFoc_Ctrl.idq_ctl[0]);
- #endif
- float err = target_d - gFoc_Ctrl.out.s_RealIdq.d;
- float id_ff = id_feedforward(gFoc_Ctrl.in.s_motVelRadusPers);
- gFoc_Ctrl.in.s_targetVdq.d = PI_Controller_Current(&gFoc_Ctrl.pi_id, err, id_ff);
- #ifdef UPDATE_Lq_By_iq
- /* update kp&ki from lq for iq PI controller */
- float lq = motor_get_lq_from_iq((s16)gFoc_Ctrl.out.s_FilterIdq.q);
- LowPass_Filter(gFoc_Ctrl.params.lq, lq, 0.01f);
- gFoc_Ctrl.pi_iq.kp = ((float)nv_get_foc_params()->n_currentBand * gFoc_Ctrl.params.lq);
- gFoc_Ctrl.pi_iq.ki = (nv_get_motor_params()->r/gFoc_Ctrl.params.lq);
- #endif
- /* limiter Vq output for PI controller */
- float max_vq = sqrtf(SQ(max_vd) - SQ(gFoc_Ctrl.in.s_targetVdq.d));
- gFoc_Ctrl.pi_iq.max = max_vq;
- gFoc_Ctrl.pi_iq.min = -max_vq;
- #ifndef CONFIG_DQ_STEP_RESPONSE
- float target_q = FOC_Get_DqRamp(&gFoc_Ctrl.idq_ctl[1]);
- #endif
- err = target_q - gFoc_Ctrl.out.s_RealIdq.q;
- float iq_ff = iq_feedforward(gFoc_Ctrl.in.s_motVelRadusPers);
- gFoc_Ctrl.in.s_targetVdq.q = PI_Controller_Current(&gFoc_Ctrl.pi_iq, err, iq_ff);
- }else {
- float max_Vdc = gFoc_Ctrl.in.s_vDC * CONFIG_SVM_MODULATION;
- float max_vd = max_Vdc * SQRT3_BY_2;
- float vd_ref = FOC_Get_DqRamp(&gFoc_Ctrl.vdq_ctl[0]);
- gFoc_Ctrl.in.s_targetVdq.d = fclamp(vd_ref, -max_vd, max_vd);
-
- float max_vq = sqrtf(SQ(max_vd) - SQ(gFoc_Ctrl.in.s_targetVdq.d));
- float vq_ref = FOC_Get_DqRamp(&gFoc_Ctrl.vdq_ctl[1]);
- gFoc_Ctrl.in.s_targetVdq.q = fclamp(vq_ref, -max_vq, max_vq);
- }
- #if 0
- 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);
- gFoc_Ctrl.out.s_OutVdq.d *= SQRT3_BY_2;
- gFoc_Ctrl.out.s_OutVdq.q *= SQRT3_BY_2;
- #else
- gFoc_Ctrl.out.s_OutVdq.d = gFoc_Ctrl.in.s_targetVdq.d;
- gFoc_Ctrl.out.s_OutVdq.q = gFoc_Ctrl.in.s_targetVdq.q;
- #endif
- RevPark(&gFoc_Ctrl.out.s_OutVdq, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_OutVAB);
-
- SVM_Duty_Fix(&gFoc_Ctrl.out.s_OutVAB, gFoc_Ctrl.in.s_vDC, FOC_PWM_Half_Period, &gFoc_Ctrl.out);
- PMSM_FOC_DeadTime_Compensate((s32)FOC_PWM_Half_Period);
- phase_current_point(&gFoc_Ctrl.out);
-
- pwm_update_duty(gFoc_Ctrl.out.n_Duty[0], gFoc_Ctrl.out.n_Duty[1], gFoc_Ctrl.out.n_Duty[2]);
- pwm_update_sample(gFoc_Ctrl.out.n_Sample1, gFoc_Ctrl.out.n_Sample2, gFoc_Ctrl.out.n_CPhases);
- return true;
- }
- void PMSM_FOC_LogDebug(void) {
- sys_debug("DC curr %f --- %f, - %f\n", gFoc_Ctrl.out.s_CalciDC, gFoc_Ctrl.out.s_FilteriDC, gFoc_Ctrl.userLim.s_iDCLim);
- sys_debug("%s\n", gFoc_Ctrl.out.empty_load?"NoLoad Running":"Load Runing");
- sys_debug("unbalance: %d, %f, %f, %f, %f\n", phase_unbalance_cnt, phase_unbalance_r, phase_a_max, phase_b_max, phase_c_max);
- if (obser_vel != 111111) {
- sys_debug("AB error: %f,%f,%f\n", obser_angle, encoder_angle, obser_vel);
- }
- }
- /*called in media task */
- u8 PMSM_FOC_CtrlMode(void) {
- u8 preMode = gFoc_Ctrl.out.n_RunMode;
- if (!gFoc_Ctrl.in.b_motEnable) {
- gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
- }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_OPEN) {
- gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
- }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_SPD || gFoc_Ctrl.in.b_cruiseEna){
- gFoc_Ctrl.out.n_RunMode = CTRL_MODE_SPD;
- }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_CURRENT) {
- gFoc_Ctrl.out.n_RunMode = CTRL_MODE_CURRENT;
- }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_EBRAKE) {
- gFoc_Ctrl.out.n_RunMode = CTRL_MODE_EBRAKE;
- }else {
- if (!gFoc_Ctrl.in.b_cruiseEna) {
- gFoc_Ctrl.out.n_RunMode = CTRL_MODE_TRQ;
- }
- }
- if (preMode != gFoc_Ctrl.out.n_RunMode) {
- if ((preMode == CTRL_MODE_SPD) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ)) {
- #ifdef CONFIG_SPEED_LADRC
- //ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
- ladrc_copy(&gFoc_Ctrl.vel_lim_adrc, &gFoc_Ctrl.vel_adrc);
- #else
- PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, gFoc_Ctrl.in.s_targetTorque);
- #endif
- }else if ((preMode == CTRL_MODE_TRQ) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD)) {
- #ifdef CONFIG_SPEED_LADRC
- ladrc_copy(&gFoc_Ctrl.vel_adrc, &gFoc_Ctrl.vel_lim_adrc);
- #else
- PI_Controller_Reset(&gFoc_Ctrl.pi_vel, gFoc_Ctrl.in.s_targetTorque);
- #endif
- }else if ((preMode == CTRL_MODE_CURRENT) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ)) {
- #ifdef CONFIG_SPEED_LADRC
- ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
- #else
- PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, gFoc_Ctrl.in.s_targetTorque);
- #endif
- }else if ((preMode != gFoc_Ctrl.out.n_RunMode) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
- eCtrl_reset_Torque(gFoc_Ctrl.in.s_targetTorque);
- eCtrl_set_TgtTorque(motor_get_ebreak_toruqe(gFoc_Ctrl.in.s_motVelocity));
- }else if ((preMode == CTRL_MODE_EBRAKE) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD)) {
- #ifdef CONFIG_SPEED_LADRC
- ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, F_get_air());
- #else
- PI_Controller_Reset(&gFoc_Ctrl.pi_vel, F_get_air());
- #endif
- }
- }
-
- return gFoc_Ctrl.out.n_RunMode;
- }
- #define RAMPE_1 CONFIG_RAMP_FIRST_TARGET
- static void crosszero_step_towards(float *value, float target) {
- static float no_cro_step = 2.0f;
- float v_now = *value;
- bool cross_zero = false;
- float high_ramp_torque = CONFIG_RAMP_SECOND_TARGET;
- if (target > 0) {
- if (v_now < -RAMPE_1) {
- step_towards(value, -RAMPE_1, 2.0f);
- cross_zero = true;
- }else if (v_now >= -RAMPE_1 && v_now <= high_ramp_torque) {
- step_towards(value, target, 0.03f);
- cross_zero = true;
- }
- }else if (target == 0) {
- if (v_now > high_ramp_torque) {
- step_towards(value, high_ramp_torque, 1.0f);
- cross_zero = true;
- }else if (v_now >= RAMPE_1 && v_now <= high_ramp_torque) {
- step_towards(value, target, 0.03f);
- cross_zero = true;
- }
- }else {
- if (v_now > high_ramp_torque) {
- step_towards(value, high_ramp_torque, 20.0f);
- cross_zero = true;
- }else if (v_now >= -RAMPE_1 && v_now <= high_ramp_torque) {
- step_towards(value, target, 0.03f);
- cross_zero = true;
- }
- }
- if (!cross_zero) {
- step_towards(&no_cro_step, 3.0f, 0.1f);
- step_towards(value, target, no_cro_step);
- }else {
- no_cro_step = 0.5f;
- }
- }
- /* MPTA, 弱磁, 功率限制,主要是分配DQ轴电流 */
- #define CHANGE_MAX_CNT 3
- static __INLINE void PMSM_FOC_VelCtrl_Decide(void) {
- #if 0
- static int change_cnt = 0;
- static bool change_done = false;
- static u32 change_time = 0xFFFFFFFF;
- float f_te = F_get_Te();
- float f_accl = F_get_accl();
- if (mc_is_epm()) {
- change_cnt = 0;
- change_time = 0xFFFFFFFF;
- change_done = false;
- gFoc_Ctrl.out.empty_load = false;
- return;
- }
- if (gFoc_Ctrl.in.s_motVelocity == 0.0f || gFoc_Ctrl.out.n_RunMode == CTRL_MODE_OPEN) {
- change_cnt = 0;
- change_time = 0xFFFFFFFF;
- change_done = false;
- gFoc_Ctrl.out.empty_load = false;
- return;
- }
- if (f_te <= 0.0f) {
- change_cnt = 0;
- change_time = 0xFFFFFFFF;
- return;
- }
- if (change_done) {
- /* 误判空转,发现电机给定的N大于空气阻力,说明不是空转 */
- if (gFoc_Ctrl.out.empty_load) {
- float f_air = F_get_air();
- if ((f_accl > 1.0f) && (f_te >= (f_air + f_accl))) {
- change_cnt ++;
- }else {
- change_cnt = 0;
- }
- if (change_cnt >= 500) {
- gFoc_Ctrl.out.empty_load = false;
- #ifdef CONFIG_SPEED_LADRC
- PMSM_FOC_Change_TrqLoop_Params(nv_get_foc_params()->f_adrc_vel_lim_Wcv, nv_get_foc_params()->f_adrc_vel_lim_B0);
- PMSM_FOC_Change_VelLoop_Params(nv_get_foc_params()->f_adrc_vel_Wcv, nv_get_foc_params()->f_adrc_vel_B0);
- #endif
- }
- }
- return;
- }
- if (change_time == 0xFFFFFFFF) {
- change_time = get_tick_ms();
- }else { //起步3s内检测是否空转
- if (get_delta_ms(change_time) > 3000) {
- return;
- }
- }
- if ((f_accl > 200.0f) && (f_accl/f_te > 3.0f )) {
- change_cnt++;
- }else if ((F_get_MotAccl() >= 10.0f) && (f_accl/f_te > 1.2f )) {
- change_cnt = CHANGE_MAX_CNT;
- }
- else {
- if ((f_te > 50) && (f_accl > 0) && (f_te > f_accl)) {
- change_cnt --;
- }else {
- change_cnt = 0;
- }
- }
- if (!change_done && (change_cnt >= CHANGE_MAX_CNT)) {
- change_done = true;
- change_cnt = 0;
- gFoc_Ctrl.out.empty_load = change_done;
- #ifdef CONFIG_SPEED_LADRC
- PMSM_FOC_Change_TrqLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
- PMSM_FOC_Change_VelLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
- #endif
- }else if (!change_done && (change_cnt <= -200)) {
- change_done = true;
- change_cnt = 0;
- gFoc_Ctrl.out.empty_load = false;
- }
- #endif
- }
- static __INLINE float PMSM_FOC_Limit_iDC(float maxTrq) {
- #if 1
- gFoc_Ctrl.pi_power.max = maxTrq;
- float errRef = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.DCCurrLimRamp) - (gFoc_Ctrl.out.s_FilteriDC);
- return PI_Controller_Run(&gFoc_Ctrl.pi_power, errRef);
- #else
- return maxTrq;
- #endif
- }
- static __INLINE float PMSM_FOC_Limit_Speed(float maxTrq) {
- #ifdef CONFIG_SPEED_LADRC
- float lim = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp);
- ladrc_set_range(&gFoc_Ctrl.vel_lim_adrc, 0, maxTrq);
- return ladrc_run(&gFoc_Ctrl.vel_lim_adrc, lim, gFoc_Ctrl.in.s_motVelocity);
- #else
- gFoc_Ctrl.pi_vel_lim.max = maxTrq;
- gFoc_Ctrl.pi_vel_lim.min = 0;
- float err = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp) - gFoc_Ctrl.in.s_motVelocity;
- return PI_Controller_RunVel(&gFoc_Ctrl.pi_vel_lim, err);
- #endif
- }
- static __INLINE void PMSM_FOC_idq_Assign(void) {
- if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
- if (gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_dqAngle != INVALID_ANGLE)) {
- float s, c;
- normal_sincosf(degree_2_pi(gFoc_Ctrl.in.s_dqAngle + 90.0f), &s, &c);
- gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.in.s_targetCurrent * c;
-
- if (gFoc_Ctrl.in.s_targetIdq.d > gFoc_Ctrl.hwLim.s_FWDCurrMax) {
- gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.hwLim.s_FWDCurrMax;
- }else if (gFoc_Ctrl.in.s_targetIdq.d < -gFoc_Ctrl.hwLim.s_FWDCurrMax) {
- gFoc_Ctrl.in.s_targetIdq.d = -gFoc_Ctrl.hwLim.s_FWDCurrMax;
- }
- gFoc_Ctrl.in.s_targetIdq.q = sqrtf(SQ(gFoc_Ctrl.in.s_targetCurrent) - SQ(gFoc_Ctrl.in.s_targetIdq.d));
- if (s < 0) {
- gFoc_Ctrl.in.s_targetIdq.q = -gFoc_Ctrl.in.s_targetIdq.q;
- }
- }else {
- gFoc_Ctrl.in.s_targetIdq.d = 0;
- gFoc_Ctrl.in.s_targetIdq.q = gFoc_Ctrl.in.s_targetCurrent;
- }
- }else if ((gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) || (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) ||
- (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
- motor_mpta_fw_lookup(gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque, &gFoc_Ctrl.in.s_targetIdq);
- }
- u32 mask = cpu_enter_critical();
- FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[0], gFoc_Ctrl.in.s_targetIdq.d);
- FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[1], gFoc_Ctrl.in.s_targetIdq.q);
- cpu_exit_critical(mask);
- }
- /*called in media task */
- void PMSM_FOC_idqCalc(void) {
- if (gFoc_Ctrl.in.b_AutoHold) {
- gFoc_Ctrl.pi_lock.max = CONFIG_MAX_LOCK_TORQUE;
- gFoc_Ctrl.pi_lock.min = -CONFIG_MAX_LOCK_TORQUE;
- float vel_count = motor_encoder_get_vel_count();
- float errRef = 0 - vel_count;
- gFoc_Ctrl.in.s_targetTorque = PI_Controller_Run(&gFoc_Ctrl.pi_lock ,errRef);
- PMSM_FOC_idq_Assign();
- return;
- }
- if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
- gFoc_Ctrl.in.s_targetCurrent = eCtrl_get_RefCurrent();
- }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE) {
- float maxTrq = eCtrl_get_RefTorque();
- if (eCtrl_get_FinalTorque() < 0.0001f && gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE) {
- maxTrq = 0;
- }
- crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
- }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
- float refTorque = min(eCtrl_get_RefTorque(), eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
- float maxTrq = PMSM_FOC_Limit_Speed(refTorque);
- maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
- crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
- }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD){
- float maxSpeed = eCtrl_get_FinalSpeed();
- float refSpeed = eCtrl_get_RefSpeed();
- if (gFoc_Ctrl.in.b_cruiseEna) {
- maxSpeed = eRamp_get_target(&gFoc_Ctrl.in.cruiseRpmRamp);
- refSpeed = eRamp_get_intepolation(&gFoc_Ctrl.in.cruiseRpmRamp);//gFoc_Ctrl.in.s_cruiseRPM;
- }
- #ifdef CONFIG_SPEED_LADRC
- if (maxSpeed >= 0) {
- ladrc_set_range(&gFoc_Ctrl.vel_adrc, -CONFIG_MAX_NEG_TORQUE, eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
- }else if (maxSpeed < 0) {
- ladrc_set_range(&gFoc_Ctrl.vel_adrc, -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp), CONFIG_MAX_NEG_TORQUE);
- }
- if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
- ladrc_set_range(&gFoc_Ctrl.vel_adrc, 0, 0);
- }
- gFoc_Ctrl.in.s_targetRPM = refSpeed;
- float maxTrq = ladrc_run(&gFoc_Ctrl.vel_adrc, refSpeed, gFoc_Ctrl.in.s_motVelocity);
- #else
- if (maxSpeed >= 0) {
- gFoc_Ctrl.pi_vel.max = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);
- gFoc_Ctrl.pi_vel.min = -CONFIG_MAX_NEG_TORQUE;
- }else if (maxSpeed < 0) {
- gFoc_Ctrl.pi_vel.min = -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);
- gFoc_Ctrl.pi_vel.max = CONFIG_MAX_NEG_TORQUE;
- }
- if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
- gFoc_Ctrl.pi_vel.max = 0;
- gFoc_Ctrl.pi_vel.min = 0; //防止倒转
- }
- gFoc_Ctrl.in.s_targetRPM = refSpeed;
- float errRef = refSpeed - gFoc_Ctrl.in.s_motVelocity;
- float maxTrq = PI_Controller_RunVel(&gFoc_Ctrl.pi_vel, errRef);
- #endif
- maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
- crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
- }
- PMSM_FOC_idq_Assign();
- }
- u8 PMSM_FOC_RunTime_Limit(void) {
- u8 changed = FOC_LIM_NO_CHANGE;
- float dc_lim = (float)vbus_under_vol_limit();
- float torque_lim = (float)min(mos_temp_high_limit(), motor_temp_high_limit());
- if (gFoc_Ctrl.protLim.s_iDCLim != dc_lim || gFoc_Ctrl.protLim.s_TorqueLim != torque_lim) {
- if ((dc_lim > gFoc_Ctrl.protLim.s_iDCLim) || (torque_lim > gFoc_Ctrl.protLim.s_TorqueLim)) {
- changed = FOC_LIM_CHANGE_H;
- }else {
- changed = FOC_LIM_CHANGE_L;
- }
- gFoc_Ctrl.protLim.s_iDCLim = dc_lim;
- gFoc_Ctrl.protLim.s_TorqueLim = torque_lim;
- }
- return changed;
- }
- bool PMSM_FOC_iDC_is_Limited(void) {
- return (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE);
- }
- bool PMSM_FOC_Torque_is_Limited(void) {
- return (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE);
- }
- void PMSM_FOC_Slow_Task(void) {
- eRamp_running(&gFoc_Ctrl.rtLim.torqueLimRamp);
- eRamp_running(&gFoc_Ctrl.rtLim.DCCurrLimRamp);
- eRamp_running(&gFoc_Ctrl.rtLim.rpmLimRamp);
- eRamp_running(&gFoc_Ctrl.in.cruiseRpmRamp);
- PMSM_FOC_VelCtrl_Decide();
- PMSM_FOC_idqCalc();
- }
- void PMSM_FOC_Change_VelLoop_Params(float wcv, float b0) {
- #ifdef CONFIG_SPEED_LADRC
- ladrc_change_b0(&gFoc_Ctrl.vel_adrc, b0);
- ladrc_change_K(&gFoc_Ctrl.vel_adrc, wcv);
- #else
- PI_Controller_Change_Kpi(&gFoc_Ctrl.pi_vel, wcv, b0);
- #endif
- }
- void PMSM_FOC_Change_TrqLoop_Params(float wcv, float b0) {
- #ifdef CONFIG_SPEED_LADRC
- ladrc_change_b0(&gFoc_Ctrl.vel_lim_adrc, b0);
- ladrc_change_K(&gFoc_Ctrl.vel_lim_adrc, wcv);
- #else
- PI_Controller_Change_Kpi(&gFoc_Ctrl.pi_vel_lim, wcv, b0);
- #endif
- }
- float PMSM_FOC_Get_Real_dqVector(void) {
- if (gFoc_Ctrl.out.s_RealCurrentFiltered == 0) {
- gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
- }
- return gFoc_Ctrl.out.s_RealCurrentFiltered;
- }
- PMSM_FOC_Ctrl *PMSM_FOC_Get(void) {
- return &gFoc_Ctrl;
- }
- void PMSM_FOC_Start(u8 nCtrlMode) {
- if (gFoc_Ctrl.in.b_motEnable) {
- return;
- }
- PMSM_FOC_CoreInit();
- eCtrl_Reset();
- gFoc_Ctrl.in.n_ctlMode = nCtrlMode;
- gFoc_Ctrl.in.b_motEnable = true;
- }
- void PMSM_FOC_Stop(void) {
- if (!gFoc_Ctrl.in.b_motEnable) {
- return;
- }
- PMSM_FOC_CoreInit();
- gFoc_Ctrl.in.b_motEnable = false;
- }
- bool PMSM_FOC_Is_Start(void) {
- return gFoc_Ctrl.in.b_motEnable;
- }
- void PMSM_FOC_DCCurrLimit(float ibusLimit) {
- if (ibusLimit > gFoc_Ctrl.hwLim.s_iDCMax) {
- ibusLimit = gFoc_Ctrl.hwLim.s_iDCMax;
- }
- if (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE) {
- ibusLimit = min(ibusLimit, gFoc_Ctrl.protLim.s_iDCLim);
- }
- gFoc_Ctrl.userLim.s_iDCLim = ibusLimit;
- if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
- eRamp_reset_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit);
- }else {
- eRamp_set_step_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit, CONFIG_eCTRL_STEP_TS);
- }
- }
- float PMSM_FOC_GetDCCurrLimit(void) {
- return gFoc_Ctrl.userLim.s_iDCLim;
- }
- void PMSM_FOC_SpeedRampLimit(float speedLimit, float speed) {
- if (speedLimit > gFoc_Ctrl.hwLim.s_motRPMMax) {
- speedLimit = gFoc_Ctrl.hwLim.s_motRPMMax;
- }
- gFoc_Ctrl.userLim.s_motRPMLim = (speedLimit);
- if (ABS(speed) <= CONFIG_ZERO_SPEED_RPM) {
- eRamp_reset_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit);
- }else {
- eRamp_set_step_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS);
- }
- }
- void PMSM_FOC_SpeedLimit(float speedLimit) {
- PMSM_FOC_SpeedRampLimit(speedLimit, gFoc_Ctrl.in.s_motVelocity);
- }
- void PMSM_FOC_SpeedDirectLimit(float limit) {
- PMSM_FOC_SpeedRampLimit(limit, 0);
- }
- float PMSM_FOC_GetSpeedLimit(void) {
- return gFoc_Ctrl.userLim.s_motRPMLim;
- }
- void PMSM_FOC_TorqueLimit(float torqueLimit) {
- if (torqueLimit > gFoc_Ctrl.hwLim.s_torqueMax) {
- torqueLimit = gFoc_Ctrl.hwLim.s_torqueMax;
- }
- if (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE) {
- torqueLimit = min(torqueLimit, gFoc_Ctrl.protLim.s_TorqueLim);
- }
- gFoc_Ctrl.userLim.s_torqueLim = torqueLimit;
- if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
- eRamp_reset_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit);
- }else {
- eRamp_set_step_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit, CONFIG_eCTRL_STEP_TS);
- }
- }
- float PMSM_FOC_GetTorqueLimit(void) {
- return gFoc_Ctrl.userLim.s_torqueLim;
- }
- void PMSM_FOC_SetEbrkTorque(s16 torque) {
- gFoc_Ctrl.userLim.s_TorqueBrkLim = (float)torque;
- //gFoc_Ctrl.userLim.s_iDCeBrkLim = fclamp(dc_curr, 0, nv_get_foc_params()->s_iDCeBrkLim);
- }
- float PMSM_FOC_GetEbrkTorque(void) {
- if (!foc_observer_is_encoder()) {
- return 0; //无感运行关闭能量回收
- }
- return gFoc_Ctrl.userLim.s_TorqueBrkLim;
- }
- float PMSM_FOC_GetVbusVoltage(void) {
- return gFoc_Ctrl.in.s_vDC;
- }
- float PMSM_FOC_GetVbusCurrent(void) {
- return gFoc_Ctrl.out.s_FilteriDC;
- }
- DQ_t* PMSM_FOC_GetDQCurrent(void) {
- return &gFoc_Ctrl.out.s_RealIdq;
- }
- bool PMSM_FOC_SetCtrlMode(u8 mode) {
- if (mode > CTRL_MODE_EBRAKE) {
- PMSM_FOC_SetErrCode(FOC_Param_Err);
- return false;
- }
- gFoc_Ctrl.in.n_ctlMode = mode;
- return true;
- }
- u8 PMSM_FOC_GetCtrlMode(void) {
- return gFoc_Ctrl.in.n_ctlMode;
- }
- void PMSM_FOC_PhaseCurrLim(float lim) {
- if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
- lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
- }
- gFoc_Ctrl.userLim.s_PhaseCurrLim = lim;
- }
- void PMSM_FOC_RT_PhaseCurrLim(float lim) {
- if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
- lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
- }
- eRamp_init_target2(&gFoc_Ctrl.rtLim.torqueLimRamp, lim, CONFIG_LIMIT_RAMP_TIME);
- }
- float PMSM_FOC_GetPhaseCurrLim(void) {
- return gFoc_Ctrl.userLim.s_PhaseCurrLim;
- }
- void PMSM_FOC_SetOpenVdq(float vd, float vq) {
- FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[0], vd);
- FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[1], vq);
- }
- void PMSM_FOC_SetOpenVdq_Immediate(float vd, float vq) {
- gFoc_Ctrl.vdq_ctl[0].s_Step = 0;
- gFoc_Ctrl.vdq_ctl[0].s_FinalTgt = vd;
- gFoc_Ctrl.vdq_ctl[0].s_Cp = vd;
- gFoc_Ctrl.vdq_ctl[1].s_Step = 0;
- gFoc_Ctrl.vdq_ctl[1].s_FinalTgt = vq;
- gFoc_Ctrl.vdq_ctl[1].s_Cp = vq;
- }
- bool PMSM_FOC_EnableCruise(bool enable) {
- if (enable != gFoc_Ctrl.in.b_cruiseEna) {
- float motSpd = PMSM_FOC_GetSpeed();
- if (enable && (motSpd < CONFIG_MIN_CRUISE_RPM)) { //
- PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
- return false;
- }
- eRamp_reset_target(&gFoc_Ctrl.in.cruiseRpmRamp, motSpd);
- gFoc_Ctrl.in.s_cruiseRPM = motSpd;
- gFoc_Ctrl.in.b_cruiseEna = enable;
- }
-
- return true;
- }
- bool PMSM_FOC_PauseCruise(void) {
- gFoc_Ctrl.in.b_cruiseEna = false;
- return true;
- }
- bool PMSM_FOC_ResumeCruise(void) {
- gFoc_Ctrl.in.b_cruiseEna = true;
- eRamp_init_target2(&gFoc_Ctrl.in.cruiseRpmRamp, PMSM_FOC_GetSpeed(), CONFIG_CRUISE_RAMP_TIME);
- eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
- return true;
- }
- bool PMSM_FOC_Is_CruiseEnabled(void) {
- return (gFoc_Ctrl.in.b_cruiseEna && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD));
- }
- bool PMSM_FOC_Set_Speed(float rpm) {
- if (gFoc_Ctrl.in.b_cruiseEna) {
- return false;
- }
- eCtrl_set_TgtSpeed(min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm));
- return true;
- }
- bool PMSM_FOC_Set_Current(float is) {
- if (is > gFoc_Ctrl.userLim.s_PhaseCurrLim) {
- is = gFoc_Ctrl.userLim.s_PhaseCurrLim;
- }else if (is < -gFoc_Ctrl.userLim.s_PhaseCurrLim) {
- is = -gFoc_Ctrl.userLim.s_PhaseCurrLim;
- }
- eCtrl_set_TgtCurrent(is);
- return true;
- }
- bool PMSM_FOC_Set_Torque(float trq) {
- if (trq > gFoc_Ctrl.userLim.s_torqueLim) {
- trq = gFoc_Ctrl.userLim.s_torqueLim;
- }else if (trq < -gFoc_Ctrl.userLim.s_torqueLim) {
- trq = -gFoc_Ctrl.userLim.s_torqueLim;
- }
- eCtrl_set_TgtTorque(trq);
- return true;
- }
- void PMSM_FOC_Reset_Torque(void) {
- float real_trq = PMSM_FOC_Get_Real_dqVector();
- eCtrl_reset_Torque(real_trq);
- }
- bool PMSM_FOC_Set_CruiseSpeed(float rpm) {
- if (PMSM_FOC_Is_CruiseEnabled()) {
- if (rpm < CONFIG_MIN_CRUISE_RPM) {
- rpm = CONFIG_MIN_CRUISE_RPM;
- }
- gFoc_Ctrl.in.s_cruiseRPM = min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm);
- eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
- return true;
- }
- PMSM_FOC_SetErrCode(FOC_NotCruiseMode);
- return false;
- }
- void PMSM_FOC_MTPA_Calibrate(bool enable) {
- if (enable) {
- gFoc_Ctrl.in.b_MTPA_calibrate = true;
- gFoc_Ctrl.in.s_dqAngle = 0;
- }else {
- gFoc_Ctrl.in.s_dqAngle = INVALID_ANGLE;
- gFoc_Ctrl.in.b_MTPA_calibrate = false;
- }
- }
- void PMSM_FOC_Set_MotAngle(float angle) {
- gFoc_Ctrl.in.s_manualAngle = (angle);
- }
- void PMSM_FOC_Set_Dq_Angle(float angle) {
- gFoc_Ctrl.in.s_dqAngle = (angle);
- }
- void PMSM_FOC_Get_TgtIDQ(DQ_t * dq) {
- dq->d = gFoc_Ctrl.in.s_targetIdq.d;
- dq->q = gFoc_Ctrl.in.s_targetIdq.q;
- }
- float PMSM_FOC_GetSpeed(void) {
- float speed = gFoc_Ctrl.in.s_motVelocity;
- if (!gFoc_Ctrl.in.b_motEnable || foc_observer_is_encoder()) {
- speed = motor_encoder_get_speed();
- }else {
- if (foc_observer_sensorless_stable()) {
- speed = foc_observer_sensorless_speed();
- }else {
- speed = 0;
- }
- }
- return speed;
- }
- void PMSM_FOC_AutoHold(bool lock) {
- if (gFoc_Ctrl.in.b_AutoHold != lock) {
- motor_encoder_lock_pos(lock);
- PI_Controller_Reset(&gFoc_Ctrl.pi_lock, 0);
- if (!lock) {
- float hold_torque = gFoc_Ctrl.in.s_targetTorque * 1.1f;
- if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
- #ifdef CONFIG_SPEED_LADRC
- ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, 0, hold_torque);
- #else
- PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, hold_torque);
- #endif
- }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) {
- #ifdef CONFIG_SPEED_LADRC
- ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, hold_torque);
- #else
- PI_Controller_Reset(&gFoc_Ctrl.pi_vel, hold_torque);
- #endif
- }
- eCtrl_reset_Torque(hold_torque);
- gFoc_Ctrl.out.f_autohold_trq = hold_torque;
- }else {
- gFoc_Ctrl.out.f_autohold_trq = 0;
- }
- gFoc_Ctrl.in.b_AutoHold = lock;
- }
- }
- bool PMSM_FOC_AutoHoldding(void) {
- return gFoc_Ctrl.in.b_AutoHold;
- }
- static PI_Controller *_pid(u8 id) {
- PI_Controller *pi = NULL;
- if (id == PID_D_id) {
- pi = &gFoc_Ctrl.pi_id;
- }else if (id == PID_Q_id) {
- pi = &gFoc_Ctrl.pi_iq;
- }else if (id == PID_TRQ_id) {
- #ifndef CONFIG_SPEED_LADRC
- pi = &gFoc_Ctrl.pi_vel_lim;
- #endif
- }else if (id == PID_Spd_id) {
- #ifndef CONFIG_SPEED_LADRC
- pi = &gFoc_Ctrl.pi_vel;
- #endif
- }
- return pi;
- }
- void PMSM_FOC_SetPid(u8 id, float kp, float ki, float kd) {
- if (id > PID_Max_id) {
- return;
- }
- PI_Controller *pi = _pid(id);
- if (pi != NULL) {
- pi->kp = kp;
- pi->ki = ki;
- pi->kd = kd;
- }
- }
- void PMSM_FOC_GetPid(u8 id, float *kp, float *ki, float *kd) {
- if (id > PID_Max_id) {
- return;
- }
- PI_Controller *pi = _pid(id);
- if (pi != NULL) {
- *kp = pi->kp;
- *ki = pi->ki;
- *kd = pi->kd;
- }
- }
- void PMSM_FOC_SetErrCode(u8 error) {
- if (gFoc_Ctrl.out.n_Error != error) {
- gFoc_Ctrl.out.n_Error = error;
- }
- }
- u8 PMSM_FOC_GetErrCode(void) {
- return gFoc_Ctrl.out.n_Error;
- }
- void PMSM_FOC_Set_PlotType(Plot_t t) {
- gFoc_Ctrl.plot_type = t;
- }
- //获取母线电流和实际输出电流矢量大小
- void PMSM_FOC_Calc_Current(void) {
- float vd = gFoc_Ctrl.out.s_OutVdq.d + gFoc_Ctrl.out.s_OutVdqDTC.d * 0.667f;
- float vq = gFoc_Ctrl.out.s_OutVdq.q + gFoc_Ctrl.out.s_OutVdqDTC.q * 0.667f;
- float id = gFoc_Ctrl.out.s_FilterIdq.d;
- float iq = gFoc_Ctrl.out.s_FilterIdq.q;
- /*
- 根据公式(等幅值变换,功率不等):
- iDC x vDC = 2/3(iq x vq + id x vd);
- */
- float m_pow = (vd * id + vq * iq); //s32q10
- float raw_idc = 0.0f;
- float v_dc = get_vbus_float();
- if (v_dc != 0.0f) {
- raw_idc = m_pow / v_dc;// * 1.5f * 0.66f; //s16q5
- }
- LowPass_Filter(gFoc_Ctrl.out.s_CalciDC, raw_idc, 0.02f);
- #ifdef VBUS_I_CHAN
- raw_idc = get_vbus_current();
- LowPass_Filter(gFoc_Ctrl.out.s_FilteriDC, raw_idc, 0.05f);
- #else
- gFoc_Ctrl.out.s_FilteriDC = gFoc_Ctrl.out.s_CalciDC;
- #endif
- gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
- }
- void PMSM_FOC_Brake(bool brake) {
- gFoc_Ctrl.in.b_eBrake = brake;
- if (gFoc_Ctrl.in.b_eBrake & gFoc_Ctrl.in.b_cruiseEna) {
- gFoc_Ctrl.in.b_cruiseEna = false;
- }
- eCtrl_brake_signal(brake);
- }
|