Go to the documentation of this file.00001
00002 import roslib; roslib.load_manifest('blob3d')
00003 import rospy
00004 from pr2_python.head import Head
00005 from pr2_python import transform_listener
00006 from sensor_msgs.msg import PointCloud2
00007 from geometry_msgs.msg import Point
00008 from projector_interface import _point_cloud
00009 import numpy as np
00010 from blob3d.srv import LookAtBlob, LookAtBlobResponse
00011 from std_msgs.msg import Bool
00012
00013 head = None
00014 sub = None
00015 def cloud_cb(msg):
00016 global cloud_msg
00017 cloud_msg = msg
00018
00019 def handle_look_at_blob(msg):
00020 target_color = np.array([msg.blob_color.r, msg.blob_color.g, msg.blob_color.b])
00021 try:
00022 cloud = _point_cloud.read_points_np(cloud_msg)
00023 colors = _point_cloud.float2rgb(cloud[:,:,3])
00024
00025 color_dists = [np.linalg.norm(c-target_color) for c in colors[0,:]]
00026
00027 target_pt = cloud[0,np.argmin(color_dists),:3]
00028
00029 pt = transform_listener.transform_point('base_footprint', cloud_msg.header.frame_id, Point(*target_pt))
00030 head.look_at_relative_point(pt.x+0.2,pt.y-0.09,pt.z)
00031 except Exception, e:
00032 print e
00033 return LookAtBlobResponse(Bool(False))
00034 else:
00035 return LookAtBlobResponse(Bool(True))
00036
00037 if __name__ == '__main__':
00038 rospy.init_node('look_at_blob_server')
00039 head = Head()
00040 head.pointing_frame = '/narrow_stereo_optical_frame'
00041 sub = rospy.Subscriber('blob_cloud', PointCloud2, cloud_cb)
00042 s = rospy.Service('look_at_blob', LookAtBlob, handle_look_at_blob)
00043 rospy.spin()