π¬ Lab9: SLAM#
π Objectives#
Students should be able to implement a ROS2 node to detect walls using LiDAR data.
π Overview#
In this project, we will enable our robot to autonomously navigate an unknown maze and build a map of the environment. As in the previous lab, we will use LiDAR to detect the walls of the maze (or obstacles) surrounding the robot. We will be utilizing the Simultaneous Localization and Mapping (SLAM) library provided by ROS2 and TurtleBot3.
SLAM, or Simultaneous Localization and Mapping, is a process used in robotics to enable a robot to build a map of an unknown environment while simultaneously determining its location within that map. It involves combining sensor data, algorithms, and probabilistic methods to perform real-time mapping and localization. SLAM is crucial for autonomous robots to operate effectively in environments where pre-existing maps are not available.
SLAM is one of the fundamental algorithms in robotics and is widely used in applications such as autonomous vehicles, drone navigation, and robotic vacuum cleaners. It enables robots to navigate dynamic and unfamiliar environments without relying on GPS or pre-defined maps, which is essential for many real-world scenarios.
SLAM integrates data from sensors like LiDAR and odometry to construct and update a map while estimating the robotβs position. Through statistical methods like Kalman Filters or Particle Filters, SLAM corrects errors in localization and mapping to achieve accurate results. While the underlying mathematics involves advanced topics in statistics and optimization, libraries provided in ROS2 simplify SLAMβs implementation, making it accessible for practical applications.
We will use Cartographer in this lab because it provides an efficient and accurate SLAM solution for 2D environments like the maze weβll be mapping. Its ability to handle LiDAR data and update maps in real time makes it ideal for this project. Furthermore, its compatibility with TurtleBot3 and ROS2 simplifies the setup, allowing us to focus on understanding the SLAM process and its applications.
π± Pre-Lab: ROS2 Client Libraries#
The ROS2 Intermediate Tutorials on actions
are a great starting point for learning about ROS2 action servers and clients.

Complete the following three tutorials. Important: Skip C++ tutorials and focus only on the Python tutorials.
Managing Dependencies with rosdep
No need to install anything - your computer already has all the required packages set up.
Creating an action
Make sure youβre working in the
ros2_ws
workspace. Avoid using themaster_ws
workspace for this one.
Writing an action server and client (Python)
As instructed at the end of this tutorial, run the action client. When the feedback appears on the screen, capture a screenshot and upload it to Gradescope.
π οΈ Lab Procedures#
Setting Up TurtleBot3 with SLAM in Gazebo#
Follow these steps to simulate SLAM with TurtleBot3 in the Gazebo environment.
Download the
maze Gazebo files
. Extract the files and move them inside the appropriate directories in~/master_ws/src/turtlebot3_simulations/turtlebot3_gazebo
. Ensure each new directory is moved to the existing directory with the same name.Launch the Gazebo world:
ros2 launch turtlebot3_gazebo maze.launch.py
It will launch the Gazebo simulation for the maze as shown in the figure below
Open another terminal and run the Cartography SLAM:
ros2 launch turtlebot3_cartographer cartographer.launch.py use_sim_time:=true
This will start the SLAM process, and Cartographer will begin building the map shown below as you move the robot.
Use
gamepad
to manually navigate the robot in Gazebo and build the map:ros2 launch lab4_gamepad gamepad.launch.py
Ensure you navigate the entire maze. The obstacles (walls) are represnted in black. As the gray pixels represent noise, solid black color means low uncertainty of the obstacles. If you complete multiple laps, the uncertainty of the obstacles will be lower - light gray pixels will become dark gray pixels.
Once the mapping process is complete, save the generated map:
ros2 run nav2_map_server map_saver_cli -f ~/map
Download
map_plotter.py
to yourhome
directory and make it executable.chmod +x map_plotter.py
Then, verify if the file is now executable using
ls -l
Important
If you are asked to write the command that makes a file executable only for the file owner, you should be able to answer in your GR. π
Complete the
TODO
section inmap_plotter.py
, and then run the script to generate the map../map_plotter.py
Verify that the dimensions of the map in the plot correpsond to the actual maze. The length of the wall pieces is 0.18 meters.