gtsam
Loading...
Searching...
No Matches
EquivInertialNavFactor_GlobalVel.h
Go to the documentation of this file.
1
2/* ----------------------------------------------------------------------------
3
4 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
5 * Atlanta, Georgia 30332-0415
6 * All Rights Reserved
7 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
8
9 * See LICENSE for the license information
10
11 * -------------------------------------------------------------------------- */
12
21
22#pragma once
23
24#include <gtsam/config.h>
25
26#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
27
31#include <gtsam/geometry/Rot3.h>
32#include <gtsam/base/Matrix.h>
33
34// Using numerical derivative to calculate d(Pose3::Expmap)/dw
36
37#include <ostream>
38
39namespace gtsam {
40
41/*
42 * NOTES:
43 * =====
44 * Concept: Based on [Lupton12tro]
45 * - Pre-integrate IMU measurements using the static function PreIntegrateIMUObservations.
46 * Pre-integrated quantities are expressed in the body system of t0 - the first time instant (in which pre-integration began).
47 * All sensor-to-body transformations are performed here.
48 * - If required, calculate inertial solution by calling the static functions: predictPose_inertial, predictVelocity_inertial.
49 * - When the time is right, incorporate pre-integrated IMU data by creating an EquivInertialNavFactor_GlobalVel factor, which will
50 * relate between navigation variables at the two time instances (t0 and current time).
51 *
52 * Other notes:
53 * - The global frame (NED or ENU) is defined by the user by specifying the gravity vector in this frame.
54 * - The IMU frame is implicitly defined by the user via the rotation matrix between global and imu frames.
55 * - Camera and IMU frames are identical
56 * - The user should specify a continuous equivalent noise covariance, which can be calculated using
57 * the static function CalcEquivalentNoiseCov based on the IMU gyro and acc measurement noise covariance
58 * matrices and the process\modeling covariance matrix. The IneritalNavFactor converts this into a
59 * discrete form using the supplied delta_t between sub-sequential measurements.
60 * - Earth-rate correction:
61 * + Currently the user should supply R_ECEF_to_G, which is the rotation from ECEF to the global
62 * frame (Local-Level system: ENU or NED, see above).
63 * + R_ECEF_to_G can be calculated by approximated values of latitude and longitude of the system.
64 * + Currently it is assumed that a relatively small distance is traveled w.r.t. to initial pose, since R_ECEF_to_G is constant.
65 * Otherwise, R_ECEF_to_G should be updated each time using the current lat-lon.
66 *
67 * - Frame Notation:
68 * Quantities are written as {Frame of Representation/Destination Frame}_{Quantity Type}_{Quatity Description/Origination Frame}
69 * So, the rotational velocity of the sensor written in the body frame is: body_omega_sensor
70 * And the transformation from the body frame to the world frame would be: world_P_body
71 * This allows visual chaining. For example, converting the sensed angular velocity of the IMU
72 * (angular velocity of the sensor in the sensor frame) into the world frame can be performed as:
73 * world_R_body * body_R_sensor * sensor_omega_sensor = world_omega_sensor
74 *
75 *
76 * - Common Quantity Types
77 * P : pose/3d transformation
78 * R : rotation
79 * omega : angular velocity
80 * t : translation
81 * v : velocity
82 * a : acceleration
83 *
84 * - Common Frames
85 * sensor : the coordinate system attached to the sensor origin
86 * body : the coordinate system attached to body/inertial frame.
87 * Unless an optional frame transformation is provided, the
88 * sensor frame and the body frame will be identical
89 * world : the global/world coordinate frame. This is assumed to be
90 * a tangent plane to the earth's surface somewhere near the
91 * vehicle
92 */
93
94template<class POSE, class VELOCITY, class IMUBIAS>
95class EquivInertialNavFactor_GlobalVel : public NoiseModelFactorN<POSE, VELOCITY, IMUBIAS, POSE, VELOCITY> {
96
97private:
98
99 typedef EquivInertialNavFactor_GlobalVel<POSE, VELOCITY, IMUBIAS> This;
100 typedef NoiseModelFactorN<POSE, VELOCITY, IMUBIAS, POSE, VELOCITY> Base;
101
102 Vector delta_pos_in_t0_;
103 Vector delta_vel_in_t0_;
104 Vector3 delta_angles_;
105 double dt12_;
106
107 Vector world_g_;
108 Vector world_rho_;
109 Vector world_omega_earth_;
110
111 Matrix Jacobian_wrt_t0_Overall_;
112
113 std::optional<IMUBIAS> Bias_initial_; // Bias used when pre-integrating IMU measurements
114 std::optional<POSE> body_P_sensor_; // The pose of the sensor in the body frame
115
116public:
117
118 // Provide access to the Matrix& version of evaluateError:
119 using Base::evaluateError;
120
121 // shorthand for a smart pointer to a factor
122 typedef typename std::shared_ptr<EquivInertialNavFactor_GlobalVel> shared_ptr;
123
125 EquivInertialNavFactor_GlobalVel() {}
126
128 EquivInertialNavFactor_GlobalVel(const Key& Pose1, const Key& Vel1, const Key& IMUBias1, const Key& Pose2, const Key& Vel2,
129 const Vector& delta_pos_in_t0, const Vector& delta_vel_in_t0, const Vector3& delta_angles,
130 double dt12, const Vector world_g, const Vector world_rho,
131 const Vector& world_omega_earth, const noiseModel::Gaussian::shared_ptr& model_equivalent,
132 const Matrix& Jacobian_wrt_t0_Overall,
133 std::optional<IMUBIAS> Bias_initial = {}, std::optional<POSE> body_P_sensor = {}) :
134 Base(model_equivalent, Pose1, Vel1, IMUBias1, Pose2, Vel2),
135 delta_pos_in_t0_(delta_pos_in_t0), delta_vel_in_t0_(delta_vel_in_t0), delta_angles_(delta_angles),
136 dt12_(dt12), world_g_(world_g), world_rho_(world_rho), world_omega_earth_(world_omega_earth), Jacobian_wrt_t0_Overall_(Jacobian_wrt_t0_Overall),
137 Bias_initial_(Bias_initial), body_P_sensor_(body_P_sensor) { }
138
139 ~EquivInertialNavFactor_GlobalVel() override {}
140
142
144 void print(const std::string& s = "EquivInertialNavFactor_GlobalVel", const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override {
145 std::cout << s << "("
146 << keyFormatter(this->key1()) << ","
147 << keyFormatter(this->key2()) << ","
148 << keyFormatter(this->key3()) << ","
149 << keyFormatter(this->key4()) << ","
150 << keyFormatter(this->key5()) << "\n";
151 std::cout << "delta_pos_in_t0: " << this->delta_pos_in_t0_.transpose() << std::endl;
152 std::cout << "delta_vel_in_t0: " << this->delta_vel_in_t0_.transpose() << std::endl;
153 std::cout << "delta_angles: " << this->delta_angles_ << std::endl;
154 std::cout << "dt12: " << this->dt12_ << std::endl;
155 std::cout << "gravity (in world frame): " << this->world_g_.transpose() << std::endl;
156 std::cout << "craft rate (in world frame): " << this->world_rho_.transpose() << std::endl;
157 std::cout << "earth's rotation (in world frame): " << this->world_omega_earth_.transpose() << std::endl;
158 if(this->body_P_sensor_)
159 this->body_P_sensor_->print(" sensor pose in body frame: ");
160 this->noiseModel_->print(" noise model");
161 }
162
164 bool equals(const NonlinearFactor& expected, double tol=1e-9) const override {
165 const This *e = dynamic_cast<const This*> (&expected);
166 return e != nullptr && Base::equals(*e, tol)
167 && (delta_pos_in_t0_ - e->delta_pos_in_t0_).norm() < tol
168 && (delta_vel_in_t0_ - e->delta_vel_in_t0_).norm() < tol
169 && (delta_angles_ - e->delta_angles_).norm() < tol
170 && (dt12_ - e->dt12_) < tol
171 && (world_g_ - e->world_g_).norm() < tol
172 && (world_rho_ - e->world_rho_).norm() < tol
173 && (world_omega_earth_ - e->world_omega_earth_).norm() < tol
174 && ((!body_P_sensor_ && !e->body_P_sensor_) || (body_P_sensor_ && e->body_P_sensor_ && body_P_sensor_->equals(*e->body_P_sensor_)));
175 }
176
177
178 POSE predictPose(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1) const {
179
180 // Correct delta_pos_in_t0_ using (Bias1 - Bias_t0)
181 Vector delta_BiasAcc = Bias1.accelerometer();
182 Vector delta_BiasGyro = Bias1.gyroscope();
183 if (Bias_initial_){
184 delta_BiasAcc -= Bias_initial_->accelerometer();
185 delta_BiasGyro -= Bias_initial_->gyroscope();
186 }
187
188 Matrix J_Pos_wrt_BiasAcc = Jacobian_wrt_t0_Overall_.block(4,9,3,3);
189 Matrix J_Pos_wrt_BiasGyro = Jacobian_wrt_t0_Overall_.block(4,12,3,3);
190 Matrix J_angles_wrt_BiasGyro = Jacobian_wrt_t0_Overall_.block(0,12,3,3);
191
192 /* Position term */
193 Vector delta_pos_in_t0_corrected = delta_pos_in_t0_ + J_Pos_wrt_BiasAcc*delta_BiasAcc + J_Pos_wrt_BiasGyro*delta_BiasGyro;
194
195 /* Rotation term */
196 Vector delta_angles_corrected = delta_angles_ + J_angles_wrt_BiasGyro*delta_BiasGyro;
197 // Another alternative:
198 // Vector delta_angles_corrected = Rot3::Logmap( Rot3::Expmap(delta_angles_)*Rot3::Expmap(J_angles_wrt_BiasGyro*delta_BiasGyro) );
199
200 return predictPose_inertial(Pose1, Vel1,
201 delta_pos_in_t0_corrected, delta_angles_corrected,
202 dt12_, world_g_, world_rho_, world_omega_earth_);
203 }
204
205 static inline POSE predictPose_inertial(const POSE& Pose1, const VELOCITY& Vel1,
206 const Vector& delta_pos_in_t0, const Vector3& delta_angles,
207 const double dt12, const Vector& world_g, const Vector& world_rho, const Vector& world_omega_earth){
208
209 const POSE& world_P1_body = Pose1;
210 const VELOCITY& world_V1_body = Vel1;
211
212 /* Position term */
213 Vector body_deltaPos_body = delta_pos_in_t0;
214
215 Vector world_deltaPos_pls_body = world_P1_body.rotation().matrix() * body_deltaPos_body;
216 Vector world_deltaPos_body = world_V1_body * dt12 + 0.5*world_g*dt12*dt12 + world_deltaPos_pls_body;
217
218 // Incorporate earth-related terms. Note - these are assumed to be constant between t1 and t2.
219 world_deltaPos_body -= 2*skewSymmetric(world_rho + world_omega_earth)*world_V1_body * dt12*dt12;
220
221 /* TODO: the term dt12*dt12 in 0.5*world_g*dt12*dt12 is not entirely correct:
222 * the gravity should be canceled from the accelerometer measurements, bust since position
223 * is added with a delta velocity from a previous term, the actual delta time is more complicated.
224 * Need to figure out this in the future - currently because of this issue we'll get some more error
225 * in Z axis.
226 */
227
228 /* Rotation term */
229 Vector body_deltaAngles_body = delta_angles;
230
231 // Convert earth-related terms into the body frame
232 Matrix body_R_world(world_P1_body.rotation().inverse().matrix());
233 Vector body_rho = body_R_world * world_rho;
234 Vector body_omega_earth = body_R_world * world_omega_earth;
235
236 // Incorporate earth-related terms. Note - these are assumed to be constant between t1 and t2.
237 body_deltaAngles_body -= (body_rho + body_omega_earth)*dt12;
238
239 return POSE(Pose1.rotation() * POSE::Rotation::Expmap(body_deltaAngles_body), Pose1.translation() + typename POSE::Translation(world_deltaPos_body));
240
241 }
242
243 VELOCITY predictVelocity(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1) const {
244
245 // Correct delta_vel_in_t0_ using (Bias1 - Bias_t0)
246 Vector delta_BiasAcc = Bias1.accelerometer();
247 Vector delta_BiasGyro = Bias1.gyroscope();
248 if (Bias_initial_){
249 delta_BiasAcc -= Bias_initial_->accelerometer();
250 delta_BiasGyro -= Bias_initial_->gyroscope();
251 }
252
253 Matrix J_Vel_wrt_BiasAcc = Jacobian_wrt_t0_Overall_.block(6,9,3,3);
254 Matrix J_Vel_wrt_BiasGyro = Jacobian_wrt_t0_Overall_.block(6,12,3,3);
255
256 Vector delta_vel_in_t0_corrected = delta_vel_in_t0_ + J_Vel_wrt_BiasAcc*delta_BiasAcc + J_Vel_wrt_BiasGyro*delta_BiasGyro;
257
258 return predictVelocity_inertial(Pose1, Vel1,
259 delta_vel_in_t0_corrected,
260 dt12_, world_g_, world_rho_, world_omega_earth_);
261 }
262
263 static inline VELOCITY predictVelocity_inertial(const POSE& Pose1, const VELOCITY& Vel1,
264 const Vector& delta_vel_in_t0,
265 const double dt12, const Vector& world_g, const Vector& world_rho, const Vector& world_omega_earth) {
266
267 const POSE& world_P1_body = Pose1;
268 const VELOCITY& world_V1_body = Vel1;
269
270 Vector body_deltaVel_body = delta_vel_in_t0;
271 Vector world_deltaVel_body = world_P1_body.rotation().matrix() * body_deltaVel_body;
272
273 VELOCITY VelDelta( world_deltaVel_body + world_g * dt12 );
274
275 // Incorporate earth-related terms. Note - these are assumed to be constant between t1 and t2.
276 VelDelta -= 2*skewSymmetric(world_rho + world_omega_earth)*world_V1_body * dt12;
277
278 // Predict
279 return Vel1 + VelDelta;
280
281 }
282
283 void predict(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, POSE& Pose2, VELOCITY& Vel2) const {
284 Pose2 = predictPose(Pose1, Vel1, Bias1);
285 Vel2 = predictVelocity(Pose1, Vel1, Bias1);
286 }
287
288 POSE evaluatePoseError(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, const POSE& Pose2, const VELOCITY& Vel2) const {
289 // Predict
290 POSE Pose2Pred = predictPose(Pose1, Vel1, Bias1);
291
292 // Luca: difference between Pose2 and Pose2Pred
293 POSE DiffPose( Pose2.rotation().between(Pose2Pred.rotation()), Pose2Pred.translation() - Pose2.translation() );
294// DiffPose = Pose2.between(Pose2Pred);
295 return DiffPose;
296 // Calculate error
297 //return Pose2.between(Pose2Pred);
298 }
299
300 VELOCITY evaluateVelocityError(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, const POSE& Pose2, const VELOCITY& Vel2) const {
301 // Predict
302 VELOCITY Vel2Pred = predictVelocity(Pose1, Vel1, Bias1);
303
304 // Calculate error
305 return Vel2Pred-Vel2;
306 }
307
308 Vector evaluateError(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, const POSE& Pose2, const VELOCITY& Vel2,
309 OptionalMatrixType H1, OptionalMatrixType H2, OptionalMatrixType H3, OptionalMatrixType H4,
310 OptionalMatrixType H5) const override {
311
312 // TODO: Write analytical derivative calculations
313 // Jacobian w.r.t. Pose1
314 if (H1){
315 Matrix H1_Pose = numericalDerivative11<POSE, POSE>(
316 std::bind(&EquivInertialNavFactor_GlobalVel::evaluatePoseError,
317 this, std::placeholders::_1, Vel1, Bias1, Pose2, Vel2),
318 Pose1);
319 Matrix H1_Vel = numericalDerivative11<VELOCITY, POSE>(
320 std::bind(&EquivInertialNavFactor_GlobalVel::evaluateVelocityError,
321 this, std::placeholders::_1, Vel1, Bias1, Pose2, Vel2),
322 Pose1);
323 *H1 = stack(std::vector<Matrix>{H1_Pose, H1_Vel});
324 }
325
326 // Jacobian w.r.t. Vel1
327 if (H2){
328 if (Vel1.size()!=3) throw std::runtime_error("Frank's hack to make this compile will not work if size != 3");
329 Matrix H2_Pose = numericalDerivative11<POSE, Vector3>(
330 std::bind(&EquivInertialNavFactor_GlobalVel::evaluatePoseError,
331 this, Pose1, std::placeholders::_1, Bias1, Pose2, Vel2),
332 Vel1);
333 Matrix H2_Vel = numericalDerivative11<Vector3, Vector3>(
334 std::bind(&EquivInertialNavFactor_GlobalVel::evaluateVelocityError,
335 this, Pose1, std::placeholders::_1, Bias1, Pose2, Vel2),
336 Vel1);
337 *H2 = stack(std::vector<Matrix>{H2_Pose, H2_Vel});
338 }
339
340 // Jacobian w.r.t. IMUBias1
341 if (H3){
342 Matrix H3_Pose = numericalDerivative11<POSE, IMUBIAS>(
343 std::bind(&EquivInertialNavFactor_GlobalVel::evaluatePoseError,
344 this, Pose1, Vel1, std::placeholders::_1, Pose2, Vel2),
345 Bias1);
346 Matrix H3_Vel = numericalDerivative11<VELOCITY, IMUBIAS>(
347 std::bind(&EquivInertialNavFactor_GlobalVel::evaluateVelocityError,
348 this, Pose1, Vel1, std::placeholders::_1, Pose2, Vel2),
349 Bias1);
350 *H3 = stack(std::vector<Matrix>{H3_Pose, H3_Vel});
351 }
352
353 // Jacobian w.r.t. Pose2
354 if (H4){
355 Matrix H4_Pose = numericalDerivative11<POSE, POSE>(
356 std::bind(&EquivInertialNavFactor_GlobalVel::evaluatePoseError,
357 this, Pose1, Vel1, Bias1, std::placeholders::_1, Vel2),
358 Pose2);
359 Matrix H4_Vel = numericalDerivative11<VELOCITY, POSE>(
360 std::bind(&EquivInertialNavFactor_GlobalVel::evaluateVelocityError,
361 this, Pose1, Vel1, Bias1, std::placeholders::_1, Vel2),
362 Pose2);
363 *H4 = stack(std::vector<Matrix>{H4_Pose, H4_Vel});
364 }
365
366 // Jacobian w.r.t. Vel2
367 if (H5){
368 if (Vel2.size()!=3) throw std::runtime_error("Frank's hack to make this compile will not work if size != 3");
369 Matrix H5_Pose = numericalDerivative11<POSE, Vector3>(
370 std::bind(&EquivInertialNavFactor_GlobalVel::evaluatePoseError,
371 this, Pose1, Vel1, Bias1, Pose2, std::placeholders::_1),
372 Vel2);
373 Matrix H5_Vel = numericalDerivative11<Vector3, Vector3>(
374 std::bind(&EquivInertialNavFactor_GlobalVel::evaluateVelocityError,
375 this, Pose1, Vel1, Bias1, Pose2, std::placeholders::_1),
376 Vel2);
377 *H5 = stack(std::vector<Matrix>{H5_Pose, H5_Vel});
378 }
379
380 Vector ErrPoseVector(POSE::Logmap(evaluatePoseError(Pose1, Vel1, Bias1, Pose2, Vel2)));
381 Vector ErrVelVector(evaluateVelocityError(Pose1, Vel1, Bias1, Pose2, Vel2));
382
383 return concatVectors(std::list<Vector>{ErrPoseVector, ErrVelVector});
384 }
385
386
387
388 static inline POSE PredictPoseFromPreIntegration(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1,
389 const Vector& delta_pos_in_t0, const Vector3& delta_angles,
390 double dt12, const Vector world_g, const Vector world_rho,
391 const Vector& world_omega_earth, const Matrix& Jacobian_wrt_t0_Overall,
392 const std::optional<IMUBIAS>& Bias_initial = {}) {
393
394
395 // Correct delta_pos_in_t0_ using (Bias1 - Bias_t0)
396 Vector delta_BiasAcc = Bias1.accelerometer();
397 Vector delta_BiasGyro = Bias1.gyroscope();
398 if (Bias_initial){
399 delta_BiasAcc -= Bias_initial->accelerometer();
400 delta_BiasGyro -= Bias_initial->gyroscope();
401 }
402
403 Matrix J_Pos_wrt_BiasAcc = Jacobian_wrt_t0_Overall.block(4,9,3,3);
404 Matrix J_Pos_wrt_BiasGyro = Jacobian_wrt_t0_Overall.block(4,12,3,3);
405 Matrix J_angles_wrt_BiasGyro = Jacobian_wrt_t0_Overall.block(0,12,3,3);
406
407 /* Position term */
408 Vector delta_pos_in_t0_corrected = delta_pos_in_t0 + J_Pos_wrt_BiasAcc*delta_BiasAcc + J_Pos_wrt_BiasGyro*delta_BiasGyro;
409
410 /* Rotation term */
411 Vector delta_angles_corrected = delta_angles + J_angles_wrt_BiasGyro*delta_BiasGyro;
412 // Another alternative:
413 // Vector delta_angles_corrected = Rot3::Logmap( Rot3::Expmap(delta_angles_)*Rot3::Expmap(J_angles_wrt_BiasGyro*delta_BiasGyro) );
414
415 return predictPose_inertial(Pose1, Vel1, delta_pos_in_t0_corrected, delta_angles_corrected, dt12, world_g, world_rho, world_omega_earth);
416 }
417
418 static inline VELOCITY PredictVelocityFromPreIntegration(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1,
419 const Vector& delta_vel_in_t0, double dt12, const Vector world_g, const Vector world_rho,
420 const Vector& world_omega_earth, const Matrix& Jacobian_wrt_t0_Overall,
421 const std::optional<IMUBIAS>& Bias_initial = {}) {
422
423 // Correct delta_vel_in_t0_ using (Bias1 - Bias_t0)
424 Vector delta_BiasAcc = Bias1.accelerometer();
425 Vector delta_BiasGyro = Bias1.gyroscope();
426 if (Bias_initial){
427 delta_BiasAcc -= Bias_initial->accelerometer();
428 delta_BiasGyro -= Bias_initial->gyroscope();
429 }
430
431 Matrix J_Vel_wrt_BiasAcc = Jacobian_wrt_t0_Overall.block(6,9,3,3);
432 Matrix J_Vel_wrt_BiasGyro = Jacobian_wrt_t0_Overall.block(6,12,3,3);
433
434 Vector delta_vel_in_t0_corrected = delta_vel_in_t0 + J_Vel_wrt_BiasAcc*delta_BiasAcc + J_Vel_wrt_BiasGyro*delta_BiasGyro;
435
436 return predictVelocity_inertial(Pose1, Vel1, delta_vel_in_t0_corrected, dt12, world_g, world_rho, world_omega_earth);
437 }
438
439 static inline void PredictFromPreIntegration(const POSE& Pose1, const VELOCITY& Vel1, const IMUBIAS& Bias1, POSE& Pose2, VELOCITY& Vel2,
440 const Vector& delta_pos_in_t0, const Vector& delta_vel_in_t0, const Vector3& delta_angles,
441 double dt12, const Vector world_g, const Vector world_rho,
442 const Vector& world_omega_earth, const Matrix& Jacobian_wrt_t0_Overall,
443 const std::optional<IMUBIAS>& Bias_initial = {}) {
444
445 Pose2 = PredictPoseFromPreIntegration(Pose1, Vel1, Bias1, delta_pos_in_t0, delta_angles, dt12, world_g, world_rho, world_omega_earth, Jacobian_wrt_t0_Overall, Bias_initial);
446 Vel2 = PredictVelocityFromPreIntegration(Pose1, Vel1, Bias1, delta_vel_in_t0, dt12, world_g, world_rho, world_omega_earth, Jacobian_wrt_t0_Overall, Bias_initial);
447 }
448
449
450 static inline void PreIntegrateIMUObservations(const Vector& msr_acc_t, const Vector& msr_gyro_t, const double msr_dt,
451 Vector& delta_pos_in_t0, Vector3& delta_angles, Vector& delta_vel_in_t0, double& delta_t,
452 const noiseModel::Gaussian::shared_ptr& model_continuous_overall,
453 Matrix& EquivCov_Overall, Matrix& Jacobian_wrt_t0_Overall, const IMUBIAS Bias_t0 = IMUBIAS(),
454 std::optional<POSE> p_body_P_sensor = {}){
455 // Note: all delta terms refer to an IMU\sensor system at t0
456 // Note: Earth-related terms are not accounted here but are incorporated in predict functions.
457
458 POSE body_P_sensor = POSE();
459 bool flag_use_body_P_sensor = false;
460 if (p_body_P_sensor){
461 body_P_sensor = *p_body_P_sensor;
462 flag_use_body_P_sensor = true;
463 }
464
465 delta_pos_in_t0 = PreIntegrateIMUObservations_delta_pos(msr_dt, delta_pos_in_t0, delta_vel_in_t0);
466 delta_vel_in_t0 = PreIntegrateIMUObservations_delta_vel(msr_gyro_t, msr_acc_t, msr_dt, delta_angles, delta_vel_in_t0, flag_use_body_P_sensor, body_P_sensor, Bias_t0);
467 delta_angles = PreIntegrateIMUObservations_delta_angles(msr_gyro_t, msr_dt, delta_angles, flag_use_body_P_sensor, body_P_sensor, Bias_t0);
468
469 delta_t += msr_dt;
470
471 // Update EquivCov_Overall
472 Matrix Z3x3 = Matrix3::Zero();
473 Matrix I3x3 = Matrix3::Identity();
474
475 Matrix H_pos_pos = numericalDerivative11<Vector3, Vector3>(
476 std::bind(&PreIntegrateIMUObservations_delta_pos, msr_dt,
477 std::placeholders::_1, delta_vel_in_t0),
478 delta_pos_in_t0);
479 Matrix H_pos_vel = numericalDerivative11<Vector3, Vector3>(
480 std::bind(&PreIntegrateIMUObservations_delta_pos, msr_dt,
481 delta_pos_in_t0, std::placeholders::_1),
482 delta_vel_in_t0);
483 Matrix H_pos_angles = Z_3x3;
484 Matrix H_pos_bias = collect(std::vector<const Matrix*>{&Z3x3, &Z3x3});
485
486 Matrix H_vel_vel = numericalDerivative11<Vector3, Vector3>(
487 std::bind(&PreIntegrateIMUObservations_delta_vel, msr_gyro_t,
488 msr_acc_t, msr_dt, delta_angles, std::placeholders::_1,
489 flag_use_body_P_sensor, body_P_sensor, Bias_t0),
490 delta_vel_in_t0);
491 Matrix H_vel_angles = numericalDerivative11<Vector3, Vector3>(
492 std::bind(&PreIntegrateIMUObservations_delta_vel, msr_gyro_t,
493 msr_acc_t, msr_dt, std::placeholders::_1, delta_vel_in_t0,
494 flag_use_body_P_sensor, body_P_sensor, Bias_t0),
495 delta_angles);
496 Matrix H_vel_bias = numericalDerivative11<Vector3, IMUBIAS>(
497 std::bind(&PreIntegrateIMUObservations_delta_vel, msr_gyro_t,
498 msr_acc_t, msr_dt, delta_angles, delta_vel_in_t0,
499 flag_use_body_P_sensor, body_P_sensor,
500 std::placeholders::_1),
501 Bias_t0);
502 Matrix H_vel_pos = Z_3x3;
503
504 Matrix H_angles_angles = numericalDerivative11<Vector3, Vector3>(
505 std::bind(&PreIntegrateIMUObservations_delta_angles, msr_gyro_t,
506 msr_dt, std::placeholders::_1, flag_use_body_P_sensor,
507 body_P_sensor, Bias_t0),
508 delta_angles);
509 Matrix H_angles_bias = numericalDerivative11<Vector3, IMUBIAS>(
510 std::bind(&PreIntegrateIMUObservations_delta_angles, msr_gyro_t,
511 msr_dt, delta_angles, flag_use_body_P_sensor, body_P_sensor,
512 std::placeholders::_1),
513 Bias_t0);
514 Matrix H_angles_pos = Z_3x3;
515 Matrix H_angles_vel = Z_3x3;
516
517 Matrix F_angles = collect(std::vector<const Matrix*>{
518 &H_angles_angles, &H_angles_pos, &H_angles_vel, &H_angles_bias});
519 Matrix F_pos = collect(std::vector<const Matrix*>{&H_pos_angles, &H_pos_pos,
520 &H_pos_vel, &H_pos_bias});
521 Matrix F_vel = collect(std::vector<const Matrix*>{&H_vel_angles, &H_vel_pos,
522 &H_vel_vel, &H_vel_bias});
523 Matrix F_bias_a =
524 collect(std::vector<const Matrix*>{&Z3x3, &Z3x3, &Z3x3, &I3x3, &Z3x3});
525 Matrix F_bias_g =
526 collect(std::vector<const Matrix*>{&Z3x3, &Z3x3, &Z3x3, &Z3x3, &I3x3});
527 Matrix F =
528 stack(std::vector<Matrix>{F_angles, F_pos, F_vel, F_bias_a, F_bias_g});
529
530 noiseModel::Gaussian::shared_ptr model_discrete_curr = calc_descrete_noise_model(model_continuous_overall, msr_dt );
531 Matrix Q_d = (model_discrete_curr->R().transpose() * model_discrete_curr->R()).inverse();
532
533 EquivCov_Overall = F * EquivCov_Overall * F.transpose() + Q_d;
534 // Luca: force identity covariance matrix (for testing purposes)
535 // EquivCov_Overall = Matrix::Identity(15,15);
536
537 // Update Jacobian_wrt_t0_Overall
538 Jacobian_wrt_t0_Overall = F * Jacobian_wrt_t0_Overall;
539 }
540
541 static inline Vector PreIntegrateIMUObservations_delta_pos(const double msr_dt,
542 const Vector& delta_pos_in_t0, const Vector& delta_vel_in_t0){
543
544 // Note: all delta terms refer to an IMU\sensor system at t0
545 // Note: delta_vel_in_t0 is already in body frame, so no need to use the body_P_sensor transformation here.
546
547 return delta_pos_in_t0 + delta_vel_in_t0 * msr_dt;
548 }
549
550
551
552 static inline Vector PreIntegrateIMUObservations_delta_vel(const Vector& msr_gyro_t, const Vector& msr_acc_t, const double msr_dt,
553 const Vector3& delta_angles, const Vector& delta_vel_in_t0, const bool flag_use_body_P_sensor, const POSE& body_P_sensor,
554 IMUBIAS Bias_t0 = IMUBIAS()){
555
556 // Note: all delta terms refer to an IMU\sensor system at t0
557
558 // Calculate the corrected measurements using the Bias object
559 Vector AccCorrected = Bias_t0.correctAccelerometer(msr_acc_t);
560 Vector body_t_a_body;
561 if (flag_use_body_P_sensor){
562 Matrix body_R_sensor = body_P_sensor.rotation().matrix();
563
564 Vector GyroCorrected(Bias_t0.correctGyroscope(msr_gyro_t));
565
566 Vector body_omega_body = body_R_sensor * GyroCorrected;
567 Matrix body_omega_body__cross = skewSymmetric(body_omega_body);
568
569 body_t_a_body = body_R_sensor * AccCorrected - body_omega_body__cross * body_omega_body__cross * body_P_sensor.translation().vector();
570 } else{
571 body_t_a_body = AccCorrected;
572 }
573
574 Rot3 R_t_to_t0 = Rot3::Expmap(delta_angles);
575
576 return delta_vel_in_t0 + R_t_to_t0.matrix() * body_t_a_body * msr_dt;
577 }
578
579
580 static inline Vector PreIntegrateIMUObservations_delta_angles(const Vector& msr_gyro_t, const double msr_dt,
581 const Vector3& delta_angles, const bool flag_use_body_P_sensor, const POSE& body_P_sensor,
582 IMUBIAS Bias_t0 = IMUBIAS()){
583
584 // Note: all delta terms refer to an IMU\sensor system at t0
585
586 // Calculate the corrected measurements using the Bias object
587 Vector GyroCorrected = Bias_t0.correctGyroscope(msr_gyro_t);
588
589 Vector body_t_omega_body;
590 if (flag_use_body_P_sensor){
591 body_t_omega_body = body_P_sensor.rotation().matrix() * GyroCorrected;
592 } else {
593 body_t_omega_body = GyroCorrected;
594 }
595
596 Rot3 R_t_to_t0 = Rot3::Expmap(delta_angles);
597
598 R_t_to_t0 = R_t_to_t0 * Rot3::Expmap( body_t_omega_body*msr_dt );
599 return Rot3::Logmap(R_t_to_t0);
600 }
601
602
603 static inline noiseModel::Gaussian::shared_ptr CalcEquivalentNoiseCov(const noiseModel::Gaussian::shared_ptr& gaussian_acc, const noiseModel::Gaussian::shared_ptr& gaussian_gyro,
604 const noiseModel::Gaussian::shared_ptr& gaussian_process){
605
606 Matrix cov_acc = ( gaussian_acc->R().transpose() * gaussian_acc->R() ).inverse();
607 Matrix cov_gyro = ( gaussian_gyro->R().transpose() * gaussian_gyro->R() ).inverse();
608 Matrix cov_process = ( gaussian_process->R().transpose() * gaussian_process->R() ).inverse();
609
610 cov_process.block(0,0, 3,3) += cov_gyro;
611 cov_process.block(6,6, 3,3) += cov_acc;
612
613 return noiseModel::Gaussian::Covariance(cov_process);
614 }
615
616 static inline void CalcEquivalentNoiseCov_DifferentParts(const noiseModel::Gaussian::shared_ptr& gaussian_acc, const noiseModel::Gaussian::shared_ptr& gaussian_gyro,
617 const noiseModel::Gaussian::shared_ptr& gaussian_process,
618 Matrix& cov_acc, Matrix& cov_gyro, Matrix& cov_process_without_acc_gyro){
619
620 cov_acc = ( gaussian_acc->R().transpose() * gaussian_acc->R() ).inverse();
621 cov_gyro = ( gaussian_gyro->R().transpose() * gaussian_gyro->R() ).inverse();
622 cov_process_without_acc_gyro = ( gaussian_process->R().transpose() * gaussian_process->R() ).inverse();
623 }
624
625 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,
626 Vector& g_NED, Vector& rho_NED, Vector& omega_earth_NED) {
627 Matrix ENU_to_NED{//
628 {0.0, 1.0, 0.0},
629 {1.0, 0.0, 0.0},
630 {0.0, 0.0, -1.0}};
631
632 Matrix NED_to_ENU{//
633 {0.0, 1.0, 0.0},
634 {1.0, 0.0, 0.0},
635 {0.0, 0.0, -1.0}};
636
637 // Convert incoming parameters to ENU
638 Vector Pos_ENU = NED_to_ENU * Pos_NED;
639 Vector Vel_ENU = NED_to_ENU * Vel_NED;
640 Vector Pos_ENU_Initial = NED_to_ENU * Pos_NED_Initial;
641
642 // Call ENU version
643 Vector g_ENU;
644 Vector rho_ENU;
645 Vector omega_earth_ENU;
646 Calc_g_rho_omega_earth_ENU(Pos_ENU, Vel_ENU, LatLonHeight_IC, Pos_ENU_Initial, g_ENU, rho_ENU, omega_earth_ENU);
647
648 // Convert output to NED
649 g_NED = ENU_to_NED * g_ENU;
650 rho_NED = ENU_to_NED * rho_ENU;
651 omega_earth_NED = ENU_to_NED * omega_earth_ENU;
652 }
653
654 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,
655 Vector& g_ENU, Vector& rho_ENU, Vector& omega_earth_ENU){
656 double R0 = 6.378388e6;
657 double e = 1/297;
658 double Re( R0*( 1-e*(sin( LatLonHeight_IC(0) ))*(sin( LatLonHeight_IC(0) )) ) );
659
660 // Calculate current lat, lon
661 Vector delta_Pos_ENU(Pos_ENU - Pos_ENU_Initial);
662 double delta_lat(delta_Pos_ENU(1)/Re);
663 double delta_lon(delta_Pos_ENU(0)/(Re*cos(LatLonHeight_IC(0))));
664 double lat_new(LatLonHeight_IC(0) + delta_lat);
665 double lon_new(LatLonHeight_IC(1) + delta_lon);
666
667 // Rotation of lon about z axis
668 Rot3 C1(cos(lon_new), sin(lon_new), 0.0,
669 -sin(lon_new), cos(lon_new), 0.0,
670 0.0, 0.0, 1.0);
671
672 // Rotation of lat about y axis
673 Rot3 C2(cos(lat_new), 0.0, sin(lat_new),
674 0.0, 1.0, 0.0,
675 -sin(lat_new), 0.0, cos(lat_new));
676
677 Rot3 UEN_to_ENU(0, 1, 0,
678 0, 0, 1,
679 1, 0, 0);
680
681 Rot3 R_ECEF_to_ENU( UEN_to_ENU * C2 * C1 );
682
683 Vector omega_earth_ECEF(Vector3(0.0, 0.0, 7.292115e-5));
684 omega_earth_ENU = R_ECEF_to_ENU.matrix() * omega_earth_ECEF;
685
686 // Calculating g
687 double height(LatLonHeight_IC(2));
688 double EQUA_RADIUS = 6378137.0; // equatorial radius of the earth; WGS-84
689 double ECCENTRICITY = 0.0818191908426; // eccentricity of the earth ellipsoid
690 double e2( pow(ECCENTRICITY,2) );
691 double den( 1-e2*pow(sin(lat_new),2) );
692 double Rm( (EQUA_RADIUS*(1-e2))/( pow(den,(3/2)) ) );
693 double Rp( EQUA_RADIUS/( sqrt(den) ) );
694 double Ro( sqrt(Rp*Rm) ); // mean earth radius of curvature
695 double g0( 9.780318*( 1 + 5.3024e-3 * pow(sin(lat_new),2) - 5.9e-6 * pow(sin(2*lat_new),2) ) );
696 double g_calc( g0/( pow(1 + height/Ro, 2) ) );
697 g_ENU = Vector{{0.0, 0.0, -g_calc}};
698
699 // Calculate rho
700 double Ve( Vel_ENU(0) );
701 double Vn( Vel_ENU(1) );
702 double rho_E = -Vn/(Rm + height);
703 double rho_N = Ve/(Rp + height);
704 double rho_U = Ve*tan(lat_new)/(Rp + height);
705 rho_ENU = Vector{{rho_E, rho_N, rho_U}};
706 }
707
708 static inline noiseModel::Gaussian::shared_ptr calc_descrete_noise_model(const noiseModel::Gaussian::shared_ptr& model, double delta_t){
709 /* Q_d (approx)= Q * delta_t */
710 /* In practice, square root of the information matrix is represented, so that:
711 * R_d (approx)= R / sqrt(delta_t)
712 * */
713 return noiseModel::Gaussian::SqrtInformation(model->R()/sqrt(delta_t));
714 }
715private:
716
717#if GTSAM_ENABLE_BOOST_SERIALIZATION
719 friend class boost::serialization::access;
720 template<class ARCHIVE>
721 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
722 ar & boost::serialization::make_nvp("NonlinearFactor2",
723 boost::serialization::base_object<Base>(*this));
724 }
725#endif
726
727
728
729}; // \class EquivInertialNavFactor_GlobalVel
730
731}
732
733#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.
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
Matrix collect(const std::vector< const Matrix * > &matrices, size_t m, size_t n)
create a matrix by concatenating Given a set of matrices: A1, A2, A3... If all matrices have the same...
Definition Matrix.cpp:435
Vector concatVectors(const std::list< Vector > &vs)
concatenate Vectors
Definition Vector.cpp:303