Welcome to ROSMASTER M1 repository
5.Gmapping-SLAM mapping
Gmapping-SLAM mapping
This lesson uses the Raspberry Pi as an example.
For Raspberry Pi and Jetson-Nano boards, you need to open a terminal on the host computer 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 from the host computer, refer to [01. Robot Configuration and Operation Guide] -- [4.Enter Docker (For JETSON Nano and RPi 5)].
For RDKX5 and Orin boards, simply open a terminal and enter the commands mentioned in this lesson.
1. Algorithm 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 filter SLAM framework.
- gmapping is based on the RBPF particle filter algorithm, separating the real-time localization and mapping processes, performing localization first and then mapping. Gmapping makes two major improvements to the RBPF algorithm: improved proposal distribution and selective resampling.
Advantages: Gmapping can build indoor maps in real time. It requires less computation and offers high accuracy for small scene maps.
Disadvantages: As the scene size increases, more particles are required. Because each particle carries a map, the memory and computational overhead required for building large maps increases. Therefore, it is not suitable for building large scene maps. Furthermore, it lacks loop detection, which can cause map misalignment when loops are closed. While increasing the number of particles can close the map, it comes at the expense of increased computation and memory.

2. Program Functionality
After running the program, the map building interface will appear in rviz. Use the keyboard or gamepad to control the robot until the map is built. Then, run the save map command to save the map.
3. Pre-use Configuration
This car is equipped with a USB camera, a depth camera, and two different LiDAR models. However, since the product cannot be automatically identified, you need to manually set the machine type and LiDAR model.
For Raspberry Pi and Jetson-Nano boards, you need to enter the Docker container first. For RDKX5 and Orin controllers, this is not necessary.
Change the following settings based on the car model, radar type, and camera type.
root@ubuntu:/# cdroot@ubuntu:~# vim .bashrc
Find this location and press i on your keyboard to change the camera and LiDAR models to the corresponding ones. The default values are Tmini and Nuwa.

After making the changes, save and exit vim, then execute:
xxxxxxxxxxroot@raspberrypi:~# source .bashrc--------------------------------------------------------ROS_DOMAIN_ID: 61 | ROS: humblemy_robot_type: M1 | my_lidar: tmini | my_camera: nuwa--------------------------------------------------------root@raspberrypi:~#
4. Program Startup
4.1. Startup Commands
For Raspberry Pi and Jetson-Nano boards, you need to enter the Docker container first. For RDKX5 and Orin controllers, this is not necessary.
Enter the Docker container (see [Docker course] --- [4. Docker Startup Script] for steps).
All the following commands must be executed within the same Docker container (see [Docker course] --- [3. Docker Submission and Multi-Terminal Access] for steps).
- Start Mapping
xxxxxxxxxxros2 launch yahboomcar_nav map_gmapping_launch.py

Enter the command to start rviz visualization of the map.
xxxxxxxxxxros2 launch yahboomcar_nav display_map_launch.py

The program has controller control enabled by default. If you're using a controller, you can now connect it directly to the receiver for control. If you prefer keyboard control, enter:
xxxxxxxxxx#keyboardros2 run yahboomcar_ctrl yahboom_keyboard
Then control the car to slowly navigate the area you want to map. After mapping is complete, enter the following command to save the map. Enter:
xxxxxxxxxxros2 launch yahboomcar_nav save_map_launch.py

A map named yahboomcar will be saved. This map is saved in:
xxxxxxxxxx~/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_nav/maps/yahboomcar.yaml
Two files will be generated, one named yahboomcar.pgm. The map is as follows. (For RDK X5 and Orin , you can directly double-click to view the map. For Raspberry Pi and Jetson nano, you need to copy this file to the /root/share shared folder on Docker and then view it on the host machine.)

