Skip to content

Create a 2D map

By the end of this tutorial, you will know how to create a 2D map of the environment (AKA an OccupancyGrid) using your robot.

Start the SLAM

By default, PAL robots start in localization mode. To start mapping, you first need to stop the localization module:

pal module stop localization

Then start the slam module:

pal module start slam

Attention

Both the localization and the slam modules publish a transform between the frames map and base_footprint. For this reason, you should not start both modules at the same time. When starting the slam module make sure you first shut down the localization module and vice‑versa.

If you are using the simulated robot, refer to the simulator documentation to learn how to start SLAM.

Create a Map

To visualize the map being created in real time, first establish communication with the robot.

Then, in the same terminal connected to the robot, start RViz:

rviz2 -d /opt/pal/alum/share/omni_base_2dnav/config/rviz/navigation.rviz

Mapping in RViz

You can now use any teleoperation method to move the robot around the environment and build the map. Alternatively, you can also move the robot autonomously using goal navigation. As the robot explores new areas, the map will update in real time in RViz.

Save the Map

Once you are satisfied with the map, you can save it using the map_server CLI:

ros2 run nav2_map_server map_saver_cli -t /map -f /home/pal/my_map --free 0.196 --occ 0.65

Alternatively, you can save the map using the slam_toolbox RViz plugin.

SLAM toolbox panel

SLAM toolbox RViz

You can also call the /slam_toolbox/save_map service:

ros2 service call /slam_toolbox/save_map slam_toolbox/srv/SaveMap "name: data: '/home/pal/my_map'"

Attention

Make sure the path you specify (for example /home/pal/) exists and is accessible.

Regardless of the method you choose, two files will be created in the specified directory (for example /home/pal/) with the name you selected (for example my_map). The my_map.pgm file contains the map image, and the my_map.yaml file contains the map metadata used to load the map in ROS 2 and convert it into an OccupancyGrid message.

Example of my_map.yaml file:

image: my_map.pgm
mode: trinary
resolution: 0.05
origin: [-13.9, -8.63, 0]
negate: 0
occupied_thresh: 0.65
free_thresh: 0.196