Skip to content

Commit 709b8ab

Browse files
committed
integrate flux observer to transition smoothly
1 parent 18f1393 commit 709b8ab

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

src/HFIBLDCMotor.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,23 @@ void HFIBLDCMotor::process_hfi(){
416416
if(flux_beta<-flux_linkage){flux_beta=-flux_linkage;}
417417
i_alpha_prev=ABcurrent.alpha;
418418
i_beta_prev=ABcurrent.beta;
419-
flux_observer_angle=atan2(flux_beta,flux_alpha)+_PI;
420-
bemf = voltage.q -phase_resistance * current_meas.q;
419+
flux_observer_angle=atan2(flux_beta,flux_alpha);
420+
if(flux_observer_angle<0){flux_observer_angle+=_2PI;}
421+
bemf=(polarity_correction*(voltage.q -phase_resistance * current_meas.q));
421422
if(bemf>bemf_threshold){
423+
bemf_count+=2;
424+
}
425+
else{
426+
bemf_count-=2;
427+
if(bemf_count<0){bemf_count=0;}
428+
}
429+
if(bemf_count>100){
430+
bemf_count+=1;
431+
if(bemf_count>200){bemf_count=200;}
422432
hfi_out=flux_observer_angle;
423-
hfi_int=hfi_out-hfi_out_prev;
433+
hfi_velocity=((bemf*KV_rating*_SQRT3*_2PI)/(60.0f));
424434
hfi_out_prev=hfi_out;
435+
hfi_v_act=0;
425436
}
426437
else
427438
{
@@ -455,20 +466,21 @@ void HFIBLDCMotor::process_hfi(){
455466
hfi_curangleest = -error_saturation_limit;
456467
hfi_error = -hfi_curangleest;
457468
hfi_int += Ts * hfi_error * hfi_gain2; // This the the double integrator
469+
hfi_velocity=hfi_int /(Ts*pole_pairs);
458470
hfi_out += hfi_gain1 * Ts * hfi_error + hfi_int; // This is the integrator and the double integrator
471+
}
472+
current_err.q = polarity_correction * current_setpoint.q - current_meas.q;
473+
current_err.d = polarity_correction * current_setpoint.d - current_meas.d;
459474

460-
current_err.q = polarity_correction * current_setpoint.q - current_meas.q;
461-
current_err.d = polarity_correction * current_setpoint.d - current_meas.d;
462-
463-
voltage_pid.q = PID_current_q(current_err.q, Ts);
464-
voltage_pid.d = PID_current_d(current_err.d, Ts);
475+
voltage_pid.q = PID_current_q(current_err.q, Ts);
476+
voltage_pid.d = PID_current_d(current_err.d, Ts);
465477

466-
// lowpass does a += on the first arg
467-
LOWPASS(voltage.q, voltage_pid.q, 0.34f);
468-
LOWPASS(voltage.d, voltage_pid.d, 0.34f);
478+
// lowpass does a += on the first arg
479+
LOWPASS(voltage.q, voltage_pid.q, 0.34f);
480+
LOWPASS(voltage.d, voltage_pid.d, 0.34f);
469481

470-
voltage.d += hfi_v_act;
471-
}
482+
voltage.d += hfi_v_act;
483+
hfi_out_prev = hfi_out;
472484
}
473485
// // PMSM decoupling control and BEMF FF
474486
// stateX->VqFF = stateX->we * ( confX->Ld * stateX->Id_SP + confX->Lambda_m);
@@ -608,7 +620,7 @@ void HFIBLDCMotor::move(float new_target) {
608620
float tmp_hfi_int = hfi_int;
609621
interrupts();
610622
shaft_angle = (hfi_full_turns *_2PI + tmp_electrical_angle)/pole_pairs;
611-
hfi_velocity = tmp_hfi_int /(Ts*pole_pairs);
623+
//hfi_velocity = tmp_hfi_int /(Ts*pole_pairs);
612624
} else {
613625
if (!sensor){
614626
shaft_angle = shaftAngle(); // read value even if motor is disabled to keep the monitoring updated but not in openloop mode

src/HFIBLDCMotor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ class HFIBLDCMotor: public FOCMotor
160160
float i_alpha_prev=0;
161161
float i_beta_prev=0;
162162
float hfi_out_prev=0;
163-
float bemf;
163+
float bemf=0;
164164
float flux_observer_angle=0;
165+
int bemf_count=0;
165166
};
166167

167168

0 commit comments

Comments
 (0)