CodeTheRobot

Ubuntu Linux for Robotics · Lesson 40 of 96

ROS 2 command-line tools

ros2 topic list, ros2 node info, ros2 service call, and other everyday commands.

Estimated time · 60–90 min

Learning goals

What you will understand

  • Explain ros 2 command-line tools 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

The ros2 command inspects a running system. ros2 node list and ros2 node info /name show nodes and what they publish, subscribe to and serve. ros2 topic list -t, ros2 topic echo, ros2 topic hz and ros2 topic pub work with topics. ros2 service list, ros2 service call and ros2 interface show work with services and message definitions.

Tab completion works for every ros2 command once you have sourced ROS. Use these tools before reading code: they tell you what is really running. If a topic is missing, the problem is upstream of any callback.

Guided lesson

Work through these steps

  1. 1.

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

  2. 2.

    Read the ros 2 command-line tools 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 run demo_nodes_cpp talker
# Start a demo node in one terminal.
ros2 node list
# In a second terminal: list running nodes.
ros2 node info /talker
# See its publishers, subscribers and services.
ros2 topic list -t
# List topics with their message types.
ros2 topic echo /chatter
# Print the messages as they arrive.
ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts '{a: 2, b: 3}'
# Call a service (start the add_two_ints_server demo first).

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 · The robot's computer

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.

robot computercpumemdiskdriverstoppedplannerstoppedloggerstoppedsafetystoppedTHE ROBOT'S COMPUTER

Demo for ROS 2 command-line tools. Expected result: sum: 5

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.

# Terminal A: ros2 run demo_nodes_cpp add_two_ints_server
# TODO: call the service /add_two_ints with a = 2 and b = 3

Expected output

sum: 5
Stuck? Show the full solution (try it yourself first)
# Terminal A: ros2 run demo_nodes_cpp add_two_ints_server
ros2 service call /add_two_ints example_interfaces/srv/AddTwoInts "{a: 2, b: 3}"

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 the talker and the add_two_ints server, then use only ros2 commands to list all nodes, topics and services, echo one topic, measure its rate, and call the service with your own numbers.

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.