diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_Sum.cpp b/libraries/AP_BattMonitor/AP_BattMonitor_Sum.cpp index 2a7401d326..354afaca28 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_Sum.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor_Sum.cpp @@ -54,6 +54,9 @@ AP_BattMonitor_Sum::read() float current_sum = 0; uint8_t current_count = 0; + float temperature_sum = 0.0; + uint8_t temperature_count = 0; + for (uint8_t i=0; i<_mon.num_instances(); i++) { if (i == _instance) { // never include self @@ -77,6 +80,12 @@ AP_BattMonitor_Sum::read() current_sum += current; current_count++; } + + float temperature; + if (_mon.get_temperature(temperature, i)) { + temperature_sum += temperature; + temperature_count++; + } } const uint32_t tnow_us = AP_HAL::micros(); const uint32_t dt_us = tnow_us - _state.last_time_micros; @@ -87,10 +96,15 @@ AP_BattMonitor_Sum::read() if (current_count > 0) { _state.current_amps = current_sum; } + if (temperature_count > 0) { + _state.temperature = temperature_sum / temperature_count; + _state.temperature_time = AP_HAL::millis(); + } update_consumed(_state, dt_us); _has_current = (current_count > 0); + _has_temperature = (temperature_count > 0); _state.healthy = (voltage_count > 0); if (_state.healthy) { diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_Sum.h b/libraries/AP_BattMonitor/AP_BattMonitor_Sum.h index 1e882bb333..000ceb5d25 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_Sum.h +++ b/libraries/AP_BattMonitor/AP_BattMonitor_Sum.h @@ -22,6 +22,9 @@ public: /// returns true if battery monitor provides current info bool has_current() const override { return _has_current; } + /// returns true if battery monitor provides temperature info + bool has_temperature() const override { return _has_temperature; } + void init(void) override {} static const struct AP_Param::GroupInfo var_info[]; @@ -31,6 +34,7 @@ private: AP_Int16 _sum_mask; uint8_t _instance; bool _has_current; + bool _has_temperature; }; #endif // AP_BATTERY_SUM_ENABLED