fix(iis2mdc): data-ready flag, sensitivity, and sample rate (#28016)

* fix(iis2mdc): wait for the Zyxda flag before reading data

STATUS_REG bit 3 (Zyxda) signals that X, Y and Z all have new data. The
ready mask also covered bits 0-2, the individual per-axis flags, so a sample
could be taken when only one axis had refreshed.

* fix(iis2mdc): correct sensitivity to 1.5 mGauss/LSB

The datasheet specifies 1.5 mGauss/LSB. The previous 100/65535 assumed the
+/-50 Gauss range maps exactly onto the 16-bit output and reads about 1.7%
high.

* fix(iis2mdc): poll on a fixed interval for a steady sample rate

RunImpl rescheduled with ScheduleDelayed(10ms) after reading the sensor, so
the period was 10 ms plus the read time and the effective rate fell below the
100 Hz ODR, dropping samples. Drive it from ScheduleOnInterval(10ms) instead
so the period is independent of the read time. A not-ready poll is now
expected occasionally rather than a fault, so it no longer counts as a comms
error.

(cherry picked from commit 9bb0d365c4)
This commit is contained in:
Jacob Dahl authored and Ramon Roche committed 2026-08-10 17:19:47 -07:00
1 parent ec3b99144b
commit 2dd85c8e7d
2 files changed
+6 -6

No files matched your search

@@ -64,9 +64,11 @@ int IIS2MDC::init()
write_register(IIS2MDC_ADDR_CFG_REG_B, OFF_CANC);
write_register(IIS2MDC_ADDR_CFG_REG_C, BDU);
_px4_mag.set_scale(100.f / 65535.f); // +/- 50 Gauss, 16bit
_px4_mag.set_scale(0.0015f); // 1.5 mGauss/LSB (datasheet)
ScheduleDelayed(20_ms);
// Poll at the 100 Hz ODR on a fixed interval so the rate does not drift with
// the time spent reading the sensor.
ScheduleOnInterval(10_ms);
return PX4_OK;
}
@@ -95,11 +97,9 @@ void IIS2MDC::RunImpl()
}
} else {
// No new sample yet; expected occasionally when polling at the data rate.
PX4_DEBUG("not ready: %u", status);
perf_count(_comms_errors);
}
ScheduleDelayed(10_ms);
}
uint8_t IIS2MDC::read_register_block(SensorData *data)
@@ -46,7 +46,7 @@
// IIS2MDC Definitions
#define IIS2MDC_WHO_AM_I 0b01000000
#define IIS2MDC_STATUS_REG_READY 0b00001111
#define IIS2MDC_STATUS_REG_READY 0b00001000 // Zyxda: X, Y and Z all have new data
// CFG_REG_A
#define COMP_TEMP_EN (1 << 7)
#define MD_CONTINUOUS (0 << 0)