| 
									
										
										
										
											2012-06-23 03:36:49 +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 | 
					
						
							|  |  |  | % | 
					
						
							|  |  |  | % @brief Simple robotics example using the pre-built planar SLAM domain | 
					
						
							|  |  |  | % @author Alex Cunningham | 
					
						
							|  |  |  | % @author Frank Dellaert | 
					
						
							|  |  |  | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-06 03:31:27 +08:00
										 |  |  | import gtsam.* | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | %% 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 | 
					
						
							|  |  |  | i1 = symbol('x',1); i2 = symbol('x',2); i3 = symbol('x',3); | 
					
						
							|  |  |  | j1 = symbol('l',1); j2 = symbol('l',2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | %% Create graph container and add factors to it | 
					
						
							| 
									
										
										
										
											2012-07-24 23:48:39 +08:00
										 |  |  | graph = NonlinearFactorGraph; | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | %% Add prior | 
					
						
							| 
									
										
										
										
											2012-07-18 23:47:06 +08:00
										 |  |  | priorMean = Pose2(0.0, 0.0, 0.0); % prior at origin | 
					
						
							| 
									
										
										
										
											2021-12-14 00:18:52 +08:00
										 |  |  | priorNoise = noiseModel.Diagonal.Sigmas([0.3; 0.3; 0.1]); | 
					
						
							| 
									
										
										
										
											2012-07-24 23:48:39 +08:00
										 |  |  | graph.add(PriorFactorPose2(i1, priorMean, priorNoise)); % add directly to graph | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | %% Add odometry | 
					
						
							| 
									
										
										
										
											2012-07-18 23:47:06 +08:00
										 |  |  | odometry = Pose2(2.0, 0.0, 0.0); | 
					
						
							| 
									
										
										
										
											2021-12-14 00:18:52 +08:00
										 |  |  | odometryNoise = noiseModel.Diagonal.Sigmas([0.2; 0.2; 0.1]); | 
					
						
							| 
									
										
										
										
											2012-07-24 23:48:39 +08:00
										 |  |  | graph.add(BetweenFactorPose2(i1, i2, odometry, odometryNoise)); | 
					
						
							|  |  |  | graph.add(BetweenFactorPose2(i2, i3, odometry, odometryNoise)); | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | %% Add bearing/range measurement factors | 
					
						
							|  |  |  | degrees = pi/180; | 
					
						
							| 
									
										
										
										
											2021-12-14 00:18:52 +08:00
										 |  |  | brNoise = noiseModel.Diagonal.Sigmas([0.1; 0.2]); | 
					
						
							| 
									
										
										
										
											2012-07-24 23:48:39 +08:00
										 |  |  | graph.add(BearingRangeFactor2D(i1, j1, Rot2(45*degrees), sqrt(4+4), brNoise)); | 
					
						
							|  |  |  | graph.add(BearingRangeFactor2D(i2, j1, Rot2(90*degrees), 2, brNoise)); | 
					
						
							|  |  |  | graph.add(BearingRangeFactor2D(i3, j2, Rot2(90*degrees), 2, brNoise)); | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | %% Initialize to noisy points | 
					
						
							| 
									
										
										
										
											2012-07-24 23:48:39 +08:00
										 |  |  | initialEstimate = Values; | 
					
						
							|  |  |  | initialEstimate.insert(i1, Pose2(0.5, 0.0, 0.2)); | 
					
						
							|  |  |  | initialEstimate.insert(i2, Pose2(2.3, 0.1,-0.2)); | 
					
						
							|  |  |  | initialEstimate.insert(i3, Pose2(4.1, 0.1, 0.1)); | 
					
						
							|  |  |  | initialEstimate.insert(j1, Point2(1.8, 2.1)); | 
					
						
							|  |  |  | initialEstimate.insert(j2, Point2(4.1, 1.8)); | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | %% Optimize using Levenberg-Marquardt optimization with an ordering from colamd | 
					
						
							| 
									
										
										
										
											2012-07-24 23:48:39 +08:00
										 |  |  | optimizer = LevenbergMarquardtOptimizer(graph, initialEstimate); | 
					
						
							|  |  |  | result = optimizer.optimizeSafely(); | 
					
						
							|  |  |  | marginals = Marginals(graph, result); | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | %% Check first pose and point equality | 
					
						
							| 
									
										
										
										
											2014-11-14 06:59:51 +08:00
										 |  |  | pose_1 = result.atPose2(symbol('x',1)); | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | marginals.marginalCovariance(symbol('x',1)); | 
					
						
							| 
									
										
										
										
											2012-07-18 23:47:06 +08:00
										 |  |  | CHECK('pose_1.equals(Pose2,1e-4)',pose_1.equals(Pose2,1e-4)); | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 06:59:51 +08:00
										 |  |  | point_1 = result.atPoint2(symbol('l',1)); | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | marginals.marginalCovariance(symbol('l',1)); | 
					
						
							| 
									
										
										
										
											2020-08-18 02:37:12 +08:00
										 |  |  | CHECK('point_1.equals(Point2(2,2),1e-4)',norm(point_1 - Point2(2,2)) < 1e-4); |