4: Services
What are ROS services, and why do we need them? How are they different?

What is a service?

  • Very related to a topic
  • Instead of “subscribing” and “publishing”
  • Services are a classic “client/server”
  • Node “N” asks a question of Service “S”
  • Service “S” responds immediately with a result
  • Services are good for questions that have immediate answers
  • They are synchronous whereas topics are asynchronous
  • Another way to think of a service is as a “Remote Procedure Call”

Defining a Service

Defining the inputs and outputs
  • Analogous to defining the “method signature” of the service
  • Very reminiscent of how Topic messages were defined
  • You will find this example in the rosbook github package
Example: WordCount.srv
  • WordCount.srv shows that it will take one input (string) and give one output (uint32)
  • Notice some hairy tweaks of both CMakeLists.txt and package.xml
  • Once done run catkin make ($ cm) to process and do the code generation
  • You will immediately be able to see the service declared with $ rossrv show WordCount
Defining the logic
  • It looks a lot like a node subscribing to a topic
  • Big difference is that it has a ‘return’ statement
  • In this example, it returns an integer equal to the number of words
  • The money quote: return len(request.words.split())
  • Notice the two ways of returning the result
  • Everything else is plubming
Checking if it worked
  • Run the service with a standard rosrun command
  • Remember our package is called rosbook
  • Remember that you must have $ roscore running!
  • rosrun rosbook service_server.py
  • You can tell that it worked:
    1. It shows up in $ rosservice list
    2. It shows up in $ rosservice info word_count
A client Node
  • It’s a normal node (nothing magic about it)
  • It includes code to send a request to the service (ask it a question)
  • The code is more, but the effect is of a function call
  • Impressive because it is inter-process and even inter-host - i.e. can be over the internet