|
|
@@ -243,9 +243,9 @@ void PMSM_FOC_CoreInit(void) {
|
|
|
}
|
|
|
//#define UPDATE_Lq_By_iq /* Q轴电感 通过Iq电流补偿 */
|
|
|
//#define Volvec_Delay_Comp /* 电压矢量角度补偿 */
|
|
|
-static __INLINE void PMSM_FOC_Update_Input(void) {
|
|
|
+static __INLINE float PMSM_FOC_Update_Input(void) {
|
|
|
AB_t iAB;
|
|
|
-
|
|
|
+ bool count_down = PWM_Direction_Down();
|
|
|
float *iabc = gFoc_Ctrl.in.s_iABC;
|
|
|
|
|
|
phase_current_get(iabc);
|
|
|
@@ -254,16 +254,25 @@ static __INLINE void PMSM_FOC_Update_Input(void) {
|
|
|
|
|
|
foc_observer_update(gFoc_Ctrl.out.s_OutVAB.a * 0.66667f, gFoc_Ctrl.out.s_OutVAB.b * 0.66667f, iAB.a, iAB.b);
|
|
|
|
|
|
+ if (!count_down) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ float enc_angle = motor_encoder_get_angle();
|
|
|
+ float enc_vel = motor_encoder_get_speed();
|
|
|
+ if (!foc_observer_diagnostic(enc_angle, enc_vel)){
|
|
|
+ /* deltect encoder angle error, do something here */
|
|
|
+ }
|
|
|
+
|
|
|
if (!gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_manualAngle != INVALID_ANGLE)) {
|
|
|
gFoc_Ctrl.in.s_motAngle = gFoc_Ctrl.in.s_manualAngle;
|
|
|
- gFoc_Ctrl.in.s_hallAngle = motor_encoder_get_angle();
|
|
|
+ gFoc_Ctrl.in.s_hallAngle = enc_angle;
|
|
|
}else {
|
|
|
- gFoc_Ctrl.in.s_hallAngle = motor_encoder_get_angle();
|
|
|
+ gFoc_Ctrl.in.s_hallAngle = enc_angle;
|
|
|
gFoc_Ctrl.in.s_motAngle = gFoc_Ctrl.in.s_hallAngle;
|
|
|
}
|
|
|
- gFoc_Ctrl.in.s_motRPM = motor_encoder_get_speed();
|
|
|
- LowPass_Filter(gFoc_Ctrl.in.s_motRPMFilted, gFoc_Ctrl.in.s_motRPM, 0.005f);
|
|
|
- gFoc_Ctrl.in.s_motVelDegreePers = gFoc_Ctrl.in.s_motRPM / 30.0f * PI * gFoc_Ctrl.params.n_poles;
|
|
|
+ gFoc_Ctrl.in.s_motVelocity = enc_vel;
|
|
|
+ LowPass_Filter(gFoc_Ctrl.in.s_motRPMFilted, gFoc_Ctrl.in.s_motVelocity, 0.005f);
|
|
|
+ gFoc_Ctrl.in.s_motVelDegreePers = gFoc_Ctrl.in.s_motVelocity / 30.0f * PI * gFoc_Ctrl.params.n_poles;
|
|
|
#ifdef CONFIG_DQ_STEP_RESPONSE
|
|
|
gFoc_Ctrl.in.s_hallAngle = 0;
|
|
|
gFoc_Ctrl.in.s_motAngle = 0;
|
|
|
@@ -284,6 +293,7 @@ static __INLINE void PMSM_FOC_Update_Input(void) {
|
|
|
rand_angle(next_angle);
|
|
|
SinCos_Lut(next_angle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
|
|
|
#endif
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
#ifdef CONFIG_DQ_STEP_RESPONSE
|
|
|
@@ -307,7 +317,7 @@ static u32 PMSM_FOC_Debug_Task(void *p) {
|
|
|
}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_motRPM);
|
|
|
+ plot_2data16(gFoc_Ctrl.in.s_targetRPM, gFoc_Ctrl.in.s_motVelocity);
|
|
|
}
|
|
|
#endif
|
|
|
}
|
|
|
@@ -332,7 +342,9 @@ void PMSM_FOC_Schedule(void) {
|
|
|
|
|
|
gFoc_Ctrl.ctrl_count++;
|
|
|
|
|
|
- PMSM_FOC_Update_Input();
|
|
|
+ if (!PMSM_FOC_Update_Input()){
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
if (gFoc_Ctrl.out.n_RunMode != CTRL_MODE_OPEN) {
|
|
|
|
|
|
@@ -392,27 +404,6 @@ void PMSM_FOC_Schedule(void) {
|
|
|
|
|
|
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);
|
|
|
-
|
|
|
- if (gFoc_Ctrl.plot_type != Plot_None) {
|
|
|
- if (gFoc_Ctrl.ctrl_count % 5 == 0) {
|
|
|
- if (gFoc_Ctrl.plot_type == Plot_Phase_curr) {
|
|
|
- plot_3data16(FtoS16(gFoc_Ctrl.in.s_iABC[0]), FtoS16(gFoc_Ctrl.in.s_iABC[1]), FtoS16(gFoc_Ctrl.in.s_iABC[2]));
|
|
|
- }else if (gFoc_Ctrl.plot_type == Plot_Phase_vol) {
|
|
|
- plot_3data16(FtoS16(gFoc_Ctrl.in.s_vABC[0]), FtoS16(gFoc_Ctrl.in.s_vABC[1]), FtoS16(gFoc_Ctrl.in.s_vABC[2]));
|
|
|
- }else if (gFoc_Ctrl.plot_type == Plot_SMO_OBS) {
|
|
|
-#ifdef CONFIG_SMO_OBSERVER
|
|
|
- float smo_angle = foc_observer_sensorless_angle();
|
|
|
- float delta = smo_angle - gFoc_Ctrl.in.s_hallAngle;
|
|
|
- if (delta > 180) {
|
|
|
- delta -= 360;
|
|
|
- }else if (delta < -180) {
|
|
|
- delta += 360;
|
|
|
- }
|
|
|
- plot_3data16(gFoc_Ctrl.in.s_hallAngle, smo_angle, delta);
|
|
|
-#endif
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
void PMSM_FOC_LogDebug(void) {
|
|
|
@@ -441,14 +432,14 @@ u8 PMSM_FOC_CtrlMode(void) {
|
|
|
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_motRPM, gFoc_Ctrl.in.s_targetTorque);
|
|
|
+ //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_torque, 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_reset(&gFoc_Ctrl.vel_adrc, gFoc_Ctrl.in.s_motRPM, gFoc_Ctrl.in.s_targetTorque);
|
|
|
+ //ladrc_reset(&gFoc_Ctrl.vel_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
|
|
|
ladrc_copy(&gFoc_Ctrl.vel_adrc, &gFoc_Ctrl.vel_lim_adrc);
|
|
|
#else
|
|
|
float target_troque = gFoc_Ctrl.in.s_targetTorque;
|
|
|
@@ -459,7 +450,7 @@ u8 PMSM_FOC_CtrlMode(void) {
|
|
|
#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_motRPM, gFoc_Ctrl.in.s_targetTorque);
|
|
|
+ ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
|
|
|
#else
|
|
|
PI_Controller_Reset(&gFoc_Ctrl.pi_torque, gFoc_Ctrl.in.s_targetTorque);
|
|
|
#endif
|
|
|
@@ -518,13 +509,13 @@ 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_motRPM);
|
|
|
+ return ladrc_run(&gFoc_Ctrl.vel_lim_adrc, lim, gFoc_Ctrl.in.s_motVelocity);
|
|
|
#else
|
|
|
#if 1
|
|
|
gFoc_Ctrl.pi_torque->max = maxTrq;
|
|
|
gFoc_Ctrl.pi_torque->min = 0;
|
|
|
|
|
|
- float err = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp) - gFoc_Ctrl.in.s_motRPM;
|
|
|
+ float err = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp) - gFoc_Ctrl.in.s_motVelocity;
|
|
|
return PI_Controller_RunLimit(&gFoc_Ctrl.pi_torque, err);
|
|
|
#else
|
|
|
return maxTrq;
|
|
|
@@ -551,7 +542,7 @@ static __INLINE void PMSM_FOC_idq_Assign(void) {
|
|
|
}
|
|
|
}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_motRPM, gFoc_Ctrl.in.s_targetTorque, &gFoc_Ctrl.in.s_targetIdq);
|
|
|
+ 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);
|
|
|
@@ -574,7 +565,7 @@ void PMSM_FOC_idqCalc(void) {
|
|
|
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_motRPM < CONFIG_MIN_RPM_EXIT_EBRAKE) {
|
|
|
+ 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);
|
|
|
@@ -597,11 +588,11 @@ void PMSM_FOC_idqCalc(void) {
|
|
|
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_motRPM < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
|
|
|
+ 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_motRPM);
|
|
|
+ float maxTrq = ladrc_run(&gFoc_Ctrl.vel_adrc, refSpeed, gFoc_Ctrl.in.s_motVelocity);
|
|
|
#else
|
|
|
if (maxSpeed >= 0) {
|
|
|
gFoc_Ctrl.pi_speed->max = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);//gFoc_Ctrl.userLim.s_PhaseCurrLim;
|
|
|
@@ -611,12 +602,12 @@ void PMSM_FOC_idqCalc(void) {
|
|
|
gFoc_Ctrl.pi_speed->max = CONFIG_MAX_NEG_TORQUE;
|
|
|
}
|
|
|
|
|
|
- if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motRPM < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
|
|
|
+ if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
|
|
|
gFoc_Ctrl.pi_speed->max = 0;
|
|
|
gFoc_Ctrl.pi_speed->min = 0; //防止倒转
|
|
|
}
|
|
|
gFoc_Ctrl.in.s_targetRPM = refSpeed;
|
|
|
- float errRef = refSpeed - gFoc_Ctrl.in.s_motRPM;
|
|
|
+ float errRef = refSpeed - gFoc_Ctrl.in.s_motVelocity;
|
|
|
float maxTrq = PI_Controller_Run(&gFoc_Ctrl.pi_speed, errRef);
|
|
|
#endif
|
|
|
gFoc_Ctrl.in.s_targetTorque = PMSM_FOC_Limit_iDC(maxTrq);
|
|
|
@@ -697,7 +688,7 @@ void PMSM_FOC_DCCurrLimit(float ibusLimit) {
|
|
|
}
|
|
|
gFoc_Ctrl.userLim.s_iDCLim = ibusLimit;
|
|
|
|
|
|
- if (ABS(gFoc_Ctrl.in.s_motRPM) <= CONFIG_ZERO_SPEED_RPM){
|
|
|
+ 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);
|
|
|
@@ -721,7 +712,7 @@ void PMSM_FOC_SpeedRampLimit(float speedLimit, float speed) {
|
|
|
}
|
|
|
|
|
|
void PMSM_FOC_SpeedLimit(float speedLimit) {
|
|
|
- PMSM_FOC_SpeedRampLimit(speedLimit, gFoc_Ctrl.in.s_motRPM);
|
|
|
+ PMSM_FOC_SpeedRampLimit(speedLimit, gFoc_Ctrl.in.s_motVelocity);
|
|
|
}
|
|
|
|
|
|
void PMSM_FOC_SpeedDirectLimit(float limit) {
|
|
|
@@ -744,7 +735,7 @@ void PMSM_FOC_TorqueLimit(float torqueLimit) {
|
|
|
|
|
|
gFoc_Ctrl.userLim.s_torqueLim = torqueLimit;
|
|
|
|
|
|
- if (ABS(gFoc_Ctrl.in.s_motRPM) <= CONFIG_ZERO_SPEED_RPM){
|
|
|
+ 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);
|
|
|
@@ -912,7 +903,7 @@ void PMSM_FOC_Get_TgtIDQ(DQ_t * dq) {
|
|
|
}
|
|
|
|
|
|
float PMSM_FOC_GetSpeed(void) {
|
|
|
- return gFoc_Ctrl.in.s_motRPM;
|
|
|
+ return gFoc_Ctrl.in.s_motVelocity;
|
|
|
}
|
|
|
|
|
|
|