Welcome to ROSMASTER M3 PRO repository
6.Gmapping-SLAM mapping
Gmapping-SLAM mapping
Gmapping-SLAM mapping1. Course Content2. Introduction to gmapping2.1 Introduction2.2 Related Materials3. Preparation3.1 Content Description3.2 Start the Agent4. Run the case4.1 Mapping Process4.2 Save the map5. Node parsing5.1 Displaying the Node Computation Graph5.2 TF Transformation5.3 gmapping node details5.4 Configuration Files
1. Course Content
Learn the robot gmapping mapping algorithm for SLAM mapping function
After running the program, control the robot through the keyboard or handle to perform SLAM mapping and save it as a raster map
2. Introduction to gmapping
2.1 Introduction
- gmapping only works when the number of 2D laser points in a single frame is less than 1440. If the number of laser points in a single frame is greater than 1440, the error [[mapping-4] process has died] will occur.
- Gmapping is a commonly used open source SLAM algorithm based on the filtering SLAM framework.
- Gmapping is based on the RBPF particle filter algorithm, which separates the real-time positioning and mapping processes, performing positioning first and then mapping.
- Gmapping makes two major improvements to the RBpf algorithm: improving the proposal distribution and selective resampling.
Advantages: Gmapping can build indoor maps in real time, and the computation required to build small scene maps is small and the accuracy is high.
Disadvantages: As the scene grows, the number of particles required increases, because each particle carries a map, so the memory and
The amount of calculation will increase. Therefore, it is not suitable for building large scene maps. And there is no loop detection, so it may cause the map to be
Misalignment, although increasing the number of particles can make the map closed, it comes at the cost of increasing computation and memory.

2.2 Related Materials
3. Preparation
3.1 Content Description
This lesson uses the Jetson Orin NX as an example. For Raspberry Pi and Jetson Nano boards, you need to open a terminal and enter the command to enter the Docker container. Once inside the Docker container, enter the commands mentioned in this lesson in the terminal. For instructions on entering the Docker container, refer to the product tutorial [Configuration and Operation Guide]--[Enter the Docker (Jetson Nano and Raspberry Pi 5 users, see here)]. For Orin and NX boards, simply open a terminal and enter the commands mentioned in this lesson.
3.2 Start the Agent
Note: To test all cases, you must start the docker agent first. If it has already been started, you do not need to start it again.
Enter the command in the vehicle terminal:
sh start_agent.sh
The terminal prints the following information, indicating that the connection is successful

