How to Create a ROS Service Server

How to Create a ROS Service Server

Written by Bayode Aderinola

23/08/2018

Hello ROS Developers! In this post (and embedded video), we will see how to create a ROS Service Server (or simply ROS Service) in just five minutes! We’ll see in it action as well as learn some theory behind it.

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 Server and call it!

ROS Services use a client/server approach. A server provides services, a client calls the server to use the services.

Here, we’ll create a simple service server based on Python and then call it from the command line, all in five easy steps! (In another post, we’ll see how to call the service server from Python code).

(1) On Shell #1, change to the source directory and create an executable python file (since this is a simple demo, we are not creating a ROS package):

user:~$ cd catkin_ws/src
user:~/catkin_ws/src$ touch sos_service.py
user:~/catkin_ws/src$ chmod +x sos_service.py
user:~/catkin_ws/src$

(2) Open sos_service.py in the Editor and copy-paste the following code into it.

#! /usr/bin/env python
import rospy                                      # the main module for ROS-python programs
from std_srvs.srv import Trigger, TriggerResponse # we are creating a 'Trigger service'...
                                                  # ...Other types are available, and you can create
                                                  # custom types
def trigger_response(request):
    ''' 
    Callback function used by the service server to process
    requests from clients. It returns a TriggerResponse
    '''
    return TriggerResponse(
        success=True,
        message="Hey, roger that; we'll be right there!"
    )

rospy.init_node('sos_service')                     # initialize a ROS node
my_service = rospy.Service(                        # create a service, specifying its name,
    '/fake_911', Trigger, trigger_response         # type, and callback
)
rospy.spin()                                       # Keep the program from exiting, until Ctrl + C is pressed

(3) Run the service server in Shell #1:

user:~/catkin_ws/src$ ./sos_service.py # press enter

(4) In Shell #2, see a list of available services, you should find /fake_911:

user:~$ rosservice list
...
/fake_911
...
user:~$

(5) Call your service in Shell #2:

user:~$ rosservice call /fake_911 "{}"
success: True
message: "Hey, roger that; we'll be right there!"

Let’s wrap up with a bit of “small talk” in the next section.

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 shell!

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!

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

2 Comments

  1. Ajith

    Thanks Bayode. Great doc. Is it possible for us to get actual topic message using ros service server?

    Reply
    • Bayode Aderinola

      Hey Ajith!

      Thanks for your response and kind words.

      Not quite sure what you mean by “get actual topic message using ros service server”, but if you mean that can we use a ROS service server to get the message published to a topic, the answer is yes! In theory, the service server can get you anything “gettable”, process it whichever way you want and then return a response.

      Reply

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This