| 
									
										
										
										
											2014-04-05 05:00:20 +08:00
										 |  |  | import gtsam.*; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-18 02:11:18 +08:00
										 |  |  | % Test GTSAM covariances on a factor graph with: | 
					
						
							|  |  |  | % Between Factors | 
					
						
							| 
									
										
										
										
											2014-04-30 03:46:43 +08:00
										 |  |  | % IMU factors (type 1 and type 2) | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  | % GPS prior factors on poses | 
					
						
							|  |  |  | % SmartProjectionPoseFactors | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | % Authors: Luca Carlone, David Jensen | 
					
						
							|  |  |  | % Date: 2014/4/6 | 
					
						
							| 
									
										
										
										
											2014-04-03 23:34:26 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  | % Check for an extneral configuration, used when running multiple tests | 
					
						
							|  |  |  | if ~exist('externallyConfigured', 'var') | 
					
						
							|  |  |  |   clc | 
					
						
							|  |  |  |   clear all | 
					
						
							|  |  |  |   close all | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   saveResults = 0; | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   %% Configuration | 
					
						
							|  |  |  |   % General options | 
					
						
							|  |  |  |   options.useRealData = 1;           % controls whether or not to use the real data (if available) as the ground truth traj | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  |   options.includeBetweenFactors = 0; % if true, BetweenFactors will be added between consecutive poses | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  |   options.includeIMUFactors = 1;     % if true, IMU factors will be added between consecutive states (biases, poses, velocities) | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  |   options.imuFactorType = 1;         % Set to 1 or 2 to use IMU type 1 or type 2 factors (will default to type 1) | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  |   options.imuNonzeroBias = 0;        % if true, a nonzero bias is applied to IMU measurements | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  |   options.includeCameraFactors = 1;  % if true, SmartProjectionPose3Factors will be used with randomly generated landmarks | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  |   options.numberOfLandmarks = 1000;  % Total number of visual landmarks (randomly generated in a box around the trajectory) | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   options.includeGPSFactors = 0;     % if true, GPS factors will be added as priors to poses | 
					
						
							|  |  |  |   options.gpsStartPose = 100;        % Pose number to start including GPS factors at | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  |   options.trajectoryLength = 100;%209;    % length of the ground truth trajectory | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  |   options.subsampleStep = 20;        % number of poses to skip when using real data (to reduce computation on long trajectories) | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  |   numMonteCarloRuns = 2;             % number of Monte Carlo runs to perform | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   % Noise values to be adjusted | 
					
						
							|  |  |  |   sigma_ang = 1e-2;       % std. deviation for rotational noise, typical 1e-2 | 
					
						
							|  |  |  |   sigma_cart = 1e-1;      % std. deviation for translational noise, typical 1e-1 | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  |   sigma_accel = 1e-3;     % std. deviation for accelerometer noise, typical 1e-3 | 
					
						
							|  |  |  |   sigma_gyro = 1e-5;      % std. deviation for gyroscope noise, typical 1e-5 | 
					
						
							|  |  |  |   sigma_accelBias = 1e-4; % std. deviation for added accelerometer constant bias, typical 1e-3 | 
					
						
							|  |  |  |   sigma_gyroBias = 1e-6;  % std. deviation for added gyroscope constant bias, typical 1e-5 | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  |   sigma_gps = 1e-4;       % std. deviation for noise in GPS position measurements, typical 1e-4 | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  |   sigma_camera = 1;  % std. deviation for noise in camera measurements (pixels) | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  |    | 
					
						
							|  |  |  |   % Set log files | 
					
						
							|  |  |  |   testName = sprintf('sa-%1.2g-sc-%1.2g-sacc-%1.2g-sg-%1.2g',sigma_ang,sigma_cart,sigma_accel,sigma_gyro) | 
					
						
							|  |  |  |   folderName = 'results/' | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |   fprintf('Tests have been externally configured.\n'); | 
					
						
							| 
									
										
										
										
											2014-04-15 02:38:20 +08:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2014-04-11 01:26:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  | %% Between metadata | 
					
						
							|  |  |  | noiseVectorPose = [sigma_ang * ones(3,1); sigma_cart * ones(3,1)]; | 
					
						
							|  |  |  | noisePose = noiseModel.Diagonal.Sigmas(noiseVectorPose); | 
					
						
							| 
									
										
										
										
											2014-04-25 05:01:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  | %% Imu metadata | 
					
						
							| 
									
										
										
										
											2014-04-17 03:01:12 +08:00
										 |  |  | metadata.imu.epsBias = 1e-10; % was 1e-7 | 
					
						
							|  |  |  | metadata.imu.g = [0;0;0]; | 
					
						
							|  |  |  | metadata.imu.omegaCoriolis = [0;0;0]; | 
					
						
							| 
									
										
										
										
											2014-04-25 05:01:08 +08:00
										 |  |  | metadata.imu.IntegrationSigma = 1e-5; | 
					
						
							|  |  |  | metadata.imu.zeroBias = imuBias.ConstantBias(zeros(3,1), zeros(3,1)); | 
					
						
							|  |  |  | metadata.imu.AccelerometerSigma = sigma_accel; | 
					
						
							|  |  |  | metadata.imu.GyroscopeSigma = sigma_gyro; | 
					
						
							|  |  |  | metadata.imu.BiasAccelerometerSigma = metadata.imu.epsBias;  % noise on expected change in accelerometer bias over time | 
					
						
							|  |  |  | metadata.imu.BiasGyroscopeSigma = metadata.imu.epsBias;      % noise on expected change in gyroscope bias over time | 
					
						
							|  |  |  | % noise on initial accelerometer and gyroscope biases | 
					
						
							|  |  |  | if options.imuNonzeroBias == 1 | 
					
						
							|  |  |  |   metadata.imu.BiasAccOmegaInit = [sigma_accelBias * ones(3,1); sigma_gyroBias * ones(3,1)]; | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |   metadata.imu.BiasAccOmegaInit = metadata.imu.epsBias * ones(6,1); | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 00:12:39 +08:00
										 |  |  | noiseVel =  noiseModel.Isotropic.Sigma(3, 1e-2); % was 0.1 | 
					
						
							| 
									
										
										
										
											2014-04-25 05:01:08 +08:00
										 |  |  | noiseBiasBetween = noiseModel.Diagonal.Sigmas([metadata.imu.BiasAccelerometerSigma * ones(3,1);... | 
					
						
							|  |  |  |                                                metadata.imu.BiasGyroscopeSigma * ones(3,1)]); % between on biases | 
					
						
							|  |  |  | noisePriorBias = noiseModel.Diagonal.Sigmas(metadata.imu.BiasAccOmegaInit); | 
					
						
							| 
									
										
										
										
											2014-04-08 11:12:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-25 05:01:08 +08:00
										 |  |  | noiseVectorAccel = metadata.imu.AccelerometerSigma * ones(3,1); | 
					
						
							|  |  |  | noiseVectorGyro = metadata.imu.GyroscopeSigma  * ones(3,1); | 
					
						
							| 
									
										
										
										
											2014-04-17 04:20:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-24 02:45:17 +08:00
										 |  |  | %% GPS metadata | 
					
						
							|  |  |  | noiseVectorGPS = sigma_gps * ones(3,1); | 
					
						
							|  |  |  | noiseGPS = noiseModel.Diagonal.Precisions([zeros(3,1); 1/sigma_gps^2 * ones(3,1)]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  | %% Camera metadata | 
					
						
							| 
									
										
										
										
											2014-05-15 22:01:53 +08:00
										 |  |  | metadata.camera.calibration = Cal3_S2(500,500,0,1920/2,1200/2); % Camera calibration | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  | metadata.camera.xlims = [-100, 650];    % x limits on area for landmark creation | 
					
						
							|  |  |  | metadata.camera.ylims = [-100, 700];    % y limits on area for landmark creation | 
					
						
							| 
									
										
										
										
											2014-05-15 22:01:53 +08:00
										 |  |  | metadata.camera.zlims = [-30, 30];      % z limits on area for landmark creation | 
					
						
							|  |  |  | metadata.camera.visualRange = 100;      % maximum distance from the camera that a landmark can be seen (meters) | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  | metadata.camera.bodyPoseCamera = Pose3; % pose of camera in body | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  | metadata.camera.CameraSigma = sigma_camera; | 
					
						
							|  |  |  | cameraMeasurementNoise = noiseModel.Isotropic.Sigma(2, metadata.camera.CameraSigma); | 
					
						
							|  |  |  | noiseVectorCamera = metadata.camera.CameraSigma .* ones(2,1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | % Create landmarks and smart factors | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  | if options.includeCameraFactors == 1 | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  |   for i = 1:options.numberOfLandmarks | 
					
						
							|  |  |  |     metadata.camera.gtLandmarkPoints(i) = Point3( ... | 
					
						
							|  |  |  |       [rand() * (metadata.camera.xlims(2)-metadata.camera.xlims(1)) + metadata.camera.xlims(1); ...   | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  |        rand() * (metadata.camera.ylims(2)-metadata.camera.ylims(1)) + metadata.camera.ylims(1); ... | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  |        rand() * (metadata.camera.zlims(2)-metadata.camera.zlims(1)) + metadata.camera.zlims(1)]); | 
					
						
							| 
									
										
										
										
											2014-05-02 00:56:24 +08:00
										 |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 00:12:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 03:01:12 +08:00
										 |  |  | %% Create ground truth trajectory and measurements | 
					
						
							|  |  |  | [gtValues, gtMeasurements] = imuSimulator.covarianceAnalysisCreateTrajectory(options, metadata); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | %% Create ground truth graph | 
					
						
							|  |  |  | % Set up noise models | 
					
						
							|  |  |  | gtNoiseModels.noisePose = noisePose; | 
					
						
							|  |  |  | gtNoiseModels.noiseVel = noiseVel; | 
					
						
							| 
									
										
										
										
											2014-04-25 05:01:08 +08:00
										 |  |  | gtNoiseModels.noiseBiasBetween = noiseBiasBetween; | 
					
						
							| 
									
										
										
										
											2014-04-17 03:01:12 +08:00
										 |  |  | gtNoiseModels.noisePriorPose = noisePose; | 
					
						
							|  |  |  | gtNoiseModels.noisePriorBias = noisePriorBias; | 
					
						
							| 
									
										
										
										
											2014-04-24 02:45:17 +08:00
										 |  |  | gtNoiseModels.noiseGPS = noiseGPS; | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  | gtNoiseModels.noiseCamera = cameraMeasurementNoise; | 
					
						
							| 
									
										
										
										
											2014-04-17 03:01:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | % Set measurement noise to 0, because this is ground truth | 
					
						
							| 
									
										
										
										
											2014-04-18 10:21:22 +08:00
										 |  |  | gtMeasurementNoise.poseNoiseVector = zeros(6,1); | 
					
						
							|  |  |  | gtMeasurementNoise.imu.accelNoiseVector = zeros(3,1); | 
					
						
							|  |  |  | gtMeasurementNoise.imu.gyroNoiseVector = zeros(3,1); | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  | gtMeasurementNoise.cameraNoiseVector = zeros(2,1); | 
					
						
							| 
									
										
										
										
											2014-04-24 02:45:17 +08:00
										 |  |  | gtMeasurementNoise.gpsNoiseVector = zeros(3,1); | 
					
						
							| 
									
										
										
										
											2014-04-16 00:12:39 +08:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2014-04-18 10:21:22 +08:00
										 |  |  | % Set IMU biases to zero | 
					
						
							|  |  |  | metadata.imu.accelConstantBiasVector = zeros(3,1); | 
					
						
							|  |  |  | metadata.imu.gyroConstantBiasVector = zeros(3,1); | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2014-05-15 22:01:53 +08:00
										 |  |  | [gtGraph, projectionFactorSeenBy] = imuSimulator.covarianceAnalysisCreateFactorGraph( ... | 
					
						
							| 
									
										
										
										
											2014-04-17 03:01:12 +08:00
										 |  |  |     gtMeasurements, ...     % ground truth measurements | 
					
						
							|  |  |  |     gtValues, ...           % ground truth Values | 
					
						
							|  |  |  |     gtNoiseModels, ...      % noise models to use in this graph | 
					
						
							|  |  |  |     gtMeasurementNoise, ... % noise to apply to measurements | 
					
						
							|  |  |  |     options, ...            % options for the graph (e.g. which factors to include) | 
					
						
							|  |  |  |     metadata);              % misc data necessary for factor creation | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | %% Display, printing, and plotting of ground truth | 
					
						
							| 
									
										
										
										
											2014-04-17 04:20:10 +08:00
										 |  |  | %gtGraph.print(sprintf('\nGround Truth Factor graph:\n')); | 
					
						
							|  |  |  | %gtValues.print(sprintf('\nGround Truth Values:\n  ')); | 
					
						
							| 
									
										
										
										
											2014-04-15 23:05:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | figure(1) | 
					
						
							|  |  |  | hold on; | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | if options.includeCameraFactors | 
					
						
							|  |  |  |   b = [-1000 2000 -2000 2000 -30 30]; | 
					
						
							|  |  |  |   for i = 1:size(metadata.camera.gtLandmarkPoints,2) | 
					
						
							| 
									
										
										
										
											2020-08-18 02:37:12 +08:00
										 |  |  |       p = metadata.camera.gtLandmarkPoints(i); | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  |       if(p(1) > b(1) && p(1) < b(2) && p(2) > b(3) && p(2) < b(4) && p(3) > b(5) && p(3) < b(6)) | 
					
						
							|  |  |  |           plot3(p(1), p(2), p(3), 'k+'); | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |   pointsToPlot = metadata.camera.gtLandmarkPoints(find(projectionFactorSeenBy > 0)); | 
					
						
							|  |  |  |   for i = 1:length(pointsToPlot) | 
					
						
							| 
									
										
										
										
											2020-08-18 02:37:12 +08:00
										 |  |  |       p = pointsToPlot(i); | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  |       plot3(p(1), p(2), p(3), 'gs', 'MarkerSize', 10); | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-05-15 22:01:53 +08:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2014-04-15 02:38:20 +08:00
										 |  |  | plot3DPoints(gtValues); | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  | %plot3DTrajectory(gtValues, '-r', [], 1, Marginals(gtGraph, gtValues)); | 
					
						
							|  |  |  | plot3DTrajectory(gtValues, '-r'); | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | axis equal | 
					
						
							| 
									
										
										
										
											2014-04-05 05:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-18 04:00:18 +08:00
										 |  |  | % optimize | 
					
						
							|  |  |  | optimizer = GaussNewtonOptimizer(gtGraph, gtValues); | 
					
						
							|  |  |  | gtEstimate = optimizer.optimize(); | 
					
						
							| 
									
										
										
										
											2014-05-16 03:57:22 +08:00
										 |  |  | plot3DTrajectory(gtEstimate, '-k'); | 
					
						
							| 
									
										
										
										
											2014-04-18 04:00:18 +08:00
										 |  |  | % estimate should match gtValues if graph is correct. | 
					
						
							|  |  |  | fprintf('Error in ground truth graph at gtValues: %g \n', gtGraph.error(gtValues) ); | 
					
						
							|  |  |  | fprintf('Error in ground truth graph at gtEstimate: %g \n', gtGraph.error(gtEstimate) ); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-11 10:56:46 +08:00
										 |  |  | disp('Plotted ground truth') | 
					
						
							| 
									
										
										
										
											2014-04-11 10:00:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-17 03:01:12 +08:00
										 |  |  | %% Monte Carlo Runs | 
					
						
							| 
									
										
										
										
											2014-04-17 04:20:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | % Set up noise models | 
					
						
							|  |  |  | monteCarloNoiseModels.noisePose = noisePose; | 
					
						
							|  |  |  | monteCarloNoiseModels.noiseVel = noiseVel; | 
					
						
							| 
									
										
										
										
											2014-04-25 05:01:08 +08:00
										 |  |  | monteCarloNoiseModels.noiseBiasBetween = noiseBiasBetween; | 
					
						
							| 
									
										
										
										
											2014-04-17 04:20:10 +08:00
										 |  |  | monteCarloNoiseModels.noisePriorPose = noisePose; | 
					
						
							|  |  |  | monteCarloNoiseModels.noisePriorBias = noisePriorBias; | 
					
						
							| 
									
										
										
										
											2014-04-24 02:45:17 +08:00
										 |  |  | monteCarloNoiseModels.noiseGPS = noiseGPS; | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  | monteCarloNoiseModels.noiseCamera = cameraMeasurementNoise; | 
					
						
							| 
									
										
										
										
											2014-04-17 04:20:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | % Set measurement noise for monte carlo runs | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  | monteCarloMeasurementNoise.poseNoiseVector = zeros(6,1); %noiseVectorPose; | 
					
						
							| 
									
										
										
										
											2014-04-17 04:20:10 +08:00
										 |  |  | monteCarloMeasurementNoise.imu.accelNoiseVector = noiseVectorAccel; | 
					
						
							|  |  |  | monteCarloMeasurementNoise.imu.gyroNoiseVector = noiseVectorGyro; | 
					
						
							| 
									
										
										
										
											2014-04-24 02:45:17 +08:00
										 |  |  | monteCarloMeasurementNoise.gpsNoiseVector = noiseVectorGPS; | 
					
						
							| 
									
										
										
										
											2014-05-09 03:27:32 +08:00
										 |  |  | monteCarloMeasurementNoise.cameraNoiseVector = noiseVectorCamera; | 
					
						
							| 
									
										
										
										
											2014-04-17 04:20:10 +08:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | for k=1:numMonteCarloRuns | 
					
						
							| 
									
										
										
										
											2014-04-25 05:01:08 +08:00
										 |  |  |   fprintf('Monte Carlo Run %d...\n', k'); | 
					
						
							| 
									
										
										
										
											2014-04-17 03:25:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-18 10:21:22 +08:00
										 |  |  |   % Create a random bias for each run | 
					
						
							|  |  |  |   if options.imuNonzeroBias == 1 | 
					
						
							| 
									
										
										
										
											2014-04-30 03:46:43 +08:00
										 |  |  |     metadata.imu.accelConstantBiasVector = metadata.imu.BiasAccOmegaInit(1:3) .* randn(3,1); | 
					
						
							|  |  |  |     metadata.imu.gyroConstantBiasVector = metadata.imu.BiasAccOmegaInit(4:6) .* randn(3,1); | 
					
						
							|  |  |  |     %metadata.imu.accelConstantBiasVector = 1e-2 * ones(3,1); | 
					
						
							|  |  |  |     %metadata.imu.gyroConstantBiasVector = 1e-3 * ones(3,1); | 
					
						
							| 
									
										
										
										
											2014-04-18 10:21:22 +08:00
										 |  |  |   else | 
					
						
							|  |  |  |     metadata.imu.accelConstantBiasVector = zeros(3,1); | 
					
						
							|  |  |  |     metadata.imu.gyroConstantBiasVector = zeros(3,1); | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2014-04-17 04:20:10 +08:00
										 |  |  |   % Create a new graph using noisy measurements | 
					
						
							| 
									
										
										
										
											2014-05-15 22:01:53 +08:00
										 |  |  |   [graph, projectionFactorSeenBy] = imuSimulator.covarianceAnalysisCreateFactorGraph( ... | 
					
						
							| 
									
										
										
										
											2014-04-17 04:20:10 +08:00
										 |  |  |     gtMeasurements, ...     % ground truth measurements | 
					
						
							|  |  |  |     gtValues, ...           % ground truth Values | 
					
						
							|  |  |  |     monteCarloNoiseModels, ...      % noise models to use in this graph | 
					
						
							|  |  |  |     monteCarloMeasurementNoise, ... % noise to apply to measurements | 
					
						
							|  |  |  |     options, ...            % options for the graph (e.g. which factors to include) | 
					
						
							|  |  |  |     metadata);              % misc data necessary for factor creation | 
					
						
							| 
									
										
										
										
											2014-04-17 03:25:05 +08:00
										 |  |  |        | 
					
						
							|  |  |  |   %graph.print('graph') | 
					
						
							| 
									
										
										
										
											2014-04-16 00:12:39 +08:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  |   % optimize | 
					
						
							|  |  |  |   optimizer = GaussNewtonOptimizer(graph, gtValues); | 
					
						
							|  |  |  |   estimate = optimizer.optimize(); | 
					
						
							|  |  |  |   figure(1) | 
					
						
							|  |  |  |   plot3DTrajectory(estimate, '-b'); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   marginals = Marginals(graph, estimate); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   % for each pose in the trajectory | 
					
						
							| 
									
										
										
										
											2014-04-18 03:23:01 +08:00
										 |  |  |   for i=0:options.trajectoryLength | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  |     % compute estimation errors | 
					
						
							| 
									
										
										
										
											2014-04-18 03:23:01 +08:00
										 |  |  |     currentPoseKey = symbol('x', i); | 
					
						
							| 
									
										
										
										
											2020-08-18 02:37:12 +08:00
										 |  |  |     gtPosition  = gtValues.atPose3(currentPoseKey).translation; | 
					
						
							|  |  |  |     estPosition = estimate.atPose3(currentPoseKey).translation; | 
					
						
							|  |  |  |     estR = estimate.atPose3(currentPoseKey).rotation.matrix; | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  |     errPosition = estPosition - gtPosition; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     % compute covariances: | 
					
						
							|  |  |  |     cov = marginals.marginalCovariance(currentPoseKey); | 
					
						
							| 
									
										
										
										
											2014-04-07 23:56:22 +08:00
										 |  |  |     covPosition = estR * cov(4:6,4:6) * estR'; | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  |     % compute NEES using (estimationError = estimatedValues - gtValues) and estimated covariances | 
					
						
							| 
									
										
										
										
											2014-04-18 03:23:01 +08:00
										 |  |  |     NEES(k,i+1) = errPosition' * inv(covPosition) * errPosition; % distributed according to a Chi square with n = 3 dof | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-04-11 07:26:53 +08:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  |   figure(2) | 
					
						
							|  |  |  |   hold on | 
					
						
							|  |  |  |   plot(NEES(k,:),'-b','LineWidth',1.5) | 
					
						
							|  |  |  | end | 
					
						
							| 
									
										
										
										
											2014-04-07 09:43:53 +08:00
										 |  |  | %% | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | ANEES = mean(NEES); | 
					
						
							|  |  |  | plot(ANEES,'-r','LineWidth',2) | 
					
						
							| 
									
										
										
										
											2014-04-11 07:26:53 +08:00
										 |  |  | plot(3*ones(size(ANEES,2),1),'k--'); % Expectation(ANEES) = number of dof | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | box on | 
					
						
							|  |  |  | set(gca,'Fontsize',16) | 
					
						
							|  |  |  | title('NEES and ANEES'); | 
					
						
							| 
									
										
										
										
											2014-04-18 04:00:18 +08:00
										 |  |  | if saveResults | 
					
						
							|  |  |  |   saveas(gcf,horzcat(folderName,'runs-',testName,'.fig'),'fig'); | 
					
						
							| 
									
										
										
										
											2014-04-24 22:24:44 +08:00
										 |  |  |   saveas(gcf,horzcat(folderName,'runs-',testName,'.png'),'png'); | 
					
						
							| 
									
										
										
										
											2014-04-18 04:00:18 +08:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 09:43:53 +08:00
										 |  |  | %% | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | figure(1) | 
					
						
							|  |  |  | box on | 
					
						
							|  |  |  | set(gca,'Fontsize',16) | 
					
						
							| 
									
										
										
										
											2014-04-07 09:43:53 +08:00
										 |  |  | title('Ground truth and estimates for each MC runs'); | 
					
						
							| 
									
										
										
										
											2014-04-18 04:00:18 +08:00
										 |  |  | if saveResults | 
					
						
							|  |  |  |   saveas(gcf,horzcat(folderName,'gt-',testName,'.fig'),'fig'); | 
					
						
							| 
									
										
										
										
											2014-04-24 22:24:44 +08:00
										 |  |  |   saveas(gcf,horzcat(folderName,'gt-',testName,'.png'),'png'); | 
					
						
							| 
									
										
										
										
											2014-04-18 04:00:18 +08:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2014-04-05 05:00:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | %% Let us compute statistics on the overall NEES | 
					
						
							|  |  |  | n = 3; % position vector dimension | 
					
						
							|  |  |  | N = numMonteCarloRuns; % number of runs | 
					
						
							|  |  |  | alpha = 0.01; % confidence level | 
					
						
							| 
									
										
										
										
											2014-04-03 23:34:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-11 07:26:53 +08:00
										 |  |  | % mean_value = n*N; % mean value of the Chi-square distribution | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | % (we divide by n * N and for this reason we expect ANEES around 1) | 
					
						
							|  |  |  | r1 = chi2inv(alpha, n * N)  / (n * N); | 
					
						
							|  |  |  | r2 = chi2inv(1-alpha, n * N)  / (n * N); | 
					
						
							| 
									
										
										
										
											2014-04-03 23:34:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | % output here | 
					
						
							|  |  |  | fprintf(1, 'r1 = %g\n', r1); | 
					
						
							|  |  |  | fprintf(1, 'r2 = %g\n', r2); | 
					
						
							| 
									
										
										
										
											2014-04-03 23:34:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | figure(3) | 
					
						
							|  |  |  | hold on | 
					
						
							|  |  |  | plot(ANEES/n,'-b','LineWidth',2) | 
					
						
							|  |  |  | plot(ones(size(ANEES,2),1),'r-'); | 
					
						
							|  |  |  | plot(r1*ones(size(ANEES,2),1),'k-.'); | 
					
						
							|  |  |  | plot(r2*ones(size(ANEES,2),1),'k-.'); | 
					
						
							|  |  |  | box on | 
					
						
							|  |  |  | set(gca,'Fontsize',16) | 
					
						
							| 
									
										
										
										
											2014-04-07 09:43:53 +08:00
										 |  |  | title('NEES normalized by dof VS bounds'); | 
					
						
							| 
									
										
										
										
											2014-04-18 04:00:18 +08:00
										 |  |  | if saveResults | 
					
						
							|  |  |  |   saveas(gcf,horzcat(folderName,'ANEES-',testName,'.fig'),'fig'); | 
					
						
							| 
									
										
										
										
											2014-04-24 22:24:44 +08:00
										 |  |  |   saveas(gcf,horzcat(folderName,'ANEES-',testName,'.png'),'png'); | 
					
						
							| 
									
										
										
										
											2014-04-18 04:00:18 +08:00
										 |  |  |   logFile = horzcat(folderName,'log-',testName); | 
					
						
							|  |  |  |   save(logFile) | 
					
						
							|  |  |  | end | 
					
						
							| 
									
										
										
										
											2014-04-03 23:34:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | %% NEES COMPUTATION (Bar-Shalom 2001, Section 5.4) | 
					
						
							| 
									
										
										
										
											2014-04-11 07:26:53 +08:00
										 |  |  | % the nees for a single experiment (i) is defined as | 
					
						
							|  |  |  | %               NEES_i = xtilda' * inv(P) * xtilda, | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | % where xtilda in R^n is the estimation | 
					
						
							|  |  |  | % error, and P is the covariance estimated by the approach we want to test | 
					
						
							| 
									
										
										
										
											2014-04-11 07:26:53 +08:00
										 |  |  | % | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | % Average NEES. Given N Monte Carlo simulations, i=1,...,N, the average | 
					
						
							|  |  |  | % NEES is: | 
					
						
							|  |  |  | %                   ANEES = sum(NEES_i)/N | 
					
						
							|  |  |  | % The quantity N*ANEES is distributed according to a Chi-square | 
					
						
							|  |  |  | % distribution with N*n degrees of freedom. | 
					
						
							|  |  |  | % | 
					
						
							| 
									
										
										
										
											2014-04-11 07:26:53 +08:00
										 |  |  | % For the single run case, N=1, therefore NEES = ANEES is distributed | 
					
						
							|  |  |  | % according to a chi-square distribution with n degrees of freedom (e.g. n=3 | 
					
						
							| 
									
										
										
										
											2014-04-07 09:05:13 +08:00
										 |  |  | % if we are testing a position estimate) | 
					
						
							|  |  |  | % Therefore its mean should be n (difficult to see from a single run) | 
					
						
							|  |  |  | % and, with probability alpha, it should hold: | 
					
						
							|  |  |  | % | 
					
						
							|  |  |  | % NEES in [r1, r2] | 
					
						
							|  |  |  | % | 
					
						
							|  |  |  | % where r1 and r2 are built from the Chi-square distribution | 
					
						
							| 
									
										
										
										
											2014-04-03 23:34:26 +08:00
										 |  |  | 
 |