ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

design question: topic vs service?

asked 2014-07-30 13:31:15 -0500

d gravatar image

updated 2014-07-30 15:27:40 -0500

topics seem like the best way for storing "states" of the robot (stateful). so that we can monitor all states/components of the robots like position, temperature, voltage, etc. that makes sense to me.

however, i also see a lot of people using topics as a way to send instructions/commands to the robots (like the topic /head_pan_joint/command for sending commands to servos, like /cmd_vel for sending movement instructions to mobile base, etc). in this case, it doesn't make sense to me.

it seems to me that this should be designed as a service (stateless). if you want to send a command to servo, create a service at the servo level that can be called upon. if you want to send a command to mobile base, create a service at the mobile base that can be called upon.

what are the advantages of designing things like /cmd_vel and /head_pan_joint/command as topics? what are the disadvantages of desigining them as services?


edit retag flag offensive close merge delete



Why are topics supposed to be stateful and services stateless?

dornhege gravatar image dornhege  ( 2014-07-30 14:24:08 -0500 )edit

that's just how i see they could work. topics seem to be best used for storing "robot states" (like temperature, position, etc). so that's what i meant by 'stateful'. services seem to be best for handling actions and don't need to store any states. would love to hear your thoughts.

d gravatar image d  ( 2014-07-30 15:10:23 -0500 )edit

and, i guess actionlib was created as something in between to feed "states" on "service" calls? i'm new to ros... so i would love to understand how/why these fundametal ros concepts were designed and intended to be used. would love to hear your thoughts.

d gravatar image d  ( 2014-07-30 15:21:11 -0500 )edit

thanks. that design pattern doc is really helpful!

d gravatar image d  ( 2014-07-31 08:23:39 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2014-07-30 14:16:20 -0500

I tend to consider Topics for use in any communication that is high speed or synchronous in nature. That communication can be either data or commands. But only commands that don't need a success/fail/etc. type of response.

I tend to consider Services only for commands that require an explicit response that needs to be acted upon. The tutorial for Services actually adds two numbers and returns the answer, so it is very useful in situations where you want a subroutine or a function that is standalone and callable by any local or remote ROS node. I would avoid using a service for commands that are high speed, though.

edit flag offensive delete link more


thanks kurt. why do topics work better for high-speed communication?

d gravatar image d  ( 2014-07-30 15:15:03 -0500 )edit

ps: i don't see a strong reason to use services just because of an explicit response. we can easily create an additional topic to do just that. for example, publish to request_topic and subscribe to response_topic. there must be a stronger reason why ros is designed with both topics and services.

d gravatar image d  ( 2014-07-30 15:15:08 -0500 )edit

How would you know that the response on that topic is for you? Also you'd have to wait for the response manually, you'll get responses for others. This would require a lot of boilerplate code to make it a unique request-respone pattern. Essentially that is done within the framework now.

dornhege gravatar image dornhege  ( 2014-07-31 09:42:15 -0500 )edit

thanks christian again. i understood much better these concepts from the design doc you mentioned earlier. much appreciated for all your help!

d gravatar image d  ( 2014-07-31 10:23:00 -0500 )edit

I said topics work better for high speed only because they are one way communications with no response and services will always answer each call with a response, whether your code uses the response or not, it uses CPU.

Kurt Leucht gravatar image Kurt Leucht  ( 2014-08-11 19:42:16 -0500 )edit

answered 2014-07-30 16:10:41 -0500

paulbovbel gravatar image

updated 2014-07-31 09:31:54 -0500

From the data receiver's perspective, topics are fire and forget communication, and are asynchronous by default - that is why callbacks need to be setup at receiving nodes.

Services are synchronous - data is requested, and then the receiver blocks until the data is received. While potentially this could be used for commands (with the data representing success and failure), the blocking nature makes this awkward in most use cases.

Basic commands, with no feedback required, are implemented using topics for their simplicity. Commands that require feedback/state tracking, and take a non-trivial time to complete, are implemented using actionlib.

Take a look at move_base in the navigation stack - a 'client' requesting the robot move would likely want to know the robot's progress throughout the move action, but would not want to halt operation (block) while waiting, hence actionlib. For a fire-and-forget movement command, the move_base_simple topic exists, but here you would have no idea if the robot ever successfully moves or not, and is not suitable for the complexities of most high-level planners.

edit flag offensive delete link more


thanks paul. really appreciated it. following your explaination, it seems like actionlib and topics can handle pretty much everything. why do we even need to use services? can you give me a couple of real-world use cases that we must use services vs. topics / actionlib?

d gravatar image d  ( 2014-07-30 16:34:57 -0500 )edit

Say one node is maintaining some giant datastore and another node needs to retrieve a small subset of that data. Actionlib is a lot of boilerplate and overhead when all you need a simple request - response. Bare topics give you no control over the contents of the data. Service API gives you a nice middle ground.

paulbovbel gravatar image paulbovbel  ( 2014-07-30 18:46:18 -0500 )edit

that makes sense. thanks paul.

d gravatar image d  ( 2014-07-31 01:14:53 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2014-07-30 13:31:15 -0500

Seen: 3,272 times

Last updated: Jul 31 '14