Skip to content

Commit

Permalink
drivers/power_monitor/ina238: retry if read fails
Browse files Browse the repository at this point in the history
* ina238: retry if read fails
* ina238: increase retries and only publish not connected if register check fails
* ina238: use I2C resets
  • Loading branch information
AlexKlimaj authored Jan 22, 2025
1 parent 6322ebc commit 07e7c64
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/drivers/power_monitor/ina238/ina238.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ int INA238::Reset()

int ret = PX4_ERROR;

_retries = 3;

if (RegisterWrite(Register::CONFIG, (uint16_t)(ADC_RESET_BIT)) != PX4_OK) {
return ret;
}
Expand Down Expand Up @@ -231,6 +233,16 @@ int INA238::collect()
success = success && (RegisterRead(Register::CURRENT, (uint16_t &)current) == PX4_OK);
success = success && (RegisterRead(Register::DIETEMP, (uint16_t &)temperature) == PX4_OK);

if (success) {
_battery.updateVoltage(static_cast<float>(bus_voltage * INA238_VSCALE));
_battery.updateCurrent(static_cast<float>(current * _current_lsb));
_battery.updateTemperature(static_cast<float>(temperature * INA238_TSCALE));

_battery.setConnected(success);

_battery.updateAndPublishBatteryStatus(hrt_absolute_time());
}

if (!success || hrt_elapsed_time(&_last_config_check_timestamp) > 100_ms) {
// check configuration registers periodically or immediately following any failure
if (RegisterCheck(_register_cfg[_checked_register])) {
Expand All @@ -242,26 +254,21 @@ int INA238::collect()
PX4_DEBUG("register check failed");
perf_count(_bad_register_perf);
success = false;
}
}

if (!success) {
PX4_DEBUG("error reading from sensor");
bus_voltage = current = 0;
}
_battery.setConnected(success);

_battery.setConnected(success);
_battery.updateVoltage(static_cast<float>(bus_voltage * INA238_VSCALE));
_battery.updateCurrent(static_cast<float>(current * _current_lsb));
_battery.updateTemperature(static_cast<float>(temperature * INA238_TSCALE));
_battery.updateAndPublishBatteryStatus(hrt_absolute_time());
_battery.updateAndPublishBatteryStatus(hrt_absolute_time());
}
}

perf_end(_sample_perf);

if (success) {
return PX4_OK;

} else {
PX4_DEBUG("error reading from sensor");

return PX4_ERROR;
}
}
Expand Down

0 comments on commit 07e7c64

Please sign in to comment.