Autonomous Navigation Introduction

Driving a robot around is fun to begin with, but if you had to drive your robot vacuum cleaner around then the novelty would quickly would wear off. Autonomous navigation is the answer. An agent (a human or an algorithm) supplies a goal position, and we expect the robot to do the rest. When a human supplies goal positions (and makes the decision where these are) this is normally called semi-autonomous navigation.

In previous weeks we have gradually built up a simulation of a robot which can generate a map (or use an existing map) and localise itself in an environment. These were key stepping stones to autonomous navigation.

For autonomous navigation we need the following capabilities of a mobile robot:

  • Locomotion given an input velocity (Kinematics, Actuation, and Control)

  • Sensing to avoid obstacles and produce a map (e.g. lidar)

  • Where is the robot (Localisation/SLAM and Transformations)

  • Start and Goal positions (in the same global frame)

  • Draw a path from the start to the goal (Path Planning)

  • Drive the robot whilst attempting to Keep the robot on that path (Control and Navigation)

We can sense obstacles (with a simulated lidar), generate a map of the environment to provide a global frame and a reference of obstacles, move a robot based on a desired command velocity, know where the robot is in the global frame. All that is left is Path Planning and Navigation.

Everything a mobile robot will need is covered by the existing ROS Nav2 Navigation Stack.

Behaviour Trees and the Navigation Stack

The Nav2 Navigation stack relies on behaviour trees. If you are familiar with state machines, behaviour trees are a somewhat similar concept. These will be covered in more detail in Week 11, so these week you can treat it like a black box.

As a very high-level overview, you can consider the behaviour tree as a solution for handling the transitions between different “modes” in navigation. For example, these modes may include what happens if the robot gets stuck (recovery behaviours).

Build a Package for This Tutorial

We will begin making use of an UPDATED VERSION of the gz_example_description_package. Please replace any existing versions you have of this package. You can download a copy here: gz_example_robot_description.zip

We will utilise a new ROS2 package called navigation_demos for these activities, which will contain two additional directories launch and config.

Make a package the usual way:

cd ~/MY_ROS_WS/src/
ros2 pkg create navigation_demos --build-type ament_python
cd navigation_demos
mkdir launch
mkdir config

Your setup.py file should have some extra lines added to include the launch and config directories:

 1from setuptools import find_packages, setup
 2import os
 3from glob import glob
 4
 5package_name = 'navigation_demos'
 6
 7setup(
 8    name=package_name,
 9    version='0.0.0',
10    packages=find_packages(exclude=['test']),
11    data_files=[
12        ('share/ament_index/resource_index/packages',
13            ['resource/' + package_name]),
14        ('share/' + package_name, ['package.xml']),
15        (os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*launch.[pxy][yma]*'))),
16        (os.path.join('share', package_name, 'config'), glob(os.path.join('config', '*.yaml'))),
17    ],
18    install_requires=['setuptools'],
19    zip_safe=True,
20    maintainer='Andy West',
21    maintainer_email='andrew.west@manchester.ac.uk',
22    description='TODO: Package description',
23    license='MIT',
24    tests_require=['pytest'],
25    entry_points={
26        'console_scripts': [
27        ],
28    },
29)

A launch file will be built up gradually, whilst we add more config files to our navigation system. In the launch directory of the package, create a launch file called nav_demo.launch.py. You can copy the code below to get you started.

 1from ament_index_python.packages import get_package_share_directory
 2from launch import LaunchDescription
 3from launch.actions import IncludeLaunchDescription
 4from launch_ros.actions import SetParameter, Node
 5from launch.launch_description_sources import PythonLaunchDescriptionSource
 6from launch.substitutions import PathJoinSubstitution
 7
 8
 9def generate_launch_description():
10    ld = LaunchDescription()
11
12    # Parameters, Nodes and Launch files go here
13
14    # Declare package directory
15    pkg_nav_demos = get_package_share_directory('navigation_demos')
16    # Necessary fixes
17    remappings = [('/tf', 'tf'), ('/tf_static', 'tf_static')]
18
19
20    # LOAD PARAMETERS FROM YAML FILES
21    config_bt_nav     = PathJoinSubstitution([pkg_nav_demos, 'config', 'bt_nav.yaml'])
22
23    # Include Gazebo Simulation
24    launch_gazebo = IncludeLaunchDescription(
25    PythonLaunchDescriptionSource([get_package_share_directory('gz_example_robot_description'), '/launch', '/sim_robot.launch.py']),
26    launch_arguments={}.items(),
27    )
28
29    # Include SLAM Toolbox standard launch file
30    launch_slamtoolbox = IncludeLaunchDescription(
31    PythonLaunchDescriptionSource([get_package_share_directory('slam_toolbox'), '/launch', '/online_async_launch.py']),
32    launch_arguments={}.items(),
33    )
34
35    # Behaviour Tree Navigator
36    node_bt_nav = Node(
37        package='nav2_bt_navigator',
38        executable='bt_navigator',
39        name='bt_navigator',
40        output='screen',
41        parameters=[config_bt_nav],
42        remappings=remappings,
43    )
44
45    # Behaviour Tree Server
46    node_behaviour = Node(
47        package='nav2_behaviors',
48        executable='behavior_server',
49        name='behaviour_server',
50        output='screen',
51        parameters=[config_bt_nav],
52        remappings=remappings,
53    )
54
55
56    # Add actions to LaunchDescription
57    ld.add_action(SetParameter(name='use_sim_time', value=True))
58    ld.add_action(launch_gazebo)
59    ld.add_action(launch_slamtoolbox)
60    ld.add_action(node_bt_nav)
61    ld.add_action(node_behaviour)
62
63    return ld

Also add the bt_nav.yaml to the config directory. Download the file bt_nav.yaml.

Then check everything builds as per usual.

cd ~/MY_ROS_WS/
colcon build
source install/setup.bash

Now we can start the tutorial.