ROS Basics – Service
Now, we will learn about a second important ROS communication method called service. For this, we will learn how to define service and how to implement service (by implementing service server and service client). Services are suitable when we need to do some thing occasionally within a bounded amount of time.
Before starting to define and implement the service, we will go through an example of service available in tutorials.
Open the terminal and run the command
Run in a new terminal
rosrun rospy_tutorials add_two_ints_server
The node “add_two_ints_server” will start a service “add_two_ints”, which take 2 numbers as input, add them and send back the sum of the numbers as output.
We can check which services are running
Open a new terminal and run
You can see that the service “add_two_ints” is listed.
Now we call the service using command prompt.
Run in terminal
rosservice call /add_two_ints 4 5
Execute same command again with different argument
e. g. Run
rosservice call /add_two_ints 3 2
The terminal to run service server is displayed in figure below
In following figure, you can see that the service is called with two integers passed to the service as input and sum of the integers is received in response which is displayed on command prompt.
We can also look into more details about the service
Run the command
rosservice info /add_two_ints
We can see that the node “add_two_ints_server” is providing service.
The type of the service is “rospy_tutorials/AddTwoInts”, where “rospy_tutorials” is the package name and “AddTwoInts” is the file name in which service message is defined.
Run the command “rossrv info” to get the information about service message.
rossrv info rospy_tutorials/AddTwoInts
(An alternate command is “rossrv show” which does same as “rossrv info”)
You will get following result
The service message has two parts separated by three dashes (- – -).
The first part which includes two int64 type variables a and b are input (also called request) and second part which include one int64 type variable is output (also called response).
The above commands and their results are shown in figure below
As we have seen in previous blogs that the message is defined in “*.msg” file which is placed in “msg” directory of the package.
Similarly service (or service message) is defined in the file of extension “.srv” and is placed in “srv” folder of the package.
We can verify this by looking into the directories of the package “ros_tutorial” that we have used to execute the example.
Run in terminal
(You will go to the package “ros_totorials”
You will see that there is a folder “srv” in the package.
Go to the folder “srv”
You will see that there is a file “AddTwoInts.srv”.
Open the file to see the definition of the service
The commands executed above are shown in figure below
The service file will look like in figure below (This is same as we have seen in command terminal using “rossrv info” or “rossrv show” command).
We can notice that the topic message has one part, but service (or service message) has two parts (request and response) separated by – – -.
The Request is the part of service message that defines how you will call the service, which means what parameters you will pass as an input (e.g. variable a and b) to complete the task.
The Response is the part of service message that defines how your service will respond after completing the functionality. It may return a simple message as a string, an integer parameter or even it may return nothing. For instance, in above example “sum” parameter will be sent as a response which may also be considered as output.
In next blogs, we will define our own service and will implement it using service server and service client.