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>
AK09918 WIA2 device ID is 0x0C (not 0x09 which belongs to AK09916).
Previous check falsely reported init failure for a working sensor.
The retry loop was resetting the sensor on every attempt, which clears
CNTL2 back to power-down before the sensor can produce a measurement.
At 100 Hz the first sample takes ~10 ms; checking isDataReady immediately
after switchMode always returned NOT_RDY, triggering another reset.
New approach: switch to continuous mode once, then poll with 15 ms
delay (one sample period + margin) up to 20 times without resetting.
Only re-apply the mode if an actual I2C read failure occurs.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The previous formula had both atan2 arguments negated relative to the
standard ZYX Euler extraction:
wrong: atan2(-2*q1*q2 - 2*q0*q3, 2*q2^2 + 2*q3^2 - 1)
correct: atan2( 2*q1*q2 + 2*q0*q3, 1 - 2*q2^2 - 2*q3^2)
Since atan2(-y,-x) = atan2(y,x) ± 180°, the identity quaternion [1,0,0,0]
produced atan2(0,-1) = 180° instead of atan2(0,1) = 0°.
This bug caused yaw to always initialise at 180° when no magnetometer
calibration is available. Roll and pitch formulas are unaffected.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- imuInit: verify WIA2 register == 0x09 before configuring the sensor.
Prints exact error with GPIO hints if the chip doesn't respond.
Fixes silent failure where initialize(AK09918_NORMAL) skipped I2C entirely.
- imuInit: initialize with POWER_DOWN before switchMode so the sensor
always transitions through a known state.
- imuDataGet: only feed the low-pass filter with real data (AK09918_ERR_OK
or OVERFLOW). On read failure, reuse the previous corrected values so
zeros don't corrupt the filter state and the calibration session.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Fix atan2 sign bug: atan2(-yHeading, xHeading) per Freescale AN4248 eq.22.
The previous atan2(yHeading, xHeading) produced counter-clockwise angles,
causing East/West to be swapped relative to compass convention.
- Add imuSetMagneticDeclination() (T:146, field "decl") to correct the
offset between magnetic north and true north (iPhone shows true north).
Value is persisted in imuConfig.json across reboots.
- Add imuSetHeadingOffset() (T:147, field "off") to compensate for sensor
mounting orientation on the rover. Also persisted.
- Persist decl and hOff in imuConfig.json (version 2, backwards compatible).
- Fix calibration feedback for web interface: T:126 now includes calDone=1
(success) or calDone=0 (fail) once after calibration completes, plus
decl and hOff fields. Calibration start message explains polling procedure.
- Remove duplicate #include <nvs_flash.h> and unused Adafruit ICM20948
includes that caused build failures when libraries were not installed.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>