How to implement Gmapping SLAM algorithms in mobile robots

Written by GuYueHome

01 What is SLAM?

The full name of SLAM stands for Simultaneous Localization and Mapping, which translates as instantaneous localization and map building. There are two key words: localization and map building, which means the robot will determine its position in an unknown environment while constructing a map, and will ultimately generate a map like this one.

In short, the SLAM map construction process consists of measuring the distance between the robot and the surrounding environment using depth sensors to complete map construction. At the same time, the robot verifies the consistency of the environment and detects whether it moves toward the point where the map was constructed, thus completing the closed loop.

The most widely used SLAM algorithm in ROS is Gmapping, a common open-source SLAM algorithm based on the filtered SLAM framework, which is in turn based on the RBpf particle filtering algorithm. That is, the localization and map construction processes are separated, with localization performed first and then the map constructed. If you're interested in the Gmapping algorithm, you can check out the paper about it at: https://openslam-org.github.io/gmapping.html

02 Motion control of the LIMO mobile robot
2.1 LIMO multimodal robot

Our common mobile robot movement types are generally four-wheel differential, Ackermann steering, crawler differential, omnidirectional movement, etc., but as the saying goes, adults do not choose, Songling Robotics LIMO one to meet all your needs, four kinds of movement at will any switch.

In the future, we will focus on testing multimodal motion types. Today, we focus on implementing SLAM application functions in the motion state of the LIMO robot with differential velocity across all four wheels.

2.2 Remote connection to LIMO

You can download NoMachine - Free Remote Desktop for Everyone directly from the official LIMO website: https://www.nomachine.com/

Once the software is installed, your computer and cart connect to the same Wi-Fi network so you can connect to the cart remotely.


2.3 Download and compile the code

The LIMO robot code is constantly being updated, as can be seen on the GitHub homepage. To ensure we're using the latest code, we use the following command to download the code package to the LIMO Embedded System workspace, thus replacing the original code.

Next, use the catkin_make command to compile the build. This is a common operation in ROS, so if you're unfamiliar with it, you can read "21 Lessons on Getting Started with ROS" by Guyue Jun.

2.4 Making the cart work

To ensure the robot's basic functionality is ready to go, let's try the most basic keyboard control to get the cart moving.

Launch a terminal to activate the car's chassis and sensors.

Once the chassis has successfully started, it will subscribe to the cmd_vel topic, just like controlling a small sea turtle, and launch the following keyboard control node; you can control LIMO's smooth movement via the keyboard.

03 Gmapping SLAM Function Configuration Analysis
3.1 Gmapping Package

Before using Gmapping package in ROS, you need to install it using the following command, here is the Melodic version, if you are using other versions of ROS, replace the version name in the command.

The SLAM algorithm isn't simple, but Gmapping is a well-packaged "building block," and we can find the interface on the ROS wiki. The description is shown in the following figure.

As you can see, Gmapping needs to subscribe to the robot joint transformation topic /tf and the LiDAR scan data topic /scan, and then publish the raster map topic /map.


As for /tf, which is divided into two parts, the TF transformations in the Gmapping feature package are shown in the following figure.

Thus, we can determine that the conditions required for the Gmapping algorithm (/tf, /odom, /scan) will be ready to be published/mapped by the algorithm.

Following the startup of the previous LIMO robot chassis, these basic conditions were normally published or updated to the underlying node, so we will focus on configuring the Gmapping function node using a launch file.

3.2 Configuring the SLAM function

Create a feature package named limo_slam in the workspace's src file. Another startup folder will be created there, where the startup file for the Gmapping startup configuration will be stored.

The details of the gmapping.launch startup file are as follows. If you're using other robots, you'll need to modify the commented sections in the following file according to your situation.

If you are familiar with the SLAM algorithm, these parameters may not be new to you, but if you are not familiar with it, don't worry, as they have default values ​​and, in most cases, you can use these values ​​or similar robot settings in ROS.

Since the Gmapping algorithm framework is shown below, the main parameters we need to modify are odom (odometer coordinate system, required), scan (LIDAR theme, required), base_footprint_frame (robot base coordinate system, required), and imu_data (IMU theme, optional). The names and values ​​of these parameters must match the theme names and values ​​published to the robot chassis controller limo_start. Otherwise, Gmapping does not receive these parameters and cannot run SALM.

04 Results of Gmapping SLAM operation

After making the necessary preparations, we can use LiDAR for SLAM.

First, the code in the workspace is compiled using catkin_make, and then a separate terminal is launched to run the following commands.

To see the effect of dynamic SLAM, we also need to use the Rviz visual display by opening a terminal and entering the following command.

Then click the ADD button to add the required configurations, such as Map, LaserScan, TF, etc., subscribe to the corresponding topic data, for example, when adding Map, we need to change the Topic in Map to /map.

We can then control the carriage's slow motion for SLAM using the keyboard. If we move the carriage after starting SLAM, we must do it again, as the carriage's TF is likely to be incorrect.

Pay attention to controlling the speed of the car's movement. If the speed is too fast when turning, the map shift phenomenon is likely to occur.

After configuring Rviz, we can also save the current configuration to the feature package and then add the following Rviz node and parameter configuration to the Gmapping startup file to avoid the need to create a new terminal each time to enter the rviz command and re-add the display in Rviz.

We can also use the rqt_graph command to see the relationships between nodes:

At the end of SLAM, the following command is required to save the map:

Due to limited space, we built the map as shown below.

At this point, we have implemented SLAM on a LIMO mobile robot using the Gmapping algorithm. Using a similar approach, we can also implement other SLAM function packages, such as hector, cartographer, and rtabmap, among others. Each algorithm has its own advantages and disadvantages, so you can choose the right algorithm package based on your needs.

Related articles

Custom HTML