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.
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:
Create a stub client file in the brain that does nothing but ask your package for a boolean and print it out (client example)
Create a ROS service .srv file in your package, placing it in a new directory called
srv
(service example).Remember to change the
CMakeLists.txt
andpackage.xml
files in your package (build file edit example)Create a stub server .py file in your package, simply force-returning
True
through the server (server example)Run
catkin_make
in the root directory of your workspaceTest 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
- Execute
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.