If you are a beginner in ROS and want to learn how to create a ROS service, this tutorial is for you. I describe all the steps in details starting with creating the package and up to use the service and print the results.
The goal of this tutorial is to add additional information to the material support for ROS Service available on the ROS wiki page.
Few words about ROS Services
- ROS Services are operating at exactly the same periods with the remote procedure calls;
- a ROS Service has inputs and outputs; the inputs and outputs are defined similarly to ROS Messages;
- a ROS Service can have as input a number and as output a char;
- usually you should define a ROS Service when it’s needed to do something occasionally. For example, I’ll create a ROS Service for a robot of mine that will take a picture when encountering an obstacle;
Software and programming language:
- I’m using ROS Kinetic version 1.12.14
- The operating system is Linux Ubuntu 16.04 LTS
- Programming language: Python
Below I detailed all the steps to implement a practical example of a ROS Service. In this example, I’ll use a ROS service with a random number as input and an ON or OFF text as output.
1. Create ROS package
I want to run this exercise with the beginning, and the first step is to create a package to store the service files. This is not a requirement if you already have a package and you just want to create a ROS Service.
Navigate to the src directory of you workspace and type the below command:
catkin_create_pkg ros_service rospy
If you finish the above commands, you should have a new ROS package called ros_service. The first step is finished and next we will start working to create the ROS Service. The first step is to create the srv directory and the service definition file.
2. Create ‘srv’ directory and ‘srv file’
Starting with this step, we create the directory when we will create the service definition file with the inputs and outputs.
Navigate to the package directory when you want to create a ROS Service:
In the srv directory create a file called ServiceExample.srv and write the below three lines:
The three dashes mark the end of the input(s) and the beginning of the output(s).
With this last three lines in an ‘srv file’, we ended the service definition file. Next, is time to update two files needed to create the code and class definitions to work with the service that we define.
3. Update package.xml and CMakeLists.txt
Open the package.xml file and write the below lines. If you put the rospy dependency when you create the ROS package, you should already have into the file the rospy build depend and run depend. Otherwise, put all the below lines into the package.xml:
We’ve done with the package.xml file and we will go next to the CMakeFiles.txt. In this file we have to add the dependencies and the service file:
(Info: you should already have as comments most of these lists)
find_package(catkin REQUIRED COMPONENTS
Now we have to run the catkin_make command to generate the classes used to interact with the service.
4. Write callService.py
In the srv directory create a new file called callService.py. Make this file executable using the command “chmod +x callService.py”.
The file shows a simple server that accept as input one or zero and as output it returns ON or OFF.
#import the code generated by catkin.
#we need ServiceExample for the first message type,
#and the ServiceExampleResponse from the second message type from the ServiceExample.srv file
from ros_service.srv import ServiceExample, ServiceExampleResponse
5. Check the service and useful commands
The service is written, so we have to try it. The first step is to open a terminal and turn on the roscore. Then open a new terminal and run:
rosrun ros_service callService.py
Your service should be up and running. To check if the service works, we have to give it some inputs:
rosservice call service_example ‘0’
Your results should look like in the below image:
6. Use the service
To use the service we have to create a new file. Let’s create in the srv directory the file called ‘useService.py’. Don’t forget to make it executable.
from ros_service.srv import ServiceExample
#wait the service to be advertised, otherwise the service use will fail
#setup a local proxy for the service
#use the service and send it a value. In this case, I can send 1 or 0
#print the result from the service