|Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.
Building a Map in SimulationDescription: Given a simple office environment and PR2 in simulation, how to create a map using the OpenSLAM GMapping package.
Tutorial Level: INTERMEDIATE
Next Tutorial: Using custom maps for simulation.
For details on building a map from logged data (bag files), see this page. But below will startup the essential components to build a map from simulated sensor data:
For this tutorial, you need to install the 'pr2all' variant of ROS (see installation instructions). On Ubuntu, this means:
sudo apt-get install ros-fuerte-pr2-apps
And you will also need to check out wg_robots_gazebo:
svn co https://code.ros.org/svn/wg-ros-pkg/stacks/wg_robots_gazebo/trunk/pr2_build_map_gazebo_demo/
Add above pr2_build_map_gazebo_demo directory path to your ROS_PACKAGE_PATH environment variable. Now you should be able to compile the simulation plugging in package:
- Start the map building demo. You can look at the referred launch scripts to see which components are loaded:
roslaunch pr2_build_map_gazebo_demo pr2_build_map_gazebo_demo.launchExpect a lot of text status to scroll through the terminal as various components of the map building demo is started.
The robot visualizer tool (rviz) should have been started automatically as well:
- Browse through the panels on the left side of the rviz window,
Under Global Options on the left hand side panel,
check that Fixed Frame is set to /odom_combined
check that Target Frame is set to /odom_combined
To see the map updates as you drive the robot around, under Map on the left hand side panel:
check to see that Topic is set to map
For more details on how to work with rviz, visit The rviz Users Guide. The map you see in rviz is a partial map of the robot environment because the robot's laser scanner only sees the front of the robot. To complete the map you will have to drive the robot around and gather more laser and odometry data.
- To drive the robot around manually in simulation using the keyboard, start a new terminal and run:
roslaunch pr2_teleop teleop_keyboard.launch
For example, hold on to the i key to drive the robot forward. teleop_pr2_keyboard sends simple velocity commands through ROS topic message named cmd_vel to PR2's base controller in the pr2_mechanism_controllers package causing PR2 to drive around. You can see the robot move around in the Gazebo simulation GUI and simultaneously watch map updates in the rviz window as more laser scan data becomes available.
Note: Be sure to keep focus on the terminal where teleop_keyboard.launch is running for commands to take effect.
Saving your new map to disk: Once you have a satisfactory map in rviz, start a new terminal and run
rosrun map_server map_saver -f map
This will save the current state of the map locally as map.pgm.
Congratulations. You can view the generated file with any image viewer (for example, run gthumb or gimp in linux); it should look very much like
when zoomed in. In case you are wondering why the displayed map example is not perfect, it's because of various imperfections in the way things work in the real world. For example, the robot's odometry generates noise due to wheel slip, the odometry algorithm uses least squares approximation algorithm to deal with redundancy (having 8 wheels), and ROS frame transform mechanism interpolates transforms temporally across various ROS node components due to the fact that nodes are not completely synchronized, to name a few.
What's next? You might want to use your new map in the navigation stack, by passing your map to the map_server. As an example, if you're working with a PR2, you would supply your new map at this point in the process of bringing up the navigation stack. Also, here's an example on how to use the map you have just built in simulation with the navigation stack in Gazebo.