yahboomcar.yaml, take a look at the contents of the yaml.
xxxxxxxxxximageyahboomcar.pgmmodetrinaryresolution0.05origin-10 -21.2 0negate0occupied_thresh0.65free_thresh0.25- image: The image representing the map, i.e., yahboomcar.pgm
- mode: This property can be one of trinary, scale, or raw, depending on the selected mode. Trinary is the default mode.
- resolution: The resolution of the map, in meters/pixels
- origin: The 2D coordinate system at the bottom left corner of the map. Pose (x, y, yaw), where yaw is 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 and free/occupied (this does not affect the interpretation of thresholds).
- occupied_thresh: Pixels with an occupied probability greater than this threshold are considered fully occupied.
- free_thresh: Pixels with an occupied probability less than this threshold are considered completely free.
5. View the Node Communication Graph
In the terminal, enter:
xxxxxxxxxxros2 run rqt_graph rqt_graph
If the graph does not display initially, select [Nodes/Topics (all)] and click the refresh button in the upper left corner.
6. View the TF Tree
Enter in the terminal:
xxxxxxxxxxros2 run rqt_tf_tree rqt_tf_treeAfter the program finishes running, a TF conversion interface will appear.

7. Code Analysis
This section only explains the map_gmapping_launch.py file for map creation. The file path is:
xxxxxxxxxx~/yahboomcar_ros2_ws/yahboomcar_ws/src/yahboomcar_nav/launch/map_gmapping_launch.pymap_gmapping_launch.py
xfrom launch import LaunchDescriptionfrom launch_ros.actions import Nodeimport osfrom launch.actions import IncludeLaunchDescriptionfrom launch.launch_description_sources import PythonLaunchDescriptionSourcefrom ament_index_python.packages import get_package_share_directorydef generate_launch_description(): laser_bringup_launch = IncludeLaunchDescription(PythonLaunchDescriptionSource( [os.path.join(get_package_share_directory('yahboomcar_nav'), 'launch'), '/laser_bringup_launch.py']) ) slam_gmapping_launch = IncludeLaunchDescription(PythonLaunchDescriptionSource( [os.path.join(get_package_share_directory('slam_gmapping'), 'launch'), '/slam_gmapping.launch.py']) ) return LaunchDescription([laser_bringup_launch, slam_gmapping_launch])Here a launch file - slam_gmapping_launch is started. See slam_gmapping_launch in detail. The file is located at
xxxxxxxxxx~/yahboomcar_ros2_ws/software/library_ws/src/ros2_gmapping/slam_gmapping/launch/slam_gmapping.launch.pyslam_gmapping.launch.py,
xxxxxxxxxxfrom launch import LaunchDescriptionfrom launch.substitutions import EnvironmentVariableimport launch.actionsimport launch_ros.actionsimport osfrom ament_index_python.packages import get_package_share_directorydef generate_launch_description(): return LaunchDescription([ launch_ros.actions.Node( package='slam_gmapping', executable='slam_gmapping', output='screen', parameters=[os.path.join(get_package_share_directory("slam_gmapping"), "params", "slam_gmapping.yaml")]), ])Here the slam_gmapping node is started and the slam_gmapping.yaml parameter file is loaded. The file is located at
xxxxxxxxxx~/yahboomcar_ros2_ws/software/library_ws/src/ros2_gmapping/slam_gmapping/params/slam_gmapping.yamlslam_gmapping.yaml
xxxxxxxxxx/slam_gmapping ros__parameters angularUpdate0.5 astep0.05 base_framebase_footprint map_framemap odom_frameodom delta0.05 iterations4 kernelSize1 lasamplerange0.005 lasamplestep0.005 linearUpdate1.0 llsamplerange0.01 llsamplestep0.01 lsigma0.075 lskip0 lstep0.05 map_update_interval5.0 maxRange8.0 maxUrange5.0 minimum_score0.1 occ_thresh0.25 ogain3.0 particles50 qos_overrides /parameter_events publisher depth1000 durabilityvolatile historykeep_last reliabilityreliable /tf publisher depth100 durabilityvolatile historykeep_last reliabilityreliable resampleThreshold0.5 sigma0.02 srr0.1 srt0.2 str0.1 stt0.2 temporalUpdate1.0 transform_publish_period0.05 use_sim_timefalse xmax10.0 xmin-10.0 ymax10.0 ymin-10.0