gtsam
Loading...
Searching...
No Matches
IMUFactor.h
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
17
18#pragma once
19
20#include <gtsam/config.h>
21
22#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
23
27
28#include <cassert>
29
30namespace gtsam {
31
39template<class POSE>
40class IMUFactor : public NoiseModelFactorT<Vector6, POSE, POSE> {
41public:
42 typedef NoiseModelFactorT<Vector6, POSE, POSE> Base;
43 typedef IMUFactor<POSE> This;
44
45protected:
46
48 Vector3 accel_, gyro_;
49 double dt_;
50
51public:
52
53 // Provide access to the Matrix& version of evaluateError:
54 using Base::evaluateError;
55
57 IMUFactor(const Vector3& accel, const Vector3& gyro,
58 double dt, const Key& key1, const Key& key2, const SharedNoiseModel& model)
59 : Base(model, key1, key2), accel_(accel), gyro_(gyro), dt_(dt) {}
60
62 IMUFactor(const Vector6& imu_vector,
63 double dt, const Key& key1, const Key& key2, const SharedNoiseModel& model)
64 : Base(model, key1, key2), accel_(imu_vector.head(3)), gyro_(imu_vector.tail(3)), dt_(dt) {}
65
66 ~IMUFactor() override {}
67
69 gtsam::NonlinearFactor::shared_ptr clone() const override {
70 return std::static_pointer_cast<gtsam::NonlinearFactor>(
71 gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
72
74 bool equals(const NonlinearFactor& e, double tol = 1e-9) const override {
75 const This* const f = dynamic_cast<const This*>(&e);
76 return f && Base::equals(e) &&
77 equal_with_abs_tol(accel_, f->accel_, tol) &&
78 equal_with_abs_tol(gyro_, f->gyro_, tol) &&
79 std::abs(dt_ - f->dt_) < tol;
80 }
81
82 void print(const std::string& s="", const gtsam::KeyFormatter& formatter = gtsam::DefaultKeyFormatter) const override {
83 std::string a = "IMUFactor: " + s;
84 Base::print(a, formatter);
85 gtsam::print((Vector)accel_, "accel");
86 gtsam::print((Vector)gyro_, "gyro");
87 std::cout << "dt: " << dt_ << std::endl;
88 }
89
90 // access
91 const Vector3& gyro() const { return gyro_; }
92 const Vector3& accel() const { return accel_; }
93 Vector6 z() const { return (Vector6() << accel_, gyro_).finished(); }
94
99 Vector6 evaluateError(const PoseRTV& x1, const PoseRTV& x2,
100 OptionalMatrixType H1,
101 OptionalMatrixType H2) const override {
102 const Vector6 meas = z();
103 if (H1) *H1 = numericalDerivative21<Vector6, PoseRTV, PoseRTV>(
104 std::bind(This::predict_proxy, std::placeholders::_1, std::placeholders::_2, dt_, meas), x1, x2, 1e-5);
105 if (H2) *H2 = numericalDerivative22<Vector6, PoseRTV, PoseRTV>(
106 std::bind(This::predict_proxy, std::placeholders::_1, std::placeholders::_2, dt_, meas), x1, x2, 1e-5);
107 return predict_proxy(x1, x2, dt_, meas);
108 }
109
111 virtual Vector evaluateError(const Pose3& x1, const Pose3& x2,
112 OptionalMatrixType H1, OptionalMatrixType H2) const {
113 assert(false); // no corresponding factor here
114 return Vector6::Zero();
115 }
116
117private:
119 static Vector6 predict_proxy(const PoseRTV& x1, const PoseRTV& x2,
120 double dt, const Vector6& meas) {
121 Vector6 hx = x1.imuPrediction(x2, dt);
122 return meas - hx;
123 }
124};
125
126} // \namespace gtsam
127
128#endif // GTSAM_ALLOW_DEPRECATED_SINCE_V43
Numerical derivative helpers for manifold-valued functions.
Non-linear factor base classes.
Pose3 with translational velocity.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
bool equal_with_abs_tol(const Eigen::DenseBase< MATRIX > &A, const Eigen::DenseBase< MATRIX > &B, double tol=1e-9)
equals with a tolerance
Definition Matrix.h:81
A convenient base class for creating your own NoiseModelFactor with n variables.
Definition NoiseModelFactorN.h:155