From 7383552ebcfae8df72d03dea57e5f9156f8748f8 Mon Sep 17 00:00:00 2001 From: Holger Steinhaus Date: Thu, 25 Aug 2016 14:53:33 +0200 Subject: [PATCH] AP_BatteryMonitor: count mWh spent since powerup --- libraries/AP_BattMonitor/AP_BattMonitor.cpp | 20 +++++++++++++++++++ libraries/AP_BattMonitor/AP_BattMonitor.h | 9 +++++++++ .../AP_BattMonitor/AP_BattMonitor_Analog.cpp | 4 +++- .../AP_BattMonitor/AP_BattMonitor_Analog.h | 3 +++ .../AP_BattMonitor/AP_BattMonitor_Backend.h | 3 +++ 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/libraries/AP_BattMonitor/AP_BattMonitor.cpp b/libraries/AP_BattMonitor/AP_BattMonitor.cpp index cc79d798ab..8ff98f20fd 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor.cpp @@ -196,6 +196,17 @@ bool AP_BattMonitor::healthy(uint8_t instance) const { return instance < _num_instances && state[instance].healthy; } +/// has_consumed_energy - returns true if battery monitor instance provides consumed energy info +bool AP_BattMonitor::has_consumed_energy(uint8_t instance) const +{ + if (instance < _num_instances && drivers[instance] != nullptr && _params[instance].type() != AP_BattMonitor_Params::BattMonitor_TYPE_NONE) { + return drivers[instance]->has_consumed_energy(); + } + + // not monitoring current + return false; +} + /// has_current - returns true if battery monitor instance provides current info bool AP_BattMonitor::has_current(uint8_t instance) const { @@ -247,6 +258,15 @@ float AP_BattMonitor::current_total_mah(uint8_t instance) const { } } +/// consumed_wh - returns energy consumed since start-up in watt-hours +float AP_BattMonitor::consumed_wh(uint8_t instance) const { + if (instance < _num_instances) { + return state[instance].consumed_wh; + } else { + return 0.0f; + } +} + /// capacity_remaining_pct - returns the % battery capacity remaining (0 ~ 100) uint8_t AP_BattMonitor::capacity_remaining_pct(uint8_t instance) const { diff --git a/libraries/AP_BattMonitor/AP_BattMonitor.h b/libraries/AP_BattMonitor/AP_BattMonitor.h index 5e9045cbdd..3d0ac082af 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor.h +++ b/libraries/AP_BattMonitor/AP_BattMonitor.h @@ -55,6 +55,7 @@ public: float voltage; // voltage in volts float current_amps; // current in amperes float current_total_mah; // total current draw since start-up + float consumed_wh; // total energy consumed in Wh since start-up uint32_t last_time_micros; // time when voltage and current was last read uint32_t low_voltage_start_ms; // time when voltage dropped below the minimum float temperature; // battery temperature in celsius @@ -77,6 +78,10 @@ public: bool healthy(uint8_t instance) const; bool healthy() const { return healthy(AP_BATT_PRIMARY_INSTANCE); } + /// has_consumed_energy - returns true if battery monitor instance provides consumed energy info + bool has_consumed_energy(uint8_t instance) const; + bool has_consumed_energy() const { return has_consumed_energy(AP_BATT_PRIMARY_INSTANCE); } + /// has_current - returns true if battery monitor instance provides current info bool has_current(uint8_t instance) const; bool has_current() const { return has_current(AP_BATT_PRIMARY_INSTANCE); } @@ -98,6 +103,10 @@ public: float current_total_mah(uint8_t instance) const; float current_total_mah() const { return current_total_mah(AP_BATT_PRIMARY_INSTANCE); } + /// consumed_wh - returns total energy drawn since start-up in watt-hours + float consumed_wh(uint8_t instance) const; + float consumed_wh() const { return consumed_wh(AP_BATT_PRIMARY_INSTANCE); } + /// capacity_remaining_pct - returns the % battery capacity remaining (0 ~ 100) virtual uint8_t capacity_remaining_pct(uint8_t instance) const; uint8_t capacity_remaining_pct() const { return capacity_remaining_pct(AP_BATT_PRIMARY_INSTANCE); } diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_Analog.cpp b/libraries/AP_BattMonitor/AP_BattMonitor_Analog.cpp index 29959cec30..46d43a12e3 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_Analog.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor_Analog.cpp @@ -44,7 +44,9 @@ AP_BattMonitor_Analog::read() // update total current drawn since startup if (_state.last_time_micros != 0 && dt < 2000000.0f) { // .0002778 is 1/3600 (conversion to hours) - _state.current_total_mah += _state.current_amps * dt * 0.0000002778f; + float mah = _state.current_amps * dt * 0.0000002778f; + _state.current_total_mah += mah; + _state.consumed_wh += 0.001f * mah * _state.voltage; } // record time diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_Analog.h b/libraries/AP_BattMonitor/AP_BattMonitor_Analog.h index 613e06fab9..f3b0954131 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_Analog.h +++ b/libraries/AP_BattMonitor/AP_BattMonitor_Analog.h @@ -115,6 +115,9 @@ public: /// Read the battery voltage and current. Should be called at 10hz void read(); + /// returns true if battery monitor provides consumed energy info + bool has_consumed_energy() const override { return has_current(); } + /// returns true if battery monitor provides current info bool has_current() const override; diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_Backend.h b/libraries/AP_BattMonitor/AP_BattMonitor_Backend.h index 3aa99b6b4d..e6549c394f 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_Backend.h +++ b/libraries/AP_BattMonitor/AP_BattMonitor_Backend.h @@ -34,6 +34,9 @@ public: // read the latest battery voltage virtual void read() = 0; + /// returns true if battery monitor instance provides consumed energy info + virtual bool has_consumed_energy() const { return false; } + /// returns true if battery monitor instance provides current info virtual bool has_current() const = 0;