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:
co-authored by
Claude Sonnet 4.6
parent
12b025367c
commit
53ba59e4cd
@@ -329,28 +329,29 @@ 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);
|
||||
delay(10); // datasheet: >=100µs after power-down; 10ms is safe
|
||||
delay(10);
|
||||
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
|
||||
// instead of resetting on every attempt (reset clears the mode setting).
|
||||
err = AK09918_ERR_NOT_RDY;
|
||||
for (int retry_times = 0; retry_times < 20 && err != AK09918_ERR_OK; retry_times++) {
|
||||
delay(15); // wait one sample period (100Hz = 10ms) plus margin
|
||||
err = magnetometer_.isDataReady();
|
||||
if (err == AK09918_ERR_READ_FAILED) {
|
||||
// I2C error – mode may have been lost, try re-applying it once.
|
||||
Serial.printf("AK09918 I2C read failed on retry %d, re-applying mode...\n", retry_times + 1);
|
||||
magnetometer_.switchMode(AK09918_CONTINUOUS_100HZ);
|
||||
// Diagnostic: read ST1 and raw data to confirm the sensor is alive.
|
||||
{
|
||||
int16_t tx = 0, ty = 0, tz = 0;
|
||||
const AK09918_err_type_t tErr = magnetometer_.getData(&tx, &ty, &tz);
|
||||
const AK09918_err_type_t rdyErr = magnetometer_.isDataReady();
|
||||
Serial.printf("AK09918 diag: getData err=%d x=%d y=%d z=%d ST1_ready=%d\n",
|
||||
tErr, tx, ty, tz, rdyErr == AK09918_ERR_OK ? 1 : 0);
|
||||
if (tErr == AK09918_ERR_OK || tErr == AK09918_ERR_OVERFLOW) {
|
||||
Serial.println("AK09918 producing data.");
|
||||
} else {
|
||||
Serial.printf("AK09918 getData failed (err=%d). "
|
||||
"Sensor may be in power-down - check 3V3 supply.\n", tErr);
|
||||
}
|
||||
}
|
||||
if (err == AK09918_ERR_OK) {
|
||||
Serial.println("AK09918 ready.");
|
||||
} else {
|
||||
Serial.printf("AK09918 init timed out (last err=%d). Magnetometer will be unavailable.\n", err);
|
||||
}
|
||||
// Serial.println("Start figure-8 calibration after 1 seconds.");
|
||||
// delay(1000);
|
||||
// calibrate(10000, &offset_x, &offset_y, &offset_z);
|
||||
|
||||
Reference in New Issue
Block a user