Writing a Service Node
Here we'll create the service ("Add_two_ints_server") node which would receive both ints and return the sum.
Change directory to the Beginner_tutorials package, your created in the earlier tutorial, creating a package:
$ ROSCD Beginner_tutorials
Sure you have followed the directions in the previous tutorial for creating the service needed in this Tutoria L, creating the addtwoints.srv (be sure to choose the right version of the build tool you ' re using at the top of the wiki page in The link).
Create the scripts/add_two_ints_server.py file within the Beginner_tutorials package and paste the following insi De it:
#!/usr/bin/Envpythonfrom beginner_tutorials.srv Import*Import Rospydef handle_add_two_ints (req): Print"returning [%s +%s =%s]"% (Req.a, req.b, (req.a +req.b)) return Addtwointsresponse (req.a+req.b) def add_two_ints_server (): Rospy.init_node ('Add_two_ints_server') s= Rospy. Service ('add_two_ints', addtwoints, handle_add_two_ints) print"Ready to Add the ints."Rospy.spin ()if__name__ = ="__main__": Add_two_ints_server ()
Don ' t forget to make the node executable:
chmod +x scripts/add_two_ints_server.py
Code Explained
We declare our node using Init_node () and then declare our service:
s = rospy. Service ('add_two_ints', addtwoints, handle_add_two_ints)
This declares a new service named add_two_ints with the addtwoints service type. All requests is passed to handle_add_two_ints function. handle_add_two_ints is called with instances of Addtwointsrequest and returns instances of Addtwoin Tsresponse.
Just like with the subscriber example, rospy.spin () keeps your code from exiting until the service is shutdown.
Writing the Client Node
#!/usr/bin/Envpythonimport sysimport rospyfrom beginner_tutorials.srv Import*def add_two_ints_client (x, y): 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.sumexcept Rospy. Serviceexception, E:print"Service call failed:%s"%edef usage (): Return"%s [x y]"%sys.argv[0]if__name__ = ="__main__": ifLen (sys.argv) = =3: x=int(sys.argv[1]) y=int(sys.argv[2]) Else: Print usage () sys.exit (1) Print"requesting%s+%s"%(x, y) print"%s +%s =%s"% (x, y, add_two_ints_client (x, y))
Don ' t forget to make the node executable:
chmod +x SCRIPTS/ADD_TWO_INTS_CLIENT.P
Code Explained
For clients you do have the to call Init_node (). We First Call:
Rospy.wait_for_service (' add_two_ints ')
This is a convenience method, blocks until the service named add_two_ints is available. Next we create a handle for calling the service:
Add_two_ints = Rospy. Serviceproxy ('add_two_ints', addtwoints)
We can use the handle just like a normal function and call it:
RESP1 = add_two_ints (x, y) return RESP1. sum
Because we ' ve declared the type of the service to being addtwoints, it does the work of generating the Addtwoint Srequest Object for-You (you ' re-free-to-pass in your own instead). The return value is an addtwointsresponse object. If The call fails, a rospy. Serviceexception May is thrown, so you should setup the appropriate try/except block.
Building Your nodes
Catkin_make
Try it out!
4 5
4+5459
Returning [4 + 5 = 9]
Writing a simple Publisher and subscriber (Python)