87 Vector measurement_acc_;
88 Vector measurement_gyro_;
93 Vector world_omega_earth_;
95 boost::optional<POSE> body_P_sensor_;
100 typedef typename boost::shared_ptr<InertialNavFactor_GlobalVelocity> shared_ptr;
107 const Vector& measurement_acc,
const Vector& measurement_gyro,
const double measurement_dt,
const Vector world_g,
const Vector world_rho,
108 const Vector& world_omega_earth,
const noiseModel::Gaussian::shared_ptr& model_continuous, boost::optional<POSE> body_P_sensor = boost::none) :
109 Base(calc_descrete_noise_model(model_continuous, measurement_dt ),
110 Pose1, Vel1, IMUBias1,
Pose2, Vel2), measurement_acc_(measurement_acc), measurement_gyro_(measurement_gyro),
111 dt_(measurement_dt), world_g_(world_g), world_rho_(world_rho), world_omega_earth_(world_omega_earth), body_P_sensor_(body_P_sensor) { }
118 void print(
const std::string& s =
"InertialNavFactor_GlobalVelocity",
const KeyFormatter& keyFormatter = DefaultKeyFormatter)
const override {
119 std::cout << s <<
"("
120 << keyFormatter(this->key1()) <<
","
121 << keyFormatter(this->key2()) <<
","
122 << keyFormatter(this->key3()) <<
","
123 << keyFormatter(this->key4()) <<
","
124 << keyFormatter(this->key5()) <<
"\n";
125 std::cout <<
"acc measurement: " << this->measurement_acc_.transpose() << std::endl;
126 std::cout <<
"gyro measurement: " << this->measurement_gyro_.transpose() << std::endl;
127 std::cout <<
"dt: " << this->dt_ << std::endl;
128 std::cout <<
"gravity (in world frame): " << this->world_g_.transpose() << std::endl;
129 std::cout <<
"craft rate (in world frame): " << this->world_rho_.transpose() << std::endl;
130 std::cout <<
"earth's rotation (in world frame): " << this->world_omega_earth_.transpose() << std::endl;
131 if(this->body_P_sensor_)
132 this->body_P_sensor_->print(
" sensor pose in body frame: ");
133 this->noiseModel_->print(
" noise model");
138 const This *e =
dynamic_cast<const This*
> (&expected);
140 && (measurement_acc_ - e->measurement_acc_).norm() < tol
141 && (measurement_gyro_ - e->measurement_gyro_).norm() < tol
142 && (dt_ - e->dt_) < tol
143 && (world_g_ - e->world_g_).norm() < tol
144 && (world_rho_ - e->world_rho_).norm() < tol
145 && (world_omega_earth_ - e->world_omega_earth_).norm() < tol
146 && ((!body_P_sensor_ && !e->body_P_sensor_) || (body_P_sensor_ && e->body_P_sensor_ && body_P_sensor_->equals(*e->body_P_sensor_)));
149 POSE predictPose(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1)
const {
151 Vector GyroCorrected(Bias1.correctGyroscope(measurement_gyro_));
153 const POSE& world_P1_body = Pose1;
154 const VELOCITY& world_V1_body = Vel1;
157 Vector body_omega_body;
159 body_omega_body = body_P_sensor_->rotation().matrix() * GyroCorrected;
161 body_omega_body = GyroCorrected;
165 Matrix body_R_world(world_P1_body.rotation().inverse().matrix());
166 Vector body_rho = body_R_world * world_rho_;
167 Vector body_omega_earth = body_R_world * world_omega_earth_;
170 body_omega_body -= body_rho + body_omega_earth;
173 return POSE(Pose1.rotation() * POSE::Rotation::Expmap(body_omega_body*dt_), Pose1.translation() +
typename POSE::Translation(world_V1_body*dt_));
176 VELOCITY predictVelocity(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1)
const {
178 Vector AccCorrected(Bias1.correctAccelerometer(measurement_acc_));
180 const POSE& world_P1_body = Pose1;
181 const VELOCITY& world_V1_body = Vel1;
184 Vector body_a_body, body_omega_body;
186 Matrix body_R_sensor = body_P_sensor_->rotation().matrix();
188 Vector GyroCorrected(Bias1.correctGyroscope(measurement_gyro_));
189 body_omega_body = body_R_sensor * GyroCorrected;
190 Matrix body_omega_body__cross =
skewSymmetric(body_omega_body);
191 body_a_body = body_R_sensor * AccCorrected - body_omega_body__cross * body_omega_body__cross * body_P_sensor_->translation();
193 body_a_body = AccCorrected;
197 Vector world_a_body = world_P1_body.rotation().matrix() * body_a_body + world_g_ - 2*
skewSymmetric(world_rho_ + world_omega_earth_)*world_V1_body;
200 VELOCITY VelDelta(world_a_body*dt_);
203 return Vel1 + VelDelta;
206 void predict(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1, POSE& Pose2, VELOCITY& Vel2)
const {
207 Pose2 = predictPose(Pose1, Vel1, Bias1);
208 Vel2 = predictVelocity(Pose1, Vel1, Bias1);
211 POSE evaluatePoseError(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1,
const POSE& Pose2,
const VELOCITY& Vel2)
const {
213 POSE Pose2Pred = predictPose(Pose1, Vel1, Bias1);
216 return Pose2.between(Pose2Pred);
219 VELOCITY evaluateVelocityError(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1,
const POSE& Pose2,
const VELOCITY& Vel2)
const {
221 VELOCITY Vel2Pred = predictVelocity(Pose1, Vel1, Bias1);
224 return Vel2Pred - Vel2;
228 Vector
evaluateError(
const POSE& Pose1,
const VELOCITY& Vel1,
const IMUBIAS& Bias1,
const POSE&
Pose2,
const VELOCITY& Vel2,
229 boost::optional<Matrix&> H1 = boost::none,
230 boost::optional<Matrix&> H2 = boost::none,
231 boost::optional<Matrix&> H3 = boost::none,
232 boost::optional<Matrix&> H4 = boost::none,
233 boost::optional<Matrix&> H5 = boost::none)
const override {
239 std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
240 this, std::placeholders::_1, Vel1, Bias1,
Pose2, Vel2),
243 std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
244 this, std::placeholders::_1, Vel1, Bias1,
Pose2, Vel2),
246 *H1 =
stack(2, &H1_Pose, &H1_Vel);
251 if (Vel1.size()!=3)
throw std::runtime_error(
"Frank's hack to make this compile will not work if size != 3");
253 std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
254 this, Pose1, std::placeholders::_1, Bias1,
Pose2, Vel2),
257 std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
258 this, Pose1, std::placeholders::_1, Bias1,
Pose2, Vel2),
260 *H2 =
stack(2, &H2_Pose, &H2_Vel);
266 std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
267 this, Pose1, Vel1, std::placeholders::_1,
Pose2, Vel2),
270 std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
271 this, Pose1, Vel1, std::placeholders::_1,
Pose2, Vel2),
273 *H3 =
stack(2, &H3_Pose, &H3_Vel);
279 std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
280 this, Pose1, Vel1, Bias1, std::placeholders::_1, Vel2),
283 std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
284 this, Pose1, Vel1, Bias1, std::placeholders::_1, Vel2),
286 *H4 =
stack(2, &H4_Pose, &H4_Vel);
291 if (Vel2.size()!=3)
throw std::runtime_error(
"Frank's hack to make this compile will not work if size != 3");
293 std::bind(&InertialNavFactor_GlobalVelocity::evaluatePoseError,
294 this, Pose1, Vel1, Bias1,
Pose2, std::placeholders::_1),
297 std::bind(&InertialNavFactor_GlobalVelocity::evaluateVelocityError,
298 this, Pose1, Vel1, Bias1,
Pose2, std::placeholders::_1),
300 *H5 =
stack(2, &H5_Pose, &H5_Vel);
303 Vector ErrPoseVector(POSE::Logmap(evaluatePoseError(Pose1, Vel1, Bias1,
Pose2, Vel2)));
304 Vector ErrVelVector(evaluateVelocityError(Pose1, Vel1, Bias1,
Pose2, Vel2));
309 static inline noiseModel::Gaussian::shared_ptr CalcEquivalentNoiseCov(
const noiseModel::Gaussian::shared_ptr& gaussian_acc,
const noiseModel::Gaussian::shared_ptr& gaussian_gyro,
310 const noiseModel::Gaussian::shared_ptr& gaussian_process){
312 Matrix cov_acc = ( gaussian_acc->R().transpose() * gaussian_acc->R() ).inverse();
313 Matrix cov_gyro = ( gaussian_gyro->R().transpose() * gaussian_gyro->R() ).inverse();
314 Matrix cov_process = ( gaussian_process->R().transpose() * gaussian_process->R() ).inverse();
316 cov_process.block(0,0, 3,3) += cov_gyro;
317 cov_process.block(6,6, 3,3) += cov_acc;
322 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,
323 Vector& g_NED, Vector& rho_NED, Vector& omega_earth_NED) {
325 Matrix ENU_to_NED = (Matrix(3, 3) <<
328 0.0, 0.0, -1.0).finished();
330 Matrix NED_to_ENU = (Matrix(3, 3) <<
333 0.0, 0.0, -1.0).finished();
336 Vector Pos_ENU = NED_to_ENU * Pos_NED;
337 Vector Vel_ENU = NED_to_ENU * Vel_NED;
338 Vector Pos_ENU_Initial = NED_to_ENU * Pos_NED_Initial;
343 Vector omega_earth_ENU;
344 Calc_g_rho_omega_earth_ENU(Pos_ENU, Vel_ENU, LatLonHeight_IC, Pos_ENU_Initial, g_ENU, rho_ENU, omega_earth_ENU);
347 g_NED = ENU_to_NED * g_ENU;
348 rho_NED = ENU_to_NED * rho_ENU;
349 omega_earth_NED = ENU_to_NED * omega_earth_ENU;
352 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,
353 Vector& g_ENU, Vector& rho_ENU, Vector& omega_earth_ENU){
354 double R0 = 6.378388e6;
356 double Re( R0*( 1-e*(sin( LatLonHeight_IC(0) ))*(sin( LatLonHeight_IC(0) )) ) );
359 Vector delta_Pos_ENU(Pos_ENU - Pos_ENU_Initial);
360 double delta_lat(delta_Pos_ENU(1)/Re);
361 double delta_lon(delta_Pos_ENU(0)/(Re*cos(LatLonHeight_IC(0))));
362 double lat_new(LatLonHeight_IC(0) + delta_lat);
363 double lon_new(LatLonHeight_IC(1) + delta_lon);
366 Rot3 C1(cos(lon_new), sin(lon_new), 0.0,
367 -sin(lon_new), cos(lon_new), 0.0,
371 Rot3 C2(cos(lat_new), 0.0, sin(lat_new),
373 -sin(lat_new), 0.0, cos(lat_new));
375 Rot3 UEN_to_ENU(0, 1, 0,
379 Rot3 R_ECEF_to_ENU( UEN_to_ENU * C2 * C1 );
381 Vector omega_earth_ECEF(Vector3(0.0, 0.0, 7.292115e-5));
382 omega_earth_ENU = R_ECEF_to_ENU.matrix() * omega_earth_ECEF;
385 double height(LatLonHeight_IC(2));
386 double EQUA_RADIUS = 6378137.0;
387 double ECCENTRICITY = 0.0818191908426;
388 double e2( pow(ECCENTRICITY,2) );
389 double den( 1-e2*pow(sin(lat_new),2) );
390 double Rm( (EQUA_RADIUS*(1-e2))/( pow(den,(3/2)) ) );
391 double Rp( EQUA_RADIUS/( sqrt(den) ) );
392 double Ro( sqrt(Rp*Rm) );
393 double g0( 9.780318*( 1 + 5.3024e-3 * pow(sin(lat_new),2) - 5.9e-6 * pow(sin(2*lat_new),2) ) );
394 double g_calc( g0/( pow(1 + height/Ro, 2) ) );
395 g_ENU = (Vector(3) << 0.0, 0.0, -g_calc).finished();
399 double Ve( Vel_ENU(0) );
400 double Vn( Vel_ENU(1) );
401 double rho_E = -Vn/(Rm + height);
402 double rho_N = Ve/(Rp + height);
403 double rho_U = Ve*tan(lat_new)/(Rp + height);
404 rho_ENU = (Vector(3) << rho_E, rho_N, rho_U).finished();
407 static inline noiseModel::Gaussian::shared_ptr calc_descrete_noise_model(
const noiseModel::Gaussian::shared_ptr& model,
double delta_t){
419 template<
class ARCHIVE>
420 void serialize(ARCHIVE & ar,
const unsigned int ) {
421 ar & boost::serialization::make_nvp(
"NonlinearFactor2",
422 boost::serialization::base_object<Base>(*
this));