2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								% GTSAM Copyright 2010, Georgia Tech Research Corporation,
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								% Atlanta, Georgia 30332-0415
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								% All Rights Reserved
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								% Authors: Frank Dellaert, et al. (see THANKS for the full author list)
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								% See LICENSE for the license information
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								% @brief Simple robotics example using the pre-built planar SLAM domain
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								% @author Alex Cunningham
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								% @author Frank Dellaert
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%% Assumptions
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%  - All values are axis aligned
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%  - Robot poses are facing along the X axis (horizontal, to the right in images)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%  - We have bearing and range information for measurements
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%  - We have full odometry for measurements
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%  - The robot and landmarks are on a grid, moving 2 meters each step
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%  - Landmarks are 2 meters away from the robot trajectory
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%% Create keys for variables
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								i1 = symbol('x',1); i2 = symbol('x',2); i3 = symbol('x',3);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								j1 = symbol('l',1); j2 = symbol('l',2);
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%% Create graph container and add factors to it
							 | 
						
					
						
							
								
									
										
										
										
											2012-01-05 00:18:38 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								graph = planarSLAMGraph;
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%% Add prior
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-03 13:25:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								priorMean = gtsamPose2(0.0, 0.0, 0.0); % prior at origin
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-10 04:04:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								priorNoise = gtsamnoiseModelDiagonal.Sigmas([0.3; 0.3; 0.1]);
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-24 10:48:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								graph.addPosePrior(i1, priorMean, priorNoise); % add directly to graph
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%% Add odometry
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								odometry = gtsamPose2(2.0, 0.0, 0.0);
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-10 04:04:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								odometryNoise = gtsamnoiseModelDiagonal.Sigmas([0.2; 0.2; 0.1]);
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-24 10:48:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								graph.addRelativePose(i1, i2, odometry, odometryNoise);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								graph.addRelativePose(i2, i3, odometry, odometryNoise);
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%% Add bearing/range measurement factors
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								degrees = pi/180;
							 | 
						
					
						
							
								
									
										
										
										
											2012-07-10 04:04:06 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								noiseModel = gtsamnoiseModelDiagonal.Sigmas([0.1; 0.2]);
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-07 10:18:24 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								graph.addBearingRange(i1, j1, gtsamRot2(45*degrees), sqrt(4+4), noiseModel);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								graph.addBearingRange(i2, j1, gtsamRot2(90*degrees), 2, noiseModel);
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								graph.addBearingRange(i3, j2, gtsamRot2(90*degrees), 2, noiseModel);
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								% print
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								graph.print(sprintf('\nFull graph:\n'));
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%% Initialize to noisy points
							 | 
						
					
						
							
								
									
										
										
										
											2012-01-05 00:18:38 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								initialEstimate = planarSLAMValues;
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								initialEstimate.insertPose(i1, gtsamPose2(0.5, 0.0, 0.2));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								initialEstimate.insertPose(i2, gtsamPose2(2.3, 0.1,-0.2));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								initialEstimate.insertPose(i3, gtsamPose2(4.1, 0.1, 0.1));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								initialEstimate.insertPoint(j1, gtsamPoint2(1.8, 2.1));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								initialEstimate.insertPoint(j2, gtsamPoint2(4.1, 1.8));
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								initialEstimate.print(sprintf('\nInitial estimate:\n'));
							 | 
						
					
						
							
								
									
										
										
										
											2011-10-22 00:56:50 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%% Optimize using Levenberg-Marquardt optimization with an ordering from colamd
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-24 10:48:12 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								result = graph.optimize(initialEstimate,1);
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								result.print(sprintf('\nFinal result:\n'));
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								%% Plot Covariance Ellipses
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-12 12:38:05 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								cla;hold on
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								marginals = graph.marginals(result);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								for i=1:3
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    key = symbol('x',i);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    pose{i} = result.pose(key);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    P{i}=marginals.marginalCovariance(key);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    if i>1
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								        plot([pose{i-1}.x;pose{i}.x],[pose{i-1}.y;pose{i}.y],'r-');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								for i=1:3
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    plotPose2(pose{i},'g',P{i})
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								end
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								point = {};
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								for j=1:2
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    key = symbol('l',j);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    point{j} = result.point(key);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    Q{j}=marginals.marginalCovariance(key);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								    plotPoint2(point{j},'b',Q{j})
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								end
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								plot([pose{1}.x;point{1}.x],[pose{1}.y;point{1}.y],'c-');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								plot([pose{2}.x;point{1}.x],[pose{2}.y;point{1}.y],'c-');
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								plot([pose{3}.x;point{2}.x],[pose{3}.y;point{2}.y],'c-');
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-13 20:22:31 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								axis([-0.6 4.8 -1 1])
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								axis equal
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-13 20:22:31 +08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								view(2)
							 | 
						
					
						
							
								
									
										
										
										
											2012-06-05 21:29:26 +08:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 |