Replace AK09918 isDataReady init loop with direct getData diagnostic

The DRDY polling loop was unreliable: it never obtained a ready state
even after 300 ms, because the DRDY bit timing depends on the exact
measurement cycle phase. The sensor is correctly in continuous mode —
the main loop reads data via getData() regardless of DRDY.

New init: power-down → switchMode(CONTINUOUS_100HZ) → delay 60 ms
(5 sample periods), then a one-shot getData() + isDataReady() call
that prints the actual register values for diagnostics. No reset loop.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Josh
2026-04-23 22:49:50 +02:00
co-authored by Claude Sonnet 4.6
parent 12b025367c
commit 53ba59e4cd
+18 -17
View File
@@ -329,27 +329,28 @@ void imuInit()
} }
} }
// Put sensor in a known state before switching to continuous mode. // Datasheet §6: must transition through power-down before setting any
// measurement mode. No DRDY polling needed here — the main loop reads
// data continuously; we just need to get the mode register set.
magnetometer_.initialize(AK09918_POWER_DOWN); magnetometer_.initialize(AK09918_POWER_DOWN);
delay(10); // datasheet: >=100µs after power-down; 10ms is safe delay(10);
magnetometer_.switchMode(AK09918_CONTINUOUS_100HZ); magnetometer_.switchMode(AK09918_CONTINUOUS_100HZ);
// Wait 5 full sample periods (100 Hz → 10 ms each) so first data is ready.
delay(60);
// At 100 Hz the first sample takes ~10 ms. Poll ST1 with a delay // Diagnostic: read ST1 and raw data to confirm the sensor is alive.
// instead of resetting on every attempt (reset clears the mode setting). {
err = AK09918_ERR_NOT_RDY; int16_t tx = 0, ty = 0, tz = 0;
for (int retry_times = 0; retry_times < 20 && err != AK09918_ERR_OK; retry_times++) { const AK09918_err_type_t tErr = magnetometer_.getData(&tx, &ty, &tz);
delay(15); // wait one sample period (100Hz = 10ms) plus margin const AK09918_err_type_t rdyErr = magnetometer_.isDataReady();
err = magnetometer_.isDataReady(); Serial.printf("AK09918 diag: getData err=%d x=%d y=%d z=%d ST1_ready=%d\n",
if (err == AK09918_ERR_READ_FAILED) { tErr, tx, ty, tz, rdyErr == AK09918_ERR_OK ? 1 : 0);
// I2C error mode may have been lost, try re-applying it once. if (tErr == AK09918_ERR_OK || tErr == AK09918_ERR_OVERFLOW) {
Serial.printf("AK09918 I2C read failed on retry %d, re-applying mode...\n", retry_times + 1); Serial.println("AK09918 producing data.");
magnetometer_.switchMode(AK09918_CONTINUOUS_100HZ);
}
}
if (err == AK09918_ERR_OK) {
Serial.println("AK09918 ready.");
} else { } else {
Serial.printf("AK09918 init timed out (last err=%d). Magnetometer will be unavailable.\n", err); Serial.printf("AK09918 getData failed (err=%d). "
"Sensor may be in power-down - check 3V3 supply.\n", tErr);
}
} }
// Serial.println("Start figure-8 calibration after 1 seconds."); // Serial.println("Start figure-8 calibration after 1 seconds.");
// delay(1000); // delay(1000);