ROS2 Nav2 Beginner Tutorial

ROS2 Nav2 Beginner Tutorial

Introduction

Nav2 (Navigation2) is an autonomous navigation system developed for ROS 2. It performs tasks such as robot localization on maps, path planning, and obstacle avoidance for mobile robots.

Requirements

ROS 2 installation (Humble, Iron, or Rolling recommended) and basic ROS 2 knowledge are required. Additionally, colcon build tool and Gazebo simulator should be installed.

Nav2 Installation

To install from Debian packages, run the following command in the terminal:

sudo apt install ros-<distro>-navigation2 ros-<distro>-nav2-bringup

Replace <distro> with your ROS 2 distribution (humble, iron, etc.).

Also install TurtleBot3 simulation packages:

sudo apt install ros-<distro>-turtlebot3*

Basic Concepts

Nav2 consists of several core components. Costmap is a grid map representing obstacles and free space around the robot. Planner calculates the global path from the starting point to the goal. Controller moves the robot by following this path. Recovery behaviors are triggered when the robot gets stuck or trapped.

First Simulation

First, set the TurtleBot3 model type:

export TURTLEBOT3_MODEL=waffle

Launch the Gazebo simulator:

ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py

In a new terminal, start Nav2:

ros2 launch turtlebot3_navigation2 navigation2.launch.py use_sim_time:=True

This command will open RViz and start the navigation stack.

Navigation with RViz

When RViz opens, click the "2D Pose Estimate" button to set the robot's initial position and mark the robot's actual location on the map. Then select the goal point with the "Nav2 Goal" button. The robot will automatically plan a path and move towards the goal.

Using with Your Own Robot

To use Nav2 with your own robot, you need a URDF robot description, sensor data (LaserScan or PointCloud2), odometry information, and cmd_vel topic for robot control.

Create a sample launch file:

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        Node(
            package='nav2_bringup',
            executable='bringup_launch.py',
            parameters=[{'use_sim_time': False}]
        )
    ])

Parameter Settings

Use a YAML configuration file to customize Nav2 behaviors. Sample params.yaml file:

controller_server:
  ros__parameters:
    controller_frequency: 20.0
    min_x_velocity_threshold: 0.001
    min_theta_velocity_threshold: 0.001

planner_server:
  ros__parameters:
    expected_planner_frequency: 20.0
    planner_plugins: ["GridBased"]
    GridBased:
      plugin: "nav2_navfn_planner/NavfnPlanner"

Mapping

You need to create a map first for navigation. Extract a map using SLAM:

ros2 launch turtlebot3_cartographer cartographer.launch.py use_sim_time:=True

Scan the environment by moving the robot with teleop:

ros2 run turtlebot3_teleop teleop_keyboard

Save the map:

ros2 run nav2_map_server map_saver_cli -f ~/my_map

Advanced Features

Nav2 can create complex navigation scenarios with behavior trees, navigate to multiple goals sequentially with waypoint follower, and execute recovery routines when the robot gets stuck using recovery behaviors.

Resources

For more information, visit the Navigation2 official documentation (https://navigation.ros.org/) and ROS 2 documentation (https://docs.ros.org/).

Comments

(0)
Top commentsNewest first

0/3000 • Press Ctrl + Enter to submit

Loading comments...

ROS2 Nav2 Beginner Tutorial | SourceDev