Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor magnetometer fusion and add mag_type "init" #3532

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/config/ekf/ekf_mag_type_selection_tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 40 additions & 20 deletions en/advanced_config/tuning_the_ecl_ekf.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,46 @@ This minimum data set is required for all EKF modes of operation. Other sensor d

### Magnetometer

Three axis body fixed magnetometer data (or external vision system pose data) at a minimum rate of 5Hz is required.

Magnetometer data can be used in two ways:

- Magnetometer measurements are converted to a yaw angle using the tilt estimate and magnetic declination.
The yaw angle is then used as an observation by the EKF.
- This method is less accurate and does not allow for learning of body frame field offsets, however it is more robust to magnetic anomalies and large start-up gyro biases.
- It is the default method used during start-up and on ground.
- The XYZ magnetometer readings are used as separate observations.
- This method is more accurate but requires that the magnetometer biases are correctly estimated.
- The biases are observable while the drone is rotating and the true heading is observable when the vehicle is accelerating (linear acceleration).
- Since the biases can change and are only observable when moving, it is safer to switch back to heading fusion when not moving.
- It assumes the earth magnetic field environment only changes slowly and performs less well when there are significant external magnetic anomalies.
- This is the default method used when the vehicle is moving.

The logic used to select these modes is set by the [EKF2_MAG_TYPE](../advanced_config/parameter_reference.md#EKF2_MAG_TYPE) parameter.
The default 'Automatic' mode (`EKF2_MAG_TYPE=0`) is recommended as it uses the more robust magnetometer yaw on the ground, and more accurate 3-axis magnetometer when moving.
Setting '3-axis' mode all the time (`EKF2_MAG_TYPE=2`) is more error-prone, and requires that all the IMUs are well calibrated.

The option is available to operate without a magnetometer, either by replacing it using [yaw from a dual antenna GPS](#yaw-measurements) or using the IMU measurements and GPS velocity data to [estimate yaw from vehicle movement](#yaw-from-gps-velocity).
Three axis body fixed magnetometer data at a minimum rate of 5Hz is required to be considered by the estimator.

::: info

- The magnetometer **biases** are only observable while the drone is rotating
- The true heading is observable when the vehicle is accelerating (linear acceleration) while absolute position or velocity measurements are fused (e.g. GPS).
This means that magnetometer heading measurements are optional after initialization if those conditions are met often enough to constrain the heading drift (caused by gyro bias).
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that we can get heading from GPS-measured position/velocity when moving so we don't need magnetometer readings when moving, as long as we're getting enough info to constrain the heading drift - right. Presumably there might be some drift when we stop in position?

When you say "optional after initialization" do you mean "before arming"? I mean, why do we need them on initialization/or does the measurement matter until we're ready to fly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably there might be some drift when we stop in position?

Yes, the heading can drift if you hover in place but usually there are enough disturbances to always provide the tiny bit of motion required to constrain the heading drift. Also, when the position controller is active, an incorrect heading makes the drone "toilet bowl", which makes the heading observable, so unless really unlucky, the heading cannot really drift away by a lot without causing disturbances that makes the heading observable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you say "optional after initialization" do you mean "before arming"? I mean, why do we need them on initialization/or does the measurement matter until we're ready to fly?

We often just need the magnetometer to align the heading and start fusing GNSS. So even before arming, if the drone is not moving, the heading won't change and the drift is really low, and if it's moving the heading is observable so we don't need a mag at all to constrain the drift


:::

Magnetometer data fusion can be configured using [EKF2_MAG_TYPE](../advanced_config/parameter_reference.md#EKF2_MAG_TYPE):

0. Automatic:
- The magnetometer readings only affect the heading estimate before arming, and the whole attitude after arming.
- Heading and tilt errors are compensated when using this method.
- Incorrect magnetic field measurements can degrade the tilt estimate.
- The magnetometer biases are estimated whenever observable.
1. Magnetic heading:
- Only the heading is corrected.
The tilt estimate is never affected by incorrect magnetic field measurements.
- Tilt errors that could arise when flying without velocity/position aiding are not corrected when using this method.
- The magnetometer biases are estimated whenever observable.
2. Deprecated
3. Deprecated
4. Deprecated
5. None:
- Magnetometer data is never used.
This is useful when the data can never be trusted (e.g.: high current close to the sensor, external anomalies).
- The estimator will use other sources of heading: [GPS heading](#yaw-measurements) or external vision.
- When using GPS measurements without another source of heading, the heading can only be initialized after sufficient horizontal acceleration.
See [Estimate yaw from vehicle movement](#yaw-from-gps-velocity) below.
6. Init only:
- Magnetometer data is only used to initialize the heading estimate.
This is useful when the data can be used before arming but not afterwards (e.g.: high current after the vehicle is armed).
- After initialization, the heading is constrained using other observations.
- Unlike mag type `None`, when combined with GPS measurements, this method allows position controlled modes to run directly during takeoff.

The following selection tree can be used to select the right option:

![EKF mag type selection tree](../../assets/config/ekf/ekf_mag_type_selection_tree.png)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, Images are best in-tree (done). I also ran prettier and fixed a few typos.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good to know, thanks!


### Height

Expand Down
Loading