| 
									
										
										
										
											2012-05-04 01:03:16 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file IMUFactor.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 | 
					
						
							|  |  |  |  * Templated to allow for different key types, but variables all | 
					
						
							|  |  |  |  * assumed to be PoseRTV | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | template<class POSE> | 
					
						
							|  |  |  | class IMUFactor : public NoiseModelFactor2<POSE, POSE> { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   typedef NoiseModelFactor2<POSE, POSE> Base; | 
					
						
							|  |  |  |   typedef IMUFactor<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: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   /** Standard constructor */ | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   IMUFactor(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) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Full IMU vector specification */ | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |   IMUFactor(const Vector6& imu_vector, | 
					
						
							| 
									
										
										
										
											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_vector.head(3)), gyro_(imu_vector.tail(3)), dt_(dt) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   virtual ~IMUFactor() {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /// @return a deep copy of this factor
 | 
					
						
							|  |  |  |   virtual gtsam::NonlinearFactor::shared_ptr clone() const { | 
					
						
							|  |  |  |     return boost::static_pointer_cast<gtsam::NonlinearFactor>( | 
					
						
							|  |  |  |         gtsam::NonlinearFactor::shared_ptr(new This(*this))); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Check if two factors are equal */ | 
					
						
							|  |  |  |   virtual bool equals(const NonlinearFactor& e, double tol = 1e-9) const { | 
					
						
							|  |  |  |     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) && | 
					
						
							|  |  |  |         fabs(dt_ - f->dt_) < tol; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void print(const std::string& s="", const gtsam::KeyFormatter& formatter = gtsam::DefaultKeyFormatter) const { | 
					
						
							|  |  |  |     std::string a = "IMUFactor: " + 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-23 08:35:27 +08:00
										 |  |  |   Vector6 z() const { return (Vector6() << accel_, gyro_).finished(); } | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * Error evaluation with optional derivatives - calculates | 
					
						
							|  |  |  |    *  z - h(x1,x2) | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   virtual Vector evaluateError(const PoseRTV& x1, const PoseRTV& x2, | 
					
						
							|  |  |  |       boost::optional<Matrix&> H1 = boost::none, | 
					
						
							|  |  |  |       boost::optional<Matrix&> H2 = boost::none) const { | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |     const Vector6 meas = z(); | 
					
						
							|  |  |  |     if (H1) *H1 = numericalDerivative21<Vector6, PoseRTV, PoseRTV>( | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |         boost::bind(This::predict_proxy, _1, _2, dt_, meas), x1, x2, 1e-5); | 
					
						
							| 
									
										
										
										
											2014-11-04 22:41:14 +08:00
										 |  |  |     if (H2) *H2 = numericalDerivative22<Vector6, PoseRTV, PoseRTV>( | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |         boost::bind(This::predict_proxy, _1, _2, dt_, meas), x1, x2, 1e-5); | 
					
						
							|  |  |  |     return predict_proxy(x1, x2, dt_, meas); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** dummy version that fails for non-dynamic poses */ | 
					
						
							|  |  |  |   virtual Vector evaluateError(const Pose3& x1, const Pose3& x2, | 
					
						
							|  |  |  |       boost::optional<Matrix&> H1 = boost::none, | 
					
						
							|  |  |  |       boost::optional<Matrix&> H2 = boost::none) const { | 
					
						
							|  |  |  |     assert(false); // no corresponding factor here
 | 
					
						
							|  |  |  |     return zero(x1.dim()); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											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 Vector6 predict_proxy(const PoseRTV& x1, const PoseRTV& x2, | 
					
						
							|  |  |  |       double dt, const Vector6& meas) { | 
					
						
							|  |  |  |     Vector6 hx = x1.imuPrediction(x2, dt); | 
					
						
							|  |  |  |     return meas - hx; | 
					
						
							| 
									
										
										
										
											2012-10-02 22:40:07 +08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2012-05-04 01:03:16 +08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // \namespace gtsam
 |