From 9638f6cab9c51928c98488150de5266540a5245b Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 14 Jun 2018 09:10:14 +1000 Subject: [PATCH] AP_BattMonitor: changes from PR review --- .../AP_BattMonitor_BLHeliESC.cpp | 26 +++++++++++++------ .../AP_BattMonitor/AP_BattMonitor_BLHeliESC.h | 5 +++- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_BLHeliESC.cpp b/libraries/AP_BattMonitor/AP_BattMonitor_BLHeliESC.cpp index b946258fda..c8fb24a6f6 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_BLHeliESC.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor_BLHeliESC.cpp @@ -29,7 +29,7 @@ void AP_BattMonitor_BLHeliESC::init(void) void AP_BattMonitor_BLHeliESC::read(void) { - AP_BLHeli *blheli = AP_BLHeli::get_instance(); + AP_BLHeli *blheli = AP_BLHeli::get_singleton(); if (!blheli) { return; } @@ -42,21 +42,25 @@ void AP_BattMonitor_BLHeliESC::read(void) uint32_t now = AP_HAL::millis(); uint32_t highest_ms=0; - for (uint8_t i=0; i<8; i++) { + for (uint8_t i=0; iget_telem_data(i, td, timestamp_ms); - if (now - timestamp_ms > 1000) { + blheli->get_telem_data(i, td); + + // accumulate consumed_sum regardless of age, to cope with ESC + // dropping out + consumed_sum += td.consumption; + + if (now - td.timestamp_ms > 1000) { // don't use old data continue; } + num_escs++; voltage_sum += td.voltage; current_sum += td.current; - consumed_sum += td.consumption; temperature_sum += td.temperature; - if (timestamp_ms > highest_ms) { - highest_ms = timestamp_ms; + if (td.timestamp_ms > highest_ms) { + highest_ms = td.timestamp_ms; } } @@ -73,6 +77,12 @@ void AP_BattMonitor_BLHeliESC::read(void) _state.consumed_mah = consumed_sum; _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; + } } #endif // HAVE_AP_BLHELI_SUPPORT diff --git a/libraries/AP_BattMonitor/AP_BattMonitor_BLHeliESC.h b/libraries/AP_BattMonitor/AP_BattMonitor_BLHeliESC.h index 486864b1c5..ebe55f7d24 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor_BLHeliESC.h +++ b/libraries/AP_BattMonitor/AP_BattMonitor_BLHeliESC.h @@ -35,5 +35,8 @@ public: void read() override; // BLHeliESC provides current info - bool has_current() const override { return true; }; + bool has_current() const override { return have_current; }; + +private: + bool have_current; };