[Documentation] [TitleIndex] [WordIndex

Defining Custom Services

This tutorial will show you how to define your own custom service data types using the ROS Service Description Language.

Generating Services

Generating a service is easy. Simply place a .srv file inside the srv directory in a package. Then, in your CMakeLists.txt file, add the gensrv() macro somewhere after the call to rospack():

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
rospack(my_package)
gensrv()

The service file could look something like this:

string first_name
string last_name
---
uint32 last_score

Any .srv file in the srv directory will generate code for use in all supported languages.

The full specification for the service format is available at the Service Description Language page.

Exporting Services

A service isn't very useful if other packages cannot access it. Some languages require service be exported explicitly.

C++

For C++ we export services through the manifest's export tag. If you don't already have a C++ export tag, just exporting the services looks like this:

<export>
  <cpp cflags="-I${prefix}/srv/cpp"/>
</export>

Including/Importing Services

C++

Services are put into a namespace that matches the name of the package. ie.

   1 #include <roscpp_tutorials/TwoInts.h>
   2 
   3 roscpp_tutorials::TwoInts service;

or

   1 roscpp_tutorials::TwoInts::Request request;
   2 roscpp_tutorials::TwoInts::Response response;

Python

   1 from rospy_tutorials.srv import TwoInts
   2 
   3 request = TwoIntsRequest()
   4 response = TwoIntsResponse()

2024-11-23 14:37