[RFC] Magnetometer bias is corrected twice when EKF2 learns mag_B on the ground

Author: dakejahlCreated Sep 26, 2026Updated Sep 26, 2026
Labelskind:rfcstatus:needs-triage

Summary

PX4 has two magnetometer bias estimators. mag_bias_estimator runs while disarmed, and the sensors module subtracts its estimate from vehicle_magnetometer before publishing. EKF2 estimates its own bias states, mag_B, and subtracts them from every sample it fuses. The design assumes the two never learn at the same time: on the ground EKF2's mag_B is unobservable, and in flight the standalone estimator is stopped. With a GNSS heading fused on the ground, both learn the same bias during the same rotation, and the field EKF2 fuses is corrected twice.

On an ARK G5 dual-antenna receiver, the magnetometer heading EKF2 fused went from 3° off the GNSS heading before the vehicle was turned by hand to 36° off after. When the GNSS heading was lost, EKF2 fell back to that heading.

This RFC proposes one of two changes:

  1. EKF2 learns that the applied standalone estimate changed and resets mag_B, as it does on a calibration change today.
  2. The sensors module applies one estimate at a time: the standalone estimate is not applied to a magnetometer while EKF2 reports a valid mag_B for it.

Terms

  • Standalone estimate: the bias that mag_bias_estimator publishes in magnetometer_bias_estimate. It runs only while disarmed and learns from rotation, using the gyro.
  • Applied bias: _calibration_estimator_bias in the sensors module, the standalone estimate it subtracts from the calibrated field before publishing vehicle_magnetometer.
  • mag_B: EKF2's magnetometer bias states, published in estimator_sensor_bias.mag_bias. EKF2 subtracts them from every fused sample.
  • Mag heading fusion: cs_mag_hdg, EKF2's fallback heading source when no GNSS or vision heading is fused.

Problems

Two estimators correct the same bias

  • mag_bias_estimator runs only while disarmed (MagBiasEstimator.cpp:120) and marks an estimate stable after 30 s (:250).
  • The sensors module stores every valid estimate as the applied bias (vehicle_magnetometer/VehicleMagnetometer.cpp:239) and subtracts it from every published sample (:517). It clears it only when the calibration changes or the magnetometer is disabled (:181).
  • EKF2 fuses the 3D field into its magnetometer states whenever a magnetometer is available, and mag_B becomes observable as soon as yaw is known from another source. With a GNSS or vision heading fused, mag heading fusion is off (aid_sources/magnetometer/mag_control.cpp:190) but the field still updates mag_I and mag_B (:239).
  • EKF2 zeroes mag_B only when vehicle_magnetometer.device_id or calibration_count changes (EKF2.cpp:2734, mag_control.cpp:68). The applied bias changes neither.

The sensors module knows about the overlap in one place: when it saves a calibration from EKF2's bias after a flight, it re-adds the applied bias so the saved offset isn't doubled (VehicleMagnetometer.cpp:315). Nothing reconciles the two while both are live.

What it looks like

ARK G5 P3H dual antenna on an ARKV6X, on the ground, never armed, internal magnetometer 52° off before any correction (log; a branch with #27102 so that GNSS yaw starts on the ground, the mechanism is the same on main). GNSS yaw fused from 26 s. The vehicle was picked up and turned three times between 60 and 83 s.

Time Standalone estimate (Ga) EKF2 mag_B (Ga) Heading EKF2 fuses, relative to GNSS
30–58 s, static none (0.005, −0.003, 0.000) +3°
70 s, turning (0.18, −0.26, 0.02), valid (0.05, −0.21, 0.20)
84–93 s, static (0.12, −0.28, 0.08) (0.07, −0.18, 0.05) −36°
137–143 s, static (0.14, −0.26, 0.09), stable (0.07, −0.15, −0.06) −28°
153–158 s, static −23°

Both estimators converged on the same bias during the turns. From then on the published field has the standalone estimate removed and EKF2 removes mag_B again. mag_B relaxes toward the new truth only as fast as its process noise allows.

At 95 s the aux antenna was unplugged. GNSS yaw stopped at 105 s and EKF2 switched to mag heading fusion on the doubly corrected field. Yaw drifted 14° over the next 30 s, and the GNSS yaw restart at 136 s reset it by 5°.

When it happens

Any vehicle with a GNSS or vision heading that turns on the ground while disarmed: carried to the launch point, rotated for a compass check, or driven. The magnetometer fallback after a heading loss, which is the reason such a vehicle carries a magnetometer, is then wrong until mag_B relearns.

Proposal

Reset mag_B when the applied bias changes

EKF2 already resets its bias learning on a calibration change. The applied bias needs the same treatment. Two ways to get there:

  1. vehicle_magnetometer carries the applied bias, three floats. EKF2 resets mag_B and its bias learning when it changes by more than a threshold. Logs then show what was subtracted, which they don't today.
  2. The sensors module increments calibration_count when the applied bias changes by more than a threshold. No message change, and EKF2 already handles it. Every step of a converging estimate resets the learning, which costs nothing on the ground.

After a reset mag_B relearns against the corrected field and converges near zero, so the two corrections add up to one.

Or apply one estimate at a time

The sensors module doesn't apply the standalone estimate to a magnetometer whose estimator_sensor_bias.mag_bias_valid is set, and mag_bias_estimator pauses for it. EKF2 is then the only estimator whenever its bias is observable, and the standalone estimator covers the case it was written for: no yaw reference on the ground. This needs mag_bias_valid to mean observable, which its variance test (EKF2.cpp:1917) doesn't guarantee.

Compatibility

  • Behavior changes only when the applied bias changes while EKF2 has a non-zero mag_B. Today that requires a GNSS or vision heading on the ground.
  • Option 1 adds three fields to vehicle_magnetometer. Option 2 changes nothing on the wire.
  • MBE_ENABLE 0 works around it today by turning the standalone estimator off.

Alternatives considered

  • Leave it. mag_B relearns, but 60 s after the turns it was still 23° off, and the loss that needs the fallback can come at any time.
  • Stop EKF2 from learning mag_B on the ground. Then the flight starts with an unlearned bias, and a heading loss right after takeoff falls back to the uncorrected magnetometer.

Feedback requested

  1. Reset or pause: which fits EKF2's ownership of the bias better?
  2. If reset, should the applied bias travel on vehicle_magnetometer or through calibration_count?
  3. Is there a signal for "mag_B is observable" that the sensors module could rely on?