[ROS2 How-to] ROS2 Topics vs Services vs Actions #5

ROS2 Topics vs Services vs Actions

Written by Ruben Alves

23/09/2022

What we are going to learn

  1. When to use Topics
  2. When to use Services
  3. When to use Actions

List of resources used in this post

  1. Use the rosject: https://app.theconstructsim.com/l/4dc71d42/
  2. The Construct: https://app.theconstructsim.com/
  3. ROS2 Courses –▸
    1. ROS2 Basics in 5 Days (Python): https://app.theconstructsim.com/#/Course/73
    2. ROS2 Basics in 5 Days (C++): https://app.theconstructsim.com/#/Course/61
    3. ROS2 Navigation training: https://www.theconstruct.ai/ros2-navigation-training/

Overview

ROS2, the improved version of ROS (ROS1), is quickly becoming the standard for developing robotics applications.

In this post, we will quickly compare the differences between three main ROS2 ways of interacting with nodes: topics, services, and actions.

We will have some insights on when to use each of them.

ROS Inside!

ROS inside

ROS inside

Before anything else, in case you want to use the logo above on your own robot, feel free to download it for free and attach it to your robot. It is really free. Find it in the link below:

ROS Inside logo

 

What is a ROS2 Action

Let’s assume you wish to wash your clothing. There are two possible ways you could go about it:

  1. Go to the Laundry service provider
    1. Put your clothes to wash.
    2. Wait until the clothes are washed.
    3. Get your clothes.
  2. If you have a washing machine at home:
    1. Put your clothes to wash
    2. Instead of waiting, you can do other things and leave the watching machine doing its jobs
    3. Check once in a while if the clothes are finished
    4. Do other things.
    5. Clothes are washed.

Option 1 is a blocking activity because you have to wait (in theory not able to do anything else) for the clothes to be washed, while option 2 is non-blocking because you can do some other things while your clothes are being washed.

This non-blocking is what defines an Action. If ROS2 Services are for instant request-responses, an Action is a task that may take a lot of time to be finished, and in the meantime, a robot (or you) is free to do other things and is also able to constantly check the status of the action.

Opening the rosject

In order to understand ROS2 Topics, Services, and Actions, we need to have ROS2 installed in our system, and it is also useful to have some simulations. To make your life easier, we already prepared a rosject with a simulation for that: https://app.theconstructsim.com/l/4dc71d42/.

You can download the rosject on your own computer if you want to work locally, but just by copying the rosject (clicking the link), you will have a setup already prepared for you.

After the rosject has been successfully copied to your own area, you should see a Run button. Just click that button to launch the rosject (below you have a rosject example).

Learn ROS2 Parameters - Run rosject

Learn ROS2 Topics vs Service vs Action – Run rosject (example of the RUN button)

After pressing the Run button, you should have the rosject loaded. Let’s now head to the next section to really get some real practice.

Launching the simulation

Let’s run a simulation before actually starting with the comparison between these three ways of interacting with ROS2 nodes.

Let’s start by opening a terminal:

 

Open a new Terminal

Open a new Terminal

 

Now, in the first terminal that we just open, let’s setup our environment:

cd ~/ros2_ws

colcon build

 

After our workspace is compiled, let’s source it:

source install/setup.bash

 

Now, we can start our simulation with the following commands:

cd ~/ros2_ws/src/t3_galactic

./start_sim_house.sh

 

If everything went well, you should have a simulation running, similar to what we have in the image below:

[ROS2 How-to] #5 - ROS2 Topics vs Services vs Actions - Simulation

[ROS2 How-to] #5 – ROS2 Topics vs Services vs Actions – Simulation

ROS2 Topics

Topics are the channel of communication between a Publisher node and a Subscriber node.

Let’s see an example of ROS2 Topics.

Let’s open a second terminal and run the following command:

ros2 topic pub /cmd_vel geometry_msgs/msg/Twist "{linear: {x: 0.2, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}" -1

 

