| 
									
										
										
										
											2016-01-27 05:19:25 +08:00
										 |  |  | """
 | 
					
						
							| 
									
										
										
										
											2016-01-28 04:03:26 +08:00
										 |  |  | A script validating the ImuFactor inference. | 
					
						
							| 
									
										
										
										
											2016-01-27 05:19:25 +08:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  | from __future__ import print_function | 
					
						
							| 
									
										
										
										
											2016-01-27 06:41:55 +08:00
										 |  |  | import math | 
					
						
							| 
									
										
										
										
											2016-01-27 05:19:25 +08:00
										 |  |  | import matplotlib.pyplot as plt | 
					
						
							|  |  |  | import numpy as np | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-27 06:41:55 +08:00
										 |  |  | from mpl_toolkits.mplot3d import Axes3D | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import gtsam | 
					
						
							| 
									
										
										
										
											2016-01-27 07:43:46 +08:00
										 |  |  | from gtsam_utils import plotPose3 | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  | from PreintegrationExample import PreintegrationExample, POSES_FIG | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # shorthand symbols: | 
					
						
							|  |  |  | BIAS_KEY = int(gtsam.Symbol('b', 0)) | 
					
						
							|  |  |  | V = lambda j: int(gtsam.Symbol('v', j)) | 
					
						
							|  |  |  | X = lambda i: int(gtsam.Symbol('x', i)) | 
					
						
							| 
									
										
										
										
											2016-01-27 06:41:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  | np.set_printoptions(precision=3, suppress=True) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 04:03:26 +08:00
										 |  |  | class ImuFactorExample(PreintegrationExample): | 
					
						
							| 
									
										
										
										
											2016-01-27 05:19:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 07:15:55 +08:00
										 |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.velocity = np.array([2, 0, 0]) | 
					
						
							| 
									
										
										
										
											2016-01-28 16:29:18 +08:00
										 |  |  |         self.priorNoise = gtsam.noiseModel.Isotropic.Sigma(6, 0.1) | 
					
						
							|  |  |  |         self.velNoise = gtsam.noiseModel.Isotropic.Sigma(3, 0.1) | 
					
						
							| 
									
										
										
										
											2016-01-28 16:58:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |         # Choose one of these twists to change scenario:  | 
					
						
							|  |  |  |         zero_twist = (np.zeros(3), np.zeros(3)) | 
					
						
							| 
									
										
										
										
											2016-01-28 16:58:31 +08:00
										 |  |  |         forward_twist = (np.zeros(3), self.velocity) | 
					
						
							|  |  |  |         loop_twist = (np.array([0, -math.radians(30), 0]), self.velocity) | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |         sick_twist = (np.array([math.radians(30), -math.radians(30), 0]), self.velocity) | 
					
						
							| 
									
										
										
										
											2016-01-28 16:58:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         accBias = np.array([-0.3, 0.1, 0.2]) | 
					
						
							|  |  |  |         gyroBias = np.array([0.1, 0.3, -0.1]) | 
					
						
							|  |  |  |         bias = gtsam.ConstantBias(accBias, gyroBias) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |         dt = 1e-2 | 
					
						
							|  |  |  |         super(ImuFactorExample, self).__init__(sick_twist, bias, dt) | 
					
						
							| 
									
										
										
										
											2016-01-28 16:58:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 16:29:18 +08:00
										 |  |  |     def addPrior(self, i, graph): | 
					
						
							|  |  |  |         state = self.scenario.navState(i) | 
					
						
							|  |  |  |         graph.push_back(gtsam.PriorFactorPose3(X(i), state.pose(), self.priorNoise)) | 
					
						
							|  |  |  |         graph.push_back(gtsam.PriorFactorVector3(V(i), state.velocity(), self.velNoise)) | 
					
						
							| 
									
										
										
										
											2016-01-28 07:15:55 +08:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2016-01-27 05:19:25 +08:00
										 |  |  |     def run(self): | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |         graph = gtsam.NonlinearFactorGraph() | 
					
						
							| 
									
										
										
										
											2016-01-28 07:15:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |         # initialize data structure for pre-integrated IMU measurements  | 
					
						
							| 
									
										
										
										
											2016-01-28 04:03:26 +08:00
										 |  |  |         pim = gtsam.PreintegratedImuMeasurements(self.params, self.actualBias) | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |         H9 = gtsam.OptionalJacobian9(); | 
					
						
							|  |  |  |          | 
					
						
							| 
									
										
										
										
											2016-01-28 16:29:18 +08:00
										 |  |  |         T = 12 | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |         num_poses = T + 1  # assumes 1 factor per second | 
					
						
							|  |  |  |         initial = gtsam.Values() | 
					
						
							|  |  |  |         initial.insert(BIAS_KEY, self.actualBias) | 
					
						
							|  |  |  |         for i in range(num_poses): | 
					
						
							|  |  |  |             state_i = self.scenario.navState(float(i)) | 
					
						
							|  |  |  |             initial.insert(X(i), state_i.pose()) | 
					
						
							|  |  |  |             initial.insert(V(i), state_i.velocity()) | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         # simulate the loop | 
					
						
							|  |  |  |         i = 0  # state index         | 
					
						
							| 
									
										
										
										
											2016-01-28 13:23:57 +08:00
										 |  |  |         actual_state_i = self.scenario.navState(0) | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |         for k, t in enumerate(np.arange(0, T, self.dt)): | 
					
						
							|  |  |  |             # get measurements and add them to PIM | 
					
						
							| 
									
										
										
										
											2016-01-27 09:37:38 +08:00
										 |  |  |             measuredOmega = self.runner.measuredAngularVelocity(t) | 
					
						
							|  |  |  |             measuredAcc = self.runner.measuredSpecificForce(t) | 
					
						
							| 
									
										
										
										
											2016-06-09 15:19:54 +08:00
										 |  |  |             pim.integrateMeasurement(measuredAcc, measuredOmega, self.dt) | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |              | 
					
						
							| 
									
										
										
										
											2016-01-28 16:29:18 +08:00
										 |  |  |             # Plot IMU many times | 
					
						
							|  |  |  |             if k % 10 == 0: | 
					
						
							|  |  |  |                 self.plotImu(t, measuredOmega, measuredAcc) | 
					
						
							|  |  |  |              | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |             # Plot every second | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |             if k % int(1 / self.dt) == 0: | 
					
						
							| 
									
										
										
										
											2016-01-28 04:03:26 +08:00
										 |  |  |                 self.plotGroundTruthPose(t) | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |              | 
					
						
							| 
									
										
										
										
											2016-01-28 16:29:18 +08:00
										 |  |  |             # create IMU factor every second | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |             if (k + 1) % int(1 / self.dt) == 0: | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |                 factor = gtsam.ImuFactor(X(i), V(i), X(i + 1), V(i + 1), BIAS_KEY, pim) | 
					
						
							|  |  |  |                 graph.push_back(factor) | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |                 if True: | 
					
						
							|  |  |  |                     print(factor) | 
					
						
							|  |  |  |                     H2 = gtsam.OptionalJacobian96(); | 
					
						
							|  |  |  |                     print(pim.predict(actual_state_i, self.actualBias, H9, H2)) | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |                 pim.resetIntegration() | 
					
						
							| 
									
										
										
										
											2016-01-28 13:23:57 +08:00
										 |  |  |                 actual_state_i = self.scenario.navState(t + self.dt) | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |                 i += 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 07:15:55 +08:00
										 |  |  |         # add priors on beginning and end | 
					
						
							| 
									
										
										
										
											2016-01-28 16:29:18 +08:00
										 |  |  |         self.addPrior(0, graph) | 
					
						
							|  |  |  |         self.addPrior(num_poses - 1, graph) | 
					
						
							| 
									
										
										
										
											2016-01-28 07:15:55 +08:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |         # optimize using Levenberg-Marquardt optimization | 
					
						
							|  |  |  |         params = gtsam.LevenbergMarquardtParams() | 
					
						
							|  |  |  |         params.setVerbosityLM("SUMMARY") | 
					
						
							|  |  |  |         optimizer = gtsam.LevenbergMarquardtOptimizer(graph, initial, params) | 
					
						
							|  |  |  |         result = optimizer.optimize() | 
					
						
							| 
									
										
										
										
											2016-02-25 03:01:19 +08:00
										 |  |  |          | 
					
						
							|  |  |  |         # Calculate and print marginal covariances | 
					
						
							|  |  |  |         marginals = gtsam.Marginals(graph, result) | 
					
						
							|  |  |  |         print("Covariance on bias:\n", marginals.marginalCovariance(BIAS_KEY)) | 
					
						
							|  |  |  |         for i in range(num_poses): | 
					
						
							|  |  |  |             print("Covariance on pose {}:\n{}\n".format(i, marginals.marginalCovariance(X(i)))) | 
					
						
							|  |  |  |             print("Covariance on vel {}:\n{}\n".format(i, marginals.marginalCovariance(V(i)))) | 
					
						
							| 
									
										
										
										
											2016-01-27 06:41:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 16:58:31 +08:00
										 |  |  |         # Plot resulting poses | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |         i = 0 | 
					
						
							|  |  |  |         while result.exists(X(i)): | 
					
						
							| 
									
										
										
										
											2016-01-28 16:58:31 +08:00
										 |  |  |             pose_i = result.atPose3(X(i)) | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |             plotPose3(POSES_FIG, pose_i, 0.1) | 
					
						
							|  |  |  |             i += 1 | 
					
						
							| 
									
										
										
										
											2016-01-28 16:58:31 +08:00
										 |  |  |         print(result.atConstantBias(BIAS_KEY)) | 
					
						
							| 
									
										
										
										
											2016-01-28 06:16:18 +08:00
										 |  |  |              | 
					
						
							| 
									
										
										
										
											2016-01-27 06:41:55 +08:00
										 |  |  |         plt.ioff() | 
					
						
							|  |  |  |         plt.show() | 
					
						
							| 
									
										
										
										
											2016-01-27 05:19:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     ImuFactorExample().run() |