$search
00001 #!/usr/bin/python 00002 import roslib 00003 roslib.load_manifest('cob_object_detection_fake') 00004 00005 import rospy 00006 00007 from cob_object_detection_msgs.srv import * 00008 from cob_object_detection_msgs.msg import * 00009 00010 from gazebo.srv import * 00011 00012 from numpy import * 00013 from numpy.linalg import inv 00014 00015 import tf 00016 00017 roslib.load_manifest('cob_script_server') 00018 from simple_script_server import * 00019 00020 00021 def handle_detect_object(req): 00022 name = req.object_name.data 00023 tf_listener = tf.TransformListener() 00024 rospy.loginfo("fake detection of %s",name) 00025 00026 # get world properties from gazebo, get all objects in gazebo (including ground_plance etc.) 00027 srv_get_world_properties = rospy.ServiceProxy('gazebo/get_world_properties', GetWorldProperties) 00028 res_world = srv_get_world_properties() 00029 00030 resp = DetectObjectsResponse() 00031 00032 # check if name is an object in gazebo, otherwise return empty response 00033 if name not in res_world.model_names: 00034 rospy.logerr("object %s not available in gazebo, available objects are %s", name, res_world.model_names) 00035 return resp 00036 00037 # get model state 00038 srv_get_model_state = rospy.ServiceProxy('/gazebo/get_model_state', GetModelState) 00039 res_model = srv_get_model_state(name,'robot::head_axis_link') 00040 trans_o_h = (res_model.pose.position.x, res_model.pose.position.y, res_model.pose.position.z) 00041 rot_o_h = (res_model.pose.orientation.x, res_model.pose.orientation.y, res_model.pose.orientation.z, res_model.pose.orientation.w) 00042 00043 frame_o_h = quaternion_matrix(rot_o_h) 00044 frame_o_h[0][3] = trans_o_h[0] 00045 frame_o_h[1][3] = trans_o_h[1] 00046 frame_o_h[2][3] = trans_o_h[2] 00047 00048 # print "frame_o_h" 00049 # print frame_o_h 00050 00051 tf_listener.waitForTransform('/head_color_camera_l_link', '/head_axis_link', rospy.Time(0), rospy.Duration(1)) 00052 (trans_h_l, rot_h_l) = tf_listener.lookupTransform('/head_color_camera_l_link', '/head_axis_link', rospy.Time(0)) 00053 00054 frame_h_l = quaternion_matrix(rot_h_l) 00055 frame_h_l[0][3] = trans_h_l[0] 00056 frame_h_l[1][3] = trans_h_l[1] 00057 frame_h_l[2][3] = trans_h_l[2] 00058 00059 # print "frame_h_l" 00060 # print frame_h_l 00061 00062 #frame = numpy.linalg.inv(frame) 00063 #rot = quaternion_from_matrix(frame) 00064 #tmp_mat = hsplit(frame, [3]) 00065 #trans = vsplit(tmp_mat[1], [3])[0] 00066 00067 # Transform 00068 frame_o_l = numpy.dot(frame_h_l, frame_o_h) 00069 # print "frame_o_l" 00070 # print frame_o_l 00071 00072 rot_o_l = quaternion_from_matrix(frame_o_l) 00073 tmp_mat = hsplit(frame_o_l, [3]) 00074 trans_o_l = vsplit(tmp_mat[1], [3])[0] 00075 00076 # Only valid for milk box 00077 box_min = (-0.06, -0.095, 0, 1) 00078 box_max = (0.06, 0.095, 0.2, 1) 00079 00080 # print "BOX MIN" 00081 # print box_min 00082 # box_min = numpy.dot(frame_o_l, box_min) 00083 # print box_min 00084 00085 00086 # print "BOX MAX" 00087 # print box_max 00088 # box_max = numpy.dot(frame_o_l, box_max) 00089 # print box_max 00090 00091 # compose Detection message 00092 detection=Detection() 00093 detection.pose.header.frame_id='/head_color_camera_l_link' 00094 detection.pose.pose.position.x = trans_o_l[0] 00095 detection.pose.pose.position.y = trans_o_l[1] 00096 detection.pose.pose.position.z = trans_o_l[2] 00097 detection.pose.pose.orientation.x = rot_o_l[0] 00098 detection.pose.pose.orientation.y = rot_o_l[1] 00099 detection.pose.pose.orientation.z = rot_o_l[2] 00100 detection.pose.pose.orientation.w = rot_o_l[3] 00101 detection.label = name 00102 detection.bounding_box_lwh.x = 0.5*(box_max[0] - box_min[0]) 00103 detection.bounding_box_lwh.y = 0.5*(box_max[1] - box_min[1]) 00104 # When the coordinate system has been defined correctly, 00105 # box_min[2] should be 0 and therefore we can shorten the 00106 # expression below 00107 detection.bounding_box_lwh.z = box_max[2] 00108 00109 # insert object to detection_list 00110 resp.object_list.detections.insert(0,detection) 00111 00112 rospy.loginfo("found object %s at:",name) 00113 print frame_o_l 00114 return resp 00115 00116 def detect_object(): 00117 rospy.init_node('detect_object') 00118 s = rospy.Service('detect_object', DetectObjects, handle_detect_object) 00119 00120 00121 rospy.loginfo("Fake object detection ready.") 00122 rospy.spin() 00123 00124 00125 if __name__ == "__main__": 00126 detect_object() 00127