πŸ”¬ 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.

../_images/Lab9_ROS2_ActionTutorials.png

Complete the following three tutorials. Important: Skip C++ tutorials and focus only on the Python tutorials.

  1. Managing Dependencies with rosdep

    • No need to install anything - your computer already has all the required packages set up.

  2. Creating an action

    • Make sure you’re working in the ros2_ws workspace. Avoid using the master_ws workspace for this one.

  3. 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.

  1. 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.

  2. 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

    ../_images/Proj1_GazeboInit.png
  3. 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.

    ../_images/Proj1_CartographerInit.png
  4. 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.

    ../_images/Proj1_CartographerDone.png
  5. Once the mapping process is complete, save the generated map:

    ros2 run nav2_map_server map_saver_cli -f ~/map
    
  6. Download map_plotter.py to your home 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. πŸ˜‰

  7. Complete the TODO section in map_plotter.py, and then run the script to generate the map.

    ./map_plotter.py
    
  8. 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.

Autonomous Navigation with SLAM#

To run autonomous SLAM using Cartographer, we need to set up Cartographer to build the map and use Navigation2 to autonomously explore the environment and update the map in real-time.

  1. Launch the TurtleBot3 in the Gazebo simulation:

    ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py
    
    ../_images/Lab9_GazeboWorld.png
  2. Start Cartographer to perform SLAM:

    ros2 launch turtlebot3_cartographer cartographer.launch.py use_sim_time:=true
    
    ../_images/Lab9_CartographerInit.png
  3. You can now run Navigation2 alongside Cartographer to allow the robot to navigate autonomously using the evolving map:

    ros2 launch turtlebot3_navigation2 navigation2.launch.py use_sim_time:=true
    
    ../_images/Lab9_Nav2WorldInit.png

    Cartographer will continue updating the map dynamically as the robot navigates.

    • Click the 2D Pose Estimate button in the RViz2 menu. Then

    • Click on the map where the actual robot is located and drag the large green arrow toward the direction where the robot is facing.

    • Use 2D Nav Goal to set a navigation target

    • As you set waypoints to navigate multiple target points, the robot will explore the maze as shown below.

    ../_images/Lab9_Nav2WorldInProgress.png
    • Accordingly, the map will be updated as shown below

    ../_images/Lab9_GazeboWorldInProgress.png
  4. Explore the entire world and create a map. Ensure you have dark gray obstacles.

  5. Take a screenshot of the cartographer window by right clicking the tileboar. Submit the screenshot on Gradescope.

More to come soon#