InertialNavFactor_GlobalVelocity.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. /**
  9. * @file InertialNavFactor_GlobalVelocity.h
  10. * @author Vadim Indelman, Stephen Williams
  11. * @brief Inertial navigation factor (velocity in the global frame)
  12. * @date Sept 13, 2012
  13. **/
  14. #pragma once
  15. #include <gtsam/nonlinear/NonlinearFactor.h>
  16. #include <gtsam/linear/NoiseModel.h>
  17. #include <gtsam/geometry/Rot3.h>
  18. #include <gtsam/base/Matrix.h>
  19. // Using numerical derivative to calculate d(Pose3::Expmap)/dw
  20. #include <gtsam/base/numericalDerivative.h>
  21. #include <boost/bind/bind.hpp>
  22. #include <boost/optional.hpp>
  23. #include <ostream>
  24. namespace gtsam {
  25. /*
  26. * NOTES:
  27. * =====
  28. * - The global frame (NED or ENU) is defined by the user by specifying the gravity vector in this frame.
  29. * - The IMU frame is implicitly defined by the user via the rotation matrix between global and imu frames.
  30. * - Camera and IMU frames are identical
  31. * - The user should specify a continuous equivalent noise covariance, which can be calculated using
  32. * the static function CalcEquivalentNoiseCov based on the IMU gyro and acc measurement noise covariance
  33. * matrices and the process\modeling covariance matrix. The IneritalNavFactor converts this into a
  34. * discrete form using the supplied delta_t between sub-sequential measurements.
  35. * - Earth-rate correction:
  36. * + Currently the user should supply R_ECEF_to_G, which is the rotation from ECEF to the global
  37. * frame (Local-Level system: ENU or NED, see above).
  38. * + R_ECEF_to_G can be calculated by approximated values of latitude and longitude of the system.
  39. * + Currently it is assumed that a relatively small distance is traveled w.r.t. to initial pose, since R_ECEF_to_G is constant.
  40. * Otherwise, R_ECEF_to_G should be updated each time using the current lat-lon.
  41. *
  42. * - Frame Notation:
  43. * Quantities are written as {Frame of Representation/Destination Frame}_{Quantity Type}_{Quatity Description/Origination Frame}
  44. * So, the rotational velocity of the sensor written in the body frame is: body_omega_sensor
  45. * And the transformation from the body frame to the world frame would be: world_P_body
  46. * This allows visual chaining. For example, converting the sensed angular velocity of the IMU
  47. * (angular velocity of the sensor in the sensor frame) into the world frame can be performed as:
  48. * world_R_body * body_R_sensor * sensor_omega_sensor = world_omega_sensor
  49. *
  50. *
  51. * - Common Quantity Types
  52. * P : pose/3d transformation
  53. * R : rotation
  54. * omega : angular velocity
  55. * t : translation
  56. * v : velocity
  57. * a : acceleration
  58. *
  59. * - Common Frames
  60. * sensor : the coordinate system attached to the sensor origin
  61. * body : the coordinate system attached to body/inertial frame.
  62. * Unless an optional frame transformation is provided, the
  63. * sensor frame and the body frame will be identical
  64. * world : the global/world coordinate frame. This is assumed to be
  65. * a tangent plane to the earth's surface somewhere near the
  66. * vehicle
  67. */
  68. template<class POSE, class VELOCITY, class IMUBIAS>
  69. class InertialNavFactor_GlobalVelocity : public NoiseModelFactor5<POSE, VELOCITY, IMUBIAS, POSE, VELOCITY> {
  70. private:
  71. typedef InertialNavFactor_GlobalVelocity<POSE, VELOCITY, IMUBIAS> This;
  72. typedef NoiseModelFactor5<POSE, VELOCITY, IMUBIAS, POSE, VELOCITY> Base;
  73. Vector measurement_acc_;
  74. Vector measurement_gyro_;
  75. double dt_;
  76. Vector world_g_;
  77. Vector world_rho_;
  78. Vector world_omega_earth_;
  79. boost::optional<POSE> body_P_sensor_; // The pose of the sensor in the body frame
  80. public:
  81. // shorthand for a smart pointer to a factor
  82. typedef typename boost::shared_ptr<InertialNavFactor_GlobalVelocity> shared_ptr;
  83. /** default constructor - only use for serialization */
  84. InertialNavFactor_GlobalVelocity() {}
  85. /** Constructor */
  86. InertialNavFactor_GlobalVelocity(const Key& Pose1, const Key& Vel1, const Key& IMUBias1, const Key& Pose2, const Key& Vel2,
  87. const Vector& measurement_acc, const Vector& measurement_gyro, const double measurement_dt, const Vector world_g, const Vector world_rho,
  88. const Vector& world_omega_earth, const noiseModel::Gaussian::shared_ptr& model_continuous, boost::optional<POSE> body_P_sensor = boost::none) :
  89. Base(calc_descrete_noise_model(model_continuous, measurement_dt ),
  90. Pose1, Vel1, IMUBias1, Pose2, Vel2), measurement_acc_(measurement_acc), measurement_gyro_(measurement_gyro),
  91. dt_(measurement_dt), world_g_(world_g), world_rho_(world_rho), world_omega_earth_(world_omega_earth), body_P_sensor_(body_P_sensor) { }
  92. ~InertialNavFactor_GlobalVelocity() override {}
  93. /** implement functions needed for Testable */
  94. /** print */
  95. void print(const std::string& s = "InertialNavFactor_GlobalVelocity", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
  96. std::cout << s << "("
  97. << keyFormatter(this->key1()) << ","
  98. << keyFormatter(this->key2()) << ","
  99. << keyFormatter(this->key3()) << ","
  100. << keyFormatter(this->key4()) << ","
  101. << keyFormatter(this->key5()) << "\n";
  102. std::cout << "acc measurement: " << this->measurement_acc_.transpose() << std::endl;
  103. std::cout << "gyro measurement: " << this->measurement_gyro_.transpose() << std::endl;
  104. std::cout << "dt: " << this->dt_ << std::endl;
  105. std::cout << "gravity (in world frame): " << this->world_g_.transpose() << std::endl;
  106. std::cout << "craft rate (in world frame): " << this->world_rho_.transpose() << std::endl;
  107. std::cout << "earth's rotation (in world frame): " << this->world_omega_earth_.transpose() << std::endl;
  108. if(this->body_P_sensor_)
  109. this->body_P_sensor_->print(" sensor pose in body frame: ");
  110. this->noiseModel_->print(" noise model");
  111. }
  112. /** equals */
  113. bool equals(const NonlinearFactor& expected, double tol=1e-9) const override {
  114. const This *e = dynamic_cast<const This*> (&expected);
  115. return e != nullptr && Base::equals(*e, tol)
  116. && (measurement_acc_ - e->measurement_acc_).norm() < tol
  117. && (measurement_gyro_ - e->measurement_gyro_).norm() < tol
  118. && (dt_ - e->dt_) < tol
  119. && (world_g_ - e->world_g_).norm() < tol
  120. && (world_rho_ - e->world_rho_).norm() < tol
  121. && (world_omega_earth_ - e->world_omega_earth_).norm() < tol
  122. && ((!body_P_sensor_ && !e->body_P_sensor_) || (body_P_sensor_ && e->body_P_sensor_ && body_P_sensor_->equals(*e->body_P_sensor_)));
  123. }
  124. POSE predictPose(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1) const {
  125. // Calculate the corrected measurements using the Bias object
  126. Vector GyroCorrected(Bias1.correctGyroscope(measurement_gyro_));
  127. const POSE& world_P1_body = Pose1;
  128. const VELOCITY& world_V1_body = Vel1;
  129. // Calculate the acceleration and angular velocity of the body in the body frame (including earth-related rotations)
  130. Vector body_omega_body;
  131. if(body_P_sensor_) {
  132. body_omega_body = body_P_sensor_->rotation().matrix() * GyroCorrected;
  133. } else {
  134. body_omega_body = GyroCorrected;
  135. }
  136. // Convert earth-related terms into the body frame
  137. Matrix body_R_world(world_P1_body.rotation().inverse().matrix());
  138. Vector body_rho = body_R_world * world_rho_;
  139. Vector body_omega_earth = body_R_world * world_omega_earth_;
  140. // Correct for earth-related terms
  141. body_omega_body -= body_rho + body_omega_earth;
  142. // The velocity is in the global frame, so composing Pose1 with v*dt is incorrect
  143. return POSE(Pose1.rotation() * POSE::Rotation::Expmap(body_omega_body*dt_), Pose1.translation() + typename POSE::Translation(world_V1_body*dt_));
  144. }
  145. VELOCITY predictVelocity(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1) const {
  146. // Calculate the corrected measurements using the Bias object
  147. Vector AccCorrected(Bias1.correctAccelerometer(measurement_acc_));
  148. const POSE& world_P1_body = Pose1;
  149. const VELOCITY& world_V1_body = Vel1;
  150. // Calculate the acceleration and angular velocity of the body in the body frame (including earth-related rotations)
  151. Vector body_a_body, body_omega_body;
  152. if(body_P_sensor_) {
  153. Matrix body_R_sensor = body_P_sensor_->rotation().matrix();
  154. Vector GyroCorrected(Bias1.correctGyroscope(measurement_gyro_));
  155. body_omega_body = body_R_sensor * GyroCorrected;
  156. Matrix body_omega_body__cross = skewSymmetric(body_omega_body);
  157. body_a_body = body_R_sensor * AccCorrected - body_omega_body__cross * body_omega_body__cross * body_P_sensor_->translation();
  158. } else {
  159. body_a_body = AccCorrected;
  160. }
  161. // Correct for earth-related terms
  162. Vector world_a_body = world_P1_body.rotation().matrix() * body_a_body + world_g_ - 2*skewSymmetric(world_rho_ + world_omega_earth_)*world_V1_body;
  163. // Calculate delta in the body frame
  164. VELOCITY VelDelta(world_a_body*dt_);
  165. // Predict
  166. return Vel1 + VelDelta;
  167. }
  168. void predict(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, POSE& Pose2, VELOCITY& Vel2) const {
  169. Pose2 = predictPose(Pose1, Vel1, Bias1);
  170. Vel2 = predictVelocity(Pose1, Vel1, Bias1);
  171. }
  172. POSE evaluatePoseError(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, const POSE& Pose2, const VELOCITY& Vel2) const {
  173. // Predict
  174. POSE Pose2Pred = predictPose(Pose1, Vel1, Bias1);
  175. // Calculate error
  176. return Pose2.between(Pose2Pred);
  177. }
  178. VELOCITY evaluateVelocityError(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, const POSE& Pose2, const VELOCITY& Vel2) const {
  179. // Predict
  180. VELOCITY Vel2Pred = predictVelocity(Pose1, Vel1, Bias1);
  181. // Calculate error
  182. return Vel2Pred - Vel2;
  183. }
  184. /** implement functions needed to derive from Factor */
  185. Vector evaluateError(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, const POSE& Pose2, const VELOCITY& Vel2,
  186. boost::optional<Matrix&> H1 = boost::none,
  187. boost::optional<Matrix&> H2 = boost::none,
  188. boost::optional<Matrix&> H3 = boost::none,
  189. boost::optional<Matrix&> H4 = boost::none,
  190. boost::optional<Matrix&> H5 = boost::none) const override {
  191. // TODO: Write analytical derivative calculations
  192. // Jacobian w.r.t. Pose1
  193. if (H1){
  194. Matrix H1_Pose = gtsam::numericalDerivative11<POSE, POSE>(
  195. std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
  196. this, std::placeholders::_1, Vel1, Bias1, Pose2, Vel2),
  197. Pose1);
  198. Matrix H1_Vel = gtsam::numericalDerivative11<VELOCITY, POSE>(
  199. std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
  200. this, std::placeholders::_1, Vel1, Bias1, Pose2, Vel2),
  201. Pose1);
  202. *H1 = stack(2, &H1_Pose, &H1_Vel);
  203. }
  204. // Jacobian w.r.t. Vel1
  205. if (H2){
  206. if (Vel1.size()!=3) throw std::runtime_error("Frank's hack to make this compile will not work if size != 3");
  207. Matrix H2_Pose = gtsam::numericalDerivative11<POSE, Vector3>(
  208. std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
  209. this, Pose1, std::placeholders::_1, Bias1, Pose2, Vel2),
  210. Vel1);
  211. Matrix H2_Vel = gtsam::numericalDerivative11<Vector3, Vector3>(
  212. std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
  213. this, Pose1, std::placeholders::_1, Bias1, Pose2, Vel2),
  214. Vel1);
  215. *H2 = stack(2, &H2_Pose, &H2_Vel);
  216. }
  217. // Jacobian w.r.t. IMUBias1
  218. if (H3){
  219. Matrix H3_Pose = gtsam::numericalDerivative11<POSE, IMUBIAS>(
  220. std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
  221. this, Pose1, Vel1, std::placeholders::_1, Pose2, Vel2),
  222. Bias1);
  223. Matrix H3_Vel = gtsam::numericalDerivative11<VELOCITY, IMUBIAS>(
  224. std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
  225. this, Pose1, Vel1, std::placeholders::_1, Pose2, Vel2),
  226. Bias1);
  227. *H3 = stack(2, &H3_Pose, &H3_Vel);
  228. }
  229. // Jacobian w.r.t. Pose2
  230. if (H4){
  231. Matrix H4_Pose = gtsam::numericalDerivative11<POSE, POSE>(
  232. std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
  233. this, Pose1, Vel1, Bias1, std::placeholders::_1, Vel2),
  234. Pose2);
  235. Matrix H4_Vel = gtsam::numericalDerivative11<VELOCITY, POSE>(
  236. std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
  237. this, Pose1, Vel1, Bias1, std::placeholders::_1, Vel2),
  238. Pose2);
  239. *H4 = stack(2, &H4_Pose, &H4_Vel);
  240. }
  241. // Jacobian w.r.t. Vel2
  242. if (H5){
  243. if (Vel2.size()!=3) throw std::runtime_error("Frank's hack to make this compile will not work if size != 3");
  244. Matrix H5_Pose = gtsam::numericalDerivative11<POSE, Vector3>(
  245. std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
  246. this, Pose1, Vel1, Bias1, Pose2, std::placeholders::_1),
  247. Vel2);
  248. Matrix H5_Vel = gtsam::numericalDerivative11<Vector3, Vector3>(
  249. std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
  250. this, Pose1, Vel1, Bias1, Pose2, std::placeholders::_1),
  251. Vel2);
  252. *H5 = stack(2, &H5_Pose, &H5_Vel);
  253. }
  254. Vector ErrPoseVector(POSE::Logmap(evaluatePoseError(Pose1, Vel1, Bias1, Pose2, Vel2)));
  255. Vector ErrVelVector(evaluateVelocityError(Pose1, Vel1, Bias1, Pose2, Vel2));
  256. return concatVectors(2, &ErrPoseVector, &ErrVelVector);
  257. }
  258. static inline noiseModel::Gaussian::shared_ptr CalcEquivalentNoiseCov(const noiseModel::Gaussian::shared_ptr& gaussian_acc, const noiseModel::Gaussian::shared_ptr& gaussian_gyro,
  259. const noiseModel::Gaussian::shared_ptr& gaussian_process){
  260. Matrix cov_acc = ( gaussian_acc->R().transpose() * gaussian_acc->R() ).inverse();
  261. Matrix cov_gyro = ( gaussian_gyro->R().transpose() * gaussian_gyro->R() ).inverse();
  262. Matrix cov_process = ( gaussian_process->R().transpose() * gaussian_process->R() ).inverse();
  263. cov_process.block(0,0, 3,3) += cov_gyro;
  264. cov_process.block(6,6, 3,3) += cov_acc;
  265. return noiseModel::Gaussian::Covariance(cov_process);
  266. }
  267. static inline void Calc_g_rho_omega_earth_NED(const Vector& Pos_NED, const Vector& Vel_NED, const Vector& LatLonHeight_IC, const Vector& Pos_NED_Initial,
  268. Vector& g_NED, Vector& rho_NED, Vector& omega_earth_NED) {
  269. Matrix ENU_to_NED = (Matrix(3, 3) <<
  270. 0.0, 1.0, 0.0,
  271. 1.0, 0.0, 0.0,
  272. 0.0, 0.0, -1.0).finished();
  273. Matrix NED_to_ENU = (Matrix(3, 3) <<
  274. 0.0, 1.0, 0.0,
  275. 1.0, 0.0, 0.0,
  276. 0.0, 0.0, -1.0).finished();
  277. // Convert incoming parameters to ENU
  278. Vector Pos_ENU = NED_to_ENU * Pos_NED;
  279. Vector Vel_ENU = NED_to_ENU * Vel_NED;
  280. Vector Pos_ENU_Initial = NED_to_ENU * Pos_NED_Initial;
  281. // Call ENU version
  282. Vector g_ENU;
  283. Vector rho_ENU;
  284. Vector omega_earth_ENU;
  285. Calc_g_rho_omega_earth_ENU(Pos_ENU, Vel_ENU, LatLonHeight_IC, Pos_ENU_Initial, g_ENU, rho_ENU, omega_earth_ENU);
  286. // Convert output to NED
  287. g_NED = ENU_to_NED * g_ENU;
  288. rho_NED = ENU_to_NED * rho_ENU;
  289. omega_earth_NED = ENU_to_NED * omega_earth_ENU;
  290. }
  291. static inline void Calc_g_rho_omega_earth_ENU(const Vector& Pos_ENU, const Vector& Vel_ENU, const Vector& LatLonHeight_IC, const Vector& Pos_ENU_Initial,
  292. Vector& g_ENU, Vector& rho_ENU, Vector& omega_earth_ENU){
  293. double R0 = 6.378388e6;
  294. double e = 1/297;
  295. double Re( R0*( 1-e*(sin( LatLonHeight_IC(0) ))*(sin( LatLonHeight_IC(0) )) ) );
  296. // Calculate current lat, lon
  297. Vector delta_Pos_ENU(Pos_ENU - Pos_ENU_Initial);
  298. double delta_lat(delta_Pos_ENU(1)/Re);
  299. double delta_lon(delta_Pos_ENU(0)/(Re*cos(LatLonHeight_IC(0))));
  300. double lat_new(LatLonHeight_IC(0) + delta_lat);
  301. double lon_new(LatLonHeight_IC(1) + delta_lon);
  302. // Rotation of lon about z axis
  303. Rot3 C1(cos(lon_new), sin(lon_new), 0.0,
  304. -sin(lon_new), cos(lon_new), 0.0,
  305. 0.0, 0.0, 1.0);
  306. // Rotation of lat about y axis
  307. Rot3 C2(cos(lat_new), 0.0, sin(lat_new),
  308. 0.0, 1.0, 0.0,
  309. -sin(lat_new), 0.0, cos(lat_new));
  310. Rot3 UEN_to_ENU(0, 1, 0,
  311. 0, 0, 1,
  312. 1, 0, 0);
  313. Rot3 R_ECEF_to_ENU( UEN_to_ENU * C2 * C1 );
  314. Vector omega_earth_ECEF(Vector3(0.0, 0.0, 7.292115e-5));
  315. omega_earth_ENU = R_ECEF_to_ENU.matrix() * omega_earth_ECEF;
  316. // Calculating g
  317. double height(LatLonHeight_IC(2));
  318. double EQUA_RADIUS = 6378137.0; // equatorial radius of the earth; WGS-84
  319. double ECCENTRICITY = 0.0818191908426; // eccentricity of the earth ellipsoid
  320. double e2( pow(ECCENTRICITY,2) );
  321. double den( 1-e2*pow(sin(lat_new),2) );
  322. double Rm( (EQUA_RADIUS*(1-e2))/( pow(den,(3/2)) ) );
  323. double Rp( EQUA_RADIUS/( sqrt(den) ) );
  324. double Ro( sqrt(Rp*Rm) ); // mean earth radius of curvature
  325. double g0( 9.780318*( 1 + 5.3024e-3 * pow(sin(lat_new),2) - 5.9e-6 * pow(sin(2*lat_new),2) ) );
  326. double g_calc( g0/( pow(1 + height/Ro, 2) ) );
  327. g_ENU = (Vector(3) << 0.0, 0.0, -g_calc).finished();
  328. // Calculate rho
  329. double Ve( Vel_ENU(0) );
  330. double Vn( Vel_ENU(1) );
  331. double rho_E = -Vn/(Rm + height);
  332. double rho_N = Ve/(Rp + height);
  333. double rho_U = Ve*tan(lat_new)/(Rp + height);
  334. rho_ENU = (Vector(3) << rho_E, rho_N, rho_U).finished();
  335. }
  336. static inline noiseModel::Gaussian::shared_ptr calc_descrete_noise_model(const noiseModel::Gaussian::shared_ptr& model, double delta_t){
  337. /* Q_d (approx)= Q * delta_t */
  338. /* In practice, square root of the information matrix is represented, so that:
  339. * R_d (approx)= R / sqrt(delta_t)
  340. * */
  341. return noiseModel::Gaussian::SqrtInformation(model->R()/std::sqrt(delta_t));
  342. }
  343. private:
  344. /** Serialization function */
  345. friend class boost::serialization::access;
  346. template<class ARCHIVE>
  347. void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
  348. ar & boost::serialization::make_nvp("NonlinearFactor2",
  349. boost::serialization::base_object<Base>(*this));
  350. }
  351. }; // \class InertialNavFactor_GlobalVelocity
  352. /// traits
  353. template<class POSE, class VELOCITY, class IMUBIAS>
  354. struct traits<InertialNavFactor_GlobalVelocity<POSE, VELOCITY, IMUBIAS> > :
  355. public Testable<InertialNavFactor_GlobalVelocity<POSE, VELOCITY, IMUBIAS> > {
  356. };
  357. } /// namespace aspn