Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use filtered throttle for battery voltage compensation #9055

Merged
merged 2 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/modules/systemlib/battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ Battery::updateBatteryStatus(hrt_abstime timestamp, float voltage_v, float curre
reset(battery_status);
battery_status->timestamp = timestamp;
filterVoltage(voltage_v);
filterThrottle(throttle_normalized);
filterCurrent(current_a);
sumDischarged(timestamp, current_a);
estimateRemaining(_voltage_filtered_v, _current_filtered_a, throttle_normalized, armed);
estimateRemaining(_voltage_filtered_v, _current_filtered_a, _throttle_filtered, armed);
computeScale();

if (_battery_initialized) {
Expand Down Expand Up @@ -141,6 +142,18 @@ Battery::filterCurrent(float current_a)
}
}

void Battery::filterThrottle(float throttle)
{
if (!_battery_initialized) {
_throttle_filtered = throttle;
}

const float filtered_next = _throttle_filtered * 0.99f + throttle * 0.01f;

if (PX4_ISFINITE(filtered_next)) {
_throttle_filtered = filtered_next;
}
}

void
Battery::sumDischarged(hrt_abstime timestamp, float current_a)
Expand All @@ -165,7 +178,7 @@ Battery::sumDischarged(hrt_abstime timestamp, float current_a)
}

void
Battery::estimateRemaining(float voltage_v, float current_a, float throttle_normalized, bool armed)
Battery::estimateRemaining(float voltage_v, float current_a, float throttle, bool armed)
{
// remaining battery capacity based on voltage
float cell_voltage = voltage_v / _n_cells.get();
Expand All @@ -176,7 +189,7 @@ Battery::estimateRemaining(float voltage_v, float current_a, float throttle_norm

} else {
// assume linear relation between throttle and voltage drop
cell_voltage += throttle_normalized * _v_load_drop.get();
cell_voltage += throttle * _v_load_drop.get();
}

_remaining_voltage = math::gradual(cell_voltage, _v_empty.get(), _v_charged.get(), 0.f, 1.f);
Expand Down
4 changes: 3 additions & 1 deletion src/modules/systemlib/battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ class Battery : public control::SuperBlock

private:
void filterVoltage(float voltage_v);
void filterThrottle(float throttle);
void filterCurrent(float current_a);
void sumDischarged(hrt_abstime timestamp, float current_a);
void estimateRemaining(float voltage_v, float current_a, float throttle_normalized, bool armed);
void estimateRemaining(float voltage_v, float current_a, float throttle, bool armed);
void determineWarning(bool connected);
void computeScale();

Expand All @@ -115,6 +116,7 @@ class Battery : public control::SuperBlock

bool _battery_initialized = false;
float _voltage_filtered_v = -1.f;
float _throttle_filtered = -1.f;
float _current_filtered_a = -1.f;
float _discharged_mah = 0.f;
float _discharged_mah_loop = 0.f;
Expand Down