ROS Basics – Call Service using Service Client Program
Since there is a service and a service provider (service server) that we have already implemented, now there must be some client that uses the service.
Using a Service
The simplest way to use a service is to call it with “rosserive call” command. The structure of this command is as follows
rosservice call [Service_Name] [Arguments]
We have already use the command to call the service in previous blog as follows
rosservice call words_count “These are four lines”
We can use above commands to check whether any service is running properly or not.
However, the more useful way to use a service is to call it from another running node. Therefore, now we will call the service by developing a service client program.
Service Client Program
We will develop a client program for the same service “WordCount” that we have defined and Implemented in previous blogs.
Run following command to see the structure of the service message
rossrv show ros_basics/WordCount
We can see the structure in figure below
Where, the input (or request) of the service is a string type variable “words”.
While the output (or response) of the service is an unsigned integer type variable “count”.
Now we go to the package “ros_basics” and create the client node.
Run the commands
roscd ros_basics cd src ls
(ls command is run just to verify that the file is not already created)
Create the file “service_client.py”
give execute permission
chmod +x service_client.py
Open the file using “gedit” command and copy and paste following code in it.
#!/usr/bin/env python import rospy from ros_basics.srv import WordCount, WordCountRequest rospy.init_node('service_client') rospy.wait_for_service('words_count') word_counter = rospy.ServiceProxy('words_count', WordCount) request = WordCountRequest() request.words = "It is raining today" result = word_counter (request) #print result #Simply "print result" will also work, however we print more clearly print "Sentence is: ", request.words print "Words Count: ", result.count
All above executed commands are shown in figure below
The “service_client.py” file will look like in figure below
Save and close the file.
Explaining the Code in Python file “service_client.py”
We will explain the important code lines here
from ros_basics.srv import WordCount, WordCountRequest
#In above line, we are importing service message classes used by the service as already done before.
#In this line, we have initiated a ROS node named service_client
#In this line, the program is waiting for the service “words_count”. It is possible that the service is not yet advertised (For example service server node is not running). Therefore, the program will not move forward until the service is available. It is different from topic, where we can subscribe to a topic which is not yet advertised. However, we will get error if we try to access a service which is not yet available.
word_counter = rospy.ServiceProxy(‘words_count’, WordCount)
# In this line, we have created a connection to the service. The object “word_counter” will be used to communicate with service server.
request = WordCountRequest()
#In above line, we have created an object of type “WordCountRequest”
request.words = “It is raining today”
#We have assigned value to the variable words of object request (you may notice that “words” is the string type variable in input part of service message)
result = word_counter (request)
# We sent the request through connection object.
#Simply “print result” will also work, however we print more clearly as below
print “Sentence is: “, request.words
print “Words Count: “, result.count
# This line will print the sentence and received result.
Executing the file “service_client.py”
Now we will use the service by running the program file “service_client.py”
First run the master
Then we will have to start the service by initiating service_server node (For this run python program file servoce_server.py)
Run following command in new terminal
rosrun ros_basics service_server.py
As the service is running, the terminal will look like in figure below
Now we call the service using service client program
Run the command in new terminal
rosrun ros_basics service_client.py
Congratulations: You have successfully created a client program to use the service.
Note: You do not need to build the work space for running a node using rosrun command. However, if you face any problem, you can again build the workspace using “catkin_make” command and “source” it as done before.
You have learned how to define, implement and use a service. We have given you core concept of the service, which is second main communication mechanism in ROS. You can learn more about service by exploring its documentation.
You should use service when it is completed in short time. If you want to perform any task which may take longer or not bounded, then you should use another ROS communication mechanism called Action.
We will learn about Action in next blogs.