forked from borglab/gtsam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquivariantFilter.h
More file actions
342 lines (297 loc) · 12.1 KB
/
EquivariantFilter.h
File metadata and controls
342 lines (297 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* @file EquivariantFilter.h
* @brief Equivariant Filter (EqF) implementation
*
* @author Darshan Rajasekaran
* @author Jennifer Oum
* @author Rohan Bansal
* @author Frank Dellaert
* @date 2025
*/
#pragma once
#include <gtsam/base/GroupAction.h>
#include <gtsam/base/Lie.h>
#include <gtsam/base/Matrix.h>
#include <gtsam/base/Vector.h>
#include <gtsam/geometry/Rot3.h>
#include <gtsam/geometry/Unit3.h>
#include <gtsam/inference/Symbol.h>
#include <gtsam/navigation/ImuBias.h>
#include <gtsam/navigation/ManifoldEKF.h>
#include <gtsam/nonlinear/Values.h>
#include <iostream>
#include <type_traits>
namespace gtsam {
/**
* Equivariant Filter (EqF) for state estimation on Lie groups.
*
* The EqF estimates a Lie group state X ∈ G and a manifold state ξ ∈ M.
* It uses a symmetry principle where the error dynamics are autonomous in a
* specific frame.
*
* This implementation supports two modes:
* 1. **Automatic**: The filter calculates Jacobian A using the input orbit.
* 2. **Explicit**: You provide the Jacobian A and the manifold covariance Qc
* directly.
*
* @tparam M Manifold type for the physical state.
* @tparam Symmetry Functor encoding the group action on the state.
*/
template <typename M, typename Symmetry>
class EquivariantFilter : public ManifoldEKF<M> {
public:
using Base = ManifoldEKF<M>;
// Manifold traits
static constexpr int DimM = Base::Dim;
using TangentM = typename Base::TangentVector;
using MatrixM = typename Base::Jacobian;
using CovarianceM = typename Base::Covariance;
// Group traits
using G = typename Symmetry::Group;
static constexpr int DimG = traits<G>::dimension;
using TangentG = typename traits<G>::TangentVector;
// Cross-dimension helpers
using MatrixMG = Eigen::Matrix<double, DimM, DimG>;
using MatrixGM = Eigen::Matrix<double, DimG, DimM>;
private:
M xi_ref_; // Origin (reference) state on the manifold
typename Symmetry::Orbit act_on_ref_; // Orbit of the reference state
MatrixMG Dphi0_; // Differential of state action at identity
MatrixGM InnovationLift_; // Innovation lift matrix ((Dphi0)^+)
G g_; // Group element estimate
protected:
/// @return Current reference manifold state.
const M& referenceState() const { return xi_ref_; }
/// Recompute Dphi0 and innovation lift matrix from current reference state.
void recomputeActionDifferentials() {
act_on_ref_ = typename Symmetry::Orbit(xi_ref_);
const G identity_at_g = traits<G>::Compose(g_.inverse(), g_);
act_on_ref_(identity_at_g, &Dphi0_);
InnovationLift_ = Dphi0_.completeOrthogonalDecomposition().pseudoInverse();
}
/// Reset reference, covariance, and group estimate; sync manifold state.
void resetReferenceAndGroup(const M& xi_ref, const CovarianceM& P,
const G& g) {
xi_ref_ = xi_ref;
g_ = g;
recomputeActionDifferentials();
if constexpr (DimM == Eigen::Dynamic) {
this->n_ = traits<M>::GetDimension(xi_ref_);
this->I_ = MatrixM::Identity(this->n_, this->n_);
}
this->P_ = P;
this->X_ = act_on_ref_(g_);
}
/// Set group estimate and update manifold state through the action.
void setGroupEstimateAndSyncState(const G& g) {
g_ = g;
this->X_ = act_on_ref_(g_);
}
/// Set error covariance directly.
void setErrorCovariance(const CovarianceM& P) { this->P_ = P; }
public:
/**
* @brief Initialize the Equivariant Filter.
*
* @param xi_ref Reference manifold state (origin of lifted coordinates).
* @param Sigma Initial covariance on the manifold.
* @param X0 Initial group estimate (default: Identity).
*/
EquivariantFilter(const M& xi_ref, const CovarianceM& Sigma,
const G& X0 = traits<G>::Identity())
: Base(xi_ref, Sigma), xi_ref_(xi_ref), act_on_ref_(xi_ref), g_(X0) {
// Compute differential of action phi at identity (Dphi0)
const G identity_at_g = traits<G>::Compose(g_.inverse(), g_);
act_on_ref_(identity_at_g, &Dphi0_);
// Precompute the Innovation Lift matrix (pseudo-inverse of Dphi0)
InnovationLift_ = Dphi0_.completeOrthogonalDecomposition().pseudoInverse();
this->X_ = act_on_ref_(g_);
}
/// State on the manifold M is given by the base class
using Base::state;
/// errorCovariance that returns P_, on the equivariant filter error
const typename Base::Covariance& errorCovariance() const { return this->P_; }
/// Covariance in the tangent space at the current state.
CovarianceM covariance() const {
MatrixM J;
if constexpr (MatrixM::RowsAtCompileTime == Eigen::Dynamic) {
J.resize(this->n_, this->n_);
}
const typename Symmetry::Diffeomorphism action_at_g(g_);
action_at_g(xi_ref_, &J);
return J.transpose() * this->P_ * J;
}
/// @return Current group estimate.
const G& groupEstimate() const { return g_; }
/**
* @brief Compute the error dynamics matrix A (Automatic).
*
* Calculates A = D_phi|_0 * D_lift|_u0, where u0 is the input mapped to the
* origin.
*
* Concept requirements:
* - `Lift` must be callable as `Lift(u_origin)(xi_ref, D_lift)` where
* D_lift is an OptionalJacobian of shape DimM x DimG.
* - `InputOrbit` must be a group action on the input space with operator()
* that accepts the current group estimate X and returns the mapped input
* (no other methods are required by the filter).
*
* @tparam Lift Functor for the lift Λ(ξ, u).
* @tparam InputOrbit Functor for the input orbit ψ_u.
* @param psi_u Input Orbit instance.
* @return MatrixM The calculated error dynamics matrix A.
*/
template <typename Lift, typename InputOrbit>
MatrixM computeErrorDynamicsMatrix(const InputOrbit& psi_u) const {
MatrixGM D_lift;
// Map current input to origin: u_origin = psi_u(X^-1)
auto u_origin = psi_u(g_.inverse());
// Lift at origin: D_lift = d(Lambda(xi_ref, u_origin))/dxi
Lift lift_u_origin(u_origin);
lift_u_origin(xi_ref_, &D_lift);
return Dphi0_ * D_lift;
}
/**
* @brief Discretize continuous-time error dynamics δ̇ = A δ over dt.
*
* On manifolds (unlike Lie groups) the error stays in a fixed tangent space
* at the chosen origin, so discretization is just the matrix exponential of
* A. K mirrors LieGroupEKF: K=1 gives Euler, K>1 calls expm(A*dt, K).
*/
template <size_t K = 1>
MatrixM transitionMatrix(const MatrixM& A, double dt) const {
if constexpr (K == 1) {
return this->I_ + A * dt;
} else {
return MatrixM(expm(A * dt, K));
}
}
/**
* @brief Propagate the filter state (Automatic).
*
* Automatically computes the error dynamics matrix A.
*
* Concept requirements:
* - `Lift` is used as `Lift(u_origin)(xi_ref_, D_lift)` to obtain the lift
* and its Jacobian w.r.t. the manifold state.
* - `InputOrbit` is only used via `psi_u(X_.inverse())` to map the current
* input to the origin; no other methods are needed.
*
* @tparam K Truncation order for discretization (1 = first order Euler,
* >1 uses matrix exponential expm(A*dt, K)).
* @tparam Lift Functor for the lift Λ(ξ, u).
* @tparam InputOrbit Functor for the input orbit ψ_u.
* @param lift_u Lift functor for the current input.
* @param psi_u Input Orbit for the current input.
* @param A Error dynamics matrix (DimM x DimM).
* @param Qc Process noise covariance on the manifold (continuous-time).
* @param dt Time step.
*/
template <size_t K = 1, typename Lift, typename InputOrbit>
void predict(const Lift& lift_u, const InputOrbit& psi_u, const MatrixM& Qc,
double dt) {
// 1. Compute A automatically
MatrixM A = computeErrorDynamicsMatrix<Lift>(psi_u);
// 2. Delegate to explicit predict with manifold Qc
predictWithJacobian<K>(lift_u, A, Qc, dt);
}
/**
* @brief Propagate the filter state (Explicit).
*
* Uses provided Jacobian A and manifold covariance Qc. This allows `psi_u`
* to be a pure Orbit without needing to implement `inputMatrixB`.
*
* Concept requirements:
* - `Lift` is only used via `Lift(xi_est)` to produce a tangent vector.
* No additional methods are needed for this overload.
*
* @tparam Lift Functor for the lift Λ(ξ, u).
* @param lift_u Lift functor for the current input.
* @param A Error dynamics matrix (DimM x DimM).
* @param Qc Process noise covariance on the manifold (continuous-time).
* @param dt Time step.
*/
template <size_t K = 1, typename Lift>
void predictWithJacobian(const Lift& lift_u, const MatrixM& A,
const MatrixM& Qc, double dt) {
// 1. Mean Propagation on Group
M xi_est = this->state(); // Pure action
TangentG Lambda = lift_u(xi_est); // Pure lift
g_ = traits<G>::Compose(g_, traits<G>::Expmap(Lambda * dt));
M xi_next = act_on_ref_(g_);
// 2. Covariance Propagation on Manifold
MatrixM Phi = transitionMatrix<K>(A, dt);
// Qc is manifold continuous-time covariance: Q_M = Qc * dt
CovarianceM Q_manifold = Qc * dt;
Base::predict(xi_next, Phi, Q_manifold);
}
/**
* Measurement update: Corrects the state and covariance using a
* pre-calculated predicted measurement and its Jacobian.
*
* Overwrites ManifoldEKF::update to modify g_ as well.
*
* @tparam Measurement type of the measurement space.
* @param prediction Predicted measurement.
* @param H Jacobian of the measurement function h.
* @param z Observed measurement.
* @param R Measurement noise covariance.
*/
template <typename Measurement>
void update(
const Measurement& prediction,
const Eigen::Matrix<double, traits<Measurement>::dimension, DimM>& H,
const Measurement& z,
const Eigen::Matrix<double, traits<Measurement>::dimension,
traits<Measurement>::dimension>& R) {
static constexpr int MeasDim = traits<Measurement>::dimension;
// Innovation: y = h(x_pred) - z. In tangent space: local(z, h(x_pred))
// NOTE: we use the `z_hat - z` sign convention, NOT `z - z_hat`.
typename traits<Measurement>::TangentVector innovation =
traits<Measurement>::Local(z, prediction);
// Kalman Gain: K = P H^T S^-1
// K will be Eigen::Matrix<double, Dim, MeasDim>
Eigen::Matrix<double, DimM, MeasDim> K = this->KalmanGain(H, R);
// Correction in Manifold tangent space
// K matches dimensions with innovation, so result is TangentM
TangentM delta_xi = -K * innovation;
// Lift correction to Group tangent space
TangentG delta_x = InnovationLift_ * delta_xi;
g_ = traits<G>::Compose(traits<G>::Expmap(delta_x), g_);
this->X_ = act_on_ref_(g_);
// Update covariance on Manifold using Joseph form
this->JosephUpdate(K, H, R);
}
/// Same API as ManifoldEKF for measurement update with model function.
template <typename Z, typename Func>
void update(Func&& h, const Z& z,
const Eigen::Matrix<double, traits<Z>::dimension,
traits<Z>::dimension>& R) {
static_assert(IsManifold<Z>::value,
"Template parameter Z must be a GTSAM Manifold.");
Matrix H(traits<Z>::GetDimension(z), this->n_);
Z prediction = h(this->X_, H);
update<Z>(prediction, H, z, R);
}
/// Same API as ManifoldEKF for measurement update with vector inputs.
void updateWithVector(const gtsam::Vector& prediction, const Matrix& H,
const gtsam::Vector& z, const Matrix& R) {
this->validateInputs(prediction, H, z, R);
update<Vector>(prediction, H, z, R);
}
/// Vector measurement update using a custom innovation lift delta_x=f(delta_xi).
template <typename InnovationLiftFn>
void updateWithVector(const gtsam::Vector& prediction, const Matrix& H,
const gtsam::Vector& z, const Matrix& R,
InnovationLiftFn&& innovationLift) {
this->validateInputs(prediction, H, z, R);
const gtsam::Vector innovation = traits<Vector>::Local(z, prediction);
const Matrix K = this->KalmanGain(H, R);
const TangentM delta_xi = -K * innovation;
const TangentG delta_x = innovationLift(delta_xi);
g_ = traits<G>::Compose(traits<G>::Expmap(delta_x), g_);
this->X_ = act_on_ref_(g_);
this->JosephUpdate(K, H, R);
}
};
} // namespace gtsam