ScenarioRunner.cpp
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 
18 #include <gtsam/base/timing.h>
20 
21 #include <cmath>
22 
23 using namespace std;
24 
25 namespace gtsam {
26 
27 static double intNoiseVar = 0.0000001;
28 static const Matrix3 kIntegrationErrorCovariance = intNoiseVar * I_3x3;
29 
30 PreintegratedImuMeasurements ScenarioRunner::integrate(
31  double T, const Bias& estimatedBias, bool corrupted) const {
32  gttic_(integrate);
33  PreintegratedImuMeasurements pim(p_, estimatedBias);
34 
35  const double dt = imuSampleTime();
36  const size_t nrSteps = T / dt;
37  double t = 0;
38  for (size_t k = 0; k < nrSteps; k++, t += dt) {
40  corrupted ? measuredAngularVelocity(t) : actualAngularVelocity(t);
42  corrupted ? measuredSpecificForce(t) : actualSpecificForce(t);
43  pim.integrateMeasurement(measuredAcc, measuredOmega, dt);
44  }
45 
46  return pim;
47 }
48 
49 NavState ScenarioRunner::predict(const PreintegratedImuMeasurements& pim,
50  const Bias& estimatedBias) const {
51  const NavState state_i(scenario_.pose(0), scenario_.velocity_n(0));
52  return pim.predict(state_i, estimatedBias);
53 }
54 
55 Matrix9 ScenarioRunner::estimateCovariance(double T, size_t N,
56  const Bias& estimatedBias) const {
57  gttic_(estimateCovariance);
58 
59  // Get predict prediction from ground truth measurements
60  NavState prediction = predict(integrate(T));
61 
62  // Sample !
63  Matrix samples(9, N);
64  Vector9 sum = Vector9::Zero();
65  for (size_t i = 0; i < N; i++) {
66  auto pim = integrate(T, estimatedBias, true);
67  NavState sampled = predict(pim);
68  Vector9 xi = sampled.localCoordinates(prediction);
69  samples.col(i) = xi;
70  sum += xi;
71  }
72 
73  // Compute MC covariance
74  Vector9 sampleMean = sum / N;
75  Matrix9 Q;
76  Q.setZero();
77  for (size_t i = 0; i < N; i++) {
78  Vector9 xi = samples.col(i) - sampleMean;
79  Q += xi * xi.transpose();
80  }
81 
82  return Q / (N - 1);
83 }
84 
85 Matrix6 ScenarioRunner::estimateNoiseCovariance(size_t N) const {
86  Matrix samples(6, N);
87  Vector6 sum = Vector6::Zero();
88  for (size_t i = 0; i < N; i++) {
89  samples.col(i) << accSampler_.sample() / sqrt_dt_,
90  gyroSampler_.sample() / sqrt_dt_;
91  sum += samples.col(i);
92  }
93 
94  // Compute MC covariance
95  Vector6 sampleMean = sum / N;
96  Matrix6 Q;
97  Q.setZero();
98  for (size_t i = 0; i < N; i++) {
99  Vector6 xi = samples.col(i) - sampleMean;
100  Q += xi * xi.transpose();
101  }
102 
103  return Q / (N - 1);
104 }
105 
106 PreintegratedCombinedMeasurements CombinedScenarioRunner::integrate(
107  double T, const Bias& estimatedBias, bool corrupted) const {
108  gttic_(integrate);
109  PreintegratedCombinedMeasurements pim(p_, estimatedBias, preintMeasCov_);
110 
111  const double dt = imuSampleTime();
112  const size_t nrSteps = T / dt;
113  double t = 0;
114  for (size_t k = 0; k < nrSteps; k++, t += dt) {
116  corrupted ? measuredAngularVelocity(t) : actualAngularVelocity(t);
118  corrupted ? measuredSpecificForce(t) : actualSpecificForce(t);
119  pim.integrateMeasurement(measuredAcc, measuredOmega, dt);
120  }
121 
122  return pim;
123 }
124 
125 NavState CombinedScenarioRunner::predict(
127  const Bias& estimatedBias) const {
128  const NavState state_i(scenario().pose(0), scenario().velocity_n(0));
129  return pim.predict(state_i, estimatedBias);
130 }
131 
132 Eigen::Matrix<double, 15, 15> CombinedScenarioRunner::estimateCovariance(
133  double T, size_t N, const Bias& estimatedBias) const {
134  gttic_(estimateCovariance);
135 
136  // Get predict prediction from ground truth measurements
137  NavState prediction = predict(integrate(T));
138 
139  // Sample !
140  Matrix samples(15, N);
141  Vector15 sum = Vector15::Zero();
142  for (size_t i = 0; i < N; i++) {
143  auto pim = integrate(T, estimatedBias, true);
144  NavState sampled = predict(pim);
145  Vector15 xi = Vector15::Zero();
146  xi << sampled.localCoordinates(prediction),
147  (estimatedBias_.vector() - estimatedBias.vector());
148  samples.col(i) = xi;
149  sum += xi;
150  }
151 
152  // Compute MC covariance
153  Vector15 sampleMean = sum / N;
155  Q.setZero();
156  for (size_t i = 0; i < N; i++) {
157  Vector15 xi = samples.col(i) - sampleMean;
158  Q += xi * xi.transpose();
159  }
160 
161  return Q / (N - 1);
162 }
163 
164 } // namespace gtsam
void integrateMeasurement(const Vector3 &measuredAcc, const Vector3 &measuredOmega, const double dt) override
Definition: ImuFactor.cpp:53
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
Quaternion Q
#define gttic_(label)
Definition: timing.h:245
Eigen::Vector3d Vector3
Definition: Vector.h:43
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:39
void integrateMeasurement(const Vector3 &measuredAcc, const Vector3 &measuredOmega, const double dt) override
Definition: BFloat16.h:88
#define N
Definition: gksort.c:12
static double intNoiseVar
static const Matrix3 kIntegrationErrorCovariance
Vector6 vector() const
Definition: ImuBias.h:59
const double dt
static const Vector3 measuredAcc
ConstantTwistScenario scenario(Z_3x1, Vector3(v, 0, 0))
Vector xi
Definition: testPose2.cpp:148
traits
Definition: chartTesting.h:28
static const Vector3 measuredOmega(w, 0, 0)
static const Pose3 pose(Rot3(Vector3(1, -1, -1).asDiagonal()), Point3(0, 0, 0.5))
NavState predict(const NavState &state_i, const imuBias::ConstantBias &bias_i, OptionalJacobian< 9, 9 > H1={}, OptionalJacobian< 9, 6 > H2={}) const
Predict state at time j.
Simple class to test navigation scenarios.
Vector9 localCoordinates(const NavState &g, OptionalJacobian< 9, 9 > H1={}, OptionalJacobian< 9, 9 > H2={}) const
localCoordinates with optional derivatives
Definition: NavState.cpp:133
Timing utilities.
Point2 t(10, 10)
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set samples


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:35:36