gtsam
Loading...
Searching...
No Matches
InertialNavFactor_GlobalVelocity.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
20
21#pragma once
22
23#include <gtsam/config.h>
24
25#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
26
30#include <gtsam/geometry/Rot3.h>
31#include <gtsam/base/Matrix.h>
32
33// Using numerical derivative to calculate d(Pose3::Expmap)/dw
35
36#include <ostream>
37
38namespace gtsam {
39
40/*
41 * NOTES:
42 * =====
43 * - The global frame (NED or ENU) is defined by the user by specifying the gravity vector in this frame.
44 * - The IMU frame is implicitly defined by the user via the rotation matrix between global and imu frames.
45 * - Camera and IMU frames are identical
46 * - The user should specify a continuous equivalent noise covariance, which can be calculated using
47 * the static function CalcEquivalentNoiseCov based on the IMU gyro and acc measurement noise covariance
48 * matrices and the process\modeling covariance matrix. The IneritalNavFactor converts this into a
49 * discrete form using the supplied delta_t between sub-sequential measurements.
50 * - Earth-rate correction:
51 * + Currently the user should supply R_ECEF_to_G, which is the rotation from ECEF to the global
52 * frame (Local-Level system: ENU or NED, see above).
53 * + R_ECEF_to_G can be calculated by approximated values of latitude and longitude of the system.
54 * + Currently it is assumed that a relatively small distance is traveled w.r.t. to initial pose, since R_ECEF_to_G is constant.
55 * Otherwise, R_ECEF_to_G should be updated each time using the current lat-lon.
56 *
57 * - Frame Notation:
58 * Quantities are written as {Frame of Representation/Destination Frame}_{Quantity Type}_{Quatity Description/Origination Frame}
59 * So, the rotational velocity of the sensor written in the body frame is: body_omega_sensor
60 * And the transformation from the body frame to the world frame would be: world_P_body
61 * This allows visual chaining. For example, converting the sensed angular velocity of the IMU
62 * (angular velocity of the sensor in the sensor frame) into the world frame can be performed as:
63 * world_R_body * body_R_sensor * sensor_omega_sensor = world_omega_sensor
64 *
65 *
66 * - Common Quantity Types
67 * P : pose/3d transformation
68 * R : rotation
69 * omega : angular velocity
70 * t : translation
71 * v : velocity
72 * a : acceleration
73 *
74 * - Common Frames
75 * sensor : the coordinate system attached to the sensor origin
76 * body : the coordinate system attached to body/inertial frame.
77 * Unless an optional frame transformation is provided, the
78 * sensor frame and the body frame will be identical
79 * world : the global/world coordinate frame. This is assumed to be
80 * a tangent plane to the earth's surface somewhere near the
81 * vehicle
82 */
83template<class POSE, class VELOCITY, class IMUBIAS>
84class InertialNavFactor_GlobalVelocity : public NoiseModelFactorN<POSE, VELOCITY, IMUBIAS, POSE, VELOCITY> {
85
86private:
87
88 typedef InertialNavFactor_GlobalVelocity<POSE, VELOCITY, IMUBIAS> This;
89 typedef NoiseModelFactorN<POSE, VELOCITY, IMUBIAS, POSE, VELOCITY> Base;
90
91 Vector measurement_acc_;
92 Vector measurement_gyro_;
93 double dt_;
94
95 Vector world_g_;
96 Vector world_rho_;
97 Vector world_omega_earth_;
98
99 std::optional<POSE> body_P_sensor_; // The pose of the sensor in the body frame
100
101public:
102
103 // Provide access to the Matrix& version of evaluateError:
104 using Base::evaluateError;
105
106 // shorthand for a smart pointer to a factor
107 typedef typename std::shared_ptr<InertialNavFactor_GlobalVelocity> shared_ptr;
108
110 InertialNavFactor_GlobalVelocity() {}
111
113 InertialNavFactor_GlobalVelocity(const Key& Pose1, const Key& Vel1, const Key& IMUBias1, const Key& Pose2, const Key& Vel2,
114 const Vector& measurement_acc, const Vector& measurement_gyro, const double measurement_dt, const Vector world_g, const Vector world_rho,
115 const Vector& world_omega_earth, const noiseModel::Gaussian::shared_ptr& model_continuous, std::optional<POSE> body_P_sensor = {}) :
116 Base(calc_descrete_noise_model(model_continuous, measurement_dt ),
117 Pose1, Vel1, IMUBias1, Pose2, Vel2), measurement_acc_(measurement_acc), measurement_gyro_(measurement_gyro),
118 dt_(measurement_dt), world_g_(world_g), world_rho_(world_rho), world_omega_earth_(world_omega_earth), body_P_sensor_(body_P_sensor) { }
119
120 ~InertialNavFactor_GlobalVelocity() override {}
121
123
125 void print(const std::string& s = "InertialNavFactor_GlobalVelocity", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
126 std::cout << s << "("
127 << keyFormatter(this->key1()) << ","
128 << keyFormatter(this->key2()) << ","
129 << keyFormatter(this->key3()) << ","
130 << keyFormatter(this->key4()) << ","
131 << keyFormatter(this->key5()) << "\n";
132 std::cout << "acc measurement: " << this->measurement_acc_.transpose() << std::endl;
133 std::cout << "gyro measurement: " << this->measurement_gyro_.transpose() << std::endl;
134 std::cout << "dt: " << this->dt_ << std::endl;
135 std::cout << "gravity (in world frame): " << this->world_g_.transpose() << std::endl;
136 std::cout << "craft rate (in world frame): " << this->world_rho_.transpose() << std::endl;
137 std::cout << "earth's rotation (in world frame): " << this->world_omega_earth_.transpose() << std::endl;
138 if(this->body_P_sensor_)
139 this->body_P_sensor_->print(" sensor pose in body frame: ");
140 this->noiseModel_->print(" noise model");
141 }
142
144 bool equals(const NonlinearFactor& expected, double tol=1e-9) const override {
145 const This *e = dynamic_cast<const This*> (&expected);
146 return e != nullptr && Base::equals(*e, tol)
147 && (measurement_acc_ - e->measurement_acc_).norm() < tol
148 && (measurement_gyro_ - e->measurement_gyro_).norm() < tol
149 && (dt_ - e->dt_) < tol
150 && (world_g_ - e->world_g_).norm() < tol
151 && (world_rho_ - e->world_rho_).norm() < tol
152 && (world_omega_earth_ - e->world_omega_earth_).norm() < tol
153 && ((!body_P_sensor_ && !e->body_P_sensor_) || (body_P_sensor_ && e->body_P_sensor_ && body_P_sensor_->equals(*e->body_P_sensor_)));
154 }
155
156 POSE predictPose(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1) const {
157 // Calculate the corrected measurements using the Bias object
158 Vector GyroCorrected(Bias1.correctGyroscope(measurement_gyro_));
159
160 const POSE& world_P1_body = Pose1;
161 const VELOCITY& world_V1_body = Vel1;
162
163 // Calculate the acceleration and angular velocity of the body in the body frame (including earth-related rotations)
164 Vector body_omega_body;
165 if(body_P_sensor_) {
166 body_omega_body = body_P_sensor_->rotation().matrix() * GyroCorrected;
167 } else {
168 body_omega_body = GyroCorrected;
169 }
170
171 // Convert earth-related terms into the body frame
172 Matrix body_R_world(world_P1_body.rotation().inverse().matrix());
173 Vector body_rho = body_R_world * world_rho_;
174 Vector body_omega_earth = body_R_world * world_omega_earth_;
175
176 // Correct for earth-related terms
177 body_omega_body -= body_rho + body_omega_earth;
178
179 // The velocity is in the global frame, so composing Pose1 with v*dt is incorrect
180 return POSE(Pose1.rotation() * POSE::Rotation::Expmap(body_omega_body*dt_), Pose1.translation() + typename POSE::Translation(world_V1_body*dt_));
181 }
182
183 VELOCITY predictVelocity(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1) const {
184 // Calculate the corrected measurements using the Bias object
185 Vector AccCorrected(Bias1.correctAccelerometer(measurement_acc_));
186
187 const POSE& world_P1_body = Pose1;
188 const VELOCITY& world_V1_body = Vel1;
189
190 // Calculate the acceleration and angular velocity of the body in the body frame (including earth-related rotations)
191 Vector body_a_body, body_omega_body;
192 if(body_P_sensor_) {
193 Matrix body_R_sensor = body_P_sensor_->rotation().matrix();
194
195 Vector GyroCorrected(Bias1.correctGyroscope(measurement_gyro_));
196 body_omega_body = body_R_sensor * GyroCorrected;
197 Matrix body_omega_body__cross = skewSymmetric(body_omega_body);
198 body_a_body = body_R_sensor * AccCorrected - body_omega_body__cross * body_omega_body__cross * body_P_sensor_->translation();
199 } else {
200 body_a_body = AccCorrected;
201 }
202
203 // Correct for earth-related terms
204 Vector world_a_body = world_P1_body.rotation().matrix() * body_a_body + world_g_ - 2*skewSymmetric(world_rho_ + world_omega_earth_)*world_V1_body;
205
206 // Calculate delta in the body frame
207 VELOCITY VelDelta(world_a_body*dt_);
208
209 // Predict
210 return Vel1 + VelDelta;
211 }
212
213 void predict(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, POSE& Pose2, VELOCITY& Vel2) const {
214 Pose2 = predictPose(Pose1, Vel1, Bias1);
215 Vel2 = predictVelocity(Pose1, Vel1, Bias1);
216 }
217
218 POSE evaluatePoseError(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, const POSE& Pose2, const VELOCITY& Vel2) const {
219 // Predict
220 POSE Pose2Pred = predictPose(Pose1, Vel1, Bias1);
221
222 // Calculate error
223 return Pose2.between(Pose2Pred);
224 }
225
226 VELOCITY evaluateVelocityError(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, const POSE& Pose2, const VELOCITY& Vel2) const {
227 // Predict
228 VELOCITY Vel2Pred = predictVelocity(Pose1, Vel1, Bias1);
229
230 // Calculate error
231 return Vel2Pred - Vel2;
232 }
233
235 Vector evaluateError(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, const POSE& Pose2, const VELOCITY& Vel2,
236 OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3, OptionalMatrixType H4,
237 OptionalMatrixType H5) const override {
238
239 // TODO: Write analytical derivative calculations
240 // Jacobian w.r.t. Pose1
241 if (H1){
243 std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
244 this, std::placeholders::_1, Vel1, Bias1, Pose2, Vel2),
245 Pose1);
247 std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
248 this, std::placeholders::_1, Vel1, Bias1, Pose2, Vel2),
249 Pose1);
250 *H1 = stack(std::vector<Matrix>{H1_Pose, H1_Vel});
251 }
252
253 // Jacobian w.r.t. Vel1
254 if (H2){
255 if (Vel1.size()!=3) throw std::runtime_error("Frank's hack to make this compile will not work if size != 3");
257 std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
258 this, Pose1, std::placeholders::_1, Bias1, Pose2, Vel2),
259 Vel1);
261 std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
262 this, Pose1, std::placeholders::_1, Bias1, Pose2, Vel2),
263 Vel1);
264 *H2 = stack(std::vector<Matrix>{H2_Pose, H2_Vel});
265 }
266
267 // Jacobian w.r.t. IMUBias1
268 if (H3){
270 std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
271 this, Pose1, Vel1, std::placeholders::_1, Pose2, Vel2),
272 Bias1);
274 std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
275 this, Pose1, Vel1, std::placeholders::_1, Pose2, Vel2),
276 Bias1);
277 *H3 = stack(std::vector<Matrix>{H3_Pose, H3_Vel});
278 }
279
280 // Jacobian w.r.t. Pose2
281 if (H4){
283 std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
284 this, Pose1, Vel1, Bias1, std::placeholders::_1, Vel2),
285 Pose2);
287 std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
288 this, Pose1, Vel1, Bias1, std::placeholders::_1, Vel2),
289 Pose2);
290 *H4 = stack(std::vector<Matrix>{H4_Pose, H4_Vel});
291 }
292
293 // Jacobian w.r.t. Vel2
294 if (H5){
295 if (Vel2.size()!=3) throw std::runtime_error("Frank's hack to make this compile will not work if size != 3");
297 std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
298 this, Pose1, Vel1, Bias1, Pose2, std::placeholders::_1),
299 Vel2);
301 std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
302 this, Pose1, Vel1, Bias1, Pose2, std::placeholders::_1),
303 Vel2);
304 *H5 = stack(std::vector<Matrix>{H5_Pose, H5_Vel});
305 }
306
307 Vector ErrPoseVector(POSE::Logmap(evaluatePoseError(Pose1, Vel1, Bias1, Pose2, Vel2)));
308 Vector ErrVelVector(evaluateVelocityError(Pose1, Vel1, Bias1, Pose2, Vel2));
309
310 return concatVectors(std::list<Vector>{ErrPoseVector, ErrVelVector});
311 }
312
313 static inline noiseModel::Gaussian::shared_ptr CalcEquivalentNoiseCov(const noiseModel::Gaussian::shared_ptr& gaussian_acc, const noiseModel::Gaussian::shared_ptr& gaussian_gyro,
314 const noiseModel::Gaussian::shared_ptr& gaussian_process){
315
316 Matrix cov_acc = ( gaussian_acc->R().transpose() * gaussian_acc->R() ).inverse();
317 Matrix cov_gyro = ( gaussian_gyro->R().transpose() * gaussian_gyro->R() ).inverse();
318 Matrix cov_process = ( gaussian_process->R().transpose() * gaussian_process->R() ).inverse();
319
320 cov_process.block(0,0, 3,3) += cov_gyro;
321 cov_process.block(6,6, 3,3) += cov_acc;
322
323 return noiseModel::Gaussian::Covariance(cov_process);
324 }
325
326 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,
327 Vector& g_NED, Vector& rho_NED, Vector& omega_earth_NED) {
328 Matrix ENU_to_NED{//
329 {0.0, 1.0, 0.0},
330 {1.0, 0.0, 0.0},
331 {0.0, 0.0, -1.0}};
332
333 Matrix NED_to_ENU{//
334 {0.0, 1.0, 0.0},
335 {1.0, 0.0, 0.0},
336 {0.0, 0.0, -1.0}};
337
338 // Convert incoming parameters to ENU
339 Vector Pos_ENU = NED_to_ENU * Pos_NED;
340 Vector Vel_ENU = NED_to_ENU * Vel_NED;
341 Vector Pos_ENU_Initial = NED_to_ENU * Pos_NED_Initial;
342
343 // Call ENU version
344 Vector g_ENU;
345 Vector rho_ENU;
346 Vector omega_earth_ENU;
347 Calc_g_rho_omega_earth_ENU(Pos_ENU, Vel_ENU, LatLonHeight_IC, Pos_ENU_Initial, g_ENU, rho_ENU, omega_earth_ENU);
348
349 // Convert output to NED
350 g_NED = ENU_to_NED * g_ENU;
351 rho_NED = ENU_to_NED * rho_ENU;
352 omega_earth_NED = ENU_to_NED * omega_earth_ENU;
353 }
354
355 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,
356 Vector& g_ENU, Vector& rho_ENU, Vector& omega_earth_ENU){
357 double R0 = 6.378388e6;
358 double e = 1/297;
359 double Re( R0*( 1-e*(sin( LatLonHeight_IC(0) ))*(sin( LatLonHeight_IC(0) )) ) );
360
361 // Calculate current lat, lon
362 Vector delta_Pos_ENU(Pos_ENU - Pos_ENU_Initial);
363 double delta_lat(delta_Pos_ENU(1)/Re);
364 double delta_lon(delta_Pos_ENU(0)/(Re*cos(LatLonHeight_IC(0))));
365 double lat_new(LatLonHeight_IC(0) + delta_lat);
366 double lon_new(LatLonHeight_IC(1) + delta_lon);
367
368 // Rotation of lon about z axis
369 Rot3 C1(cos(lon_new), sin(lon_new), 0.0,
370 -sin(lon_new), cos(lon_new), 0.0,
371 0.0, 0.0, 1.0);
372
373 // Rotation of lat about y axis
374 Rot3 C2(cos(lat_new), 0.0, sin(lat_new),
375 0.0, 1.0, 0.0,
376 -sin(lat_new), 0.0, cos(lat_new));
377
378 Rot3 UEN_to_ENU(0, 1, 0,
379 0, 0, 1,
380 1, 0, 0);
381
382 Rot3 R_ECEF_to_ENU( UEN_to_ENU * C2 * C1 );
383
384 Vector omega_earth_ECEF(Vector3(0.0, 0.0, 7.292115e-5));
385 omega_earth_ENU = R_ECEF_to_ENU.matrix() * omega_earth_ECEF;
386
387 // Calculating g
388 double height(LatLonHeight_IC(2));
389 double EQUA_RADIUS = 6378137.0; // equatorial radius of the earth; WGS-84
390 double ECCENTRICITY = 0.0818191908426; // eccentricity of the earth ellipsoid
391 double e2( pow(ECCENTRICITY,2) );
392 double den( 1-e2*pow(sin(lat_new),2) );
393 double Rm( (EQUA_RADIUS*(1-e2))/( pow(den,(3/2)) ) );
394 double Rp( EQUA_RADIUS/( sqrt(den) ) );
395 double Ro( sqrt(Rp*Rm) ); // mean earth radius of curvature
396 double g0( 9.780318*( 1 + 5.3024e-3 * pow(sin(lat_new),2) - 5.9e-6 * pow(sin(2*lat_new),2) ) );
397 double g_calc( g0/( pow(1 + height/Ro, 2) ) );
398 g_ENU = Vector{{0.0, 0.0, -g_calc}};
399
400 // Calculate rho
401 double Ve( Vel_ENU(0) );
402 double Vn( Vel_ENU(1) );
403 double rho_E = -Vn/(Rm + height);
404 double rho_N = Ve/(Rp + height);
405 double rho_U = Ve*tan(lat_new)/(Rp + height);
406 rho_ENU = Vector{{rho_E, rho_N, rho_U}};
407 }
408
409 static inline noiseModel::Gaussian::shared_ptr calc_descrete_noise_model(const noiseModel::Gaussian::shared_ptr& model, double delta_t){
410 /* Q_d (approx)= Q * delta_t */
411 /* In practice, square root of the information matrix is represented, so that:
412 * R_d (approx)= R / sqrt(delta_t)
413 * */
414 return noiseModel::Gaussian::SqrtInformation(model->R()/std::sqrt(delta_t));
415 }
416
417private:
418
419#if GTSAM_ENABLE_BOOST_SERIALIZATION
421 friend class boost::serialization::access;
422 template<class ARCHIVE>
423 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
424 ar & boost::serialization::make_nvp("NonlinearFactor2",
425 boost::serialization::base_object<Base>(*this));
426 }
427#endif
428
429}; // \class InertialNavFactor_GlobalVelocity
430
432template<class POSE, class VELOCITY, class IMUBIAS>
433struct traits<InertialNavFactor_GlobalVelocity<POSE, VELOCITY, IMUBIAS> > :
434 public Testable<InertialNavFactor_GlobalVelocity<POSE, VELOCITY, IMUBIAS> > {
435};
436
437}
438
439#endif // GTSAM_ALLOW_DEPRECATED_SINCE_V43
typedef and functions to augment Eigen's MatrixXd
Numerical derivative helpers for manifold-valued functions.
3D rotation represented as a rotation matrix or quaternion
Base class for noise model factors with N variables.
Non-linear factor base classes.
internal::MatrixMN< traits< internal::OutputType< Y, F, X > >::dimension, N >::type numericalDerivative11(F &&h, const X &x, double delta=1e-5)
New-style numerical derivatives using manifold_traits.
Definition numericalDerivative.h:180
Global functions in a separate testing namespace.
Definition chartTesting.h:28
NoiseModelFactorT< Vector, ValueTypes... > NoiseModelFactorN
Noise model factor with N value types and dynamic-sized error vector.
Definition NoiseModelFactorN.h:561
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
Matrix3 skewSymmetric(double wx, double wy, double wz)
skew symmetric matrix returns this: 0 -wz wy wz 0 -wx -wy wx 0
Definition Matrix.h:365
Vector concatVectors(const std::list< Vector > &vs)
concatenate Vectors
Definition Vector.cpp:303
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152