1#!/usr/bin/env python
2
3from beginner_tutorials.srv import AddTwoInts,AddTwoIntsResponse
4import rospy
5
6def handle_add_two_ints(req):
7 print "Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b))
8 return AddTwoIntsResponse(req.a + req.b)
9
10def add_two_ints_server():
11 rospy.init_node('add_two_ints_server')
12 s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
13 print "Ready to add two ints."
14 rospy.spin()
15
16if __name__ == "__main__":
17 add_two_ints_server()
1#!/usr/bin/env python
2
3import sys
4import rospy
5from beginner_tutorials.srv import *
6
7def add_two_ints_client(x, y):
8 rospy.wait_for_service('add_two_ints')
9 try:
10 add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
11 resp1 = add_two_ints(x, y)
12 return resp1.sum
13 except rospy.ServiceException, e:
14 print "Service call failed: %s"%e
15
16def usage():
17 return "%s [x y]"%sys.argv[0]
18
19if __name__ == "__main__":
20 if len(sys.argv) == 3:
21 x = int(sys.argv[1])
22 y = int(sys.argv[2])
23 else:
24 print usage()
25 sys.exit(1)
26 print "Requesting %s+%s"%(x, y)
27 print "%s + %s = %s"%(x, y, add_two_ints_client(x, y))
28