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

How do I know the variable names in a ros service handler

asked 2014-06-25 10:09:01 -0500

RigorMortis gravatar image

In the beginner tutorial here:

def handle_add_two_ints(req):
print "Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b))
return AddTwoIntsResponse(req.a + req.b)

I understand that the message received will get passed to the 'handle_add_two_ints' function as the object 'req' -- what I do not understand is where the naming convention comes from. They seem to have pulled '.a' and '.b' out of thin air. Can someone please clarify or point me to a site that does? What if I am passing many variables of different types... will they always be alphabetically named?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-06-26 00:25:13 -0500

janindu gravatar image

updated 2021-06-26 07:10:31 -0500

An introduction tutorial to services and messages is available here

In the Beginner Tutorial you have linked, under

  1. Writing a Service Node

it is clearly mentioned that you should have already completed the introduction to services and messages tutorial!

Edit following @gvdhoom 's feedback.

The service interfaces are defined in .srv files in a package, which are then compiled into .h files by the ROS client libraries. Each service interface consists of two components. Request and Response. For an example, the AddTwoInts interface is defined as

int64 a
int64 b
---
int64 sum

The request component is defined above --- in the .srv file and the response component below it. This is why the service server accesses the two integers passed by the client as req.a and req.b. The client, once the response is received into the variable res, will access the sum as res.sum.

You can define your own service interface with your own field names and your own data types. The data types you can use to define custom message and service interfaces are described here. How to define custom service interfaces is explained here.

For an example, I here is a custom service interface I have defined.

string command

uint8[] ids
string addr_name
int32 value
---
bool comm_result
edit flag offensive delete link more

Comments

1

This isn't an answer to the question just yet. @janindu: I understand what you're trying to say, but to make it completely clear, could you add the actual answer to your post here? That would be appreciated.

gvdhoorn gravatar image gvdhoorn  ( 2021-06-26 02:19:32 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-06-25 10:09:01 -0500

Seen: 153 times

Last updated: Jun 26 '21