Generating a map

In this tutorial we will be mapping an area by driving the TurtleBot 4 around and using SLAM. Start by making sure that the area you will be mapping is clear of unwanted obstacles. Ideally, you don't want people or animals moving around the area while creating the map.

Launch SLAM

Note

Remember to always source your ROS 2 environment before trying to launch a node.

  • First, install turtlebot4_navigation:

    sudo apt install ros-galactic-turtlebot4-navigation
    

    Then run SLAM. It is recommended to run synchronous SLAM on a remote PC to get a higher resolution map.

    ros2 launch turtlebot4_navigation slam_sync.launch.py
    

    Asynchronous SLAM can be used as well.

    ros2 launch turtlebot4_navigation slam_async.launch.py
    
  • First, install turtlebot4_navigation:

    sudo apt install ros-humble-turtlebot4-navigation
    

    Then run SLAM. It is recommended to run synchronous SLAM on a remote PC to get a higher resolution map.

    ros2 launch turtlebot4_navigation slam.launch.py
    

    Asynchronous SLAM can be used as well.

    ros2 launch turtlebot4_navigation slam.launch.py sync:=false
    

    To change map resolution or other SLAM parameters, copy the default config and modify it. Then, pass the parameters in as a launch argument.

    ros2 launch turtlebot4_navigation slam.launch.py params:=/full/path/to/slam.yaml
    

Launch Rviz2

To visualise the map, launch Rviz2 with the view_robot launch file.

ros2 launch turtlebot4_viz view_robot.launch.py
RVIZ SLAM
Rviz2 showing a map generate by SLAM

Drive the TurtleBot 4

Use any method to drive the robot around the area you wish to map. Check out the driving tutorial if you are unsure of how to drive the robot.

Keep watch of RVIZ as you drive the robot around the area to make sure that the map gets filled out properly.

mapping
Generating a map by driving the TurtleBot 4

Save the map

Once you are happy with your map, you can save it with the following command:

ros2 service call /slam_toolbox/save_map slam_toolbox/srv/SaveMap "name:
  data: 'map_name'"
Note

If you are using namespacing, you will need to call the map saver tool directly: ros2 run nav2_map_server map_saver_cli -f "map_name" --ros-args -p map_subscribe_transient_local:=true -r __ns:=/namespace

This will save the map to your current directory.

View the map

Once the map is saved it will generate a map_name.pgm file which can be viewed in an image editor. A map_name.yaml file is also created. You can edit this file to adjust the map parameters.

map
Generated map image