4. Run the case
4.1 Mapping Process
Notice:
When building a map, the slower the speed, the better the effect (mainly the slower the rotation speed). If the speed is too fast, the effect will be very poor.
Jetson Nano and Raspberry Pi series controllers need to enter the Docker container first (please refer to the [Docker course chapter - Entering the robot's Docker container] for steps).
The vehicle terminal starts the mapping command:
xxxxxxxxxxros2 launch slam_mapping gmapping.launch.py
The rviz visualization function can be started on the vehicle side or the virtual machine side. You can choose either method to start it. It is forbidden to start it on the virtual machine side and the vehicle side repeatedly:
Taking the configuration of a virtual machine as an example, open a terminal and start the rviz visualization interface:
xxxxxxxxxxros2 launch slam_view slam_view.launch.py
Start the rviz visualization interface command on the vehicle:
xxxxxxxxxxros2 launch slam_mapping slam_view.launch.py

Open another terminal in the virtual machine and start the keyboard control node (you can also use the controller remote control, you need to start the controller control node in advance, refer to [5. Chassis Control - 2. Controller Control]):
xxxxxxxxxxros2 run yahboomcar_ctrl yahboom_keyboard
Click the window in the terminal and press z to reduce the speed appropriately.

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

4.2 Save the map
Open a new terminal on the car and save the map
xxxxxxxxxxros2 launch slam_mapping save_map.launch.py
If the terminal prompts "Map saved successfully ", it means that the map is saved successfully. If the map fails to be saved, try saving again.

The map save path is as follows:
jetson orin nano, jetson orin NX:
xxxxxxxxxx/home/jetson/M3Pro_ws/install/M3Pro_navigation/share/M3Pro_navigation/map
Jetson Orin Nano, Raspberry Pi:
You need to enter docker first
xxxxxxxxxx/root/M3Pro_ws/install/M3Pro_navigation/share/M3Pro_navigation/map/
A pgm image, a yaml file yahboom_map.yaml
xxxxxxxxxximageyahboom_map.pgmmodetrinaryresolution0.05origin-10 -10 0negate0occupied_thresh0.65free_thresh0.25Parameter analysis:
- image: The path of the map file, which can be an absolute path or a relative path
- mode: This attribute can be one of trinary, scale or raw, depending on the selected mode. Trinary mode is the default mode.
- resolution: map resolution, meters/pixels
- origin: The 2D position (x, y, yaw) of the lower-left corner of the map, where yaw is a counterclockwise rotation (yaw=0 means no rotation). Currently, many parts of the system ignore the yaw value.
- negate: whether to invert the meaning of white/black, 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 completely free.
5. Node parsing
5.1 Displaying the Node Computation Graph
The virtual machine terminal runs:
xxxxxxxxxxros2 run rqt_graph rqt_graph

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

5.3 gmapping node details
xxxxxxxxxxros2 node info /slam_gmapping
Enter the above command in the terminal to view the subscription and publishing topics related to the gmapping node.
x/slam_gmappingSubscribers:/parameter_events: rcl_interfaces/msg/ParameterEvent/scan: sensor_msgs/msg/LaserScanPublishers:/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/TFMessageService 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/SetParametersAtomicallyService Clients:Action Servers:Action Clients:
5.4 Configuration Files
Configuration file path:
jetson orin nano, jetson orin NX:
xxxxxxxxxx/home/jetson/M3Pro_ws/src/M3Pro_core/slam_gmapping/params/slam_gmapping.yaml
Jetson Orin Nano, Raspberry Pi:
You need to enter docker first
xxxxxxxxxxroot/M3Pro_ws/src/M3Pro_core/slam_gmapping/params/slam_gmapping.yaml
Default configuration parameters:
xxxxxxxxxxslam_gmapping ros__parameters # Angular update threshold (radians): update the map when the robot rotates beyond this angle angularUpdate0.25 # Angle sampling step (radians): step size for angular search in scan matching astep0.05 # Robot base frame name base_framebase_footprint # Map frame name map_framemap # Odometry frame name odom_frameodom # Termination condition for scan matching iteration: stop when parameter change is smaller than this value delta0.05 # Maximum number of iterations for scan matching iterations5 # Kernel size used in scan matching, affects the smoothness of matching kernelSize1 # Large-scale scan matching sampling range (meters) lasamplerange0.005 # Large-scale scan matching sampling step (meters) lasamplestep0.005 # Linear update threshold (meters): update the map when the robot moves beyond this distance linearUpdate0.5 # Small-scale scan matching sampling range (meters) llsamplerange0.01 # Small-scale scan matching sampling step (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): step size for linear search in scan matching lstep0.05 # Map update interval (seconds) map_update_interval3.0 # Maximum laser range (meters): points beyond this distance will be ignored maxRange6.0 # Maximum usable laser range (meters): maximum distance used for mapping maxUrange4.0 # Minimum score threshold for scan matching: matches below this value will be discarded minimum_score0.0 # Occupancy probability threshold: cells above this value are considered occupied occ_thresh0.25 # Gain factor: affects the speed of map updates ogain3.0 # Number of particles in 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 # Resample threshold: resampling is triggered when the effective particle count drops below this ratio resampleThreshold0.5 # Angular noise covariance for scan matching sigma0.05 # Odometry model parameter: linear error ratio caused by linear motion srr0.1 # Odometry model parameter: angular error ratio caused by linear motion srt0.2 # Odometry model parameter: linear error ratio caused by angular motion str0.1 # Odometry model parameter: angular error ratio caused by angular motion stt0.2 # Temporal update interval (seconds): update map even when the robot is stationary temporalUpdate1.0 # Transform publish period (seconds) transform_publish_period0.05 # Use simulation time use_sim_timefalse # Map maximum range on x-axis (meters) xmax100.0 # Map minimum range on x-axis (meters) xmin-100.0 # Map maximum range on y-axis (meters)