From 4d821b2c9139e735155716b721ba0132fb7aca02 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 23 Oct 2023 18:42:21 +1100 Subject: [PATCH] AP_BattMonitor: added get_cell_voltage() for scripting --- libraries/AP_BattMonitor/AP_BattMonitor.cpp | 17 +++++++++++++++++ libraries/AP_BattMonitor/AP_BattMonitor.h | 3 +++ 2 files changed, 20 insertions(+) diff --git a/libraries/AP_BattMonitor/AP_BattMonitor.cpp b/libraries/AP_BattMonitor/AP_BattMonitor.cpp index ca73aab478e86e..5f1ecf8f7382d5 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor.cpp +++ b/libraries/AP_BattMonitor/AP_BattMonitor.cpp @@ -874,6 +874,23 @@ const AP_BattMonitor::cells & AP_BattMonitor::get_cell_voltages(const uint8_t in } } +// get once cell voltage (for scripting) +bool AP_BattMonitor::get_cell_voltage(uint8_t instance, uint8_t cell, float &voltage) const +{ + if (!has_cell_voltages(instance) || + cell >= AP_BATT_MONITOR_CELLS_MAX) { + return false; + } + const auto &cell_voltages = get_cell_voltages(instance); + const uint16_t voltage_mv = cell_voltages.cells[cell]; + if (voltage_mv == 0 || voltage_mv == UINT16_MAX) { + // UINT16_MAX is used as invalid indicator + return false; + } + voltage = voltage_mv*0.001; + return true; +} + // returns true if there is a temperature reading bool AP_BattMonitor::get_temperature(float &temperature, const uint8_t instance) const { diff --git a/libraries/AP_BattMonitor/AP_BattMonitor.h b/libraries/AP_BattMonitor/AP_BattMonitor.h index bcd802bcce0710..78311d25412396 100644 --- a/libraries/AP_BattMonitor/AP_BattMonitor.h +++ b/libraries/AP_BattMonitor/AP_BattMonitor.h @@ -232,6 +232,9 @@ class AP_BattMonitor const cells &get_cell_voltages() const { return get_cell_voltages(AP_BATT_PRIMARY_INSTANCE); } const cells &get_cell_voltages(const uint8_t instance) const; + // get once cell voltage (for scripting) + bool get_cell_voltage(uint8_t instance, uint8_t cell, float &voltage) const; + // temperature bool get_temperature(float &temperature) const { return get_temperature(temperature, AP_BATT_PRIMARY_INSTANCE); } bool get_temperature(float &temperature, const uint8_t instance) const;