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

ekf2: use Joseph covariance update #22770

Merged
merged 4 commits into from
Feb 26, 2024
Merged

ekf2: use Joseph covariance update #22770

merged 4 commits into from
Feb 26, 2024

Conversation

bresch
Copy link
Member

@bresch bresch commented Feb 16, 2024

Solved Problem

The conventional covariance update (P = P - KHP or P = P - KSK') is only correct when an optimal Kalman gain is used. EKF2 is closer to a Schmidt-Kalman filter as it "manually" clears part of the Kalman gain when states are poorly observable or when affecting a certain state can harm the estimate. For example, airspeed fusion is only allowed to affect the wind states when another velocity/position aiding source is active. Using the conventional update can be dangerous in that case as the covariances are not updated anymore while the state variance decreases; artificially increasing the correlation between the states.

Solution

Use the famous Joseph stabilized covariance update as this equation is correct with any Kalman gain (not only the optimal one). Another benefit is that this equation is numerically more stable. The implementation ensures to have a symmetrical matrix as only the upper triangular matrix is computed and copied to the lower part.

The CPU increase is about 1%, which is IMO acceptable given the benefits of this equation.

Changelog Entry

For release notes:

Use Joseph stabilized covariance update in EKF2
New parameter: -
Documentation: -

Alternatives

Clear the covariance of a state when the Kalman gain has been modified significantly.

@bresch bresch added the EKF2 label Feb 16, 2024
@bresch bresch requested a review from dagar February 16, 2024 15:05
@bresch bresch self-assigned this Feb 16, 2024
src/modules/ekf2/EKF/ekf.h Outdated Show resolved Hide resolved
src/modules/ekf2/EKF/ekf.h Outdated Show resolved Hide resolved
@dagar
Copy link
Member

dagar commented Feb 18, 2024

As discussed one big piece to understand here is how much we can actually afford.

Then potentially one option could be doing this switch automatically if the Kalman gains are modified substantially (by clearInhibitedStateKalmanGains(K)).

src/modules/ekf2/EKF/ekf.h Outdated Show resolved Hide resolved
@bresch bresch force-pushed the pr-ekf2-joseph branch 2 times, most recently from 09fe932 to 5b23532 Compare February 21, 2024 12:49
@bresch
Copy link
Member Author

bresch commented Feb 23, 2024

The Joseph stabilized update is too expensive for an F7 processor (EKF2 takes 40% of CPU vs 10% for the simplified update).
Instead of using the state-of-the-art Schmidt-Kalman filter with consider states, we then need to clear out the covariance of the inhibited states to prevent the covariance matrix from developing incorrect high correlations. Next steps would be to minimize the amount of dynamic state inhibition in general (e.g.: run full mag 3D all the time, don't inhibit accel biases except in some extreme cases).

edit: I found a much faster implementation. Now the CPU increase is ~1%, which is totally acceptable given the gains in numerical stability and code cleanness.

src/modules/ekf2/EKF/ekf.h Outdated Show resolved Hide resolved
src/modules/ekf2/EKF/ekf.h Outdated Show resolved Hide resolved
@dagar dagar changed the title ekf2: use Joseph covariance update for optical flow fusion ekf2: use Joseph covariance update Feb 23, 2024
@dagar dagar marked this pull request as ready for review February 23, 2024 18:52
@bresch
Copy link
Member Author

bresch commented Feb 26, 2024

@dagar I squashed the relevant commits together and rebased on main. Good to go on my side.

@dagar dagar merged commit 8001132 into main Feb 26, 2024
91 of 92 checks passed
@dagar dagar deleted the pr-ekf2-joseph branch February 26, 2024 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants