From ca568953d98c53c256a07033645b5d9c3a731d39 Mon Sep 17 00:00:00 2001 From: Josh Henderson Date: Thu, 23 Sep 2021 00:57:41 -0400 Subject: [PATCH] AP_BattMonitor: capacity_remaining_pct add checks for healthy status and has_current --- libraries/AP_BattMonitor/AP_BattMonitor_Backend.cpp | 9 ++++++++- libraries/AP_BattMonitor/AP_BattMonitor_Backend.h | 3 ++- libraries/AP_BattMonitor/AP_BattMonitor_UAVCAN.cpp | 6 ++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_Backend.cpp b/libraries/AP_BattMonitor/AP_BattMonitor_Backend.cpp index f5c8d2ff73..edce254eb1 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_Backend.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor_Backend.cpp @@ -30,13 +30,20 @@ AP_BattMonitor_Backend::AP_BattMonitor_Backend(AP_BattMonitor &mon, AP_BattMonit { } -/// capacity_remaining_pct - returns true if the battery % is available and writes to the percentage argument +// capacity_remaining_pct - returns true if the battery % is available and writes to the percentage argument +// return false if the battery is unhealthy, does not have current monitoring, or the pack_capacity is too small bool AP_BattMonitor_Backend::capacity_remaining_pct(uint8_t &percentage) const { // we consider anything under 10 mAh as being an invalid capacity and so will be our measurement of remaining capacity if ( _params._pack_capacity <= 10) { return false; } + + // the monitor must have current readings in order to estimate consumed_mah and be healthy + if (!has_current() || !_state.healthy) { + return false; + } + const float mah_remaining = _params._pack_capacity - _state.consumed_mah; percentage = constrain_float(100 * mah_remaining / _params._pack_capacity, 0, UINT8_MAX); return true; diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_Backend.h b/libraries/AP_BattMonitor/AP_BattMonitor_Backend.h index eaf83c5d55..53e39976ad 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_Backend.h +++ b/libraries/AP_BattMonitor/AP_BattMonitor_Backend.h @@ -46,7 +46,8 @@ public: // returns true if battery monitor provides temperature virtual bool has_temperature() const { return false; } - /// capacity_remaining_pct - returns true if the percentage is valid and writes to percentage argument + // capacity_remaining_pct - returns true if the battery % is available and writes to the percentage argument + // returns false if the battery is unhealthy, does not have current monitoring, or the pack_capacity is too small virtual bool capacity_remaining_pct(uint8_t &percentage) const WARN_IF_UNUSED; // return true if cycle count can be provided and fills in cycles argument diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_UAVCAN.cpp b/libraries/AP_BattMonitor/AP_BattMonitor_UAVCAN.cpp index 60dda33e48..ee9223bb65 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_UAVCAN.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor_UAVCAN.cpp @@ -155,6 +155,12 @@ bool AP_BattMonitor_UAVCAN::capacity_remaining_pct(uint8_t &percentage) const // the user can set the option to use current integration in the backend instead. return AP_BattMonitor_Backend::capacity_remaining_pct(percentage); } + + // the monitor must have current readings in order to estimate consumed_mah and be healthy + if (!has_current() || !_state.healthy) { + return false; + } + percentage = _soc; return true; }