CodeTheRobot

ROS 2 Lyrical Luth · Lesson 65 of 86

Recovery behaviors

Spin, back up, wait, clear costmaps and recovery nodes in the tree.

Estimated time · 45–75 min

Learning goals

What you will understand

  • Explain recovery behaviors 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

When navigation fails (no path, stuck, oscillating) Nav2 runs recovery behaviors: clear the costmaps, spin to look around, back up, and wait. They live in the behavior_server and are called from the behavior tree through RecoveryNode, which retries the main action after each recovery, up to a limit.

Recovery is not a cure for a bad configuration. If the robot recovers constantly, investigate the costmap, localization and speeds. Set retry limits so the robot eventually reports failure to its supervisor rather than spinning forever.

Guided lesson

Work through these steps

  1. 1.

    Source the ROS 2 Lyrical environment in a fresh terminal.

  2. 2.

    Read the recovery behaviors example and identify what each line does.

  3. 3.

    Build only the affected package, source the workspace overlay, and run the example.

  4. 4.

    Inspect the result from a second terminal with the ros2 CLI or RViz2 and compare it with the expected result.

Worked example

Read it before you run it

<RecoveryNode number_of_retries="6" name="NavigateRecovery">
<!-- Retry the child up to 6 times, running recovery between attempts. -->
  <PipelineSequence name="Navigate"><ComputePathToPose goal="{goal}" path="{path}"/><FollowPath path="{path}"/></PipelineSequence>
<!-- The normal navigation attempt. -->
  <ReactiveFallback name="RecoveryFallback">
<!-- Recovery options, tried in order. -->
    <GoalUpdated/>
<!-- Stop recovering if a new goal arrives. -->
    <SequenceStar name="RecoveryActions"><ClearEntireCostmap name="ClearLocal" service_name="local_costmap/clear_entirely_local_costmap"/><Spin spin_dist="1.57"/><Wait wait_duration="5"/></SequenceStar>
<!-- Clear the costmap, turn 90 degrees, then wait 5 seconds. -->
  </ReactiveFallback>
<!-- End of the recovery options. -->
</RecoveryNode>
<!-- End of the retry loop. -->

What the example is doing

  1. 1.The ros2 commands query the running graph; they do not prove that a node's internal behavior is correct.
  2. 2.Build from the workspace root, then source install/setup.bash so the shell can discover the new package.
  3. 3.Use a second sourced terminal to inspect nodes and interfaces while the example runs.

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 Recovery behaviors. Expected result: The robot retries navigation up to 6 times

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.

<!-- Retry the navigation child, running recoveries between tries -->
<!-- TODO: use a RecoveryNode with number_of_retries 6 -->
  <PipelineSequence name="Navigate">
    <ComputePathToPose goal="{goal}" path="{path}"/>
    <FollowPath path="{path}"/>
  </PipelineSequence>
  <Spin spin_dist="1.57"/>
</RecoveryNode>

Expected output

The robot retries navigation up to 6 times
Stuck? Show the full solution (try it yourself first)
<!-- Retry the navigation child, running recoveries between tries -->
<RecoveryNode number_of_retries="6" name="NavigateRecovery">
  <PipelineSequence name="Navigate">
    <ComputePathToPose goal="{goal}" path="{path}"/>
    <FollowPath path="{path}"/>
  </PipelineSequence>
  <Spin spin_dist="1.57"/>
</RecoveryNode>

Expected result

How you know it worked

  • ROS_DISTRO is lyrical in every terminal used for the exercise.
  • The complete package from the exercise builds and the expected graph entity appears in a second terminal.

Practical exercise

Apply the lesson

Trap the robot in a narrow gap in simulation, watch which recovery runs first, and change the tree so it backs up before spinning. Set a retry limit and confirm the goal finally fails.

Before you continue

  • ROS_DISTRO reports lyrical in every terminal you used
  • The example builds and runs without errors
  • You checked the result with a ros2 command, RViz2 or a test, not just by reading the code

If something goes wrong

ros2 cannot find a package or executable.

Source /opt/ros/lyrical/setup.bash, build from the workspace root, then source install/setup.bash in that terminal.

Nodes run but cannot discover each other.

Confirm both terminals use Lyrical, the same ROS_DOMAIN_ID, and compatible network/discovery settings.