Vulcan
Aerospace Engineering Utilities Built on Janus
Loading...
Searching...
No Matches
KalmanFilter.hpp
Go to the documentation of this file.
1// Kalman Filter - Linear Kalman filter predict/update utilities
2// Part of Vulcan - Aerospace Engineering Utilities
3#pragma once
4
6
7namespace vulcan::estimation {
8
9// =============================================================================
10// Linear Kalman Filter Predict
11// =============================================================================
12
30template <typename Scalar, int N, int U>
32 const Eigen::Matrix<double, N, N> &F,
33 const Eigen::Matrix<double, N, U> &B,
34 const Eigen::Matrix<Scalar, U, 1> &u,
35 const ProcessNoise<N> &noise) {
36 FilterState<Scalar, N> predicted;
37
38 // State prediction: x⁻ = F*x + B*u
39 predicted.x =
40 F.template cast<Scalar>() * state.x + B.template cast<Scalar>() * u;
41
42 // Covariance prediction: P⁻ = F*P*Fᵀ + Q
43 auto F_scalar = F.template cast<Scalar>();
44 predicted.P = F_scalar * state.P * F_scalar.transpose() +
45 noise.Q.template cast<Scalar>();
46
47 return predicted;
48}
49
60template <typename Scalar, int N>
62 const Eigen::Matrix<double, N, N> &F,
63 const ProcessNoise<N> &noise) {
64 FilterState<Scalar, N> predicted;
65
66 // State prediction: x⁻ = F*x
67 predicted.x = F.template cast<Scalar>() * state.x;
68
69 // 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// Linear Kalman Filter Update
79// =============================================================================
80
99template <typename Scalar, int N, int M>
101 const Eigen::Matrix<Scalar, M, 1> &z,
102 const Eigen::Matrix<double, M, N> &H,
103 const MeasurementNoise<M> &noise) {
104 // Cast matrices to Scalar type for mixed operations
105 auto H_scalar = H.template cast<Scalar>();
106 auto R_scalar = noise.R.template cast<Scalar>();
107
108 // Innovation covariance: S = H*P*Hᵀ + R
109 Eigen::Matrix<Scalar, M, M> S =
110 H_scalar * state.P * H_scalar.transpose() + R_scalar;
111
112 // Kalman gain: K = P*Hᵀ*S⁻¹
113 Eigen::Matrix<Scalar, N, M> K =
114 state.P * H_scalar.transpose() * S.inverse();
115
116 // Innovation (measurement residual): y = z - H*x
117 Eigen::Matrix<Scalar, M, 1> innovation = z - H_scalar * state.x;
118
119 // State update: x̂ = x⁻ + K*y
121 updated.x = state.x + K * innovation;
122
123 // Covariance update: P = (I - K*H)*P⁻
124 Eigen::Matrix<Scalar, N, N> I = Eigen::Matrix<Scalar, N, N>::Identity();
125 updated.P = (I - K * H_scalar) * state.P;
126
127 return updated;
128}
129
130// =============================================================================
131// Combined Predict + Update
132// =============================================================================
133
144template <typename Scalar, int N, int U, int M>
146 const FilterState<Scalar, N> &state, const Eigen::Matrix<Scalar, U, 1> &u,
147 const Eigen::Matrix<Scalar, M, 1> &z, const Eigen::Matrix<double, N, N> &F,
148 const Eigen::Matrix<double, N, U> &B, const Eigen::Matrix<double, M, N> &H,
149 const ProcessNoise<N> &Q, const MeasurementNoise<M> &R) {
150 // Predict
151 auto predicted = kf_predict<Scalar, N, U>(state, F, B, u, Q);
152
153 // Update
154 return kf_update<Scalar, N, M>(predicted, z, H, R);
155}
156
160template <typename Scalar, int N, int M>
162 const Eigen::Matrix<Scalar, M, 1> &z,
163 const Eigen::Matrix<double, N, N> &F,
164 const Eigen::Matrix<double, M, N> &H,
165 const ProcessNoise<N> &Q,
166 const MeasurementNoise<M> &R) {
167 // Predict
168 auto predicted = kf_predict<Scalar, N>(state, F, Q);
169
170 // Update
171 return kf_update<Scalar, N, M>(predicted, z, H, R);
172}
173
174} // namespace vulcan::estimation
Definition EstimationTypes.hpp:8
FilterState< Scalar, N > kf_update(const FilterState< Scalar, N > &state, const Eigen::Matrix< Scalar, M, 1 > &z, const Eigen::Matrix< double, M, N > &H, const MeasurementNoise< M > &noise)
Linear Kalman filter update step.
Definition KalmanFilter.hpp:100
FilterState< Scalar, N > kf_step(const FilterState< Scalar, N > &state, const Eigen::Matrix< Scalar, U, 1 > &u, const Eigen::Matrix< Scalar, M, 1 > &z, const Eigen::Matrix< double, N, N > &F, const Eigen::Matrix< double, N, U > &B, const Eigen::Matrix< double, M, N > &H, const ProcessNoise< N > &Q, const MeasurementNoise< M > &R)
Combined Kalman filter step (predict + update).
Definition KalmanFilter.hpp:145
FilterState< Scalar, N > kf_predict(const FilterState< Scalar, N > &state, const Eigen::Matrix< double, N, N > &F, const Eigen::Matrix< double, N, U > &B, const Eigen::Matrix< Scalar, U, 1 > &u, const ProcessNoise< N > &noise)
Linear Kalman filter predict step.
Definition KalmanFilter.hpp:31
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