add_two_ints_client.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2008, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Willow Garage, Inc. nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 #
00034 # Revision $Id$
00035 
00036 ## Simple demo of a rospy service client that calls a service to add
00037 ## two integers. 
00038 
00039 PKG = 'rospy_tutorials' # this package name
00040 
00041 import roslib; roslib.load_manifest(PKG) 
00042 
00043 import sys
00044 import os
00045 
00046 import rospy
00047 
00048 # imports the AddTwoInts service 
00049 from rospy_tutorials.srv import *
00050 
00051 ## add two numbers using the add_two_ints service
00052 ## @param x int: first number to add
00053 ## @param y int: second number to add
00054 def add_two_ints_client(x, y):
00055 
00056     # NOTE: you don't have to call rospy.init_node() to make calls against
00057     # a service. This is because service clients do not have to be
00058     # nodes.
00059 
00060     # block until the add_two_ints service is available
00061     # you can optionally specify a timeout
00062     rospy.wait_for_service('add_two_ints')
00063     
00064     try:
00065         # create a handle to the add_two_ints service
00066         add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
00067         
00068         print "Requesting %s+%s"%(x, y)
00069         
00070         # simplified style
00071         resp1 = add_two_ints(x, y)
00072 
00073         # formal style
00074         resp2 = add_two_ints.call(AddTwoIntsRequest(x, y))
00075 
00076         if not resp1.sum == (x + y):
00077             raise Exception("test failure, returned sum was %s"%resp1.sum)
00078         if not resp2.sum == (x + y):
00079             raise Exception("test failure, returned sum was %s"%resp2.sum)
00080         return resp1.sum
00081     except rospy.ServiceException, e:
00082         print "Service call failed: %s"%e
00083 
00084 def usage():
00085     return "%s [x y]"%sys.argv[0]
00086 
00087 if __name__ == "__main__":
00088     
00089     argv = rospy.myargv()
00090     if len(argv) == 3:
00091         try:
00092             x = int(argv[1])
00093             y = int(argv[2])
00094         except:
00095             print usage()
00096             sys.exit(1)
00097     else:
00098         print usage()
00099         sys.exit(1)
00100     print "%s + %s = %s"%(x, y, add_two_ints_client(x, y))


agentsystem_examples
Author(s): k-okada
autogenerated on Mon Oct 6 2014 12:08:22