[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)

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.


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:

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

Including/Importing Services


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

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


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


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

2024-06-22 14:20