CodeTheRobot

C++ Advanced · Lesson 20 of 23

Navigation stack basics (Nav2)

Maps, localization, planners and controllers, and sending a NavigateToPose goal.

Estimated time · 90–120 min

Learning goals

What you will understand

  • Explain navigation stack basics (nav2) in your own words
  • Use it in a small robotics example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

Nav2 is ROS 2's navigation framework. It needs a map, a localization source (AMCL or SLAM), a costmap of obstacles, a global planner (path from A to B), a local controller (follows the path and avoids obstacles) and behaviour-tree recovery actions. Everything is configured with parameters.

Your code usually talks to Nav2 through the NavigateToPose action: send a goal pose in the map frame and wait for the result. Start with the Nav2 TurtleBot demo in simulation, and check the TF tree first (map → odom → base_link).

Guided lesson

Work through these steps

  1. 1.

    Read the navigation stack basics (nav2) example and identify what each line owns or changes.

  2. 2.

    Create a small source file, compile with -std=c++17 and warnings enabled, and run it.

  3. 3.

    Change one input, predict the result before running, then compare the output.

  4. 4.

    Record one compiler or runtime error and explain the correction in your own words.

Worked example

Read it before you run it

#include "nav2_msgs/action/navigate_to_pose.hpp"
// The action type Nav2 exposes.
using Nav = nav2_msgs::action::NavigateToPose;
// A short name for it.
auto client = rclcpp_action::create_client<Nav>(node, "navigate_to_pose");
// Create an action client for the Nav2 server.
if (!client->wait_for_action_server(std::chrono::seconds(5))) return;
// Give up if Nav2 is not running.
Nav::Goal goal;
// The goal message.
goal.pose.header.frame_id = "map";
// Express the pose in the map frame.
goal.pose.pose.position.x = 2.0;
// Two metres along the map's X axis.
goal.pose.pose.orientation.w = 1.0;
// Face along X (identity quaternion).
auto future = client->async_send_goal(goal);
// Send the goal; the future completes when the server answers.

What the example is doing

  1. 1.Read declarations from right to left: identify each value's type, name, and initial value.
  2. 2.Follow the data through each expression before considering syntax shortcuts.
  3. 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.

Watch it run · Mobile robot

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

goalMOBILE ROBOT

Demo for Navigation stack basics (Nav2). Expected result: The robot drives to x = 2.0 m in the map frame

Practice code

Now type it yourself

Copy the starter into your ROS 2 package, replace every TODO, then build with colcon build and run it from a sourced terminal.

// Inside a node that owns  rclcpp_action::Client<Nav>::SharedPtr client_;
#include "nav2_msgs/action/navigate_to_pose.hpp"
#include "rclcpp_action/rclcpp_action.hpp"

using Nav = nav2_msgs::action::NavigateToPose;

void go_to_two_metres() {
  Nav::Goal goal;
  // TODO: set the goal pose: frame "map", x = 2.0, orientation w = 1.0, then send it asynchronously
}

Expected output

The robot drives to x = 2.0 m in the map frame
Stuck? Show the full solution (try it yourself first)
// Inside a node that owns  rclcpp_action::Client<Nav>::SharedPtr client_;
#include "nav2_msgs/action/navigate_to_pose.hpp"
#include "rclcpp_action/rclcpp_action.hpp"

using Nav = nav2_msgs::action::NavigateToPose;

void go_to_two_metres() {
  Nav::Goal goal;
  goal.pose.header.frame_id = "map";
  goal.pose.pose.position.x = 2.0;
  goal.pose.pose.orientation.w = 1.0;
  client_->async_send_goal(goal);
}

Expected result

How you know it worked

  • After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
  • Changing the stated input produces the result you predicted.

Practical exercise

Apply the lesson

Start the Nav2 simulation demo, send a goal from RViz2, then write a C++ client that sends three goals in sequence and prints the result of each. Cancel one goal halfway and confirm the robot stops.

Before you continue

  • The program builds with -std=c++17 and no warnings
  • You can explain the key rule without reading the example
  • The normal and edge cases both have recorded results

If something goes wrong

The compiler reports that a name or type is unknown.

Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.

The program runs but the value is wrong.

Print the inputs and intermediate values, then check conversions, units, and collection order.