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

AP_BattMonitor: ESC: integrate consumed mah if not provided by ESC #27672

Merged
merged 1 commit into from
Jul 30, 2024
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
23 changes: 17 additions & 6 deletions libraries/AP_BattMonitor/AP_BattMonitor_ESC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ void AP_BattMonitor_ESC::read(void)
float voltage_sum = 0;
float current_sum = 0;
float temperature_sum = 0;
float consumed_mah_sum = 0.0;
uint32_t highest_ms = 0;
_state.consumed_mah = delta_mah;

const bool all_enabled = _mask == 0;
for (uint8_t i=0; i<ESC_TELEM_MAX_ESCS; i++) {
Expand All @@ -77,7 +77,8 @@ void AP_BattMonitor_ESC::read(void)
if (telem.get_consumption_mah(i, consumption_mah)) {
// accumulate consumed_sum regardless of age, to cope with ESC
// dropping out
_state.consumed_mah += consumption_mah;
consumed_mah_sum += consumption_mah;
have_consumed_mah = true;
}

if (telem.get_voltage(i, voltage)) {
Expand All @@ -87,6 +88,7 @@ void AP_BattMonitor_ESC::read(void)

if (telem.get_current(i, current)) {
current_sum += current;
have_current = true;
}

if (telem.get_temperature(i, temperature_cdeg)) {
Expand Down Expand Up @@ -117,10 +119,19 @@ void AP_BattMonitor_ESC::read(void)
_state.last_time_micros = highest_ms * 1000;
_state.temperature_time = highest_ms;

if (current_sum > 0) {
// if we have ever got a current value then we know we have a
// current sensor
have_current = true;
const uint32_t now_us = AP_HAL::micros();
const uint32_t dt_us = now_us - last_read_us;
last_read_us = now_us;

if (have_consumed_mah) {
// Report the cumulative consumed mah as reported by the ESCs
// delta_mah allows reset_remaining to function without being able to reset the values sent by the ESCs
_state.consumed_mah = delta_mah + consumed_mah_sum;

} else if (have_current) {
// ESCs provide current but not consumed mah, integrate manually
update_consumed(_state, dt_us);

}
}

Expand Down
3 changes: 3 additions & 0 deletions libraries/AP_BattMonitor/AP_BattMonitor_ESC.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ class AP_BattMonitor_ESC :public AP_BattMonitor_Backend
AP_Int32 _mask;

bool have_current;
bool have_consumed_mah;
bool have_temperature;
float delta_mah;

uint32_t last_read_us;
};

#endif // AP_BATTERY_ESC_ENABLED
Loading