Vulcan
Aerospace Engineering Utilities Built on Janus
Loading...
Searching...
No Matches
ExtendedKF.hpp
Go to the documentation of this file.
1// Extended Kalman Filter - EKF predict/update for nonlinear systems
2// Part of Vulcan - Aerospace Engineering Utilities
3#pragma once
4
6
7namespace vulcan::estimation {
8
9// =============================================================================
10// Extended Kalman Filter Predict
11// =============================================================================
12
33template <typename Scalar, int N, int U, typename DynamicsFunc>
35 const Eigen::Matrix<Scalar, U, 1> &u,
36 DynamicsFunc f,
37 const Eigen::Matrix<double, N, N> &F,
38 const ProcessNoise<N> &noise) {
39 FilterState<Scalar, N> predicted;
40
41 // Nonlinear state prediction: x⁻ = f(x, u)
42 predicted.x = f(state.x, u);
43
44 // Linearized covariance prediction: P⁻ = F*P*Fᵀ + Q
45 auto F_scalar = F.template cast<Scalar>();
46 predicted.P = F_scalar * state.P * F_scalar.transpose() +
47 noise.Q.template cast<Scalar>();
48
49 return predicted;
50}
51
59template <typename Scalar, int N, typename DynamicsFunc>
61 DynamicsFunc f,
62 const Eigen::Matrix<double, N, N> &F,
63 const ProcessNoise<N> &noise) {
64 FilterState<Scalar, N> predicted;
65
66 // Nonlinear state prediction: x⁻ = f(x)
67 predicted.x = f(state.x);
68
69 // Linearized covariance prediction: P⁻ = F*P*Fᵀ + Q
70 auto F_scalar = F.template cast<Scalar>();
71 predicted.P = F_scalar * state.P * F_scalar.transpose() +
72 noise.Q.template cast<Scalar>();
73
74 return predicted;
75}
76
77// =============================================================================
78// Extended Kalman Filter Update
79// =============================================================================
80
102template <typename Scalar, int N, int M, typename MeasurementFunc>
104 const Eigen::Matrix<Scalar, M, 1> &z,
105 MeasurementFunc h,
106 const Eigen::Matrix<double, M, N> &H,
107 const MeasurementNoise<M> &noise) {
108 // Cast matrices to Scalar type
109 auto H_scalar = H.template cast<Scalar>();
110 auto R_scalar = noise.R.template cast<Scalar>();
111
112 // Predicted measurement: z_pred = h(x)
113 Eigen::Matrix<Scalar, M, 1> z_pred = h(state.x);
114
115 // Innovation covariance: S = H*P*Hᵀ + R
116 Eigen::Matrix<Scalar, M, M> S =
117 H_scalar * state.P * H_scalar.transpose() + R_scalar;
118
119 // Kalman gain: K = P*Hᵀ*S⁻¹
120 Eigen::Matrix<Scalar, N, M> K =
121 state.P * H_scalar.transpose() * S.inverse();
122
123 // Innovation (measurement residual): y = z - h(x)
124 Eigen::Matrix<Scalar, M, 1> innovation = z - z_pred;
125
126 // State update: x̂ = x⁻ + K*y
128 updated.x = state.x + K * innovation;
129
130 // Covariance update: P = (I - K*H)*P⁻
131 Eigen::Matrix<Scalar, N, N> I = Eigen::Matrix<Scalar, N, N>::Identity();
132 updated.P = (I - K * H_scalar) * state.P;
133
134 return updated;
135}
136
137// =============================================================================
138// Combined EKF Step
139// =============================================================================
140
147template <typename Scalar, int N, int U, int M, typename DynamicsFunc,
148 typename MeasurementFunc>
150 const FilterState<Scalar, N> &state, const Eigen::Matrix<Scalar, U, 1> &u,
151 const Eigen::Matrix<Scalar, M, 1> &z, DynamicsFunc f, MeasurementFunc h,
152 const Eigen::Matrix<double, N, N> &F, const Eigen::Matrix<double, M, N> &H,
153 const ProcessNoise<N> &Q, const MeasurementNoise<M> &R) {
154 // Predict
155 auto predicted = ekf_predict<Scalar, N, U>(state, u, f, F, Q);
156
157 // Update
158 return ekf_update<Scalar, N, M>(predicted, z, h, H, R);
159}
160
161} // namespace vulcan::estimation
Definition EstimationTypes.hpp:8
FilterState< Scalar, N > ekf_update(const FilterState< Scalar, N > &state, const Eigen::Matrix< Scalar, M, 1 > &z, MeasurementFunc h, const Eigen::Matrix< double, M, N > &H, const MeasurementNoise< M > &noise)
Extended Kalman filter update step.
Definition ExtendedKF.hpp:103
FilterState< Scalar, N > ekf_predict(const FilterState< Scalar, N > &state, const Eigen::Matrix< Scalar, U, 1 > &u, DynamicsFunc f, const Eigen::Matrix< double, N, N > &F, const ProcessNoise< N > &noise)
Extended Kalman filter predict step.
Definition ExtendedKF.hpp:34
FilterState< Scalar, N > ekf_step(const FilterState< Scalar, N > &state, const Eigen::Matrix< Scalar, U, 1 > &u, const Eigen::Matrix< Scalar, M, 1 > &z, DynamicsFunc f, MeasurementFunc h, const Eigen::Matrix< double, N, N > &F, const Eigen::Matrix< double, M, N > &H, const ProcessNoise< N > &Q, const MeasurementNoise< M > &R)
Combined EKF step (predict + update).
Definition ExtendedKF.hpp:149
Filter state container holding estimate and covariance.
Definition EstimationTypes.hpp:20
Eigen::Matrix< Scalar, N, 1 > x
State estimate.
Definition EstimationTypes.hpp:21
Eigen::Matrix< Scalar, N, N > P
State covariance.
Definition EstimationTypes.hpp:22
Measurement noise specification.
Definition EstimationTypes.hpp:79
Eigen::Matrix< double, M, M > R
Measurement noise covariance.
Definition EstimationTypes.hpp:80
Process noise specification.
Definition EstimationTypes.hpp:46
Eigen::Matrix< double, N, N > Q
Process noise covariance.
Definition EstimationTypes.hpp:47