ROS Autonomous SLAM using Rapidly Exploring Random Tree (RRT) | by Mohamed Fazil | Towards Data Science

The Robot Operating System(ROS) has become a game-changer in the field of modern Robotics which has an extremely high potential in this field which is yet to be explored with the upcoming generation of more skilled open-source robotics communities. One of the greatest advantages of ROS is it allows seamless integration of different processes aka nodes in a robot system which can also be reused in other systems without any fuss with the support of the opensource community.
One of the most popular applications of ROS is SLAM(Simultaneous Localization and Mapping). The objective of the SLAM in mobile robotics is constructing and updating the map of an unexplored environment with help of the available sensors attached to the robot which is will be used for exploring.
In this demo, we will see the implementation of the prebuilt ROS packages i.e Navigation stack and Gmapping to map an unexplored house using SLAM autonomously using the Randomly Exploring Random Tree (RRT) algorithm to make the robot cover all the regions of the unknown environment.
A Rapidly-exploring Random Tree (RRT) is a data structure and algorithm that is designed for efficiently searching nonconvex high-dimensional spaces. RRTs are constructed incrementally in a way that quickly reduces the expected distance of a randomly-chosen point to the tree. RRTs are particularly suited for path planning problems that involve obstacles and differential constraints (nonholonomic or kinodynamic). source
We will be using the RRT algorithm for the robot to path plan to all the reachable distant endpoints within the sensor’s vicinity aka frontier points which in return makes the robot map new regions continuously using SLAM as it tries to reach its new distant endpoints. Applying RRT to mobile robots in such a way enables us to create a self-exploring autonomous robot with no human interventions required. The nature of the algorithm tends to be biased towards unexplored regions which become very beneficial to environment exploring tasks. More in-depth information and flow of this strategy can found in this publication: Autonomous robotic exploration based on multiple rapidly-exploring randomized trees.
The Gazebo Simulator is a well-designed standalone robot simulator which can be used to rapidly test algorithms, design robots, perform regression testing, and train AI system using realistic scenarios. We will be using the Gazebo environment with a house model for our robot to explore and produce a map of the house.
For this demo feel free to download my pre-built ROS package ros_autonomous_slam from my Github repository.
This repository consists of a ROS package that uses the Navigation Stack to autonomously explore an unknown environment with help of GMAPPING and constructs a map of the explored environment. Finally, a path planning algorithm from the Navigation stack is used in the newly generated map to reach the goal. The Gazebo simulator is used for the simulation of the Turtlebot3 Waffle Pi robot. Various algorithms have been integrated for Autonomously exploring the region and constructing the map with help of the 360-degree Lidar sensor. Different environments can be swapped within launch files to generate the required map of the environment. The current most efficient algorithm used for autonomous exploration is the Rapidly Exploring Random Tree (RRT) algorithm. The RRT algorithm is implemented using the package from rrt_exploration which was created to support the Kobuki robots which I further modified the source files and built it for the Turtlebot3 robots in this package.
Step 1: Place the Robot in the Environment within Gazebo
Step 2: Perform Autonomous exploration of the environment and generate the Map
Step 3: Perform path planning and go to goal in the environment
Before starting to execute the steps please make sure you have the prerequisites and setup for this project demo completed successfully. There are three setup parts (Gazebo ROS Installation, Turtlebot3 Packages, and Navigation Stack Installation).
I used Ubuntu 18 OS with ROS Melodic Version. Check the ROS official documentation for the Installation ROS Installation
The main Gazebo Simulator which is a stand-alone application must be installed. Go through the documentation Gazebo Installation. Test the working of Gazebo and its version with
gazebowhich gzserverwhich gzclient
After Installing the Gazebo, the Gazebo ROS Package must be installed separately
sudo apt-get install ros-melodic-gazebo-ros-pkgs ros-melodic-gazebo-ros-control
Replace melodic with your version of ROS everywhere in this tutorial.
The Turtlebot3 ROS packages can be either downloaded and built from source files in your workspace or else directly installed from the Linux terminal. Either way works, I would recommend doing both as it installs all the missing dependencies required automatically.
Direct Installation
source /opt/ros/melodic/setup.bashsudo apt-get install ros-melodic-turtlebot3-msgssudo apt-get install ros-melodic-turtlebot3
Building the packages
cd catkin_ws/srcgit clone -b melodic-devel https://github.com/ROBOTIS-GIT/turtlebot3git clone -b melodic-devel https://github.com/ROBOTIS-GIT/turtlebot3_simulationscd ..catkin_makesource /devel/setup.bash
The Navigation stack can also be downloaded as source files to your workspace and built.
sudo apt-get install ros-melodic-navigationcd catkin_ws/srcgit clone -b melodic-devel https://github.com/ros-planning/navigationcd ..catkin_makesource /devel/setup.bash
Set your environment variable to the model robot to be used.
export TURTLEBOT3_MODEL=waffle_pisource ~/.bashrc
Execute the given launch to open Gazebo with the given world file and place the robot Turtlebot3 Waffle pi model in it.
roslaunch ros_autonomous_slam turtlebot3_world.launch
Keep this process running always and execute other commands in a different terminal.
roslaunch ros_autonomous_slam autonomous_explorer.launch
Run the Autonomous Explorer launch file which executes two tasks for us at the same time.
It starts the SLAM node in the Navigation stack with a custom modified RVIZ file to monitor the mapping of the environment.It simultaneously starts the Autonomous explorer which is a Python-based controller to move around the robot grazing all the areas which help the SLAM Node to complete the mapping. The default algorithm used for exploration is the RRT algorithm. I have also created an explorer method that uses Bug Wall following algorithm for exploration which can be tested by adding explorer the argument to the launch which takes [RRT,BUG_WALLFOLLOW].
It starts the SLAM node in the Navigation stack with a custom modified RVIZ file to monitor the mapping of the environment.
It simultaneously starts the Autonomous explorer which is a Python-based controller to move around the robot grazing all the areas which help the SLAM Node to complete the mapping. The default algorithm used for exploration is the RRT algorithm. I have also created an explorer method that uses Bug Wall following algorithm for exploration which can be tested by adding explorer the argument to the launch which takes [RRT,BUG_WALLFOLLOW].
The RRT exploration requires a rectangular region around the robot to be defined in the RVIZ window using four points and a starting point for exploration within the known region of the robot. The total five points must be defined in the exact sequence given below using the RVIZ Publish Points option.
Monitor the Mapping process in the RVIZ window and sit back and relax until our robot finishes mapping XD.
Once you are satisfied with the constructed map, Save the map.
rosrun map_server map_saver -f my_map
The my_map.pgm and my_map.yaml gets saved in your home directory. Move these files to the package’s maps folder (catkin_ws\src\ros_autonomous_slam\maps).Now your new map which is basically an occupancy grid is constructed!
Incase of Autonomous Fails you can manually control the robot in the environment using the keyboard with a separate launch execution given below. You can also manually explore and construct the map like a game.
roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch
We will be using the Navigation stack of the ROS to perform the path planning and go to goal using /move_base/goal actions. The given blow launch execution opens up an RVIZ window which shows the Robot location within the previously constructed map.
roslaunch ros_autonomous_slam turtlebot3_navigation.launch
The RVIZ Window shows the robot’s local map construction using its Laser sensors with respect to the Global Map previously constructed in Step 2 with help of a cost map.
First, estimate the initial Pose i.e locating the real robot location with respect to the Map. This can be set in the RVIZ window itself using the 2D Pose Estimate and pointing and dragging the arrow in the current robot’s location and orientation.
A GOAL point can be set in the RVIZ window itself using the 2D Nav Goal option which will be available in the top window tab. This allows you to set a goal point in the map within the RVIZ environment, then the robot automatically performs the path planning and starts to move in its path.
ROS Navigation Stack requires tuning its parameters which work differently for different environment types to get the Optimal SLAM and Pathplanning performance. Here is ROS’s Navigation Stack parameter tuning guide for Turtlebot3. Turtlebot3 Navigation Parameter Tuning Guide