The above command will command the robot to drive with a forward velocity of 0.2 m/s.

We can now create a subscriber to the /odom topic in a third terminal with the command below:

ros2 topic echo /odom

 

You should see odometry messages published to the /odom topic in the console

Many messages will start coming. You can stop the subscriber by pressing CTRL+C in the terminal where you launched it.

To stop the robot, we can use the command below in the second terminal:

ros2 topic pub /cmd_vel geometry_msgs/msg/Twist "{linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}" -1

 

When should you use a topic?

Topics are used for one-way communication. The publisher just sends messages, and the subscriber just “listen” to the messages being published.

Topics are useful when you need to react quickly, and useful also if you want to publish updates at a high frequency, like the values of the /odom topic, used to determine the position of the robot.

You can use topics to change the speed of a robot, for example, to accelerate or reduce its speed.

We could say that it is mostly used for real-time communication.
Topics provide a continuous flow of data, and can be used for one-to-many and many-to-many communications, which means you can have many publishers  publishing on a given topic, and many subscribers subscribed to a given topic.

ROS2 Services

ROS2 Services allow connections between a Service Client and a Service Server. It’s used basically for “instant questions and answers“, like “retrieving the current time“, for example, or “getting the current battery state” of the robot.

Let’s create a Service Server using the following commands in the second terminal:

ros2 run services_pkg service

 

Now that the service server is created, let’s create the Service Client in the third terminal, that sends a request to the Service Server:

ros2 service call /moving std_srvs/srv/Empty

 

If everything went well, the robot should have started moving in circles.

To stop the robot, we can make use of ROS2 Topic again, just like we did in the previous section, by running the command below:

ros2 topic pub /cmd_vel geometry_msgs/msg/Twist "{linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.0}}" -1

 

If we want to make the robot move to its initial position (reset the simulation), we can call the following service:

ros2 service call /reset_simulation std_srvs/srv/Empty

 

You can also call a service to get the list of models in the gazebo world:

ros2 service call /get_model_list gazebo_msgs/srv/GetModelList "{}"

 

When should you use a ROS2 Service?

ROS2 Services could be used ideally when the number of data requests is really small, especially if compared with ROS2 Topics.

With ROS2 Services, you have a request and a response, so, it is useful for communication that happens quickly.

ROS2 Actions

Somehow similar to ROS2 Services, for ROS2 Actions we need a server and a client. In this case, it would be an Action Server node and the Action Client node.

ROS2 Actions are more indicated for situations where you give a task to the robot, and you are not going to be there waiting until the task has finished. You could do other things while the robot is doing his work. An example of an action would be: Robot, please clean the house. You just give it a task, you don’t know when the task is going to finish, but you can have constant feedback on how the task is going.

Let’s start an action server, by running the following command in the second terminal:

ros2 launch turtlebot3_as action_server.launch.py

 

Now, in the third terminal, let’s send the goal to the Action Service:

ros2 action send_goal /turtlebot3_as t3_action_msg/action/Move "{secs: 20}"

 

Now, the robot will be moving for 20 seconds, but the Client used to send the goal has finished “instantly”. The terminal does not have to be stuck for 20 seconds until the robot finishes its task.

When should you use ROS2 Actions?

The ideal scenario when ROS2 Actions could be used is when the task takes several seconds to complete or minutes to complete.

ROS Actions are used mainly for the processing of longer tasks in the background.

Use it also for operations that might me terminated or canceled before completion.

 

Congratulations. You have just learned when to use Topics, Services, and Actions. We hope this post was really helpful to you. If you want a live version of this post, please check the video in the next section.

Youtube video

So this is the post for today. Remember that we have the live version of this post on YouTube. If you liked the content, please consider subscribing to our youtube channel. We are publishing new content ~every day.

Keep pushing your ROS Learning.

Related Courses & Training

If you want to learn more about ROS and ROS2, we recommend the following courses:

Topics:
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

0 Comments

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This