• +4917626969472
  • info@ur-techpartner.de

ROS Basics (08/17) – Define Service

Robot Operating System(ROS1/ROS2) ROS Programming

ROS Basics – Define Your Own Service

As we have already define our own message in previous blogs. In this blog we will learn how to define a new service.

You may remember that the message file was defined in a message definition file with extension “.msg”. However the extension of service-definition file is “.srv”.

Also the service files are defined in a directory named “srv” of the package.

You may create a new package and create a directory name “srv” inside the package. However, since we have already created a package “ros_basics”, we will define service for it.

Create a Service Definition File

Run in terminal

roscd ros_basics

(You will move to the package “ros_basics”)

Run the command

ls

(See the contents to verify if there is no “srv” folder yet)

Run the command

mkdir srv

(This will create “srv” directory)

Now run ls command again

ls

You can see there is “srv” directory

To go to srv directory, run the command

cd srv

Inside the directory, create a file “WordCount.srv”

Run the command

touch WordCount.srv

Run following command to give execute permission

chmod +x WordCount.srv

Open the file using gedit command

gedit WordCount.srv

Write following lines into the file

string words
---
uint32 count

All above executed commands are shown in figure below

The file “WordCount.srv” will look like as shown in figure below

Explanation

The file “WordCount.srv” contains two parts. First part is a string type variable named “words”. It is called request. We can also say it input. Because during a service call, these parameters are passed as an input (or as a request). There may be more variables before dashes (—) . Second part is unsigned integer variable of type uint32 named “count”. This part is called response. It can also be called output. Both parts are separated by three dashes (—).

It is possible that there is only a request part and no response. In this case only variables before — will be defined, while after — it will be empty. However you have to enter dashes (—) in any case.

If there is no request or response part in the service message (in case of empty service file), still there will be dashes (—).

Note: There is actually a ROS service definition file named “Empty.srv” which has no request or response part. You can view it by running the command

rossrv show std_srvs/Empty

You will observe that there is only dashes (—) in the service file.

Modify CMakeLists.txt and package.xml files for defined Service Compilation

Now we come back to the service file “WordCount.srv” that we have just defined. In order to compile this service message file, we need to modify “CMakeLists.txt” and “package.xml” files as we have already done in topic: define message blog.

For this go to the package “ros_basics”

Run the command

roscd ros_basics

Run ls command to see the contents

ls

You can see the file “CMakeLists.txt” and “package.xml” inside the directory

Modify “CMakeLists.txt”

Run following command to modify the file

gedit CMakeLists.txt

The file will be opened as shown in figure below, you will see most of the code lines are commented. You will have to make changes in following four functions. You do not need to write the function names (they are already there), You only need to find them and uncomment them (if they are commented). The four functions are

1. find_package()
2. add_service_files()
3. generate_messages()
4. catkin_package()

You can notice that three of these functions are same as we modified in topic message section.

Only one function is different which is “add_service_files ()”. While it was “add_message_files()” in case of message compilation. You can also see it in figure below

The above four functions should be modified like this

1.
find_package(catkin REQUIRED COMPONENTS
rospy
std_msgs
message_generation # Add “ message_generation” here, after other packages
)

2.
add_service_files(
FILES
WordCount.srv
# Add “WordCount.srv” here, and uncomment function & parenthesis
)

3.
generate_messages(
DEPENDENCIES
std_msgs
) #Uncomment as shown (if commented)

4.
catkin_package(
CATKIN_DEPENDS rospy std_msgs message_runtime
)

#In above line “CATKIN_DEPENDS rospy std_msgs message_runtime”
# The part “std_msgs message_runtime” is added previously for message compilation. Therefore we kept it as it is.
#Otherwise for defining service, the line “ CATKIN_DEPENDS rospy” is enough.

When all four functions are modified accordingly.

Then save the file and close it.

Modify “package.xml”

Run following command to modify the file

gedit package.xml

You need to find the part of code lines which are starting with the line

<buildtool_depend>catkin</buildtool_depend>


In the end of these lines
Add following three lines

<build_depend>message_generation</build_depend>
<build_export_depend>message_runtime</build_export_depend>
<exec_depend>message_runtime</exec_depend>

You can see it in the figure below

(This way we add build and execution dependencies for our defined messages/services).

Save and close the file.

Build the “catkin_ws” Workspace

Now the last step is to build the “catkin_ws” workspace

Go to the catkin_ws directory

Run command

roscd ~/catkin_ws

Run the command

catkin_make

(This will build all packages)

Run the command

source devel/setup.bash

(Don’t forget to execute above command)

All commands executed above are shown in figure below

After executing above commands, your service message is ready to use.

We can verify it by executing the following command

rossrv show ros_basics/WordCount

(or following command will do the same thing as that of above command)

rossrv show ros_basics/WordCount.srv

Congratulations! You have successfully customized your own service

You can also confirm that if your defined service is available in ROS service list

Run following command

rossrv list | grep WordCount

Yes, it is also present in the list as shown in figure below

After executing catkin_make, it will generate three classes which are “WordCount”, “WordCountRequest” , and “WordCountResponse”. These Classes will be used to interact with the services.

In next blog we will create service server node and service client node, that will use our defined service “WordCount”

No comments