Learn how to create a ROS Service Client in less than five minutes, and understand the relationship between a ROS Service Server and a ROS Service Client.
Let’s go!
Step1: Create an account and/or Login to Robot Ignite Academy (RIA)
On RIA, you get access to the best online ROS courses and environment. No need to install and set up ROS locally – the only thing you need is a browser!
- Create an account and/or login here.
- Launch the ROS Basics in 5 Days Python course.
- You’ll now have access to the simulation screen with a Notebook, an Editor, four Shells, and a Simulation Window. We are only using the Editor and Shells for this demo.
Step 2: Create a ROS Service Client and execute it!
ROS Services use a client/server approach. A server provides services, a client calls the server to use the services. In this post, we will create a Python-based client that will call a Python-based server in five easy steps minus one!
(1) I wrote about how to create a ROS service server in another post. We will use the server code here. Please follow steps 1 to 5 of the section named “Step 2: Create a ROS Service Server and call it” in that post to create and run the service server.
(2) Create a sos_service_client.py
file on Shell #3:
user:~$ cd catkin_ws/src user:~/catkin_ws/src$ touch sos_service_client.py && chmod +x sos_service_client.py
(3) Open sos_service_client.py
in the Editor and copy-paste the following code into it.
#! /usr/bin/env python import rospy from std_srvs.srv import Trigger, TriggerRequest # init a node as usual rospy.init_node('sos_service_client') # wait for this sevice to be running rospy.wait_for_service('/fake_911') # Create the connection to the service. Remember it's a Trigger service sos_service = rospy.ServiceProxy('/fake_911', Trigger) # Create an object of the type TriggerRequest. We nned a TriggerRequest for a Trigger service sos = TriggerRequest() # Now send the request through the connection result = sos_service(sos) # Done print result
(4) Run sos_service_client.py
in Shell #3:
user:~/catkin_ws/src$ ./sos_service_client.py success: True message: "Hey, roger that; we'll be right there!"
Easy-peasy, right? Let’s consolidate the learning in the next step.
Step 3: Master the Concept: Creating a ROS Service Server
- A ROS Service Server accepts a request and returns a response. In this case, our server is the Python code.
- A ROS Service Client makes requests to a ROS Service Server. Our “client” in this case was the Python script!
Please see the inline comments for an explanation of the code.
Done!
Extra: Video
Prefer to see the ‘sights and sounds’ version of this post? We made this video just for you!
Feedback
If you are interested in this topic, please check our ROS Basics in 5 Days course where you’ll learn how to create topic, service, and action in ROS.
Did you like this post? Please leave a comment on the comments section below, so we can interact and learn from each other. Thank you!
Thanks Bayode. Great doc. Is it possible for us to get actual topic message using ros service server?
Does it work if I have a service server in python and a client in roslibjs connected through rosbridge?
Does it work if I have a service server in python and a client in roslibjs connected through rosbridge?