diff --git a/src/drivers/power_monitor/ina220/ina220.cpp b/src/drivers/power_monitor/ina220/ina220.cpp index bde68ce875..05ad028f22 100644 --- a/src/drivers/power_monitor/ina220/ina220.cpp +++ b/src/drivers/power_monitor/ina220/ina220.cpp @@ -247,8 +247,16 @@ INA220::collect() switch (_ch_type) { case PM_CH_TYPE_VBATT: { _battery.setConnected(success); - _battery.updateVoltage(_voltage); - _battery.updateCurrent(_current); + + // Sometimes the read operation "succeeds" but results in wrong + // zero readings. Given that with noise a true reading of + // exactly 0 is very improbable, we just ignore those readings. + // The battery library keeps the old value. + + if (_bus_voltage) { _battery.updateVoltage(_voltage); } + + if (_bus_current) { _battery.updateCurrent(_current); } + _battery.updateAndPublishBatteryStatus(hrt_absolute_time()); } break; diff --git a/src/drivers/power_monitor/ina226/ina226.cpp b/src/drivers/power_monitor/ina226/ina226.cpp index 60fb333699..a02b91eb2c 100644 --- a/src/drivers/power_monitor/ina226/ina226.cpp +++ b/src/drivers/power_monitor/ina226/ina226.cpp @@ -227,8 +227,15 @@ INA226::collect() // success = success && (read(INA226_REG_SHUNTVOLTAGE, _shunt) == PX4_OK); if (setConnected(success)) { - _battery.updateVoltage(static_cast(_bus_voltage * INA226_VSCALE)); - _battery.updateCurrent(static_cast(_current * _current_lsb)); + + // Sometimes the read operation "succeeds" but results in wrong + // zero readings. Given that with noise a true reading of + // exactly 0 is very improbable, we just ignore those readings. + // The battery library keeps the old value. + + if (_bus_voltage) { _battery.updateVoltage(static_cast(_bus_voltage * INA226_VSCALE)); } + + if (_current) { _battery.updateCurrent(static_cast(_current * _current_lsb)); } } _battery.updateAndPublishBatteryStatus(hrt_absolute_time()); diff --git a/src/drivers/power_monitor/ina228/ina228.cpp b/src/drivers/power_monitor/ina228/ina228.cpp index fd7494d3f3..cbdddeccdd 100644 --- a/src/drivers/power_monitor/ina228/ina228.cpp +++ b/src/drivers/power_monitor/ina228/ina228.cpp @@ -322,8 +322,16 @@ INA228::collect() success = success && (read(INA228_REG_DIETEMP, _temperature) == PX4_OK); if (setConnected(success)) { - _battery.updateVoltage(static_cast(_bus_voltage * INA228_VSCALE)); - _battery.updateCurrent(static_cast(_current * _current_lsb)); + + // Sometimes the read operation "succeeds" but results in wrong + // zero readings. Given that with noise a true reading of + // exactly 0 is very improbable, we just ignore those readings. + // The battery library keeps the old value. + + if (_bus_voltage) { _battery.updateVoltage(static_cast(_bus_voltage * INA228_VSCALE)); } + + if (_current) { _battery.updateCurrent(static_cast(_current * _current_lsb)); } + _battery.updateTemperature(static_cast(_temperature * INA228_TSCALE)); } diff --git a/src/drivers/power_monitor/ina238/ina238.cpp b/src/drivers/power_monitor/ina238/ina238.cpp index f42ae87764..233f4677d9 100644 --- a/src/drivers/power_monitor/ina238/ina238.cpp +++ b/src/drivers/power_monitor/ina238/ina238.cpp @@ -251,9 +251,17 @@ int INA238::collect() success = success && (RegisterRead(Register::DIETEMP, (uint16_t &)temperature) == PX4_OK); if (setConnected(success)) { - _battery.updateVoltage(static_cast(bus_voltage * INA238_VSCALE)); - _battery.updateCurrent(static_cast(current * _current_lsb)); - _battery.updateTemperature(static_cast(temperature * INA238_TSCALE)); + + // Sometimes the read operation "succeeds" but results in wrong + // zero readings. Given that with noise a true reading of + // exactly 0 is very improbable, we just ignore those readings. + // The battery library keeps the old value. + + if (bus_voltage) { _battery.updateVoltage(static_cast(bus_voltage * INA238_VSCALE)); } + + if (current) { _battery.updateCurrent(static_cast(current * _current_lsb)); } + + if (temperature) { _battery.updateTemperature(static_cast(temperature * INA238_TSCALE)); } _battery.updateAndPublishBatteryStatus(hrt_absolute_time()); }