Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
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 * 
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     
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     
00068     topic = 'tf'
00069     tf_msg = None
00070     
00071     n=0
00072     
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