SLAM Mapping Operations
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.
cp ~/repos/common_platform/launch/cartographer_simple.launch.py ~/ros2_ws/src/cartographer_ros/cartographer_ros/launch/
cd ~/ros2_ws
colcon build --packages-select cartographer_ros --symlink-install
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.
cd ~/repos/common_platform/common_platform_ws
source install/setup.bash
ros2 launch common_platform pub_robot_state.launch.py use_sim_time:=false
cd ~/ros2_ws
source install/setup.bash
ros2 launch cartographer_ros cartographer_simple.launch.py
ros2 run cartographer_ros cartographer_occupancy_grid_node --ros-args -p resolution:=0.05 -p publish_period_sec:=1.0 -r __ns:=${ROS_NAMESPACE}
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.
ros2 topic list
ros2 topic echo /map
ros2 run tf2_tools view_frames
ROS_NAME=${ROS_NAME} ros2 launch qos_relay qos_relay.launch.py
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.
ros2 launch common_platform cartographer_2d.launch.py resolution:=0.05 publish_period_sec:=1.0
ros2 launch common_platform cartographer_2d.launch.py
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.
ros2 topic echo /${ROS_NAME}/constraint_list
ros2 topic hz /${ROS_NAME}/constraint_list
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.
ros2 service call ${ROS_NAMESPACE}/finish_trajectory cartographer_ros_msgs/srv/FinishTrajectory "{trajectory_id: 0}"
ros2 service call ${ROS_NAMESPACE}/write_state cartographer_ros_msgs/srv/WriteState "{filename: '/home/rcr/map.pbstream'}"
ros2 run cartographer_ros cartographer_pbstream_to_ros_map -i /home/rcr/map.pbstream -o /home/rcr/map -r 0.05
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.
ros2 launch common_platform view_robot.launch.py