| 
									
										
										
										
											2012-05-04 01:03:16 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file FullIMUFactor.h | 
					
						
							|  |  |  |  * @brief Factor to express an IMU measurement between dynamic poses | 
					
						
							|  |  |  |  * @author Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/base/numericalDerivative.h>
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/NonlinearFactor.h>
 | 
					
						
							| 
									
										
										
										
											2012-05-04 01:03:25 +08:00
										 |  |  | #include <gtsam_unstable/dynamics/PoseRTV.h>
 | 
					
						
							| 
									
										
										
										
											2012-05-04 01:03:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Class that represents integrating IMU measurements over time for dynamic systems | 
					
						
							|  |  |  |  * This factor has dimension 9, with a built-in constraint for velocity modeling | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Templated to allow for different key types, but variables all | 
					
						
							|  |  |  |  * assumed to be PoseRTV | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | template<class POSE> | 
					
						
							| 
									
										
										
										
											2022-12-23 06:25:48 +08:00
										 |  |  | class FullIMUFactor : public NoiseModelFactorN<POSE, POSE> { | 
					
						
							| 
									
										
										
										
											2012-05-04 01:03:16 +08:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-12-23 06:25:48 +08:00
										 |  |  |   typedef NoiseModelFactorN<POSE, POSE> Base; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   typedef FullIMUFactor<POSE> This; | 
					
						
							| 
									
										
										
										
											2012-05-04 01:03:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /** measurements from the IMU */ | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   Vector3 accel_, gyro_; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   double dt_; /// time between measurements
 | 
					
						
							| 
									
										
										
										
											2012-05-04 01:03:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-01-11 05:07:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Provide access to the Matrix& version of evaluateError:
 | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |   using Base::evaluateError; | 
					
						
							| 
									
										
										
										
											2012-05-04 01:03:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /** Standard constructor */ | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   FullIMUFactor(const Vector3& accel, const Vector3& gyro, | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       double dt, const Key& key1, const Key& key2, const SharedNoiseModel& model) | 
					
						
							|  |  |  |   : Base(model, key1, key2), accel_(accel), gyro_(gyro), dt_(dt) { | 
					
						
							|  |  |  |     assert(model->dim() == 9); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Single IMU vector - imu = [accel, gyro] */ | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   FullIMUFactor(const Vector6& imu, | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |       double dt, const Key& key1, const Key& key2, const SharedNoiseModel& model) | 
					
						
							|  |  |  |   : Base(model, key1, key2), accel_(imu.head(3)), gyro_(imu.tail(3)), dt_(dt) { | 
					
						
							|  |  |  |     assert(imu.size() == 6); | 
					
						
							|  |  |  |     assert(model->dim() == 9); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-29 12:02:13 +08:00
										 |  |  |   ~FullIMUFactor() override {} | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /// @return a deep copy of this factor
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |   gtsam::NonlinearFactor::shared_ptr clone() const override { | 
					
						
							| 
									
										
										
										
											2023-01-18 06:39:55 +08:00
										 |  |  |     return std::static_pointer_cast<gtsam::NonlinearFactor>( | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |         gtsam::NonlinearFactor::shared_ptr(new This(*this))); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Check if two factors are equal */ | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |   bool equals(const NonlinearFactor& e, double tol = 1e-9) const override { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     const This* const f = dynamic_cast<const This*>(&e); | 
					
						
							|  |  |  |     return f && Base::equals(e) && | 
					
						
							|  |  |  |         equal_with_abs_tol(accel_, f->accel_, tol) && | 
					
						
							|  |  |  |         equal_with_abs_tol(gyro_, f->gyro_, tol) && | 
					
						
							| 
									
										
										
										
											2019-09-19 03:24:09 +08:00
										 |  |  |         std::abs(dt_ - f->dt_) < tol; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |   void print(const std::string& s="", const gtsam::KeyFormatter& formatter = gtsam::DefaultKeyFormatter) const override { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     std::string a = "FullIMUFactor: " + s; | 
					
						
							|  |  |  |     Base::print(a, formatter); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |     gtsam::print((Vector)accel_, "accel"); | 
					
						
							|  |  |  |     gtsam::print((Vector)gyro_, "gyro"); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     std::cout << "dt: " << dt_ << std::endl; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // access
 | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   const Vector3& gyro() const { return gyro_; } | 
					
						
							|  |  |  |   const Vector3& accel() const { return accel_; } | 
					
						
							| 
									
										
										
										
											2014-11-25 03:14:56 +08:00
										 |  |  |   Vector6 z() const { return (Vector(6) << accel_, gyro_).finished(); } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Error evaluation with optional derivatives - calculates | 
					
						
							|  |  |  |    *  z - h(x1,x2) | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2020-07-26 15:57:54 +08:00
										 |  |  |   Vector evaluateError(const PoseRTV& x1, const PoseRTV& x2, | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       OptionalMatrixType H1, OptionalMatrixType H2) const override { | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |     Vector9 z; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     z.head(3).operator=(accel_); // Strange syntax to work around ambiguous operator error with clang
 | 
					
						
							|  |  |  |     z.segment(3, 3).operator=(gyro_); // Strange syntax to work around ambiguous operator error with clang
 | 
					
						
							| 
									
										
										
										
											2016-02-12 16:06:07 +08:00
										 |  |  |     z.tail(3).operator=(x2.t()); // Strange syntax to work around ambiguous operator error with clang
 | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |     if (H1) *H1 = numericalDerivative21<Vector9, PoseRTV, PoseRTV>( | 
					
						
							| 
									
										
										
										
											2021-07-11 09:01:20 +08:00
										 |  |  |         std::bind(This::predict_proxy, std::placeholders::_1, std::placeholders::_2, dt_), x1, x2, 1e-5); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |     if (H2) *H2 = numericalDerivative22<Vector9, PoseRTV, PoseRTV>( | 
					
						
							| 
									
										
										
										
											2021-07-11 09:01:20 +08:00
										 |  |  |         std::bind(This::predict_proxy, std::placeholders::_1, std::placeholders::_2, dt_), x1, x2, 1e-5); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     return z - predict_proxy(x1, x2, dt_); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** dummy version that fails for non-dynamic poses */ | 
					
						
							|  |  |  |   virtual Vector evaluateError(const Pose3& x1, const Pose3& x2, | 
					
						
							| 
									
										
										
										
											2023-01-10 06:52:56 +08:00
										 |  |  |       OptionalMatrixType H1, OptionalMatrixType H2) const { | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     assert(false); | 
					
						
							| 
									
										
										
										
											2014-12-26 03:04:28 +08:00
										 |  |  |     return Vector6::Zero(); | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-05-04 01:03:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /** copy of the measurement function formulated for numerical derivatives */ | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   static Vector9 predict_proxy(const PoseRTV& x1, const PoseRTV& x2, double dt) { | 
					
						
							|  |  |  |     Vector9 hx; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |     hx.head(6).operator=(x1.imuPrediction(x2, dt)); // Strange syntax to work around ambiguous operator error with clang
 | 
					
						
							| 
									
										
										
										
											2016-02-12 16:06:07 +08:00
										 |  |  |     hx.tail(3).operator=(x1.translationIntegration(x2, dt)); // Strange syntax to work around ambiguous operator error with clang
 | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |     return hx; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-05-04 01:03:16 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // \namespace gtsam
 |