gtsam  4.0.0
gtsam
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 
22 #pragma once
23 
24 /* GTSAM includes */
28 #include <gtsam/base/debug.h>
29 
30 namespace gtsam {
31 
32 #ifdef GTSAM_TANGENT_PREINTEGRATION
33 typedef TangentPreintegration PreintegrationType;
34 #else
35 typedef ManifoldPreintegration PreintegrationType;
36 #endif
37 
38 /*
39  * If you are using the factor, please cite:
40  * L. Carlone, Z. Kira, C. Beall, V. Indelman, F. Dellaert, "Eliminating
41  * conditionally independent sets in factor graphs: a unifying perspective based
42  * on smart factors", Int. Conf. on Robotics and Automation (ICRA), 2014.
43  *
44  * C. Forster, L. Carlone, F. Dellaert, D. Scaramuzza, "IMU Preintegration on
45  * Manifold for Efficient Visual-Inertial Maximum-a-Posteriori Estimation",
46  * Robotics: Science and Systems (RSS), 2015.
47  *
48  * REFERENCES:
49  * [1] G.S. Chirikjian, "Stochastic Models, Information Theory, and Lie Groups",
50  * Volume 2, 2008.
51  * [2] T. Lupton and S.Sukkarieh, "Visual-Inertial-Aided Navigation for
52  * High-Dynamic Motion in Built Environments Without Initial Conditions",
53  * TRO, 28(1):61-76, 2012.
54  * [3] L. Carlone, S. Williams, R. Roberts, "Preintegrated IMU factor:
55  * Computation of the Jacobian Matrices", Tech. Report, 2013.
56  * [4] C. Forster, L. Carlone, F. Dellaert, D. Scaramuzza, "IMU Preintegration on
57  * Manifold for Efficient Visual-Inertial Maximum-a-Posteriori Estimation",
58  * Robotics: Science and Systems (RSS), 2015.
59  */
60 
71 class PreintegratedImuMeasurements: public PreintegrationType {
72 
73  friend class ImuFactor;
74  friend class ImuFactor2;
75 
76 protected:
77 
78  Matrix9 preintMeasCov_;
79 
83  preintMeasCov_.setZero();
84  }
85 
86 public:
87 
93  PreintegratedImuMeasurements(const boost::shared_ptr<PreintegrationParams>& p,
94  const imuBias::ConstantBias& biasHat = imuBias::ConstantBias()) :
95  PreintegrationType(p, biasHat) {
96  preintMeasCov_.setZero();
97  }
98 
104  PreintegratedImuMeasurements(const PreintegrationType& base, const Matrix9& preintMeasCov)
105  : PreintegrationType(base),
106  preintMeasCov_(preintMeasCov) {
107  }
108 
110  void print(const std::string& s = "Preintegrated Measurements:") const override;
111 
113  bool equals(const PreintegratedImuMeasurements& expected, double tol = 1e-9) const;
114 
116  void resetIntegration() override;
117 
124  void integrateMeasurement(const Vector3& measuredAcc,
125  const Vector3& measuredOmega, const double dt) override;
126 
128  void integrateMeasurements(const Matrix& measuredAccs, const Matrix& measuredOmegas,
129  const Matrix& dts);
130 
132  Matrix preintMeasCov() const { return preintMeasCov_; }
133 
134 #ifdef GTSAM_TANGENT_PREINTEGRATION
135  void mergeWith(const PreintegratedImuMeasurements& pim, Matrix9* H1, Matrix9* H2);
137 #endif
138 
139 #ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V4
143  const Matrix3& measuredAccCovariance,
144  const Matrix3& measuredOmegaCovariance,
145  const Matrix3& integrationErrorCovariance,
146  bool use2ndOrderIntegration = true);
147 
150  void integrateMeasurement(const Vector3& measuredAcc,
151  const Vector3& measuredOmega, double dt,
152  boost::optional<Pose3> body_P_sensor);
153 #endif
154 
155 private:
156 
159  template<class ARCHIVE>
160  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
161  namespace bs = ::boost::serialization;
162  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(PreintegrationType);
163  ar & bs::make_nvp("preintMeasCov_", bs::make_array(preintMeasCov_.data(), preintMeasCov_.size()));
164  }
165 };
166 
179 class ImuFactor: public NoiseModelFactor5<Pose3, Vector3, Pose3, Vector3,
180  imuBias::ConstantBias> {
181 private:
182 
183  typedef ImuFactor This;
184  typedef NoiseModelFactor5<Pose3, Vector3, Pose3, Vector3,
186 
188 
189 public:
190 
192 #if !defined(_MSC_VER) && __GNUC__ == 4 && __GNUC_MINOR__ > 5
193  typedef typename boost::shared_ptr<ImuFactor> shared_ptr;
194 #else
195  typedef boost::shared_ptr<ImuFactor> shared_ptr;
196 #endif
197 
200 
209  ImuFactor(Key pose_i, Key vel_i, Key pose_j, Key vel_j, Key bias,
210  const PreintegratedImuMeasurements& preintegratedMeasurements);
211 
212  virtual ~ImuFactor() {
213  }
214 
216  virtual gtsam::NonlinearFactor::shared_ptr clone() const;
217 
220  GTSAM_EXPORT friend std::ostream& operator<<(std::ostream& os, const ImuFactor&);
221  virtual void print(const std::string& s, const KeyFormatter& keyFormatter =
222  DefaultKeyFormatter) const;
223  virtual bool equals(const NonlinearFactor& expected, double tol = 1e-9) const;
225 
229  return _PIM_;
230  }
231 
234  Vector evaluateError(const Pose3& pose_i, const Vector3& vel_i,
236  const Pose3& pose_j, const Vector3& vel_j,
237  const imuBias::ConstantBias& bias_i, boost::optional<Matrix&> H1 =
238  boost::none, boost::optional<Matrix&> H2 = boost::none,
239  boost::optional<Matrix&> H3 = boost::none, boost::optional<Matrix&> H4 =
240  boost::none, boost::optional<Matrix&> H5 = boost::none) const;
241 
242 #ifdef GTSAM_TANGENT_PREINTEGRATION
243  static PreintegratedImuMeasurements Merge(
245  const PreintegratedImuMeasurements& pim01,
246  const PreintegratedImuMeasurements& pim12);
247 
249  static shared_ptr Merge(const shared_ptr& f01, const shared_ptr& f12);
250 #endif
251 
252 #ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V4
253  typedef PreintegratedImuMeasurements PreintegratedMeasurements;
255 
257  ImuFactor(Key pose_i, Key vel_i, Key pose_j, Key vel_j, Key bias,
258  const PreintegratedMeasurements& preintegratedMeasurements,
259  const Vector3& n_gravity, const Vector3& omegaCoriolis,
260  const boost::optional<Pose3>& body_P_sensor = boost::none,
261  const bool use2ndOrderCoriolis = false);
262 
265  static void Predict(const Pose3& pose_i, const Vector3& vel_i, Pose3& pose_j,
266  Vector3& vel_j, const imuBias::ConstantBias& bias_i,
267  PreintegratedMeasurements& pim, const Vector3& n_gravity,
268  const Vector3& omegaCoriolis, const bool use2ndOrderCoriolis = false);
269 #endif
270 
271 private:
272 
274  friend class boost::serialization::access;
275  template<class ARCHIVE>
276  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
277  ar & boost::serialization::make_nvp("NoiseModelFactor5",
278  boost::serialization::base_object<Base>(*this));
279  ar & BOOST_SERIALIZATION_NVP(_PIM_);
280  }
281 };
282 // class ImuFactor
283 
288 class ImuFactor2 : public NoiseModelFactor3<NavState, NavState, imuBias::ConstantBias> {
289 private:
290 
291  typedef ImuFactor2 This;
293 
295 
296 public:
297 
300 
307  ImuFactor2(Key state_i, Key state_j, Key bias,
308  const PreintegratedImuMeasurements& preintegratedMeasurements);
309 
310  virtual ~ImuFactor2() {
311  }
312 
314  virtual gtsam::NonlinearFactor::shared_ptr clone() const;
315 
318  GTSAM_EXPORT friend std::ostream& operator<<(std::ostream& os, const ImuFactor2&);
319  virtual void print(const std::string& s, const KeyFormatter& keyFormatter =
320  DefaultKeyFormatter) const;
321  virtual bool equals(const NonlinearFactor& expected, double tol = 1e-9) const;
323 
327  return _PIM_;
328  }
329 
332  Vector evaluateError(const NavState& state_i, const NavState& state_j,
334  const imuBias::ConstantBias& bias_i, //
335  boost::optional<Matrix&> H1 = boost::none,
336  boost::optional<Matrix&> H2 = boost::none,
337  boost::optional<Matrix&> H3 = boost::none) const;
338 
339 private:
340 
342  friend class boost::serialization::access;
343  template<class ARCHIVE>
344  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
345  ar & boost::serialization::make_nvp("NoiseModelFactor3",
346  boost::serialization::base_object<Base>(*this));
347  ar & BOOST_SERIALIZATION_NVP(_PIM_);
348  }
349 };
350 // class ImuFactor2
351 
352 template <>
353 struct traits<PreintegratedImuMeasurements> : public Testable<PreintegratedImuMeasurements> {};
354 
355 template <>
356 struct traits<ImuFactor> : public Testable<ImuFactor> {};
357 
358 template <>
359 struct traits<ImuFactor2> : public Testable<ImuFactor2> {};
360 
361 }
Definition: ImuBias.h:30
PreintegratedImuMeasurements()
Default constructor for serialization.
Definition: ImuFactor.h:82
Nonlinear factor base class.
Definition: NonlinearFactor.h:52
friend class boost::serialization::access
Serialization function.
Definition: ImuFactor.h:158
virtual boost::shared_ptr< ManifoldPreintegration > clone() const
Dummy clone for MATLAB.
Definition: ManifoldPreintegration.h:109
This is the base class for all factor types.
Definition: Factor.h:51
boost::shared_ptr< ImuFactor > shared_ptr
Shorthand for a smart pointer to a factor.
Definition: ImuFactor.h:195
A convenient base class for creating your own NoiseModelFactor with 3 variables.
Definition: NonlinearFactor.h:420
const PreintegratedImuMeasurements & preintegratedMeasurements() const
Access the preintegrated measurements.
Definition: ImuFactor.h:228
Definition: ImuFactor.h:179
void integrateMeasurements(const Matrix &measuredAccs, const Matrix &measuredOmegas, const Matrix &dts)
Add multiple measurements, in matrix columns.
Definition: ImuFactor.cpp:80
Definition: Pose3.h:37
Matrix preintMeasCov() const
Return pre-integrated measurement covariance.
Definition: ImuFactor.h:132
Navigation state: Pose (rotation, translation) + velocity NOTE(frank): it does not make sense to make...
Definition: NavState.h:34
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
PreintegratedImuMeasurements(const boost::shared_ptr< PreintegrationParams > &p, const imuBias::ConstantBias &biasHat=imuBias::ConstantBias())
Constructor, initializes the class with no measurements.
Definition: ImuFactor.h:93
Definition: ImuFactor.h:71
PreintegratedImuMeasurements(const PreintegrationType &base, const Matrix9 &preintMeasCov)
Construct preintegrated directly from members: base class and preintMeasCov.
Definition: ImuFactor.h:104
bool equals(const PreintegratedImuMeasurements &expected, double tol=1e-9) const
equals
Definition: ImuFactor.cpp:40
ImuFactor2()
Default constructor - only use for serialization.
Definition: ImuFactor.h:299
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
void resetIntegration() override
Re-initialize PreintegratedIMUMeasurements.
Definition: ImuFactor.cpp:47
A convenient base class for creating your own NoiseModelFactor with 5 variables.
Definition: NonlinearFactor.h:578
void integrateMeasurement(const Vector3 &measuredAcc, const Vector3 &measuredOmega, const double dt) override
Add a single IMU measurement to the preintegration.
Definition: ImuFactor.cpp:53
void print(const std::string &s="Preintegrated Measurements:") const override
print
Definition: ImuFactor.cpp:34
Global debugging flags.
Non-linear factor base classes.
Matrix9 preintMeasCov_
COVARIANCE OF: [PreintPOSITION PreintVELOCITY PreintROTATION].
Definition: ImuFactor.h:78
Definition: ImuFactor.h:288
const PreintegratedImuMeasurements & preintegratedMeasurements() const
Access the preintegrated measurements.
Definition: ImuFactor.h:326
const Params & p() const
const reference to params
Definition: PreintegrationBase.h:116
ImuFactor()
Default constructor - only use for serialization.
Definition: ImuFactor.h:199
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33