29 #include <boost/optional.hpp> 78 template<
class POSE,
class VELOCITY,
class IMUBIAS>
86 Vector measurement_acc_;
87 Vector measurement_gyro_;
92 Vector world_omega_earth_;
94 boost::optional<POSE> body_P_sensor_;
99 typedef typename boost::shared_ptr<InertialNavFactor_GlobalVelocity> shared_ptr;
106 const Vector& measurement_acc,
const Vector& measurement_gyro,
const double measurement_dt,
const Vector world_g,
const Vector world_rho,
107 const Vector& world_omega_earth,
const noiseModel::Gaussian::shared_ptr& model_continuous, boost::optional<POSE> body_P_sensor = boost::none) :
108 Base(calc_descrete_noise_model(model_continuous, measurement_dt ),
109 Pose1, Vel1, IMUBias1,
Pose2, Vel2), measurement_acc_(measurement_acc), measurement_gyro_(measurement_gyro),
110 dt_(measurement_dt), world_g_(world_g), world_rho_(world_rho), world_omega_earth_(world_omega_earth), body_P_sensor_(body_P_sensor) { }
117 virtual void print(
const std::string& s =
"InertialNavFactor_GlobalVelocity",
const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const {
118 std::cout << s <<
"(" 119 << keyFormatter(this->
key1()) <<
"," 120 << keyFormatter(this->key2()) <<
"," 121 << keyFormatter(this->key3()) <<
"," 122 << keyFormatter(this->key4()) <<
"," 123 << keyFormatter(this->key5()) <<
"\n";
124 std::cout <<
"acc measurement: " << this->measurement_acc_.transpose() << std::endl;
125 std::cout <<
"gyro measurement: " << this->measurement_gyro_.transpose() << std::endl;
126 std::cout <<
"dt: " << this->dt_ << std::endl;
127 std::cout <<
"gravity (in world frame): " << this->world_g_.transpose() << std::endl;
128 std::cout <<
"craft rate (in world frame): " << this->world_rho_.transpose() << std::endl;
129 std::cout <<
"earth's rotation (in world frame): " << this->world_omega_earth_.transpose() << std::endl;
130 if(this->body_P_sensor_)
131 this->body_P_sensor_->print(
" sensor pose in body frame: ");
132 this->noiseModel_->print(
" noise model");
137 const This *e = dynamic_cast<const This*> (&expected);
139 && (measurement_acc_ - e->measurement_acc_).norm() < tol
140 && (measurement_gyro_ - e->measurement_gyro_).norm() < tol
141 && (dt_ - e->dt_) < tol
142 && (world_g_ - e->world_g_).norm() < tol
143 && (world_rho_ - e->world_rho_).norm() < tol
144 && (world_omega_earth_ - e->world_omega_earth_).norm() < tol
145 && ((!body_P_sensor_ && !e->body_P_sensor_) || (body_P_sensor_ && e->body_P_sensor_ && body_P_sensor_->
equals(*e->body_P_sensor_)));
148 POSE predictPose(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1)
const {
150 Vector GyroCorrected(Bias1.correctGyroscope(measurement_gyro_));
152 const POSE& world_P1_body = Pose1;
153 const VELOCITY& world_V1_body = Vel1;
156 Vector body_omega_body;
158 body_omega_body = body_P_sensor_->rotation().matrix() * GyroCorrected;
160 body_omega_body = GyroCorrected;
164 Matrix body_R_world(world_P1_body.rotation().inverse().matrix());
165 Vector body_rho = body_R_world * world_rho_;
166 Vector body_omega_earth = body_R_world * world_omega_earth_;
169 body_omega_body -= body_rho + body_omega_earth;
172 return POSE(Pose1.rotation() * POSE::Rotation::Expmap(body_omega_body*dt_), Pose1.translation() +
typename POSE::Translation(world_V1_body*dt_));
175 VELOCITY predictVelocity(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1)
const {
177 Vector AccCorrected(Bias1.correctAccelerometer(measurement_acc_));
179 const POSE& world_P1_body = Pose1;
180 const VELOCITY& world_V1_body = Vel1;
183 Vector body_a_body, body_omega_body;
185 Matrix body_R_sensor = body_P_sensor_->rotation().matrix();
187 Vector GyroCorrected(Bias1.correctGyroscope(measurement_gyro_));
188 body_omega_body = body_R_sensor * GyroCorrected;
189 Matrix body_omega_body__cross =
skewSymmetric(body_omega_body);
190 body_a_body = body_R_sensor * AccCorrected - body_omega_body__cross * body_omega_body__cross * body_P_sensor_->translation();
192 body_a_body = AccCorrected;
196 Vector world_a_body = world_P1_body.rotation().matrix() * body_a_body + world_g_ - 2*
skewSymmetric(world_rho_ + world_omega_earth_)*world_V1_body;
199 VELOCITY VelDelta(world_a_body*dt_);
202 return Vel1 + VelDelta;
205 void predict(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1, POSE& Pose2, VELOCITY& Vel2)
const {
206 Pose2 = predictPose(Pose1, Vel1, Bias1);
207 Vel2 = predictVelocity(Pose1, Vel1, Bias1);
210 POSE evaluatePoseError(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1,
const POSE& Pose2,
const VELOCITY& Vel2)
const {
212 POSE Pose2Pred = predictPose(Pose1, Vel1, Bias1);
215 return Pose2.between(Pose2Pred);
218 VELOCITY evaluateVelocityError(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1,
const POSE& Pose2,
const VELOCITY& Vel2)
const {
220 VELOCITY Vel2Pred = predictVelocity(Pose1, Vel1, Bias1);
223 return Vel2Pred - Vel2;
227 Vector
evaluateError(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1,
const POSE&
Pose2,
const VELOCITY& Vel2,
228 boost::optional<Matrix&> H1 = boost::none,
229 boost::optional<Matrix&> H2 = boost::none,
230 boost::optional<Matrix&> H3 = boost::none,
231 boost::optional<Matrix&> H4 = boost::none,
232 boost::optional<Matrix&> H5 = boost::none)
const {
237 Matrix H1_Pose = gtsam::numericalDerivative11<POSE, POSE>(boost::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
this, _1, Vel1, Bias1,
Pose2, Vel2), Pose1);
238 Matrix H1_Vel = gtsam::numericalDerivative11<VELOCITY, POSE>(boost::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
this, _1, Vel1, Bias1,
Pose2, Vel2), Pose1);
239 *H1 =
stack(2, &H1_Pose, &H1_Vel);
244 if (Vel1.size()!=3)
throw std::runtime_error(
"Frank's hack to make this compile will not work if size != 3");
245 Matrix H2_Pose = gtsam::numericalDerivative11<POSE, Vector3>(boost::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
this, Pose1, _1, Bias1,
Pose2, Vel2), Vel1);
246 Matrix H2_Vel = gtsam::numericalDerivative11<Vector3, Vector3>(boost::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
this, Pose1, _1, Bias1,
Pose2, Vel2), Vel1);
247 *H2 =
stack(2, &H2_Pose, &H2_Vel);
252 Matrix H3_Pose = gtsam::numericalDerivative11<POSE, IMUBIAS>(boost::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
this, Pose1, Vel1, _1,
Pose2, Vel2), Bias1);
253 Matrix H3_Vel = gtsam::numericalDerivative11<VELOCITY, IMUBIAS>(boost::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
this, Pose1, Vel1, _1,
Pose2, Vel2), Bias1);
254 *H3 =
stack(2, &H3_Pose, &H3_Vel);
259 Matrix H4_Pose = gtsam::numericalDerivative11<POSE, POSE>(boost::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
this, Pose1, Vel1, Bias1, _1, Vel2),
Pose2);
260 Matrix H4_Vel = gtsam::numericalDerivative11<VELOCITY, POSE>(boost::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
this, Pose1, Vel1, Bias1, _1, Vel2),
Pose2);
261 *H4 =
stack(2, &H4_Pose, &H4_Vel);
266 if (Vel2.size()!=3)
throw std::runtime_error(
"Frank's hack to make this compile will not work if size != 3");
267 Matrix H5_Pose = gtsam::numericalDerivative11<POSE, Vector3>(boost::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
this, Pose1, Vel1, Bias1,
Pose2, _1), Vel2);
268 Matrix H5_Vel = gtsam::numericalDerivative11<Vector3, Vector3>(boost::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
this, Pose1, Vel1, Bias1,
Pose2, _1), Vel2);
269 *H5 =
stack(2, &H5_Pose, &H5_Vel);
272 Vector ErrPoseVector(POSE::Logmap(evaluatePoseError(Pose1, Vel1, Bias1,
Pose2, Vel2)));
273 Vector ErrVelVector(evaluateVelocityError(Pose1, Vel1, Bias1,
Pose2, Vel2));
278 static inline noiseModel::Gaussian::shared_ptr CalcEquivalentNoiseCov(
const noiseModel::Gaussian::shared_ptr& gaussian_acc,
const noiseModel::Gaussian::shared_ptr& gaussian_gyro,
279 const noiseModel::Gaussian::shared_ptr& gaussian_process){
281 Matrix cov_acc = ( gaussian_acc->R().transpose() * gaussian_acc->R() ).inverse();
282 Matrix cov_gyro = ( gaussian_gyro->R().transpose() * gaussian_gyro->R() ).inverse();
283 Matrix cov_process = ( gaussian_process->R().transpose() * gaussian_process->R() ).inverse();
285 cov_process.block(0,0, 3,3) += cov_gyro;
286 cov_process.block(6,6, 3,3) += cov_acc;
291 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,
292 Vector& g_NED, Vector& rho_NED, Vector& omega_earth_NED) {
294 Matrix ENU_to_NED = (Matrix(3, 3) <<
297 0.0, 0.0, -1.0).finished();
299 Matrix NED_to_ENU = (Matrix(3, 3) <<
302 0.0, 0.0, -1.0).finished();
305 Vector Pos_ENU = NED_to_ENU * Pos_NED;
306 Vector Vel_ENU = NED_to_ENU * Vel_NED;
307 Vector Pos_ENU_Initial = NED_to_ENU * Pos_NED_Initial;
312 Vector omega_earth_ENU;
313 Calc_g_rho_omega_earth_ENU(Pos_ENU, Vel_ENU, LatLonHeight_IC, Pos_ENU_Initial, g_ENU, rho_ENU, omega_earth_ENU);
316 g_NED = ENU_to_NED * g_ENU;
317 rho_NED = ENU_to_NED * rho_ENU;
318 omega_earth_NED = ENU_to_NED * omega_earth_ENU;
321 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,
322 Vector& g_ENU, Vector& rho_ENU, Vector& omega_earth_ENU){
323 double R0 = 6.378388e6;
325 double Re( R0*( 1-e*(sin( LatLonHeight_IC(0) ))*(sin( LatLonHeight_IC(0) )) ) );
328 Vector delta_Pos_ENU(Pos_ENU - Pos_ENU_Initial);
329 double delta_lat(delta_Pos_ENU(1)/Re);
330 double delta_lon(delta_Pos_ENU(0)/(Re*cos(LatLonHeight_IC(0))));
331 double lat_new(LatLonHeight_IC(0) + delta_lat);
332 double lon_new(LatLonHeight_IC(1) + delta_lon);
335 Rot3 C1(cos(lon_new), sin(lon_new), 0.0,
336 -sin(lon_new), cos(lon_new), 0.0,
340 Rot3 C2(cos(lat_new), 0.0, sin(lat_new),
342 -sin(lat_new), 0.0, cos(lat_new));
344 Rot3 UEN_to_ENU(0, 1, 0,
348 Rot3 R_ECEF_to_ENU( UEN_to_ENU * C2 * C1 );
350 Vector omega_earth_ECEF(Vector3(0.0, 0.0, 7.292115e-5));
351 omega_earth_ENU = R_ECEF_to_ENU.matrix() * omega_earth_ECEF;
354 double height(LatLonHeight_IC(2));
355 double EQUA_RADIUS = 6378137.0;
356 double ECCENTRICITY = 0.0818191908426;
357 double e2( pow(ECCENTRICITY,2) );
358 double den( 1-e2*pow(sin(lat_new),2) );
359 double Rm( (EQUA_RADIUS*(1-e2))/( pow(den,(3/2)) ) );
360 double Rp( EQUA_RADIUS/( sqrt(den) ) );
361 double Ro( sqrt(Rp*Rm) );
362 double g0( 9.780318*( 1 + 5.3024e-3 * pow(sin(lat_new),2) - 5.9e-6 * pow(sin(2*lat_new),2) ) );
363 double g_calc( g0/( pow(1 + height/Ro, 2) ) );
364 g_ENU = (Vector(3) << 0.0, 0.0, -g_calc).finished();
368 double Ve( Vel_ENU(0) );
369 double Vn( Vel_ENU(1) );
370 double rho_E = -Vn/(Rm + height);
371 double rho_N = Ve/(Rp + height);
372 double rho_U = Ve*tan(lat_new)/(Rp + height);
373 rho_ENU = (Vector(3) << rho_E, rho_N, rho_U).finished();
376 static inline noiseModel::Gaussian::shared_ptr calc_descrete_noise_model(
const noiseModel::Gaussian::shared_ptr& model,
double delta_t){
388 template<
class ARCHIVE>
389 void serialize(ARCHIVE & ar,
const unsigned int ) {
390 ar & boost::serialization::make_nvp(
"NonlinearFactor2",
391 boost::serialization::base_object<Base>(*
this));
397 template<
class POSE,
class VELOCITY,
class IMUBIAS>
399 public Testable<InertialNavFactor_GlobalVelocity<POSE, VELOCITY, IMUBIAS> > {
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:403
This is the base class for all factor types.
Definition: Factor.h:54
InertialNavFactor_GlobalVelocity(const Key &Pose1, const Key &Vel1, const Key &IMUBias1, const Key &Pose2, const Key &Vel2, const Vector &measurement_acc, const Vector &measurement_gyro, const double measurement_dt, const Vector world_g, const Vector world_rho, const Vector &world_omega_earth, const noiseModel::Gaussian::shared_ptr &model_continuous, boost::optional< POSE > body_P_sensor=boost::none)
Constructor.
Definition: InertialNavFactor_GlobalVelocity.h:105
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
virtual bool equals(const NonlinearFactor &expected, double tol=1e-9) const
equals
Definition: InertialNavFactor_GlobalVelocity.h:136
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
static shared_ptr SqrtInformation(const Matrix &R, bool smart=true)
A Gaussian noise model created by specifying a square root information matrix.
Definition: NoiseModel.cpp:81
InertialNavFactor_GlobalVelocity()
default constructor - only use for serialization
Definition: InertialNavFactor_GlobalVelocity.h:102
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33
Nonlinear factor base class.
Definition: NonlinearFactor.h:50
virtual void print(const std::string &s="InertialNavFactor_GlobalVelocity", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
implement functions needed for Testable
Definition: InertialNavFactor_GlobalVelocity.h:117
Definition: InertialNavFactor_GlobalVelocity.h:79
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Non-linear factor base classes.
3D rotation represented as a rotation matrix or quaternion
Vector concatVectors(const std::list< Vector > &vs)
concatenate Vectors
Definition: Vector.cpp:264
A convenient base class for creating your own NoiseModelFactor with 5 variables.
Definition: NonlinearFactor.h:578
Matrix stack(size_t nrMatrices,...)
create a matrix by stacking other matrices Given a set of matrices: A1, A2, A3...
Definition: Matrix.cpp:392
Key key1() const
methods to retrieve keys
Definition: NonlinearFactor.h:616
Vector evaluateError(const POSE &Pose1, const VELOCITY &Vel1, const IMUBIAS &Bias1, const POSE &Pose2, const VELOCITY &Vel2, boost::optional< Matrix & > H1=boost::none, boost::optional< Matrix & > H2=boost::none, boost::optional< Matrix & > H3=boost::none, boost::optional< Matrix & > H4=boost::none, boost::optional< Matrix & > H5=boost::none) const
implement functions needed to derive from Factor
Definition: InertialNavFactor_GlobalVelocity.h:227
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
typedef and functions to augment Eigen's MatrixXd
static shared_ptr Covariance(const Matrix &covariance, bool smart=true)
A Gaussian noise model created by specifying a covariance matrix.
Definition: NoiseModel.cpp:112
Some functions to compute numerical derivatives.
virtual bool equals(const NonlinearFactor &f, double tol=1e-9) const
Check if two factors are equal.
Definition: NonlinearFactor.cpp:71
bool equals(const This &other, double tol=1e-9) const
check equality
Definition: Factor.cpp:42
friend class boost::serialization::access
Serialization function.
Definition: InertialNavFactor_GlobalVelocity.h:387