From 9fbd6b912d59ae1dbe9419af1359abf987a70ac6 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Mon, 23 Oct 2017 17:03:30 +0200 Subject: [PATCH] Battery: adapt output constraints to possible values, only calculate remaining capacity if capacity is configured --- src/modules/systemlib/battery.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/modules/systemlib/battery.cpp b/src/modules/systemlib/battery.cpp index 640a65e775..5cd5090ab7 100644 --- a/src/modules/systemlib/battery.cpp +++ b/src/modules/systemlib/battery.cpp @@ -183,18 +183,11 @@ Battery::estimateRemaining(float voltage_v, float current_a, float throttle_norm const float cell_voltage = voltage_v / _n_cells.get(); _remaining_voltage = math::gradual(cell_voltage, _v_empty.get(), _v_charged.get(), 0.f, 1.f); - // remaining battery capacity based on used current integrated time - _remaining_capacity = 1.f - _discharged_mah / _capacity.get(); - - // limit to sane values - _remaining_voltage = (_remaining_voltage < 0.f) ? 0.f : _remaining_voltage; - _remaining_voltage = (_remaining_voltage > 1.f) ? 1.f : _remaining_voltage; - - _remaining_capacity = (_remaining_capacity < 0.f) ? 0.f : _remaining_capacity; - _remaining_capacity = (_remaining_capacity > 1.f) ? 1.f : _remaining_capacity; - // choose which quantity we're using for final reporting if (_capacity.get() > 0.f) { + // remaining battery capacity based on used current integrated time + _remaining_capacity = math::max(1.f - _discharged_mah / _capacity.get(), 0.f); + // if battery capacity is known, use discharged current for estimate, // but don't show more than voltage estimate _remaining = fminf(_remaining_voltage, _remaining_capacity);