38 #include <boost/program_options.hpp> 55 using namespace gtsam;
62 namespace po = boost::program_options;
65 po::options_description desc;
66 desc.add_options()(
"help,h",
"produce help message")(
67 "data_csv_path", po::value<string>()->default_value(
"imuAndGPSdata.csv"),
68 "path to the CSV file with the IMU data")(
70 po::value<string>()->default_value(
"imuFactorExampleResults.csv"),
71 "path to the result file to use")(
"use_isam", po::bool_switch(),
72 "use ISAM as the optimizer");
75 po::store(po::parse_command_line(argc, argv, desc), vm);
77 if (vm.count(
"help")) {
85 std::shared_ptr<PreintegratedCombinedMeasurements::Params>
imuParams() {
87 double accel_noise_sigma = 0.0003924;
88 double gyro_noise_sigma = 0.000205689024915;
89 double accel_bias_rw_sigma = 0.004905;
90 double gyro_bias_rw_sigma = 0.000001454441043;
91 Matrix33 measured_acc_cov = I_3x3 *
pow(accel_noise_sigma, 2);
92 Matrix33 measured_omega_cov = I_3x3 *
pow(gyro_noise_sigma, 2);
93 Matrix33 integration_error_cov =
95 Matrix33 bias_acc_cov = I_3x3 *
pow(accel_bias_rw_sigma, 2);
96 Matrix33 bias_omega_cov = I_3x3 *
pow(gyro_bias_rw_sigma, 2);
97 Matrix66 bias_acc_omega_init =
102 p->accelerometerCovariance =
104 p->integrationCovariance =
105 integration_error_cov;
108 p->gyroscopeCovariance =
111 p->biasAccCovariance = bias_acc_cov;
112 p->biasOmegaCovariance = bias_omega_cov;
113 p->biasAccOmegaInt = bias_acc_omega_init;
118 int main(
int argc,
char* argv[]) {
121 bool use_isam =
false;
126 output_filename = var_map[
"output_filename"].as<
string>();
127 use_isam = var_map[
"use_isam"].as<
bool>();
131 printf(
"Using ISAM2\n");
135 isam2 =
new ISAM2(parameters);
138 printf(
"Using Levenberg Marquardt Optimizer\n");
142 FILE* fp_out = fopen(output_filename.c_str(),
"w+");
144 "#time(s),x(m),y(m),z(m),qx,qy,qz,qw,gt_x(m),gt_y(m),gt_z(m),gt_qx," 145 "gt_qy,gt_qz,gt_qw\n");
150 ifstream
file(data_filename.c_str());
154 Vector10 initial_state;
155 getline(
file, value,
',');
156 for (
int i = 0;
i < 9;
i++) {
157 getline(
file, value,
',');
158 initial_state(
i) = stof(value.c_str());
160 getline(
file, value,
'\n');
161 initial_state(9) = stof(value.c_str());
162 cout <<
"initial state:\n" << initial_state.transpose() <<
"\n\n";
167 initial_state(4), initial_state(5));
168 Point3 prior_point(initial_state.head<3>());
169 Pose3 prior_pose(prior_rotation, prior_point);
170 Vector3 prior_velocity(initial_state.tail<3>());
174 int correction_count = 0;
175 initial_values.
insert(
X(correction_count), prior_pose);
176 initial_values.
insert(
V(correction_count), prior_velocity);
177 initial_values.
insert(
B(correction_count), prior_imu_bias);
181 (
Vector(6) << 0.01, 0.01, 0.01, 0.5, 0.5, 0.5)
188 graph->
addPrior(
X(correction_count), prior_pose, pose_noise_model);
189 graph->
addPrior(
V(correction_count), prior_velocity, velocity_noise_model);
190 graph->
addPrior(
B(correction_count), prior_imu_bias, bias_noise_model);
194 std::shared_ptr<PreintegrationType> preintegrated =
195 std::make_shared<PreintegratedImuMeasurements>(
p, prior_imu_bias);
197 assert(preintegrated);
200 NavState prev_state(prior_pose, prior_velocity);
205 double current_position_error = 0.0, current_orientation_error = 0.0;
207 double output_time = 0.0;
212 while (
file.good()) {
214 getline(
file, value,
',');
215 int type = stoi(value.c_str());
219 for (
int i = 0;
i < 5; ++
i) {
220 getline(
file, value,
',');
221 imu(
i) = stof(value.c_str());
223 getline(
file, value,
'\n');
224 imu(5) = stof(value.c_str());
227 preintegrated->integrateMeasurement(imu.head<3>(), imu.tail<3>(), dt);
229 }
else if (type == 1) {
231 for (
int i = 0;
i < 6; ++
i) {
232 getline(
file, value,
',');
233 gps(
i) = stof(value.c_str());
235 getline(
file, value,
'\n');
236 gps(6) = stof(value.c_str());
243 ImuFactor imu_factor(
X(correction_count - 1),
V(correction_count - 1),
244 X(correction_count),
V(correction_count),
245 B(correction_count - 1), preint_imu);
246 graph->
add(imu_factor);
249 B(correction_count - 1),
B(correction_count), zero_bias,
258 graph->
add(gps_factor);
261 prop_state = preintegrated->predict(prev_state, prev_bias);
262 initial_values.
insert(
X(correction_count), prop_state.
pose());
263 initial_values.
insert(
V(correction_count), prop_state.
v());
264 initial_values.
insert(
B(correction_count), prev_bias);
269 isam2->
update(*graph, initial_values);
270 result = isam2->calculateEstimate();
274 initial_values.
clear();
287 preintegrated->resetIntegrationAndSetBias(prev_bias);
291 Vector3 position_error = gtsam_position - gps.head<3>();
292 current_position_error = position_error.norm();
295 Quaternion gps_quat(gps(6), gps(3), gps(4), gps(5));
298 Vector3 euler_angle_error(quat_error.
x() * 2, quat_error.
y() * 2,
300 current_orientation_error = euler_angle_error.norm();
303 cout <<
"Position error:" << current_position_error <<
"\t " 304 <<
"Angular error:" << current_orientation_error <<
"\n";
306 fprintf(fp_out,
"%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",
307 output_time, gtsam_position(0), gtsam_position(1),
308 gtsam_position(2), gtsam_quat.
x(), gtsam_quat.
y(), gtsam_quat.
z(),
309 gtsam_quat.
w(), gps(0), gps(1), gps(2), gps_quat.
x(),
310 gps_quat.
y(), gps_quat.
z(), gps_quat.
w());
315 cerr <<
"ERROR parsing file\n";
320 cout <<
"Complete, results written to " << output_filename <<
"\n\n";
static std::shared_ptr< PreintegrationCombinedParams > MakeSharedD(double g=9.81)
EIGEN_DEVICE_FUNC CoeffReturnType x() const
const Point3 & translation(OptionalJacobian< 3, 6 > Hself={}) const
get translation
virtual const Values & optimize()
Factor Graph consisting of non-linear factors.
const ValueType at(Key j) const
EIGEN_DEVICE_FUNC CoeffReturnType z() const
void update(Key j, const Value &val)
EIGEN_DEVICE_FUNC CoeffReturnType w() const
NonlinearFactorGraph graph
Rot3 is a 3D rotation represented as a rotation matrix if the preprocessor symbol GTSAM_USE_QUATERNIO...
IsDerived< DERIVEDFACTOR > add(std::shared_ptr< DERIVEDFACTOR > factor)
add is a synonym for push_back.
void addPrior(Key key, const T &prior, const SharedNoiseModel &model=nullptr)
const string output_filename
static Rot3 Quaternion(double w, double x, double y, double z)
Header file for GPS factor.
const Vector3 & v() const
Return velocity as Vector3. Computation-free.
EIGEN_DEVICE_FUNC void normalize()
Incremental update functionality (ISAM2) for BayesTree, with fluid relinearization.
EIGEN_DEVICE_FUNC Quaternion< Scalar > inverse() const
A nonlinear optimizer that uses the Levenberg-Marquardt trust-region scheme.
Array< double, 1, 3 > e(1./3., 0.5, 2.)
po::variables_map parseOptions(int argc, char *argv[])
EIGEN_DEVICE_FUNC CoeffReturnType y() const
static ConjugateGradientParameters parameters
std::shared_ptr< PreintegratedCombinedMeasurements::Params > imuParams()
RelinearizationThreshold relinearizeThreshold
gtsam::Quaternion toQuaternion() const
GTSAM_EXPORT std::string findExampleDataFile(const std::string &name)
virtual void resize(size_t size)
static shared_ptr Sigmas(const Vector &sigmas, bool smart=true)
The quaternion class used to represent 3D orientations and rotations.
void insert(Key j, const Value &val)
const Rot3 & rotation(OptionalJacobian< 3, 6 > Hself={}) const
get rotation
Jet< T, N > pow(const Jet< T, N > &f, double g)
utility functions for loading datasets
int main(int argc, char *argv[])
static shared_ptr Sigma(size_t dim, double sigma, bool smart=true)