From 9bb0d365c4ff06616bc95742cb7aba5af74af1a3 Mon Sep 17 00:00:00 2001 From: Jacob Dahl <37091262+dakejahl@users.noreply.github.com> Date: Thu, 23 Jul 2026 20:28:28 -0600 Subject: [PATCH] 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. --- src/drivers/magnetometer/st/iis2mdc/iis2mdc.cpp | 10 +++++----- src/drivers/magnetometer/st/iis2mdc/iis2mdc.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/drivers/magnetometer/st/iis2mdc/iis2mdc.cpp b/src/drivers/magnetometer/st/iis2mdc/iis2mdc.cpp index 9a295d100d0..b1ae7ec23d0 100644 --- a/src/drivers/magnetometer/st/iis2mdc/iis2mdc.cpp +++ b/src/drivers/magnetometer/st/iis2mdc/iis2mdc.cpp @@ -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) diff --git a/src/drivers/magnetometer/st/iis2mdc/iis2mdc.h b/src/drivers/magnetometer/st/iis2mdc/iis2mdc.h index 40da40469d6..9982d66fa3e 100644 --- a/src/drivers/magnetometer/st/iis2mdc/iis2mdc.h +++ b/src/drivers/magnetometer/st/iis2mdc/iis2mdc.h @@ -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)