0
$\begingroup$

Rosanswers logo

Hello,

It might be a total newbie question but I can't find the solution. I know that for Subscriber I can call callback_args but how am I suppose to do it for Services ?

I'm creating 2 services that are going to do the same thing BUT I want the callback to receive an id so it knows from which robot the service was required. I could pass it in the message but a better way to do it for me is to add a constant number associated to each call_back. I though of something like this but this abviously doesn't work :

service.append(rospy.Service(name, sentobject, self.check, callback_args=0) ) #Here 0 would be my id
service.append(rospy.Service(name, sentobject, self.check, callback_args=1) ) #Here 1 would be my id

With check being defined as :

def check(self, rep, robot_id):

How can I do it in python ?

Thanks a lot


Originally posted by Maya on ROS Answers with karma: 1172 on 2014-06-26

Post score: 1

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Hi,

i am not sure why you want two different services that perform the same function. Other than that i would do as you mentioned and include this data in the service definition (.srv file).

Edit:

When you look at the definition of the rospy services you can clearly see that there are no callback arguments expected. You can always use your Service definition in the .srv you are using to add a field like an string or int for the robot ID to be passed to the service call. For example you could take the AddTwoInts.srv

int64 a
int64 b
---
int64 sum

and modify it to look like this

int64 a
int64 b
int64 robotid
---
int64 sum

In your service callback you can than access this value and change the response message accordingly. For this you should rename the file, add it to the CMakeLists.txt in the package the .srv file is in and make a catkin_make to generate the needed include files.


Originally posted by bajo with karma: 206 on 2014-06-26

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by Maya on 2014-06-30:
That's exactly what I end up doing. Why can"t we use argument in services ? :/

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.