GEKF_Rot3Example.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
20 #include <gtsam/base/Matrix.h>
22 #include <gtsam/geometry/Rot3.h>
24 
25 #include <iostream>
26 
27 using namespace std;
28 using namespace gtsam;
29 
30 // --- 1) Closed‐loop dynamics f(X): xi = –k·[φx,φy,0], H = ∂xi/∂φ·Dφ ---
31 static constexpr double k = 0.5;
33  // φ = Logmap(R), Dφ = ∂φ/∂δR
34  Matrix3 D_phi;
35  Vector3 phi = Rot3::Logmap(X, D_phi);
36  // zero out yaw
37  phi[2] = 0.0;
38  D_phi.row(2).setZero();
39 
40  if (H) *H = -k * D_phi; // ∂(–kφ)/∂δR
41  return -k * phi; // xi ∈ 𝔰𝔬(3)
42 }
43 
44 // --- 2) Magnetometer model: z = R⁻¹ m, H = –[z]_× ---
45 static const Vector3 m_world(0, 0, -1);
47  Vector3 z = X.inverse().rotate(m_world);
48  if (H) *H = -skewSymmetric(z);
49  return z;
50 }
51 
52 int main() {
53  // Initial estimate (identity) and covariance
54  const Rot3 R0 = Rot3::RzRyRx(0.1, -0.2, 0.3);
55  const Matrix3 P0 = Matrix3::Identity() * 0.1;
56  LieGroupEKF<Rot3> ekf(R0, P0);
57 
58  // Timestep, process noise, measurement noise
59  double dt = 0.1;
60  Matrix3 Q = Matrix3::Identity() * 0.01;
61  Matrix3 Rm = Matrix3::Identity() * 0.05;
62 
63  cout << "=== Init ===\nR:\n"
64  << ekf.state().matrix() << "\nP:\n"
65  << ekf.covariance() << "\n\n";
66 
67  // Predict using state‐dependent f
68  ekf.predict(dynamicsSO3, dt, Q);
69  cout << "--- After predict ---\nR:\n" << ekf.state().matrix() << "\n\n";
70 
71  // Magnetometer measurement = body‐frame reading of m_world
72  Vector3 z = h_mag(R0);
73  ekf.update(h_mag, z, Rm);
74  cout << "--- After update ---\nR:\n" << ekf.state().matrix() << "\n";
75 
76  return 0;
77 }
H
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange[*:*] noreverse nowriteback set trange[*:*] noreverse nowriteback set urange[*:*] noreverse nowriteback set vrange[*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Definition: gnuplot_common_settings.hh:74
Matrix.h
typedef and functions to augment Eigen's MatrixXd
dt
const double dt
Definition: testVelocityConstraint.cpp:15
X
#define X
Definition: icosphere.cpp:20
gtsam::Vector3
Eigen::Vector3d Vector3
Definition: Vector.h:44
gtsam::skewSymmetric
Matrix3 skewSymmetric(double wx, double wy, double wz)
Definition: base/Matrix.h:381
main
int main()
Definition: GEKF_Rot3Example.cpp:52
Rot3.h
3D rotation represented as a rotation matrix or quaternion
LieGroupEKF.h
Extended Kalman Filter derived class for Lie groups G.
simple::R0
static Rot3 R0
Definition: testInitializePose3.cpp:48
gtsam::ManifoldEKF< G >::covariance
const Covariance & covariance() const
Definition: ManifoldEKF.h:111
gtsam::LieGroupEKF
Extended Kalman Filter on a Lie group G, derived from ManifoldEKF.
Definition: LieGroupEKF.h:49
gtsam::Rot3
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
Definition: Rot3.h:58
gtsam::ManifoldEKF< G >::state
const G & state() const
Definition: ManifoldEKF.h:108
gtsam::LieGroupEKF::predict
void predict(Dynamics &&f, double dt, const Covariance &Q)
Definition: LieGroupEKF.h:129
OptionalJacobian.h
Special class for optional Jacobian arguments.
pybind_wrapper_test_script.z
z
Definition: pybind_wrapper_test_script.py:61
gtsam::ManifoldEKF< G >::update
void update(const Measurement &prediction, const Eigen::Matrix< double, traits< Measurement >::dimension, Dim > &H, const Measurement &z, const Eigen::Matrix< double, traits< Measurement >::dimension, traits< Measurement >::dimension > &R)
Definition: ManifoldEKF.h:152
m_world
static const Vector3 m_world(0, 0, -1)
Eigen::Quaternion
The quaternion class used to represent 3D orientations and rotations.
Definition: ForwardDeclarations.h:293
gtsam
traits
Definition: ABC.h:17
gtsam::OptionalJacobian
Definition: OptionalJacobian.h:38
std
Definition: BFloat16.h:88
gtsam::Rot3::matrix
Matrix3 matrix() const
Definition: Rot3M.cpp:222
P0
static double P0[5]
Definition: ndtri.c:59
dynamicsSO3
Vector3 dynamicsSO3(const Rot3 &X, OptionalJacobian< 3, 3 > H={})
Definition: GEKF_Rot3Example.cpp:32
h_mag
Vector3 h_mag(const Rot3 &X, OptionalJacobian< 3, 3 > H={})
Definition: GEKF_Rot3Example.cpp:46
k
static constexpr double k
Definition: GEKF_Rot3Example.cpp:31


gtsam
Author(s):
autogenerated on Wed May 28 2025 03:01:18