Mapping 101: ROS2 SLAM Beginner Guide – Your Robot Draws Its First Floorplan

RobotisimRobotisim
3 min read

In this guide, you’ll learn how to use ROS2 SLAM to make your Raspberry Pi robot create its first digital map. This is an important step for any robotics project. Whether you’re a student, hobbyist, or entrepreneur, this guide will help you get your robot up and running with autonomous mapping.

1. Set Up Remote Access — "Connect to Your Raspberry Pi"

First, you need remote access to your Raspberry Pi. Robots don’t have screens in the field, so you’ll use SSH to access it remotely:

bash

ssh ubuntu@<raspberry-pi-ip>
Make sure to change the default password for security.

2. Connect Your LiDAR — "Your Robot’s Eyes"

Now, connect your 2D USB LiDAR to the Raspberry Pi. This sensor will scan your robot’s surroundings to create the map. Check the device with:

bash

ls /dev/tty*

If you’re using a LiDAR that draws a lot of power, consider using an external 5V converter to avoid power issues.

3. Visualize LiDAR Data in RViz

After connecting the LiDAR, visualize its data using RViz, a standard tool for ROS visualization. Launch the LiDAR driver, then add a LaserScan display in RViz:

bash

ros2 launch <lidar_driver_package> <launch_file>.py

As your LiDAR scans the area, you should see a 360-degree view of the environment.

4. Understanding the Occupancy Grid

As the LiDAR scans, it creates an occupancy grid. This grid shows:

  • White: Open space the robot can move through

  • Black: Obstacles or walls

  • Gray: Areas the robot doesn’t know about yet
    This grid is the map your robot uses to navigate.

5. Start Live Mapping with SLAM Toolbox

To create the map, launch SLAM Toolbox in asynchronous mode:

bash

ros2 launch slam_toolbox online_async_launch.py

Now, in RViz, you’ll see the map update as your robot moves, with walls and corners appearing in real time.

6. Save the Map for Later Use

Once your robot has completed the scan, save the map for future use in navigation:

bash

ros2 run nav2_map_server map_saver_cli -f my_first_map

This creates two files:

  • my_first_map.pgm: The map image

  • my_first_map.yaml: The map metadata

These files can be used for autonomous navigation with ROS2 in the future.

7. Test Loop Closure

To test your SLAM setup, drive the robot manually in a square or circular pattern. If the SLAM system is working well, the robot should align the map edges when it returns to the starting point.

8. Why the Map Has Gaps — The Need for Sensor Fusion

If you notice gaps or drift in your map, it's because LiDAR alone can’t account for the robot’s movement. To fix this, you need sensor fusion, which combines data from the LiDAR, IMU, and encoders to reduce drift and make the map more accurate.

9. Share Your Map with the Community

Once you’ve created your first map, share it with the Robotisim community on Discord. Compare your results with others and get tips for improving your setup.

10. Next Steps: Sensor Fusion and Navigation

Now that your robot can create a basic map, it’s time to enhance its accuracy. In our next post, we’ll show you how to:

  • Use sensor fusion to improve mapping accuracy

  • Integrate Nav2 for autonomous navigation and path planning

Take Your Skills Further with Our Mobile Robotics Engineering Course

Ready to dive deeper into robotics? Check out our Mobile Robotics Engineering Course at Robotisim. This course will guide you through building advanced autonomous robots, mastering ROS2, and creating robust navigation systems.

Start learning today and take your robotics projects to the next level!

0
Subscribe to my newsletter

Read articles from Robotisim directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Robotisim
Robotisim

Learn robotics with ROS2, micro-ROS, Raspberry Pi & ESP32. Free & paid hands-on courses at robotisim.com