A ROS 2 simulation package for autonomous navigation with the Sambot robot, using Gazebo Harmonic for simulation and Nav2 for mapping and autonomous navigation.
- ROS 2 Jazzy installed
- Gazebo Harmonic installed
Create a workspace and clone the repository in src folder and build it:
mkdir -p ~/nav2_ws/src
cd ~/nav2_ws/src
git clone https://github.com/ariccspstk/ros2_nav_sim.git
cd ~/nav2_wsInstall any missing dependencies using rosdep:
rosdep update
rosdep install --from-paths src --ignore-src -r -yThen build the workspace:
colcon build
source install/setup.bashOpens Gazebo and RViz with the Sambot robot spawned in the arena:
ros2 launch my_robot_description gazebo_sim.launch.pyIn a new terminal, start mapping the arena:
ros2 launch my_robot_description slam.launch.pyNote: In RViz, make sure the Map display's topic is set to
/mapso you can see the map being built in real time as you drive the robot around.
In another terminal, use the keyboard to drive the robot around and build up the map:
ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args -p stamped:=true -r /cmd_vel:=/demo/cmd_velDrive around until the arena is fully mapped.
Create the maps folder if it doesn't already exist:
mkdir -p ~/nav2_ws/src/my_robot_description/mapsOnce the arena is fully mapped, save it:
ros2 run nav2_map_server map_saver_cli -f ~/nav2_ws/src/ros2_nav_sim/my_robot_description/maps/arena_mapThis saves arena_map.yaml and arena_map.pgm to the maps directory.
Rebuild the workspace so the newly saved map is picked up:
cd ~/nav2_ws
colcon build
source install/setup.bashRelaunch the simulation:
ros2 launch ros2_nav_sim gazebo_sim.launch.pyIn a new terminal, launch Nav2 to begin autonomous navigation using the saved map:
ros2 launch ros2_nav_sim nav2.launch.pyYou can now set navigation goals for the robot through RViz using the saved map.
Note: Once
nav2.launch.pyis up, RViz won't have an initial pose for the robot and the frames won't align correctly until you set one. In RViz, click 2D Pose Estimate, then click and drag on the map at the robot's actual position/orientation in the arena. This localizes the robot against the map and loads the frames correctly (make sure RViz's Global Options > Fixed Frame is set tomap). Only after this step will Nav2 be able to plan and navigate accurately.
| Step | Command | Purpose |
|---|---|---|
| 1 | gazebo_sim.launch.py |
Launch Gazebo + RViz simulation |
| 2 | slam.launch.py |
Start SLAM mapping |
| 3 | teleop_twist_keyboard |
Manually drive the robot to map the arena |
| 4 | map_saver_cli |
Save the completed map |
| 5 | colcon build + gazebo_sim.launch.py + nav2.launch.py |
Rebuild and run autonomous navigation |
- Make sure to source your workspace (
source install/setup.bash) in every new terminal before running the above commands. - The
/cmd_veltopic is remapped to/demo/cmd_velfor teleop control.