gtsam/gtsam/navigation/ImuFactor.cpp

193 lines
7.8 KiB
C++
Raw Normal View History

2014-12-03 03:59:21 +08:00
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
2014-12-03 03:59:21 +08:00
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
/**
* @file ImuFactor.cpp
2014-12-03 03:59:21 +08:00
* @author Luca Carlone
* @author Stephen Williams
* @author Richard Roberts
* @author Vadim Indelman
* @author David Jensen
* @author Frank Dellaert
**/
#include <gtsam/navigation/ImuFactor.h>
/* External or standard includes */
#include <ostream>
namespace gtsam {
using namespace std;
2014-12-03 03:59:21 +08:00
//------------------------------------------------------------------------------
// Inner class PreintegratedImuMeasurements
2014-12-03 03:59:21 +08:00
//------------------------------------------------------------------------------
void PreintegratedImuMeasurements::print(const string& s) const {
2014-12-27 01:23:14 +08:00
PreintegrationBase::print(s);
2015-08-11 07:30:25 +08:00
cout << " preintMeasCov \n[" << preintMeasCov_ << "]" << endl;
2014-12-03 03:59:21 +08:00
}
//------------------------------------------------------------------------------
bool PreintegratedImuMeasurements::equals(
const PreintegratedImuMeasurements& other, double tol) const {
2015-07-23 23:00:07 +08:00
return PreintegrationBase::equals(other, tol)
&& equal_with_abs_tol(preintMeasCov_, other.preintMeasCov_, tol);
2014-12-03 03:59:21 +08:00
}
//------------------------------------------------------------------------------
void PreintegratedImuMeasurements::resetIntegration() {
2014-12-27 01:23:14 +08:00
PreintegrationBase::resetIntegration();
preintMeasCov_.setZero();
2014-12-03 03:59:21 +08:00
}
//------------------------------------------------------------------------------
void PreintegratedImuMeasurements::integrateMeasurement(
const Vector3& measuredAcc, const Vector3& measuredOmega, double dt) {
2014-12-27 01:23:14 +08:00
// Update preintegrated measurements (also get Jacobian)
Matrix9 A; // overall Jacobian wrt preintegrated measurements (df/dx)
Matrix93 B, C;
2015-08-01 06:31:45 +08:00
Matrix3 D_incrR_integratedOmega;
2015-08-02 08:17:14 +08:00
PreintegrationBase::update(measuredAcc, measuredOmega, dt,
&D_incrR_integratedOmega, &A, &B, &C);
2014-12-03 03:59:21 +08:00
2014-12-27 01:23:14 +08:00
// first order covariance propagation:
// as in [2] we consider a first order propagation that can be seen as a
// prediction phase in EKF
// propagate uncertainty
// TODO(frank): use noiseModel routine so we can have arbitrary noise models.
const Matrix3& aCov = p().accelerometerCovariance;
const Matrix3& wCov = p().gyroscopeCovariance;
const Matrix3& iCov = p().integrationCovariance;
// NOTE(luca): (1/dt) allows to pass from continuous time noise to discrete
// time noise
// measurementCovariance_discrete = measurementCovariance_contTime/dt
preintMeasCov_ = A * preintMeasCov_ * A.transpose();
preintMeasCov_.noalias() += B * (aCov / dt) * B.transpose();
preintMeasCov_.noalias() += C * (wCov / dt) * C.transpose();
// NOTE(frank): (Gi*dt)*(C/dt)*(Gi'*dt)
static const Matrix93 Gi = (Matrix93() << Z_3x3, I_3x3, Z_3x3).finished();
preintMeasCov_.noalias() += Gi * (iCov * dt) * Gi.transpose();
2014-12-03 03:59:21 +08:00
}
2014-12-03 03:59:21 +08:00
//------------------------------------------------------------------------------
2016-01-28 17:13:55 +08:00
#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V4
PreintegratedImuMeasurements::PreintegratedImuMeasurements(
const imuBias::ConstantBias& biasHat, const Matrix3& measuredAccCovariance,
const Matrix3& measuredOmegaCovariance,
2015-07-20 12:23:16 +08:00
const Matrix3& integrationErrorCovariance, bool use2ndOrderIntegration) {
2015-07-23 23:00:07 +08:00
if (!use2ndOrderIntegration)
2016-01-18 13:31:29 +08:00
throw("PreintegratedImuMeasurements no longer supports first-order integration: it incorrectly compensated for gravity");
biasHat_ = biasHat;
boost::shared_ptr<Params> p = Params::MakeSharedD();
p->gyroscopeCovariance = measuredOmegaCovariance;
p->accelerometerCovariance = measuredAccCovariance;
p->integrationCovariance = integrationErrorCovariance;
p_ = p;
resetIntegration();
}
2014-12-03 03:59:21 +08:00
void PreintegratedImuMeasurements::integrateMeasurement(
const Vector3& measuredAcc, const Vector3& measuredOmega, double deltaT,
2015-08-25 06:15:57 +08:00
boost::optional<Pose3> body_P_sensor) {
// modify parameters to accommodate deprecated method:-(
2015-08-25 06:15:57 +08:00
p_->body_P_sensor = body_P_sensor;
integrateMeasurement(measuredAcc, measuredOmega, deltaT);
}
2015-12-28 10:41:50 +08:00
#endif
//------------------------------------------------------------------------------
// ImuFactor methods
2014-12-03 03:59:21 +08:00
//------------------------------------------------------------------------------
ImuFactor::ImuFactor(Key pose_i, Key vel_i, Key pose_j, Key vel_j, Key bias,
2015-07-23 23:00:07 +08:00
const PreintegratedImuMeasurements& pim) :
Base(noiseModel::Gaussian::Covariance(pim.preintMeasCov_), pose_i, vel_i,
pose_j, vel_j, bias), _PIM_(pim) {
}
2014-12-03 03:59:21 +08:00
//------------------------------------------------------------------------------
gtsam::NonlinearFactor::shared_ptr ImuFactor::clone() const {
return boost::static_pointer_cast<gtsam::NonlinearFactor>(
gtsam::NonlinearFactor::shared_ptr(new This(*this)));
}
2016-01-28 06:15:16 +08:00
//------------------------------------------------------------------------------
std::ostream& operator<<(std::ostream& os, const ImuFactor& f) {
os << " preintegrated measurements:\n" << f._PIM_;
;
// Print standard deviations on covariance only
os << " noise model sigmas: " << f.noiseModel_->sigmas().transpose();
return os;
}
2014-12-03 03:59:21 +08:00
//------------------------------------------------------------------------------
void ImuFactor::print(const string& s, const KeyFormatter& keyFormatter) const {
cout << s << "ImuFactor(" << keyFormatter(this->key1()) << ","
<< keyFormatter(this->key2()) << "," << keyFormatter(this->key3()) << ","
<< keyFormatter(this->key4()) << "," << keyFormatter(this->key5())
<< ")\n";
2016-01-28 06:15:16 +08:00
cout << *this << endl;
2014-12-03 03:59:21 +08:00
}
//------------------------------------------------------------------------------
bool ImuFactor::equals(const NonlinearFactor& other, double tol) const {
const This *e = dynamic_cast<const This*>(&other);
2016-01-18 13:31:29 +08:00
const bool base = Base::equals(*e, tol);
const bool pim = _PIM_.equals(e->_PIM_, tol);
return e != nullptr && base && pim;
2014-12-03 03:59:21 +08:00
}
//------------------------------------------------------------------------------
2014-12-27 01:23:14 +08:00
Vector ImuFactor::evaluateError(const Pose3& pose_i, const Vector3& vel_i,
const Pose3& pose_j, const Vector3& vel_j,
const imuBias::ConstantBias& bias_i, boost::optional<Matrix&> H1,
boost::optional<Matrix&> H2, boost::optional<Matrix&> H3,
boost::optional<Matrix&> H4, boost::optional<Matrix&> H5) const {
return _PIM_.computeErrorAndJacobians(pose_i, vel_i, pose_j, vel_j, bias_i,
2015-07-23 23:00:07 +08:00
H1, H2, H3, H4, H5);
}
//------------------------------------------------------------------------------
2016-01-28 17:13:55 +08:00
#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V4
ImuFactor::ImuFactor(Key pose_i, Key vel_i, Key pose_j, Key vel_j, Key bias,
const PreintegratedImuMeasurements& pim, const Vector3& n_gravity,
2015-07-23 23:00:07 +08:00
const Vector3& omegaCoriolis, const boost::optional<Pose3>& body_P_sensor,
const bool use2ndOrderCoriolis) :
2016-01-18 13:31:29 +08:00
Base(noiseModel::Gaussian::Covariance(pim.preintMeasCov_), pose_i, vel_i,
pose_j, vel_j, bias), _PIM_(pim) {
boost::shared_ptr<PreintegratedImuMeasurements::Params> p = boost::make_shared<
2016-01-18 13:31:29 +08:00
PreintegratedImuMeasurements::Params>(pim.p());
p->n_gravity = n_gravity;
p->omegaCoriolis = omegaCoriolis;
p->body_P_sensor = body_P_sensor;
p->use2ndOrderCoriolis = use2ndOrderCoriolis;
_PIM_.p_ = p;
}
void ImuFactor::Predict(const Pose3& pose_i, const Vector3& vel_i,
2015-07-23 23:00:07 +08:00
Pose3& pose_j, Vector3& vel_j, const imuBias::ConstantBias& bias_i,
PreintegratedImuMeasurements& pim, const Vector3& n_gravity,
2015-07-23 23:00:07 +08:00
const Vector3& omegaCoriolis, const bool use2ndOrderCoriolis) {
2015-07-19 16:52:23 +08:00
// use deprecated predict
PoseVelocityBias pvb = pim.predict(pose_i, vel_i, bias_i, n_gravity,
2015-07-19 16:52:23 +08:00
omegaCoriolis, use2ndOrderCoriolis);
pose_j = pvb.pose;
vel_j = pvb.velocity;
2014-12-03 03:59:21 +08:00
}
2015-12-28 10:41:50 +08:00
#endif
//------------------------------------------------------------------------------
2016-01-18 13:31:29 +08:00
}
// namespace gtsam