Browse Source

BATTERY_STATUS: improve filling cell voltage

according to review comment
master
Matthias Grob 3 years ago
parent
commit
e8676fe87a
  1. 6
      src/lib/mathlib/math/Limits.hpp
  2. 28
      src/modules/mavlink/streams/BATTERY_STATUS.hpp

6
src/lib/mathlib/math/Limits.hpp

@ -56,6 +56,12 @@ constexpr _Tp min(_Tp a, _Tp b)
return (a < b) ? a : b; return (a < b) ? a : b;
} }
template<typename _Tp>
constexpr _Tp min(_Tp a, _Tp b, _Tp c)
{
return min(min(a, b), c);
}
template<typename _Tp> template<typename _Tp>
constexpr _Tp max(_Tp a, _Tp b) constexpr _Tp max(_Tp a, _Tp b)
{ {

28
src/modules/mavlink/streams/BATTERY_STATUS.hpp

@ -115,8 +115,8 @@ private:
} }
// fill cell voltages // fill cell voltages
static constexpr uint8_t mavlink_cell_slots = (sizeof(bat_msg.voltages) / sizeof(bat_msg.voltages[0])); static constexpr int 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_extension
= (sizeof(bat_msg.voltages_ext) / sizeof(bat_msg.voltages_ext[0])); = (sizeof(bat_msg.voltages_ext) / sizeof(bat_msg.voltages_ext[0]));
uint16_t cell_voltages[mavlink_cell_slots + mavlink_cell_slots_extension]; uint16_t cell_voltages[mavlink_cell_slots + mavlink_cell_slots_extension];
@ -124,28 +124,32 @@ private:
voltage = UINT16_MAX; 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.connected) {
if (battery_status.cell_count == 0 || battery_status.voltage_cell_v[0] < 0.0001f) { // 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
cell_voltages[0] = battery_status.voltage_filtered_v * 1000.f; 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 { } else {
static constexpr uint8_t uorb_cell_slots = static constexpr int uorb_cell_slots =
(sizeof(battery_status.voltage_cell_v) / sizeof(battery_status.voltage_cell_v[0])); (sizeof(battery_status.voltage_cell_v) / sizeof(battery_status.voltage_cell_v[0]));
const int cell_slots = math::min(static_cast<int>(battery_status.cell_count),
uorb_cell_slots,
mavlink_cell_slots + mavlink_cell_slots_extension);
for (uint8_t cell = 0; cell < mavlink_cell_slots + mavlink_cell_slots_extension; cell++) { for (int cell = 0; cell < cell_slots; cell++) {
if (battery_status.connected && cell < math::min(battery_status.cell_count, uorb_cell_slots)) {
cell_voltages[cell] = battery_status.voltage_cell_v[cell] * 1000.f; cell_voltages[cell] = battery_status.voltage_cell_v[cell] * 1000.f;
} }
} }
} }
// voltage fields 1-10 // 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]; bat_msg.voltages[cell] = cell_voltages[cell];
} }
// voltage fields 11-14 into the extension // 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]; bat_msg.voltages_ext[cell] = cell_voltages[mavlink_cell_slots + cell];
} }

Loading…
Cancel
Save