get_msg_example_client.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #
00003 # Copyright (c) 2010, Georgia Tech Research Corporation
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the Georgia Tech Research Corporation nor the
00014 #       names of its contributors may be used to endorse or promote products
00015 #       derived from this software without specific prior written permission.
00016 #
00017 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00018 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00021 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00025 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00026 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 #
00028 
00029 #  \author Jason Okerman (Healthcare Robotics Lab, Georgia Tech.)
00030 import roslib; roslib.load_manifest('pr2_clutter_helper')
00031 from sensor_msgs.msg import CameraInfo
00032 
00033 import sys
00034 
00035 import rospy
00036 from pr2_lcs_helper.srv import * #Get four .srv definitions
00037 
00038 ''' Usage: topic_name = None for default topic (listed in launch file), or 
00039         a string refering to ROS topic; msg_type is string refering to  
00040         returned msg type; and srv_type is a srv object (not string) generally
00041         named 'Get'+msg_type
00042     This client should be run to demonstrate success of the get_msg_server.py service.
00043     
00044 '''
00045 def get_single_msg(topic_name, msg_type, srv_type):
00046     service_name = 'get_'+msg_type
00047     rospy.wait_for_service(service_name)
00048     try:
00049         get_one_msg = rospy.ServiceProxy(service_name, srv_type)
00050         return get_one_msg(topic_name, msg_type)
00051     except:
00052         print 'Failed to acquire message from topic', topic_name
00053         return None
00054 
00055 def create_cam_model( info_msg):
00056     from image_geometry import PinholeCameraModel
00057     import numpy as np
00058     cam_model = PinholeCameraModel()
00059     
00060     #  <have an info message > 
00061     cam_model.fromCameraInfo(info_msg)
00062     print 'cam_model = ', np.array( cam_model.intrinsicMatrix() )
00063     print 'cam tf = ', cam_model.tfFrame()
00064     return cam_model
00065 
00066 def grab_tf(child_id = '/wide_stereo_optical_frame', frame_id = '/base_footprint'):
00067     #hopefully unused 
00068     topic = 'tf'
00069     tf_msg = None
00070     #tf publishes tfMessage which is a container holding a TransformStamped
00071     n=0
00072     # < grab tf messages until correct one is fetched >
00073     while not tf_msg:
00074         print 'try #', n; n+=1
00075         resp = get_single_msg(topic, 'tfMessage', GettfMessage) 
00076         tf_msg = resp.transform
00077         if tf_msg.header.frame_id == frame_id and tf_msg.child_frame_id==child_id:
00078             break;
00079         else: tf_msg = None
00080     print 'Got a tf', tf_msg, tf_msg.child_frame_id
00081     return tf_msg
00082 
00083 def usage():
00084     return "%s [x y]"%sys.argv[0]
00085 
00086 if __name__ == "__main__":
00087     topic = '/wide_stereo/right/camera_info'
00088     resp = get_single_msg(topic, 'CameraInfo', GetCameraInfo) 
00089     info_msg = resp.info
00090     
00091     topic = '/wide_stereo/right/image_rect_color'
00092     resp = get_single_msg(topic, 'Image', GetImage) 
00093     image_msg = resp.image
00094     print 'got image with h=', image_msg.height
00095 
00096     topic = '/wide_stereo/right/image_rect_color'
00097     resp = get_single_msg(topic, 'Image', GetImage) 
00098     image_msg = resp.image
00099 
00100     print "creating_cam_model"
00101     cam_model = create_cam_model(info_msg)
00102     
00103     print "Done ; next is to write transform"
00104     
00105 
00106 
00107 
00108   
00109     
00110 #---------------------------------------------    
00111 '''
00112     frame_id = 'base_footprint'
00113     acquisition_time = info_msg.header.stamp #type is ros.Time
00114     timeout = rospy.Duration(10) #at most wait 5 seconds
00115 
00116     do_some_tf():
00117         cam_frame = '/wide_stereo_optical_frame' #cam_model.tfFrame()
00118         other_frame = '/base_footprint'
00119         print "cam model frame :", cam_model.tfFrame()
00120         import tf
00121         #  <frame1 to frame2, time, timeout >
00122         try:
00123             rospy.init_node('client_calling_tf')
00124             print 'make listener'
00125             tf_listener = tf.TransformListener()
00126             print 'wait'
00127             latest = rospy.Time(0) #hack -- actually want time synchronized.
00128             #tf_listener.waitForTransform(cam_frame, other_frame, acquisition_time, timeout);
00129             #(trans,rot)
00130             print 'get tf now'
00131             print 'skipping transform'
00132             #transform = tf_listener.lookupTransform(cam_frame, other_frame);
00133         except:
00134             print "[get] TF exception: "
00135             return
00136         print "the transform is ", transform
00137         return transform
00138          
00139     do_some_tf()
00140     print "tf done"
00141 '''
00142     
00143     
00144     
00145     
00146     
00147     
00148 


pr2_clutter_helper
Author(s): Jason Okerman, Advisors: Prof. Charlie Kemp and Jim Regh, Lab: Healthcare Robotics Lab at Georgia Tech
autogenerated on Wed Nov 27 2013 11:53:06