@@ -364,7 +364,7 @@ void IRAM_ATTR HFIBLDCMotor::process_hfi(){
364
364
current_meas.d = ABcurrent.alpha * _ca + ABcurrent.beta * _sa;
365
365
current_meas.q = ABcurrent.beta * _ca - ABcurrent.alpha * _sa;
366
366
367
- if (current_meas.d + current_meas.q >ocp_protection_limit){
367
+ if (fabs ( current_meas.d )+ fabs ( current_meas.q ) >ocp_protection_limit){
368
368
ocp_cycles_counter+=1 ;
369
369
}
370
370
@@ -431,23 +431,29 @@ void IRAM_ATTR HFIBLDCMotor::process_hfi(){
431
431
i_beta_prev=ABcurrent.beta ;
432
432
433
433
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 ));
438
444
439
445
if (bemf>bemf_threshold || bemf<-bemf_threshold){
440
446
bemf_count+=2 ;
441
447
}else {
442
448
bemf_count-=2 ;
443
449
if (bemf_count<0 ) {bemf_count=0 ;}
444
450
}
445
- if (bemf_count>100 ){ // use flux observer after
451
+ if (bemf_count>fo_hysteresis_threshold ){ // use flux observer after
446
452
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 ;}
448
454
}
449
455
450
- if (bemf_count>102 ){
456
+ if (bemf_count>fo_hysteresis_threshold ){
451
457
// sensorless_out=flux_observer_angle;
452
458
// sensorless_velocity = flux_observer_velocity;
453
459
hfi_v_act=0 ;
@@ -531,31 +537,36 @@ void IRAM_ATTR HFIBLDCMotor::process_hfi(){
531
537
current_err.q = polarity_correction * current_setpoint.q - current_meas.q ;
532
538
current_err.d = polarity_correction * current_setpoint.d - current_meas.d ;
533
539
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
+ }
534
550
535
551
voltage_pid.q = PID_current_q (current_err.q , Ts, Ts_div);
536
552
voltage_pid.d = PID_current_d (current_err.d , Ts, Ts_div);
537
553
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
+
538
557
539
558
// 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;;
542
564
543
565
voltage.d = _constrain (voltage.d ,-voltage_limit, voltage_limit);
544
566
voltage.q = _constrain (voltage.q ,-voltage_limit, voltage_limit);
545
567
546
568
voltage.d += hfi_v_act;
547
569
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
-
559
570
sensorless_out_prev = sensorless_out;
560
571
}
561
572
0 commit comments