[ROS in 5 mins] 030 – How to compile ROS Service messages

[ROS in 5 mins] 030 – How to compile ROS Service messages

 

Hello ROS Developers!

In today’s video we are going learn how to create a ROS Service file, how to compile and how to import it.

For that we are going to use Robot Ignite Academy, which is the best tool if you want to Learn ROS Fast.

Before we start, if you are new to ROS, I highly recommend you to take any of the following courses on Robot Ignite Academy:

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.

...
<build_depend>message_generation</build_depend>
...
<run_depend>message_runtime</run_depend>
...

You’ll also need to change the following part in the CMakeLists.txt

...
find_package(catkin REQUIRED COMPONENTS
  rospy
  std_msgs
  message_generation
)
...
catkin_package(
  DEPENDS message_runtime
)
...
add_service_files(
  FILES
  AddTwoNumbers.srv
)
...
generate_messages(
  DEPENDENCIES
  std_msgs
)
...

Then we can finally compile the package.

cd ~/catkin_ws
catkin_make
source devel/setup.bash

You can now use your new message type in your code. For example, you can do the following to import it in a python script.

from services_tutorial.srv import AddTwoNumbers

Want to learn more?

You can learn more about ROS topic, service and action in our ROS Basics in 5 Days course.

 

Edit by: Tony Huang

How to Create a ROS Service Server

How to Create a ROS Service Server

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!

[ROS in 5 mins] 027 – What is ROS Service

[ROS in 5 mins] 027 – What is ROS Service

Hello ROS Developers!

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.

rosservice call /gazebo/delete_model "model_name: 'cafe_table'"

Want to learn more?

If you are new to ROS, I highly recommend you to take any of the following courses on Robot Ignite Academy:

ROS Basics In 5 Days (Python) 
ROS Basics In 5 Days (C++) 

We love feedback, so, whether you like the video or not, please share your thoughts in the comments section below.

 

Edit by: Tony Huang

Pin It on Pinterest