ROS Services

How Do They Work

ROS Services are a central feature of ROS that are particularly favored when it comes to interpackage communication. Essentially, a ROS Service is an independently existing function. A client package (usually the brain) sends parameters to another, which processes those parameters and decides on a return value. The client package receives that return value and can modulate its own behaviour accordingly.

In comparison to the publisher-subscriber mechanism, service-client communication is synchronous, meaning that the client blocks when calling the service until the server callback function is completed. The following chart visualizes the general mechanism of a ROS service.

_images/service.jpg

It usually requires some custom Python code to link up a new ROS Service into the brain, but using the variety of ROS Service examples in the Fezzik code as a template, it should hopefully not be too difficult.

For non-status checks, we have typically created additional client files in the brain itself. See, for example, grasping_handler_client.py. We would suggest you do the same.

Write Your Own Service

The most instructive method, especially if you are new to ROS Services, is to follow these steps:

  1. Create a stub client file in the brain that does nothing but ask your package for a boolean and print it out (client example)

  2. Create a ROS service .srv file in your package, placing it in a new directory called srv (service example).

  3. Remember to change the CMakeLists.txt and package.xml files in your package (build file edit example)

  4. Create a stub server .py file in your package, simply force-returning True through the server (server example)

  5. Run catkin_make in the root directory of your workspace

  6. Test your service with the following commands each in a new active terminal window. Do not forget to run source devel/setup.bash in the workspace root directory for each new terminal window.

    • Execute roscore in one terminal window
    • Execute rosrun <YOUR_PACKAGE_NAME> <YOUR_SERVER_FILE>.py in another terminal window
    • Execute rosrun brain <YOUR_CLIENT_FILE>.py in a third terminal window

That should be all you need to see your test output. If you succeed, then you need to simply increase the complexity of the logic of the server to make whatever checks you would like before returning its output value.