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

How do ros service responses work.

asked 2011-07-15 01:51:41 -0500

boy gravatar image

updated 2011-07-18 04:05:40 -0500

hello everyone,

I have the following problem:

When I can call a service"server node" to calculate something(for simplicity float_A/float_B), how can i use the response to do something in my client node(for instance make a dissension in my state_2). The code of the server node and client node is given below. (.srv file float32[] a ------- float32[] sum)

#client node
class State_2(smach.State):

def __init__(self):
    smach.State.__init__(self, outcomes=['outcome1','outcome2'])

self.check = False
self.subscriber = rospy.Subscriber("chatter",Array, self.callback)

def callback(self,data):
self.ranges_list = list(data.ranges)

def execute(self, userdata):
rospy.sleep(1.0)
rospy.wait_for_service('check_teammember_behind_turtle')

check_teammember_behind_turtle = rospy.ServiceProxy('check_teammember_behind_turtle', teammember)
check_teammember_behind_turtle(self.ranges_list[0:3])
## THINK I MISS HERE SOMETHING##
    ## MAYBE YOU CAN HELP ME##


rospy.loginfo('Executing state STATE_2')

if self.check: 
    return 'outcome1'
else:
    return 'outcome2'

## CODE SERVER ##
def handle_teammember(req):
list_teammember = list(req.a)
tx = list_teammember[0]/list_teammember[1]
ty = list_teammember[1]/list_teammember[2]
if (tx>ty):
tx_list = [tx]
print ("tx")
else:
tx_list = [ty]
print ("ty")

return teammemberResponse(tx_list)

def add_two_ints_server():
rospy.init_node('add_two_ints_server')
s = rospy.Service('check_teammember_behind_turtle', teammember, handle_teammember)
print "Ready to add two ints."
rospy.spin()
edit retag flag offensive close merge delete

Comments

Please try to make your head line informative next time. (I fixed this one)
Asomerville gravatar image Asomerville  ( 2011-07-18 04:06:32 -0500 )edit
I will do that, SORRY!
boy gravatar image boy  ( 2011-07-18 05:23:19 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2011-07-15 02:37:27 -0500

dornhege gravatar image

updated 2011-07-17 21:32:04 -0500

The response is returned by the call. See this tutorial: http://www.ros.org/wiki/ROS/Tutorials/WritingServiceClient(python)

You need to adapt this code to yours:

rospy.wait_for_service('add_two_ints')
try:
    add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
    resp1 = add_two_ints(x, y)
    return resp1.sum
except rospy.ServiceException, e:
    print "Service call failed: %s"%e
edit flag offensive delete link more

Comments

Thanks for the answer, but I still don't have it yet. Could you give me some hits or small example?? Reminder: I have to make a decision with the info obtained from server
boy gravatar image boy  ( 2011-07-17 20:47:54 -0500 )edit
thats what I thought too, but I cannot adapt the code without getting a lot of errors. Maybe It's my lack in experience in python and ROS. Could you give me some more hints??
boy gravatar image boy  ( 2011-07-17 22:32:48 -0500 )edit
If the code above runs, you just change one line to: resp = check_teammember_behind_turtle(self.ranges_list[0:3]) and then handle the response resp. Obviously the try/catch would be advisable, but to just get the information this should work.
dornhege gravatar image dornhege  ( 2011-07-17 23:06:01 -0500 )edit
I have it working :) Thanks
boy gravatar image boy  ( 2011-07-17 23:38:41 -0500 )edit
If it's working, accept the answer...karma karma karma!
Mac gravatar image Mac  ( 2011-07-20 06:28:22 -0500 )edit

Question Tools

Stats

Asked: 2011-07-15 01:51:41 -0500

Seen: 3,894 times

Last updated: Jul 18 '11