We love feedback, so, whether you like the video or not, please share your thoughts on the comments section below:)
Step1. Create a project in Robot Ignite Academy(RIA)
We have the best online ROS course available in RIA. It helps you learn ROS in the easiest way without setting up ROS environment locally. The only thing you need is a browser! Create an account here and start to browse the trial course for free now! We’ll use the ROS Basics in 5 Days course unit3 as an example today.
Step2. Create a package
We’ll put our new service message in a new package called services_tutorial
catkin_create_pkg services_tutorial rospy
Then we create the definition of the message called AddTwoNumbers.srv and put it into a folder called srv with the following content.
float32 a
float32 b
---
float32 sum
In the package.xml file, you have to uncomment the following part.
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!
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):
(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!
In today’s video, we are going to learn what are ROS Services, how to list them and how to call a ROS Service.
Step1. Create a project in Robot Ignite Academy(RIA)
We have the best online ROS course available in RIA. It helps you learn ROS in the easiest way without setting up ROS environment locally. The only thing you need is a browser! Create an account here and start to browse the trial course for free now! We’ll use the ROS Basics in 5 Days course unit 5 as an example today.
Step2. ROS service
Compare to the topic, the ROS service is a more syncronize why to communicate between nodes. You’ll also get feedback after a service is done. You can see all the service available with
rosservice list
and check any of them with
rosservice info /SERVICE
To call a service, use
rosservice call /SERVICE "SERVICE MSG"
For example, to call the /gazebo/delete_model service, we have to pass the model name you want to delete like the following.