forked from borglab/gtsam
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGEKF_Rot3Example.cpp
More file actions
78 lines (62 loc) · 2.44 KB
/
GEKF_Rot3Example.cpp
File metadata and controls
78 lines (62 loc) · 2.44 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
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
/**
* @file GEKF_Rot3Example.cpp
* @brief Left‐Invariant EKF on SO(3) with state‐dependent pitch/roll control
* and a single magnetometer update.
* @date April 25, 2025
* @authors Scott Baker, Matt Kielo, Frank Dellaert
*/
#include <gtsam/base/Matrix.h>
#include <gtsam/base/OptionalJacobian.h>
#include <gtsam/geometry/Rot3.h>
#include <gtsam/navigation/LieGroupEKF.h>
#include <iostream>
using namespace std;
using namespace gtsam;
// --- 1) Closed‐loop dynamics f(X): xi = –k·[φx,φy,0], H = ∂xi/∂φ·Dφ ---
static constexpr double k = 0.5;
Vector3 dynamicsSO3(const Rot3& X, OptionalJacobian<3, 3> H = {}) {
// φ = Logmap(R), Dφ = ∂φ/∂δR
Matrix3 D_phi;
Vector3 phi = Rot3::Logmap(X, D_phi);
// zero out yaw
phi[2] = 0.0;
D_phi.row(2).setZero();
if (H) *H = -k * D_phi; // ∂(–kφ)/∂δR
return -k * phi; // xi ∈ 𝔰𝔬(3)
}
// --- 2) Magnetometer model: z = R⁻¹ m, H = –[z]_× ---
static const Vector3 m_world(0, 0, -1);
Vector3 h_mag(const Rot3& X, OptionalJacobian<3, 3> H = {}) {
Vector3 z = X.inverse().rotate(m_world);
if (H) *H = -skewSymmetric(z);
return z;
}
int main() {
// Initial estimate (identity) and covariance
const Rot3 R0 = Rot3::RzRyRx(0.1, -0.2, 0.3);
const Matrix3 P0 = Matrix3::Identity() * 0.1;
LieGroupEKF<Rot3> ekf(R0, P0);
// Timestep, process noise, measurement noise
double dt = 0.1;
// Continuous-time process noise (scaled by dt inside predict).
Matrix3 Qc = Matrix3::Identity() * 0.1;
Matrix3 Rm = Matrix3::Identity() * 0.05;
cout << "=== Init ===\nR:\n"
<< ekf.state().matrix() << "\nP:\n"
<< ekf.covariance() << "\n\n";
// Predict using state‐dependent f
ekf.predict(dynamicsSO3, dt, Qc);
cout << "--- After predict ---\nR:\n" << ekf.state().matrix() << "\n\n";
// Magnetometer measurement = body‐frame reading of m_world
Vector3 z = h_mag(R0);
ekf.update(h_mag, z, Rm);
cout << "--- After update ---\nR:\n" << ekf.state().matrix() << "\n";
return 0;
}