This part of the guide shows how to finally add Nav2 to our setup.

Install

  sudo apt install ros-$ROS_DISTRO-navigation2
sudo apt install ros-$ROS_DISTRO-nav2-bringup
  

Gazebo

New node diagram

image.png

Download the nav2_params.yaml in the config folder. Later we will fine tune it for our robot.

image.png

nav2_params.yaml

  colcon build --symlink-install
  

in 2 different terminals run:

  ros2 launch mbot_pkg display.launch.py use_sim_time:=true
  
  ros2 launch nav2_bringup navigation_launch.py use_sim_time:=true params_file:=<path/to/nav2_params.yaml>
  

To know if nav2_bringup ran correctly, in logs wait for “Creating bond timer”

rviz

image.png

Definitely recommend adding other visualizations such as:

  • /cmd_vel_nav
  • /goal_pose

Result:

image.png

Publishing Goal pose

image.png

image.png

image.png

click and drag anywhere on the map to put the goal of where the robot should go to

image.png

This publishes a message to /goal_pose

The robot should then start to automatically move to that spot

ctrl+s to save everything

Physical

New node diagram

image.png

in 2 different terminals run:

  ros2 launch mbot_pkg display.launch.py
  
  ros2 launch nav2_bringup navigation_launch.py use_sim_time:=true params_file:=**path/to/nav2_params.yaml**
  

You should be able to publish a goal pose in rviz like in the section above

If you need your robot to autonomously put goal poses down, such as a match start in Robomasters, you just need to publish geometry_msgs/PoseStamped on /goal_pose inside of my_node

Adding nav2_bringup to launch file

    
def generate_launch_description():
    pkg_share = get_package_share_directory('mbot_pkg') # gets the location of mbot_pkg
    default_model_path = os.path.join(pkg_share, 'description', 'mbot_description.urdf') # gets the location of the urdf
    default_rviz_config_path = os.path.join(pkg_share, 'rviz', 'config.rviz') # gets the location of the rviz config
    bridge_config_path = os.path.join(pkg_share, 'config', 'bridge_config.yaml') # gets location of gazebo config
    world_path = os.path.join(pkg_share, 'world', 'my_world.sdf') # gets the gazebo world file
    slam_yaml_path = os.path.join(pkg_share, 'config', 'slam.yaml') # gets the slam config file 
    nav2_yaml = os.path.join(pkg_share, 'config', 'nav2_params.yaml') # gets the nav2 config file
     
     ...
     
    nav2_bringup_node = IncludeLaunchDescription(
        PythonLaunchDescriptionSource([
            FindPackageShare("nav2_bringup"), '/launch', '/navigation_launch.py']),
        launch_arguments={
            'params_file': nav2_yaml,
            'use_sim_time': LaunchConfiguration('use_sim_time')

        }.items()
    )
    
    return LaunchDescription([
        DeclareLaunchArgument(name='use_sim_time', default_value='False', description='Flag to enable use_sim_time'),
        # joint_state_publisher_gui_node, # debugs urdf joints
        my_node, # swaps joint_state_publisher_gui_node for physical robot
        robot_state_publisher_node, # publishes urdf to ROS
        rviz_node, # starts rviz

        # stuff to start gazebo
        # ExecuteProcess(cmd=['gz', 'sim', '-g'], output='screen'),
        # gz_server,
        # ros_gz_bridge,
        # spawn_entity,
        
        lidar_node # lidar for physical setup 
        
        slam_toolbox_node, # providing the map => odom transform.

        nav2_bringup_node, # starts nav2

    ])
  

If you have gotten to this part of the guide:

🎉CONGRATS YOU GOT NAV2 WORKING 🎉

However, now there is a lot more tuning that needs to be done

Tuning Nav2 settings

Depending on what your final robot looks like you should change the footprint and robot_radius parameter. These were the green outline in rviz around the robot and are used to calculate the path finding on the 2D map.

Guide for footprint tuning

All the other settings in the nav2_params.yaml also need to be tuned because the nav2_bringup_node launches multiple nodes each with many parameters. Here is a general guide from the official nav2 docs that goes over what each node does and how to tune them. However, the next guide will go a little more indepth on how to better turn the nav2_param.yaml file.