ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The line

beginner_tutorials::AddTwoInts srv;

Is actually declaring a service message, which is defined here as:

int64 a
int64 b
---
int64 sum

A service message, by convention is split into a "request" and "response" part. For this message "a" and "b" are part of the request and "sum" is part of the response. To access these variables in code you can do like:

 beginner_tutorials::AddTwoInts srv;
 srv.request.a = 5;
 srv.request.b = 4;

Your service, defined by:

ros::ServiceServer service = n.advertiseService("add_two_ints", add);

Has a "callback" function, here named "add" whose signature by design explicitly names the the request AND response parts. That is why "add" takes two arguments.

bool add(beginner_tutorials::AddTwoInts::Request  &req,
               beginner_tutorials::AddTwoInts::Response &res)

For practical purposes however all you have to do to call the service is declare the client:

ros::ServiceClient client = n.serviceClient<beginner_tutorials::AddTwoInts>("add_two_ints");

And then call it with the SINGLE argument, srv:

 client.call(srv)

And after the call, it it was successful, you can access the result like this:

srv.response.sum

Does that help answer your question at all?

Cheers, JB