diff --git a/src/lib/battery/battery.cpp b/src/lib/battery/battery.cpp index f668ef9a8d..dba0bf38d1 100644 --- a/src/lib/battery/battery.cpp +++ b/src/lib/battery/battery.cpp @@ -215,24 +215,17 @@ void Battery::estimateStateOfCharge(const float voltage_v, const float current_a _state_of_charge_volt_based = math::gradual(cell_voltage, _params.v_empty, _params.v_charged, 0.f, 1.f); // choose which quantity we're using for final reporting - if (_params.capacity > 0.f) { + if (_params.capacity > 0.f && _battery_initialized) { // if battery capacity is known, fuse voltage measurement with used capacity - if (!_battery_initialized) { - // initialization of the estimation state - _state_of_charge = _state_of_charge_volt_based; + // The lower the voltage the more adjust the estimate with it to avoid deep discharge + const float weight_v = 3e-4f * (1 - _state_of_charge_volt_based); + _state_of_charge = (1 - weight_v) * _state_of_charge + weight_v * _state_of_charge_volt_based; + // directly apply current capacity slope calculated using current + _state_of_charge -= _discharged_mah_loop / _params.capacity; + _state_of_charge = math::max(_state_of_charge, 0.f); - } else { - // The lower the voltage the more adjust the estimate with it to avoid deep discharge - const float weight_v = 3e-4f * (1 - _state_of_charge_volt_based); - _state_of_charge = (1 - weight_v) * _state_of_charge + weight_v * _state_of_charge_volt_based; - // directly apply current capacity slope calculated using current - _state_of_charge -= _discharged_mah_loop / _params.capacity; - _state_of_charge = math::max(_state_of_charge, 0.f); - - const float state_of_charge_current_based = math::max(1.f - _discharged_mah / _params.capacity, 0.f); - _state_of_charge = math::min(state_of_charge_current_based, _state_of_charge); - - } + const float state_of_charge_current_based = math::max(1.f - _discharged_mah / _params.capacity, 0.f); + _state_of_charge = math::min(state_of_charge_current_based, _state_of_charge); } else { _state_of_charge = _state_of_charge_volt_based;