diff --git a/src/lib/mathlib/math/Limits.hpp b/src/lib/mathlib/math/Limits.hpp index 39ce00fce0..900b426665 100644 --- a/src/lib/mathlib/math/Limits.hpp +++ b/src/lib/mathlib/math/Limits.hpp @@ -56,6 +56,12 @@ constexpr _Tp min(_Tp a, _Tp b) return (a < b) ? a : b; } +template +constexpr _Tp min(_Tp a, _Tp b, _Tp c) +{ + return min(min(a, b), c); +} + template constexpr _Tp max(_Tp a, _Tp b) { diff --git a/src/modules/mavlink/streams/BATTERY_STATUS.hpp b/src/modules/mavlink/streams/BATTERY_STATUS.hpp index 8222ecfe71..3e0bc7ef90 100644 --- a/src/modules/mavlink/streams/BATTERY_STATUS.hpp +++ b/src/modules/mavlink/streams/BATTERY_STATUS.hpp @@ -115,8 +115,8 @@ private: } // fill cell voltages - static constexpr uint8_t mavlink_cell_slots = (sizeof(bat_msg.voltages) / sizeof(bat_msg.voltages[0])); - static constexpr uint8_t mavlink_cell_slots_extension + static constexpr int mavlink_cell_slots = (sizeof(bat_msg.voltages) / sizeof(bat_msg.voltages[0])); + static constexpr int mavlink_cell_slots_extension = (sizeof(bat_msg.voltages_ext) / sizeof(bat_msg.voltages_ext[0])); uint16_t cell_voltages[mavlink_cell_slots + mavlink_cell_slots_extension]; @@ -124,28 +124,32 @@ private: voltage = UINT16_MAX; } - // We don't know the cell count or we don't know the indpendent cell voltages so we report the total voltage in the first cell - if (battery_status.cell_count == 0 || battery_status.voltage_cell_v[0] < 0.0001f) { - cell_voltages[0] = battery_status.voltage_filtered_v * 1000.f; + if (battery_status.connected) { + // We don't know the cell count or we don't know the indpendent cell voltages so we report the total voltage in the first cell + if (battery_status.cell_count == 0 || battery_status.voltage_cell_v[0] < 0.0001f) { + cell_voltages[0] = battery_status.voltage_filtered_v * 1000.f; - } else { - static constexpr uint8_t uorb_cell_slots = - (sizeof(battery_status.voltage_cell_v) / sizeof(battery_status.voltage_cell_v[0])); + } else { + static constexpr int uorb_cell_slots = + (sizeof(battery_status.voltage_cell_v) / sizeof(battery_status.voltage_cell_v[0])); - for (uint8_t cell = 0; cell < mavlink_cell_slots + mavlink_cell_slots_extension; cell++) { - if (battery_status.connected && cell < math::min(battery_status.cell_count, uorb_cell_slots)) { + const int cell_slots = math::min(static_cast(battery_status.cell_count), + uorb_cell_slots, + mavlink_cell_slots + mavlink_cell_slots_extension); + + for (int cell = 0; cell < cell_slots; cell++) { cell_voltages[cell] = battery_status.voltage_cell_v[cell] * 1000.f; } } } // voltage fields 1-10 - for (uint8_t cell = 0; cell < mavlink_cell_slots; cell++) { + for (int cell = 0; cell < mavlink_cell_slots; cell++) { bat_msg.voltages[cell] = cell_voltages[cell]; } // voltage fields 11-14 into the extension - for (uint8_t cell = 0; cell < mavlink_cell_slots_extension; cell++) { + for (int cell = 0; cell < mavlink_cell_slots_extension; cell++) { bat_msg.voltages_ext[cell] = cell_voltages[mavlink_cell_slots + cell]; }