[wpimath] Improve EKF numerical stability (#4093)

The Joseph form of the error covariance update equation is more
numerically stable when the Kalman gain isn't optimal. Numerical
instability and filter divergence can occur if the user goes long time
periods between updates and the error covariance becomes ill-conditioned
(the ratio between the largest and smallest eigenvalue gets too large).
This commit is contained in:
Tyler Veness
2022-03-19 20:41:28 -07:00
committed by GitHub
parent d5cb6fed67
commit 95ae23b0e7
2 changed files with 15 additions and 5 deletions

View File

@@ -366,7 +366,13 @@ public class ExtendedKalmanFilter<States extends Num, Inputs extends Num, Output
// x̂ₖ₊₁⁺ = x̂ₖ₊₁⁻ + K(y h(x̂ₖ₊₁⁻, uₖ₊₁))
m_xHat = addFuncX.apply(m_xHat, K.times(residualFuncY.apply(y, h.apply(m_xHat, u))));
// Pₖ₊₁⁺ = (I KC)Pₖ₊₁⁻
m_P = Matrix.eye(m_states).minus(K.times(C)).times(m_P);
// Pₖ₊₁⁺ = (IKₖ₊₁C)Pₖ₊₁⁻(IKₖ₊₁C)ᵀ + Kₖ₊₁RKₖ₊₁ᵀ
// Use Joseph form for numerical stability
m_P =
Matrix.eye(m_states)
.minus(K.times(C))
.times(m_P)
.times(Matrix.eye(m_states).minus(K.times(C)).transpose())
.plus(K.times(discR).times(K.transpose()));
}
}