Welcome to ROSMASTER M3 repository
06.Gmapping-SLAM mapping
GMapping-SLAM Mapping
GMapping-SLAM Mapping1. Course Content2. GMapping Algorithm Introduction3. Start the Agent4. Run the Example4.1 Mapping Process4.2 Save the Map5. Node Analysis5.1 Display Node Computation Graph5.2 TF Transformation5.3 GMapping Node Details5.4 Configuration File
1. Course Content
Basic: Run the sample program to perform SLAM mapping using the GMapping algorithm and save the grid map.
Advanced: Understand the configuration parameters of the GMapping algorithm.
2. GMapping Algorithm Introduction
- GMapping is only suitable for 2D laser scans with fewer than 1440 points per frame. If the number of points exceeds 1440, an issue like
[mapping-4] process has diedwill occur. - GMapping is a popular open-source SLAM algorithm based on the filter SLAM framework.
- GMapping is based on the RBPF (Rao-Blackwellized Particle Filter) algorithm, where the localization and mapping processes are separated. Localization is performed first, followed by mapping.
- GMapping makes two main improvements to the RBPF algorithm: an improved proposal distribution and selective resampling.
Advantages: GMapping can build indoor maps in real-time. It requires less computational power and offers higher accuracy for small-scale mapping.
Disadvantages: As the scene size increases, more particles are needed. Since each particle carries a map, both memory and computational requirements increase when building large maps. Therefore, it is not suitable for large-scale mapping. It also lacks loop closure detection, which can cause map misalignment when a loop is closed. Although increasing the number of particles can help close the map, it comes at the cost of increased computation and memory.

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 Mapping Process
- Note: When mapping, a slower speed (especially rotational speed) yields better results. If the speed is too high, the mapping quality will be poor.
- Launch the mapping command in the terminal:
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
Enter the command,
(ORIN & PI5 boards)
xxxxxxxxxxros2 launch m3_bringup gmapping.launch.py(RDK board) 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/gmapping_rviz.rviz# On the host machineros2 launch m3_bringup gmapping.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.

Press I, <, J, L to control the car to move forward, backward, turn left, and turn right, respectively, to complete the mapping.

4.2 Save the Map
- Run the following command in the terminal to save the grid 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. If it fails, try saving again.

The default map save path is as follows:
xxxxxxxxxx~/map/map.yaml
This includes a PGM image and a YAML file, yahboom_map.yaml.
xxxxxxxxxximage: yahboom_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.
5. Node Analysis
5.1 Display Node Computation Graph
Run in the virtual machine terminal:
xxxxxxxxxxros2 run rqt_graph rqt_graph

5.2 TF Transformation
Run in the virtual machine terminal:
xxxxxxxxxxros2 run rqt_tf_tree rqt_tf_tree The image size is too large. The original image can be viewed in this lesson's course folder.

5.3 GMapping Node Details
xxxxxxxxxxros2 node info /slam_gmapping
Entering the above command in the terminal allows you to view the topics subscribed to and published by the GMapping node.
x/slam_gmapping Subscribers: /parameter_events: rcl_interfaces/msg/ParameterEvent /scan: sensor_msgs/msg/LaserScan Publishers: /entropy: std_msgs/msg/Float64 /map: nav_msgs/msg/OccupancyGrid /map_metadata: nav_msgs/msg/MapMetaData /parameter_events: rcl_interfaces/msg/ParameterEvent /rosout: rcl_interfaces/msg/Log /tf: tf2_msgs/msg/TFMessage Service Servers: /slam_gmapping/describe_parameters: rcl_interfaces/srv/DescribeParameters /slam_gmapping/get_parameter_types: rcl_interfaces/srv/GetParameterTypes /slam_gmapping/get_parameters: rcl_interfaces/srv/GetParameters /slam_gmapping/list_parameters: rcl_interfaces/srv/ListParameters /slam_gmapping/set_parameters: rcl_interfaces/srv/SetParameters /slam_gmapping/set_parameters_atomically: rcl_interfaces/srv/SetParametersAtomically Service Clients: Action Servers: Action Clients:
5.4 Configuration File
- Configuration file path:
xxxxxxxxxx~/yahboomM3_ws/user_ws/src/m3_bringup/config/slam_gmapping.yaml
- Default configuration parameters:
xxxxxxxxxxslam_gmapping ros__parameters # Angular update threshold (radians): Update the map when the robot rotates more than this angle angularUpdate0.25 # Angular sampling step (radians): Angular search step for scan matching astep0.05 # Robot base coordinate frame name base_framebase_footprint # Map coordinate frame name map_framemap # Odometry coordinate frame name odom_frameodom # Iteration termination condition for scan matching: Stop iterating when parameter change is less than this value delta0.05 # Maximum number of iterations for scan matching iterations5 # Kernel size for scan matching, affects matching smoothness kernelSize1 # Distance sampling range for large-scale scan matching (meters) lasamplerange0.005 # Distance sampling step for large-scale scan matching (meters) lasamplestep0.005 # Linear update threshold (meters): Update the map when the robot moves more than this distance linearUpdate0.5 # Distance sampling range for small-scale scan matching (meters) llsamplerange0.01 # Distance sampling step for small-scale scan matching (meters) llsamplestep0.01 # Measurement noise covariance for laser scan lsigma0.075 # Number of laser beams to skip: 0 means use all laser points lskip0 # Linear sampling step (meters): Linear search step for scan matching lstep0.05 # Map update time interval (seconds) map_update_interval3.0 # Maximum laser range (meters): Points beyond this distance are ignored maxRange6.0 # Maximum usable laser range (meters): Maximum distance used for mapping maxUrange4.0 # Minimum score threshold for scan matching: Matches below this score are discarded minimum_score0.0 # Occupancy probability threshold: Cells with a probability greater than this are considered occupied occ_thresh0.25 # Gain factor: Affects the speed of map updates ogain3.0 # Number of particles for the particle filter particles30 # QoS (Quality of Service) parameter settings qos_overrides /parameter_events publisher depth1000 # Message queue depth durabilityvolatile # Durability policy historykeep_all # History policy reliabilityreliable # Reliability policy /tf publisher depth1000 durabilityvolatile historykeep_last # Keep only the latest message reliabilityreliable # Resampling threshold: Trigger resampling when the number of effective particles is below this ratio resampleThreshold0.5 # Angular noise covariance for scan matching sigma0.05 # Odometry model parameter: Proportional linear error from linear motion srr0.1 # Odometry model parameter: Proportional angular error from linear motion srt0.2 # Odometry model parameter: Proportional linear error from angular motion str0.1 # Odometry model parameter: Proportional angular error from angular motion stt0.2 # Time update interval (seconds): Update the map even if the robot has not moved temporalUpdate1.0 # Coordinate transform publication period (seconds) transform_publish_period0.05 # Whether to use simulation time use_sim_timefalse # Maximum x-range of the map (meters) xmax100.0 # Minimum x-range of the map (meters) xmin-100.0 # Maximum y-range of the map (meters) ymax100.0 # Minimum y-range of the map (meters) ymin-100.0
These are the configurable parameters for GMapping. If you need to modify the configuration parameters, you must recompile the slam_gmapping package in the M3Pro_ws workspace for the changes to take effect:
xxxxxxxxxxcolcon build --packages-select slam_gmapping