Welcome to ROSMASTER M3 repository
07.Cartographer-SLAM mapping
Cartographer-SLAM Mapping
Cartographer-SLAM Mapping1. Course Content2. Cartographer Introduction3. Start the Agent4. Run the Example4.1 Start the Mapping Node4.2 Save the Map4.3 Save Pose Map in pbstream Format5. Node Analysis5.1 View Node Communication Graph5.2 TF Transformation
1. Course Content
- Run the sample Cartographer SLAM algorithm to perform SLAM mapping and save the grid map and pose map.
2. Cartographer Introduction
Cartographer is a 2D and 3D SLAM library open-sourced by Google, supported by the ROS system. It is a graph-based SLAM algorithm (with multi-threaded backend optimization and problem optimization built with Ceres). It can combine data from multiple sensors (e.g., LIDAR, IMU, and cameras) to simultaneously compute the sensor's position and map the surrounding environment.
The source code of Cartographer mainly consists of three parts: cartographer, cartographer_ros, and ceres-solver (for backend optimization).

Cartographer uses a mainstream SLAM framework, which is a three-stage process of feature extraction, loop closure detection, and backend optimization. A certain number of LaserScans form a submap, and a series of submaps constitute the global map. The cumulative error is small when building a submap from LaserScans over a short period, but a large cumulative error can occur when building a global map from submaps over a long period. Therefore, loop closure detection is needed to correct the positions of these submaps. The basic unit for loop closure detection is the submap, and the scan_match strategy is used for this. The key aspects of Cartographer are the creation of submaps by fusing multi-sensor data (odometry, IMU, LaserScan, etc.) and the implementation of the scan_match strategy for loop closure detection.
cartographer_ros
cartographer_ros runs under ROS and can receive various sensor data as ROS messages. After processing, it publishes the results as messages, which is convenient for debugging and visualization.
3. Start the Agent
Note: If it is already running, you do not need to start it again.
Enter the command in the vehicle's terminal:
start_agent
The terminal will print the following information, indicating a successful connection:

4. Run the Example
4.1 Start the Mapping Node
For Raspberry Pi 5 users, if the ROS container is not started, you need to start it first,
xxxxxxxxxxbringup_m3
Open a terminal inside the container,
xxxxxxxxxxexec_m3
(ORIN board)
xxxxxxxxxxros2 launch m3_bringup cartographer.launch.py(RDK & PI5 boards) Run in separate steps, with visualization launched on the accompanying virtual machine Rosmaster-M3-Ubuntu22.04,
xxxxxxxxxx# On the virtual machinerviz2 -d /home/yahboom/yahboomM3_ws/user_ws/src/m3_bringup/rviz/slam_cartographer_rviz.rviz# On the host machineros2 launch m3_bringup cartographer.launch.py gui:=False
- To control the vehicle's movement, you can use either keyboard or joystick control. Here, we use keyboard control as an example:
xxxxxxxxxxkeyboard_control- Click on the terminal window with the mouse and press
zto reduce the speed appropriately. PressI,<,J,Lto control the car to move forward, backward, turn left, and turn right, respectively, to complete the mapping slowly.

4.2 Save the Map
Open a new terminal on the vehicle and save the map.
xxxxxxxxxxros2 launch m3_bringup save_map.launch.py
[!TIP]
Optional Parameters:
map_name: Specifies the name of the saved map, with the default value beingmap.For example, to save a map named
test.yaml, add the parameter:ros2 launch m3_bringup save_map.launch.py map_name:=test
The terminal prompt Map saved successfully indicates that the map was saved successfully.

- The map save path is as follows:
xxxxxxxxxx~/map/map.yaml
- This includes a PGM image and a YAML file,
map.yaml.
xxxxxxxxxximage: map.pgmmode: trinaryresolution: 0.05origin: [-10, -10, 0]negate: 0occupied_thresh: 0.65free_thresh: 0.25
Parameter analysis:
image: The path to the map file, which can be an absolute or relative path.mode: This attribute can betrinary,scale, orraw, depending on the chosen mode.trinaryis the default.resolution: The resolution of the map, in meters/pixel.origin: The 2D pose (x, y, yaw) of the bottom-left corner of the map. Here,yawis a counter-clockwise rotation (yaw=0 means no rotation). Many parts of the system currently ignore the yaw value.negate: Whether to invert the meaning of white/black and free/occupied (the interpretation of thresholds is not affected).occupied_thresh: Pixels with an occupancy probability greater than this threshold are considered fully occupied.free_thresh: Pixels with an occupancy probability less than this threshold are considered fully free.
4.3 Save Pose Map in pbstream Format
The pbstream format map file is used for Cartographer's localization and navigation, which will be explained in subsequent chapters.
- Open a terminal and enter the following command:
xxxxxxxxxxros2 service call /write_state cartographer_ros_msgs/srv/WriteState "{filename: '/home/jetson/map/map.pbstream'}"[!WARNING]
filename: '/home/jetson/yahboom_map.pbstream' is the path to save the file. When saving, replace it with the corresponding path in the~/map/directory on your board.
5. Node Analysis
5.1 View Node Communication Graph
- Run the following command in the terminal:
xxxxxxxxxxros2 run rqt_graph rqt_graph
5.2 TF Transformation
- Run the command in the terminal:
xxxxxxxxxxros2 run rqt_tf_tree rqt_tf_tree
