| 
									
										
										
										
											2010-10-14 12:54:38 +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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file PlanarSLAMExample.cpp | 
					
						
							| 
									
										
										
										
											2011-08-19 21:11:04 +08:00
										 |  |  |  * @brief Simple robotics example using the pre-built planar SLAM domain | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  |  * @author Alex Cunningham | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // pull in the planar SLAM domain with all typedefs and helper functions defined
 | 
					
						
							|  |  |  | #include <gtsam/slam/planarSLAM.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-03 03:05:38 +08:00
										 |  |  | // we will use Symbol keys
 | 
					
						
							|  |  |  | #include <gtsam/nonlinear/Symbol.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | using namespace gtsam::noiseModel; | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  |  * Example of a simple 2D planar slam example with landmarls | 
					
						
							|  |  |  |  *  - The robot and landmarks are on a 2 meter grid | 
					
						
							|  |  |  |  *  - Robot poses are facing along the X axis (horizontal, to the right in 2D) | 
					
						
							|  |  |  |  *  - The robot moves 2 meters each step | 
					
						
							|  |  |  |  *  - We have full odometry between poses | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  |  *  - We have bearing and range information for measurements | 
					
						
							|  |  |  |  *  - Landmarks are 2 meters away from the robot trajectory | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | int main(int argc, char** argv) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  |   // create the graph (defined in planarSlam.h, derived from NonlinearFactorGraph)
 | 
					
						
							|  |  |  | 	planarSLAM::Graph graph; | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-03 00:18:40 +08:00
										 |  |  | 	// Create some keys
 | 
					
						
							|  |  |  | 	static Symbol i1('x',1), i2('x',2), i3('x',3); | 
					
						
							|  |  |  | 	static Symbol j1('l',1), j2('l',2); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  | 	// add a Gaussian prior on pose x_1
 | 
					
						
							|  |  |  | 	Pose2 priorMean(0.0, 0.0, 0.0); // prior mean is at origin
 | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | 	SharedDiagonal priorNoise = Diagonal::Sigmas(Vector_(3, 0.3, 0.3, 0.1)); // 30cm std on x,y, 0.1 rad on theta
 | 
					
						
							| 
									
										
										
										
											2012-06-24 10:48:12 +08:00
										 |  |  | 	graph.addPosePrior(i1, priorMean, priorNoise); // add directly to graph
 | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  | 	// add two odometry factors
 | 
					
						
							|  |  |  | 	Pose2 odometry(2.0, 0.0, 0.0); // create a measurement for both factors (the same in this case)
 | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | 	SharedDiagonal odometryNoise  = Diagonal::Sigmas(Vector_(3, 0.2, 0.2, 0.1)); // 20cm std on x,y, 0.1 rad on theta
 | 
					
						
							| 
									
										
										
										
											2012-06-24 10:48:12 +08:00
										 |  |  | 	graph.addRelativePose(i1, i2, odometry, odometryNoise); | 
					
						
							|  |  |  | 	graph.addRelativePose(i2, i3, odometry, odometryNoise); | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  | 	// create a noise model for the landmark measurements
 | 
					
						
							| 
									
										
										
										
											2012-06-23 03:36:49 +08:00
										 |  |  | 	SharedDiagonal measurementNoise = Diagonal::Sigmas(Vector_(2, 0.1, 0.2)); // 0.1 rad std on bearing, 20cm on range
 | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// create the measurement values - indices are (pose id, landmark id)
 | 
					
						
							|  |  |  | 	Rot2 bearing11 = Rot2::fromDegrees(45), | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  | 		   bearing21 = Rot2::fromDegrees(90), | 
					
						
							|  |  |  | 		   bearing32 = Rot2::fromDegrees(90); | 
					
						
							| 
									
										
										
										
											2012-05-24 02:51:49 +08:00
										 |  |  | 	double range11 = std::sqrt(4.0+4.0), | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  | 		     range21 = 2.0, | 
					
						
							|  |  |  | 		     range32 = 2.0; | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  | 	// add bearing/range factors (created by "addBearingRange")
 | 
					
						
							| 
									
										
										
										
											2012-06-03 00:18:40 +08:00
										 |  |  | 	graph.addBearingRange(i1, j1, bearing11, range11, measurementNoise); | 
					
						
							|  |  |  | 	graph.addBearingRange(i2, j1, bearing21, range21, measurementNoise); | 
					
						
							|  |  |  | 	graph.addBearingRange(i3, j2, bearing32, range32, measurementNoise); | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  | 	// print
 | 
					
						
							|  |  |  | 	graph.print("Factor graph"); | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  | 	// create (deliberatly inaccurate) initial estimate
 | 
					
						
							| 
									
										
										
										
											2012-02-06 06:34:35 +08:00
										 |  |  | 	planarSLAM::Values initialEstimate; | 
					
						
							| 
									
										
										
										
											2012-06-03 00:18:40 +08:00
										 |  |  | 	initialEstimate.insertPose(i1, Pose2(0.5, 0.0, 0.2)); | 
					
						
							|  |  |  | 	initialEstimate.insertPose(i2, Pose2(2.3, 0.1,-0.2)); | 
					
						
							|  |  |  | 	initialEstimate.insertPose(i3, Pose2(4.1, 0.1, 0.1)); | 
					
						
							|  |  |  | 	initialEstimate.insertPoint(j1, Point2(1.8, 2.1)); | 
					
						
							|  |  |  | 	initialEstimate.insertPoint(j2, Point2(4.1, 1.8)); | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  | 	initialEstimate.print("Initial estimate:\n  "); | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-14 09:08:22 +08:00
										 |  |  | 	// optimize using Levenberg-Marquardt optimization with an ordering from colamd
 | 
					
						
							| 
									
										
										
										
											2012-05-21 04:31:33 +08:00
										 |  |  | 	planarSLAM::Values result = graph.optimize(initialEstimate); | 
					
						
							|  |  |  | 	result.print("Final result:\n  "); | 
					
						
							| 
									
										
										
										
											2010-08-27 05:21:15 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 |