Skip to content

Commit 7559ba5

Browse files
committed
changes to get it working on hv driver
1 parent a392fd1 commit 7559ba5

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/HFIBLDCMotor.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ void IRAM_ATTR HFIBLDCMotor::process_hfi(){
364364
current_meas.d = ABcurrent.alpha * _ca + ABcurrent.beta * _sa;
365365
current_meas.q = ABcurrent.beta * _ca - ABcurrent.alpha * _sa;
366366

367-
if(current_meas.d+current_meas.q>ocp_protection_limit){
367+
if(fabs(current_meas.d)+fabs(current_meas.q)>ocp_protection_limit){
368368
ocp_cycles_counter+=1;
369369
}
370370

@@ -431,23 +431,29 @@ void IRAM_ATTR HFIBLDCMotor::process_hfi(){
431431
i_beta_prev=ABcurrent.beta;
432432

433433
flux_observer_angle=_atan2(flux_beta,flux_alpha);
434-
if(flux_observer_angle<0) {flux_observer_angle+=_2PI;}
435-
436-
bemf=(polarity_correction*(voltage.q -phase_resistance * current_meas.q));
437-
flux_observer_velocity=((bemf*KV_rating*_SQRT3*_2PI)/(60.0f));
434+
if(polarity_correction<0){
435+
flux_observer_angle-=_PI;
436+
}
437+
while(flux_observer_angle<0){
438+
flux_observer_angle+=_2PI;
439+
}
440+
float iir_coef=0.99;
441+
//
442+
bemf=bemf*(iir_coef)+(1.0-iir_coef)*(polarity_correction*(voltage.q - phase_resistance * current_meas.q));
443+
flux_observer_velocity = flux_observer_velocity*iir_coef+(1.0-iir_coef)*((bemf*KV_rating*_SQRT3*_2PI)/(60.0f));
438444

439445
if(bemf>bemf_threshold || bemf<-bemf_threshold){
440446
bemf_count+=2;
441447
}else{
442448
bemf_count-=2;
443449
if(bemf_count<0) {bemf_count=0;}
444450
}
445-
if(bemf_count>100){ // use flux observer after
451+
if(bemf_count>fo_hysteresis_threshold){ // use flux observer after
446452
bemf_count+=1;
447-
if(bemf_count>200){bemf_count=200;}
453+
if(bemf_count>2*fo_hysteresis_threshold){bemf_count=2*fo_hysteresis_threshold;}
448454
}
449455

450-
if(bemf_count>102){
456+
if(bemf_count>fo_hysteresis_threshold){
451457
// sensorless_out=flux_observer_angle;
452458
// sensorless_velocity = flux_observer_velocity;
453459
hfi_v_act=0;
@@ -531,31 +537,36 @@ void IRAM_ATTR HFIBLDCMotor::process_hfi(){
531537
current_err.q = polarity_correction * current_setpoint.q - current_meas.q;
532538
current_err.d = polarity_correction * current_setpoint.d - current_meas.d;
533539

540+
if (bemf_count <= fo_hysteresis_threshold)
541+
{
542+
sensorless_out = hfi_angle;
543+
sensorless_velocity = hfi_velocity;
544+
}else
545+
{
546+
sensorless_out = flux_observer_angle;
547+
sensorless_velocity = flux_observer_velocity;
548+
usedFOlast = true;
549+
}
534550

535551
voltage_pid.q = PID_current_q(current_err.q, Ts, Ts_div);
536552
voltage_pid.d = PID_current_d(current_err.d, Ts, Ts_div);
537553

554+
//voltage_pid.q+= bemf;//+delta_current.q/Ts*Lq+sensorless_velocity*pole_pairs*polarity_correction * current_setpoint.d*Ld;
555+
//voltage_pid.d-=sensorless_velocity*pole_pairs*polarity_correction * current_setpoint.q*Lq;//
556+
538557

539558
// lowpass does a += on the first arg
540-
LOWPASS(voltage.q, voltage_pid.q, 0.34f);
541-
LOWPASS(voltage.d, voltage_pid.d, 0.34f);
559+
// LOWPASS(voltage.q, voltage_pid.q, 0.34f);
560+
// LOWPASS(voltage.d, voltage_pid.d, 0.34f);
561+
562+
voltage.q=voltage_pid.q;//+bemf*pole_pairs+sensorless_velocity*pole_pairs*polarity_correction * current_setpoint.d*Ld;
563+
voltage.d=voltage_pid.d;//-sensorless_velocity*pole_pairs*polarity_correction * current_setpoint.q*Lq;;
542564

543565
voltage.d = _constrain(voltage.d ,-voltage_limit, voltage_limit);
544566
voltage.q = _constrain(voltage.q ,-voltage_limit, voltage_limit);
545567

546568
voltage.d += hfi_v_act;
547569

548-
if (bemf_count < 100)
549-
{
550-
sensorless_out = hfi_angle;
551-
sensorless_velocity = hfi_velocity;
552-
}else
553-
{
554-
sensorless_out = flux_observer_angle;
555-
sensorless_velocity = flux_observer_velocity;
556-
usedFOlast = true;
557-
}
558-
559570
sensorless_out_prev = sensorless_out;
560571
}
561572

src/HFIBLDCMotor.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,14 @@ class HFIBLDCMotor: public FOCMotor
5050

5151
float Ts = 1.0f/60000.0f;
5252
float Ts_L = Ts * ( 1 / Lq - 1 / Ld );
53-
float current_bandwidth = 300;
53+
float current_bandwidth = 1000;
5454
float polarity_max_pos=0;
5555
float polarity_max_neg=0;
5656
float polarity_detection=0;
5757
float polarity_alignment_voltage=0.5;
5858
float bemf_threshold=5;
5959
float deadtime_compensation=0.0;
60+
float fo_hysteresis_threshold=200;
6061

6162
DQCurrent_s current_meas;
6263
DQCurrent_s current_high;
@@ -182,6 +183,7 @@ class HFIBLDCMotor: public FOCMotor
182183
float Ts_pp_div = 1.0f / (Ts * pole_pairs);
183184
float Ts_div = 1.0f / Ts;
184185
float predivAngleest = 1.0f / (hfi_v * Ts * ( 1.0f / Lq - 1.0f / Ld ) );
186+
float fo_prev = 0;
185187

186188
};
187189

0 commit comments

Comments
 (0)