← Back to Docs

SLAM Mapping Operations

Prepare the Cartographer launch file, start the robot state publisher, run Cartographer SLAM and occupancy grid mapping, verify topics, tune parameters, monitor constraints and save the resulting map.
1

Prepare Cartographer launch file

Copy the cartographer_simple.launch.py file into the cartographer_ros package and rebuild.

The common_platform repository provides a customized Cartographer launch file. Copy it into the cartographer_ros package and rebuild Cartographer.

bash
cp ~/repos/common_platform/launch/cartographer_simple.launch.py ~/ros2_ws/src/cartographer_ros/cartographer_ros/launch/
bash
cd ~/ros2_ws
colcon build --packages-select cartographer_ros --symlink-install
2

Launch robot state publisher and Cartographer

Start the robot state publisher, Cartographer SLAM and occupancy grid nodes.

Run these commands in separate terminals to start the robot state publisher, launch Cartographer for SLAM, and publish an occupancy grid map.

bash
cd ~/repos/common_platform/common_platform_ws
source install/setup.bash
ros2 launch common_platform pub_robot_state.launch.py use_sim_time:=false
bash
cd ~/ros2_ws
source install/setup.bash
ros2 launch cartographer_ros cartographer_simple.launch.py
bash
ros2 run cartographer_ros cartographer_occupancy_grid_node --ros-args -p resolution:=0.05 -p publish_period_sec:=1.0 -r __ns:=${ROS_NAMESPACE}
3

Verify SLAM topics

Check that Cartographer publishes the expected topics and visualize the TF tree.

After launching SLAM, verify that the relevant topics are active and inspect the transform tree. It’s common to run a QoS relay for the odometry topic to ensure reliability.

bash
ros2 topic list
bash
ros2 topic echo /map
bash
ros2 run tf2_tools view_frames
bash
ROS_NAME=${ROS_NAME} ros2 launch qos_relay qos_relay.launch.py
4

Tune Cartographer parameters

Adjust SLAM parameters such as resolution and update rate to improve mapping quality.

The default Cartographer settings may not yield an optimal map for all environments. You can tune the map resolution and publish period using parameters when launching the 2D SLAM configuration. A higher resolution produces finer details but increases computational load.

bash
ros2 launch common_platform cartographer_2d.launch.py resolution:=0.05 publish_period_sec:=1.0
bash
ros2 launch common_platform cartographer_2d.launch.py
5

Monitor SLAM constraint topics

Inspect constraint list and trajectory topics to diagnose SLAM performance.

Cartographer publishes a list of loop closure constraints and a trajectory builder status. If SLAM quality degrades, inspect these topics to understand the number of constraints and their quality.

bash
ros2 topic echo /${ROS_NAME}/constraint_list
bash
ros2 topic hz /${ROS_NAME}/constraint_list
6

Save and convert the SLAM map

Finish the current Cartographer trajectory, write the state to disk and convert to a ROS map.

When SLAM mapping is complete, use these commands to stop trajectory accumulation, persist the pose graph and convert the Cartographer state file (.pbstream) into an occupancy grid map file usable by Nav2.

bash
ros2 service call ${ROS_NAMESPACE}/finish_trajectory cartographer_ros_msgs/srv/FinishTrajectory "{trajectory_id: 0}"
bash
ros2 service call ${ROS_NAMESPACE}/write_state cartographer_ros_msgs/srv/WriteState "{filename: '/home/rcr/map.pbstream'}"
bash
ros2 run cartographer_ros cartographer_pbstream_to_ros_map -i /home/rcr/map.pbstream -o /home/rcr/map -r 0.05
7

View the robot SLAM visualization

Launch a combined RViz session to inspect the live map and robot state.

The common_platform repository includes a launch file for viewing the robot in RViz together with the SLAM map. Launch it in a new terminal.

bash
ros2 launch common_platform view_robot.launch.py

Subscribe to our newsletter

The latest educational robotics news and articles, sent to your inbox weekly.