This project demonstrates 2D mapping and robot localization using LiDAR with slam_toolbox in ROS 2. The package enables autonomous mapping of environments and subsequent robot localization within those maps using a differential drive robot in Gazebo simulation. Check out the 2d mapping and localization here.
- Ubuntu: 24.04 LTS
- ROS 2: Jazzy Jalapa
- Gazebo: Harmonic
Install required ROS 2 packages:
sudo apt install ros-jazzy-robot-state-publisher \
ros-jazzy-joint-state-publisher \
ros-jazzy-xacro \
ros-jazzy-teleop-twist-keyboard \
ros-jazzy-ros-gz-sim \
ros-jazzy-ros-gz-bridge \
ros-jazzy-slam-toolbox - Create Workspace and Clone Repository
mkdir -p my_ws/src && cd my_ws/src
git clone https://github.com/yashikasharma0301/lidar_2d_mapping.git- Build the Workspace
cd ..
colcon build- Source the Workspace
source install/setup.bash- Launch Robot with LiDAR in Gazebo
ros2 launch lidar_2d_mapping gazebo_spawn.launch.py- Start SLAM Mapping
- In a new terminal:
cd my_ws
source install/setup.bash
ros2 launch lidar_2d_mapping slam_mapping.launch.py- Wait for a minute and let it load
- Create the Map
-
In Gazebo, open the teleop plugin to control the robot
-
Move the robot around the environment to generate a comprehensive map
-
Monitor the mapping progress in RViz
- Save the Map
-
In RViz's "SlamToolboxPlugin" panel, enter the location where you want to save the map infront of the Serialize Map button :
/home/YOUR_USERNAME/my_ws/src/lidar_2d_mapping/maps/map_serializeReplace
YOUR_USERNAMEwith your actual username -
Click on Serialize Map to save the generated map
-
Saving the map in Save Map allows you to use the map for external environments also whearas to use the map with slam_toolbox, you need to save the serialized map
-
Close the mapping terminal once mapping is complete
- Configure Localization Parameters
gedit ~/my_ws/src/lidar_2d_mapping/params/mapper_params_localization.yaml
- Update the
map_file_nameparameter with your saved map location - Optionally, set your preferred initial pose using the
map_start_poseparameter - Save and close the file
- Close all the terminals
- Launch Localization
- In a new terminal rebuild the workspace and launch the gazebo world again:
cd my_ws
colcon build
source install/setup.bash
ros2 launch lidar_2d_mapping gazebo_spawn.launch.py- In another terminal:
cd my_ws
source install/setup.bash
ros2 launch lidar_2d_mapping slam_localization.launch.py- Test Localization
-
Use the Gazebo teleop plugin to move the robot
-
Observe how the robot localizes itself within the previously created map
Robot Model: This project uses a URDF model adapted from the TortoiseBot example in the OSRF ROS Book. The original model has been modified for ROS 2 Jazzy and Gazebo Harmonic integration with LiDAR sensor capabilities.
Original Authors: Open Source Robotics Foundation (OSRF)