Repository navigation
AP_NavEKF3: LearnedMagField manages learned compass offsets - #35128
Conversation
Previous review (2026-11-12)Automated review note — AI-generated (Claude+Codex), validated against the live diff. Please sanity-check before acting. Reviewed at head This is a clean refactor: the three existing and new compass-learn autotests pass at this head, and a mutation check shows the existing CompassLearnCopyFromEKF test does exercise the new copy. Two small points remain. Should fix
A note that the description did not mention saves now using the last converged copy was withdrawn: the PR text and commit message both describe it. |
Global Size Summary (Total Flash delta in bytes)Built from 7ce5815 rebased onto a995235
|
…landing The EKF only handed out learned compass offsets while the learning conditions held (in-flight mag initialisation complete, mag states not inhibited), so offsets could be lost if either changed before the disarm that saves them. Keep a copy of the learned body field while it is valid and use it for getMagOffsets(). It is cleared when the compass offsets change, the compass is switched, a flight starts, or the compass is no longer used. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The EKF now holds the learned field after landing, so the offsets no longer have to be saved before the vehicle is on the ground. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…turned off Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
4131df3 to
7ce5815
Compare
|
@AP-Review, thanks for the review. My AI and I have rebased this on master and updated the comment re EKF3 and EKF2 behaviour. Re your 2nd suggestion to add another autotest, I don't think we should add an autotest. I think the existing CompassLearnCopyFromEKF already fails if the copy is lost at landing which is the code path that matters. |
|
Automated review note — AI-generated (Claude+Codex), validated against the live diff. Please sanity-check before acting. Reviewed at head No blocking issues at this head: the rebase leaves the EKF3 and autotest commits unchanged. Minor (2)
|
|
Thanks again for the review. Re the two Minor points:
|
Summary
With this change, each EKF3 core keeps a copy of the learned body magnetic field in a new nested class called LearnedMagField, instead of relying on the live body_magfield of the EKF state. This separation reduces dependencies on the order in which the live mag field may be cleared and when the last valid learned offsets can be saved to the COMPASSn_OFS_X/Y/Z parameters. This difference has almost no functional impact in master but becomes important for a follow-up PR #35105 which affects when the EKF3's onGround is set/cleared.
How the class is used
During each EKF update, the latest learned body magnetic field is copied into the LearnedMagField class and the validity flag is set, but only if all of these are true:
a. the final in-flight mag field initialisation after takeoff has finished (see finalInflightMagInit)
b. the mag field states are being learned (see !inhibitMagStates)
c. the compass is in use (use_compass()) and healthy (compass.healthy(magSelectIndex))
d. the field estimate has converged (its variances are small, P[19][19] to P[21][21] are below 5e-6)
If any of them is false the previous copy is left as it is, so the last good value survives a landing.
These are the same conditions master checks when it saves the offsets at disarm, except that use_compass() is also checked here (on master the compass library checks it at save time). The copy also records which compass it is for (magSelectIndex), and is only returned for that compass, which replaces the check mag_idx == magSelectIndex that master makes at save time.
Testing
These existing autotests exercise the code change and pass on both this PR and master:
An new autotest has been added:
CompassLearnCopyFromEKF and CompassLearnCopyFromEKFAffinity pass.
More human testing will be performed before we merge
Classification & Testing (check all that apply and add your own)
Claude greatly helped me creating and testing this PR