| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * GTSAM Copyright 2010, Georgia Tech Research Corporation, | 
					
						
							|  |  |  |  * 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file   testImuPreintegration.cpp | 
					
						
							|  |  |  |  * @brief  Unit tests for IMU Preintegration | 
					
						
							|  |  |  |  * @author Russell Buchanan | 
					
						
							|  |  |  |  **/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <CppUnitLite/TestHarness.h>
 | 
					
						
							|  |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | #include <gtsam/base/numericalDerivative.h>
 | 
					
						
							|  |  |  | #include <gtsam/navigation/CombinedImuFactor.h>
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  | #include <gtsam/slam/dataset.h>
 | 
					
						
							|  |  |  | #include <tests/ImuMeasurement.h>
 | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * \brief Uses the GTSAM library to perform IMU preintegration on an | 
					
						
							|  |  |  |  * acceleration input. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | TEST(TestImuPreintegration, LoadedSimulationData) { | 
					
						
							| 
									
										
										
										
											2020-12-15 23:50:28 +08:00
										 |  |  |   Vector3 finalPos(0, 0, 0); | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |   vector<ImuMeasurement> imuMeasurements; | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   double accNoiseSigma = 0.001249; | 
					
						
							|  |  |  |   double accBiasRwSigma = 0.000106; | 
					
						
							|  |  |  |   double gyrNoiseSigma = 0.000208; | 
					
						
							|  |  |  |   double gyrBiasRwSigma = 0.000004; | 
					
						
							|  |  |  |   double integrationCovariance = 1e-8; | 
					
						
							|  |  |  |   double biasAccOmegaInt = 1e-5; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   double gravity = 9.81; | 
					
						
							|  |  |  |   double rate = 400.0;  // Hz
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |   string inFileString = findExampleDataFile("quadraped_imu_data.csv"); | 
					
						
							|  |  |  |   ifstream inputFile(inFileString); | 
					
						
							|  |  |  |   string line; | 
					
						
							|  |  |  |   while (getline(inputFile, line)) { | 
					
						
							|  |  |  |     stringstream ss(line); | 
					
						
							|  |  |  |     string str; | 
					
						
							|  |  |  |     vector<double> results; | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  |     while (getline(ss, str, ',')) { | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |       results.push_back(atof(str.c_str())); | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |     ImuMeasurement measurement; | 
					
						
							|  |  |  |     measurement.dt = static_cast<size_t>(1e9 * (1 / rate)); | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  |     measurement.time = results[2]; | 
					
						
							|  |  |  |     measurement.I_a_WI = {results[29], results[30], results[31]}; | 
					
						
							|  |  |  |     measurement.I_w_WI = {results[17], results[18], results[19]}; | 
					
						
							|  |  |  |     imuMeasurements.push_back(measurement); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |   // Assume a Z-up navigation (assuming we are performing optimization in the
 | 
					
						
							|  |  |  |   // IMU frame).
 | 
					
						
							| 
									
										
										
										
											2020-12-15 23:50:28 +08:00
										 |  |  |   auto imuPreintegratedParams = | 
					
						
							|  |  |  |       PreintegratedCombinedMeasurements::Params::MakeSharedU(gravity); | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |   imuPreintegratedParams->accelerometerCovariance = | 
					
						
							|  |  |  |       I_3x3 * pow(accNoiseSigma, 2); | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  |   imuPreintegratedParams->biasAccCovariance = I_3x3 * pow(accBiasRwSigma, 2); | 
					
						
							|  |  |  |   imuPreintegratedParams->gyroscopeCovariance = I_3x3 * pow(gyrNoiseSigma, 2); | 
					
						
							|  |  |  |   imuPreintegratedParams->biasOmegaCovariance = I_3x3 * pow(gyrBiasRwSigma, 2); | 
					
						
							|  |  |  |   imuPreintegratedParams->integrationCovariance = I_3x3 * integrationCovariance; | 
					
						
							|  |  |  |   imuPreintegratedParams->biasAccOmegaInt = I_6x6 * biasAccOmegaInt; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Initial state
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |   Pose3 priorPose; | 
					
						
							| 
									
										
										
										
											2020-12-15 23:50:28 +08:00
										 |  |  |   Vector3 priorVelocity(0, 0, 0); | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |   imuBias::ConstantBias priorImuBias; | 
					
						
							|  |  |  |   PreintegratedCombinedMeasurements imuPreintegrated; | 
					
						
							| 
									
										
										
										
											2020-12-15 23:50:28 +08:00
										 |  |  |   Vector3 position(0, 0, 0); | 
					
						
							|  |  |  |   Vector3 velocity(0, 0, 0); | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |   NavState propState; | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |   NavState initialNavState(priorPose, priorVelocity); | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-19 18:49:42 +08:00
										 |  |  |   // Assume zero bias for simulated data
 | 
					
						
							| 
									
										
										
										
											2021-01-20 04:47:15 +08:00
										 |  |  |   priorImuBias = | 
					
						
							|  |  |  |       imuBias::ConstantBias(Eigen::Vector3d(0, 0, 0), Eigen::Vector3d(0, 0, 0)); | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 23:50:28 +08:00
										 |  |  |   imuPreintegrated = | 
					
						
							|  |  |  |       PreintegratedCombinedMeasurements(imuPreintegratedParams, priorImuBias); | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |   // start at 1 to skip header
 | 
					
						
							|  |  |  |   for (size_t n = 1; n < imuMeasurements.size(); n++) { | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  |     // integrate
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |     imuPreintegrated.integrateMeasurement(imuMeasurements[n].I_a_WI, | 
					
						
							|  |  |  |                                           imuMeasurements[n].I_w_WI, 1 / rate); | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  |     // predict
 | 
					
						
							|  |  |  |     propState = imuPreintegrated.predict(initialNavState, priorImuBias); | 
					
						
							|  |  |  |     position = propState.pose().translation(); | 
					
						
							|  |  |  |     velocity = propState.velocity(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  |   Vector3 rotation = propState.pose().rotation().rpy(); | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Dont have ground truth for x and y position yet
 | 
					
						
							|  |  |  |   // DOUBLES_EQUAL(0.1, position[0], 1e-2);
 | 
					
						
							|  |  |  |   // DOUBLES_EQUAL(0.1, position[1], 1e-2);
 | 
					
						
							|  |  |  |   DOUBLES_EQUAL(0.0, position[2], 1e-2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   DOUBLES_EQUAL(0.0, rotation[0], 1e-2); | 
					
						
							|  |  |  |   DOUBLES_EQUAL(0.0, rotation[1], 1e-2); | 
					
						
							|  |  |  |   DOUBLES_EQUAL(0.0, rotation[2], 1e-2); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-12-15 03:32:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-08 23:22:01 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | int main() { | 
					
						
							|  |  |  |   TestResult tr; | 
					
						
							|  |  |  |   return TestRegistry::runAllTests(tr); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ |