gtsam 4.1.1
gtsam
FullIMUFactor.h
Go to the documentation of this file.
1
7#pragma once
8
12
13#include <boost/bind/bind.hpp>
14
15namespace gtsam {
16
24template<class POSE>
25class FullIMUFactor : public NoiseModelFactor2<POSE, POSE> {
26public:
29
30protected:
31
33 Vector3 accel_, gyro_;
34 double dt_;
35
36public:
37
39 FullIMUFactor(const Vector3& accel, const Vector3& gyro,
40 double dt, const Key& key1, const Key& key2, const SharedNoiseModel& model)
41 : Base(model, key1, key2), accel_(accel), gyro_(gyro), dt_(dt) {
42 assert(model->dim() == 9);
43 }
44
46 FullIMUFactor(const Vector6& imu,
47 double dt, const Key& key1, const Key& key2, const SharedNoiseModel& model)
48 : Base(model, key1, key2), accel_(imu.head(3)), gyro_(imu.tail(3)), dt_(dt) {
49 assert(imu.size() == 6);
50 assert(model->dim() == 9);
51 }
52
53 ~FullIMUFactor() override {}
54
56 gtsam::NonlinearFactor::shared_ptr clone() const override {
57 return boost::static_pointer_cast<gtsam::NonlinearFactor>(
58 gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
59
61 bool equals(const NonlinearFactor& e, double tol = 1e-9) const override {
62 const This* const f = dynamic_cast<const This*>(&e);
63 return f && Base::equals(e) &&
64 equal_with_abs_tol(accel_, f->accel_, tol) &&
65 equal_with_abs_tol(gyro_, f->gyro_, tol) &&
66 std::abs(dt_ - f->dt_) < tol;
67 }
68
69 void print(const std::string& s="", const gtsam::KeyFormatter& formatter = gtsam::DefaultKeyFormatter) const override {
70 std::string a = "FullIMUFactor: " + s;
71 Base::print(a, formatter);
72 gtsam::print((Vector)accel_, "accel");
73 gtsam::print((Vector)gyro_, "gyro");
74 std::cout << "dt: " << dt_ << std::endl;
75 }
76
77 // access
78 const Vector3& gyro() const { return gyro_; }
79 const Vector3& accel() const { return accel_; }
80 Vector6 z() const { return (Vector(6) << accel_, gyro_).finished(); }
81
86 Vector evaluateError(const PoseRTV& x1, const PoseRTV& x2,
87 boost::optional<Matrix&> H1 = boost::none,
88 boost::optional<Matrix&> H2 = boost::none) const override {
89 Vector9 z;
90 z.head(3).operator=(accel_); // Strange syntax to work around ambiguous operator error with clang
91 z.segment(3, 3).operator=(gyro_); // Strange syntax to work around ambiguous operator error with clang
92 z.tail(3).operator=(x2.t()); // Strange syntax to work around ambiguous operator error with clang
93 if (H1) *H1 = numericalDerivative21<Vector9, PoseRTV, PoseRTV>(
94 std::bind(This::predict_proxy, std::placeholders::_1, std::placeholders::_2, dt_), x1, x2, 1e-5);
95 if (H2) *H2 = numericalDerivative22<Vector9, PoseRTV, PoseRTV>(
96 std::bind(This::predict_proxy, std::placeholders::_1, std::placeholders::_2, dt_), x1, x2, 1e-5);
97 return z - predict_proxy(x1, x2, dt_);
98 }
99
101 virtual Vector evaluateError(const Pose3& x1, const Pose3& x2,
102 boost::optional<Matrix&> H1 = boost::none,
103 boost::optional<Matrix&> H2 = boost::none) const {
104 assert(false);
105 return Vector6::Zero();
106 }
107
108private:
109
111 static Vector9 predict_proxy(const PoseRTV& x1, const PoseRTV& x2, double dt) {
112 Vector9 hx;
113 hx.head(6).operator=(x1.imuPrediction(x2, dt)); // Strange syntax to work around ambiguous operator error with clang
114 hx.tail(3).operator=(x1.translationIntegration(x2, dt)); // Strange syntax to work around ambiguous operator error with clang
115 return hx;
116 }
117};
118
119} // \namespace gtsam
Some functions to compute numerical derivatives.
Non-linear factor base classes.
Pose3 with translational velocity.
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:155
noiseModel::Base::shared_ptr SharedNoiseModel
Note, deliberately not in noiseModel namespace.
Definition: NoiseModel.h:736
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:69
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:84
Definition: Pose3.h:37
This is the base class for all factor types.
Definition: Factor.h:56
Nonlinear factor base class.
Definition: NonlinearFactor.h:43
bool equals(const NonlinearFactor &f, double tol=1e-9) const override
Check if two factors are equal.
Definition: NonlinearFactor.cpp:71
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Print.
Definition: NonlinearFactor.cpp:63
A convenient base class for creating your own NoiseModelFactor with 2 variables.
Definition: NonlinearFactor.h:369
Key key1() const
methods to retrieve both keys
Definition: NonlinearFactor.h:401
Class that represents integrating IMU measurements over time for dynamic systems This factor has dime...
Definition: FullIMUFactor.h:25
FullIMUFactor(const Vector6 &imu, double dt, const Key &key1, const Key &key2, const SharedNoiseModel &model)
Single IMU vector - imu = [accel, gyro].
Definition: FullIMUFactor.h:46
FullIMUFactor(const Vector3 &accel, const Vector3 &gyro, double dt, const Key &key1, const Key &key2, const SharedNoiseModel &model)
time between measurements
Definition: FullIMUFactor.h:39
gtsam::NonlinearFactor::shared_ptr clone() const override
Definition: FullIMUFactor.h:56
Vector3 accel_
measurements from the IMU
Definition: FullIMUFactor.h:33
virtual Vector evaluateError(const Pose3 &x1, const Pose3 &x2, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const
dummy version that fails for non-dynamic poses
Definition: FullIMUFactor.h:101
void print(const std::string &s="", const gtsam::KeyFormatter &formatter=gtsam::DefaultKeyFormatter) const override
Print.
Definition: FullIMUFactor.h:69
Vector evaluateError(const PoseRTV &x1, const PoseRTV &x2, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none) const override
Error evaluation with optional derivatives - calculates z - h(x1,x2)
Definition: FullIMUFactor.h:86
bool equals(const NonlinearFactor &e, double tol=1e-9) const override
Check if two factors are equal.
Definition: FullIMUFactor.h:61
Robot state for use with IMU measurements.
Definition: PoseRTV.h:23
Point3 translationIntegration(const Rot3 &r2, const Velocity3 &v2, double dt) const
predict measurement and where Point3 for x2 should be, as a way of enforcing a velocity constraint Th...
Definition: PoseRTV.cpp:161
Vector6 imuPrediction(const PoseRTV &x2, double dt) const
Dynamics predictor for both ground and flying robots, given states at 1 and 2 Always move from time 1...
Definition: PoseRTV.cpp:133