CodeTheRobot

Ubuntu Linux for Robotics · Lesson 46 of 96

Services and actions

Calling services from the CLI, sending action goals, feedback and cancellation.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain services and actions 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

A service is a quick request and response; an action is a long-running goal with feedback and cancellation. On Ubuntu you can test both without writing a client: ros2 service call sends a request, ros2 action send_goal --feedback sends a goal and prints progress.

Use ros2 service list -t and ros2 action list -t to discover interface types, and ros2 interface show to see the fields you must send. Write the server first, test it from the CLI, then write the C++ client.

Guided lesson

Work through these steps

  1. 1.

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

  2. 2.

    Read the services and actions 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 service list -t
# List services and their types.
ros2 interface show example_interfaces/srv/AddTwoInts
# See the request and response fields.
ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts '{a: 4, b: 6}'
# Call it from the terminal.
ros2 action list -t
# List actions and their types.
ros2 action send_goal /fibonacci example_interfaces/action/Fibonacci '{order: 6}' --feedback
# Send a goal and watch the feedback.

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 · Messages between nodes

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.

requestresponseclientservercalls answered: 0MESSAGES BETWEEN NODES

Demo for Services and actions. Expected result: sum: 10

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: call /add_two_ints with a = 4 and b = 6

Expected output

sum: 10
Stuck? Show the full solution (try it yourself first)
ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 4, b: 6}"

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

Start a service server and an action server, test each with the CLI, write a C++ client for the service, and cancel an action goal from a second terminal.

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.