CodeTheRobot

Ubuntu Linux for Robotics · Lesson 82 of 96

Recovery behaviors

Spin, back up, wait, clear costmaps and testing each recovery from the terminal.

Estimated time · 30–60 min

Learning goals

What you will understand

  • Explain recovery behaviors in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

When navigation fails, Nav2 runs recovery behaviors from the behavior server: spin, back up, wait, and clearing the costmaps. The behavior tree decides the order and the number of retries. Each recovery is also an action you can call directly for testing.

Frequent recovery means a deeper problem: poor localization, a wrong costmap or unrealistic speeds. Read the server logs (ros2 launch output or ~/.ros/log) to see why the recovery started. Set a retry limit so the robot reports failure instead of trying forever.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the recovery behaviors commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

ros2 action list | grep -E 'spin|backup|wait'
# List the recovery actions.
ros2 action send_goal /spin nav2_msgs/action/Spin "{target_yaw: 1.57}"
# Rotate 90 degrees.
ros2 action send_goal /backup nav2_msgs/action/BackUp "{target: {x: 0.3}, speed: 0.1}"
# Drive back 30 cm.
ros2 service call /local_costmap/clear_entirely_local_costmap nav2_msgs/srv/ClearEntireCostmap
# Clear the local costmap.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

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 rotates about 90 degrees

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# TODO: send the Spin action a target_yaw of 1.57 radians

Expected output

The robot rotates about 90 degrees
Stuck? Show the full solution (try it yourself first)
ros2 action send_goal /spin nav2_msgs/action/Spin "{target_yaw: 1.57}"

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Test each recovery by hand, then trap the robot in a narrow gap in simulation and watch which recoveries the tree runs and in which order.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.