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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 from __future__ import division
00041 import roslib
00042 roslib.load_manifest('object_manipulator')
00043 import rospy
00044 from object_manipulation_msgs.srv import FindClusterBoundingBox, FindClusterBoundingBoxResponse, \
00045 FindClusterBoundingBox2, FindClusterBoundingBox2Response
00046 import object_manipulator.cluster_bounding_box_finder as cluster_bounding_box_finder
00047 import object_manipulator.cluster_bounding_box_finder_3d as cluster_bounding_box_finder_3d
00048 import scipy
00049 from object_manipulator.convert_functions import *
00050 import tf
00051
00052
00053 class ClusterBoundingBoxFinderServer:
00054
00055 def __init__(self, tf_listener = None):
00056 if tf_listener == None:
00057 self.tf_listener = tf.TransformListener()
00058 else:
00059 self.tf_listener = tf_listener
00060 self.cbbf = cluster_bounding_box_finder.ClusterBoundingBoxFinder(self.tf_listener)
00061 self.cbbf3d = cluster_bounding_box_finder_3d.ClusterBoundingBoxFinder3D(self.tf_listener)
00062
00063
00064 rospy.Service('find_cluster_bounding_box', FindClusterBoundingBox, self.find_cluster_bounding_box_callback)
00065 rospy.Service('find_cluster_bounding_box_3d', FindClusterBoundingBox, self.find_cluster_bounding_box_3d_callback)
00066
00067
00068 rospy.Service('find_cluster_bounding_box2', FindClusterBoundingBox2, self.find_cluster_bounding_box2_callback)
00069 rospy.Service('find_cluster_bounding_box2_3d', FindClusterBoundingBox2, self.find_cluster_bounding_box2_3d_callback)
00070
00071
00072
00073 def find_cluster_bounding_box_callback(self, req):
00074 rospy.loginfo("finding the bounding box for a point cluster")
00075 (box_pose, box_dims, error) = self.find_cluster_bounding_box(req)
00076 return FindClusterBoundingBoxResponse(box_pose, box_dims, error)
00077
00078
00079
00080 def find_cluster_bounding_box2_callback(self, req):
00081 rospy.loginfo("finding the bounding box for a PointCloud2 point cluster")
00082 (box_pose, box_dims, error) = self.find_cluster_bounding_box(req)
00083 return FindClusterBoundingBox2Response(box_pose, box_dims, error)
00084
00085
00086
00087 def find_cluster_bounding_box_3d_callback(self, req):
00088 rospy.loginfo("finding the bounding box for a point cluster (3D)")
00089 (box_pose, box_dims, error) = self.find_cluster_bounding_box_3d(req)
00090 return FindClusterBoundingBoxResponse(box_pose, box_dims, error)
00091
00092
00093
00094 def find_cluster_bounding_box2_3d_callback(self, req):
00095 rospy.loginfo("finding the bounding box for a PointCloud2 point cluster (3D)")
00096 (box_pose, box_dims, error) = self.find_cluster_bounding_box_3d(req)
00097 return FindClusterBoundingBox2Response(box_pose, box_dims, error)
00098
00099
00100
00101 def find_cluster_bounding_box(self, req):
00102
00103
00104 (object_points, object_bounding_box_dims, object_bounding_box, object_to_base_frame, object_to_cluster_frame) = \
00105 self.cbbf.find_object_frame_and_bounding_box(req.cluster)
00106
00107
00108 if object_points == None:
00109 return (PoseStamped(), Vector3(0,0,0), 1)
00110
00111
00112 transform_mat = scipy.matrix([[1,0,0,0],
00113 [0,1,0,0],
00114 [0,0,1,object_bounding_box_dims[2]/2.],
00115 [0,0,0,1]])
00116 center_mat = object_to_base_frame * transform_mat
00117
00118
00119 pose = mat_to_pose(center_mat)
00120 pose_stamped = stamp_pose(pose, self.cbbf.base_frame)
00121
00122
00123 transformed_pose_stamped = change_pose_stamped_frame(self.tf_listener, pose_stamped, req.cluster.header.frame_id)
00124
00125
00126 return (transformed_pose_stamped, Vector3(*object_bounding_box_dims), 0)
00127
00128
00129
00130 def find_cluster_bounding_box_3d(self, req):
00131
00132
00133 (object_points, object_bounding_box_dims, object_bounding_box, object_to_base_frame, object_to_cluster_frame) = \
00134 self.cbbf3d.find_object_frame_and_bounding_box(req.cluster)
00135
00136
00137 if object_points == None:
00138 return (PoseStamped(), Vector3(0,0,0), 1)
00139
00140 center_mat = object_to_cluster_frame
00141
00142
00143 pose = mat_to_pose(center_mat)
00144 pose_stamped = stamp_pose(pose, req.cluster.header.frame_id)
00145
00146
00147 return (pose_stamped, Vector3(*object_bounding_box_dims), 0)
00148
00149
00150 if __name__ == '__main__':
00151 rospy.init_node('cluster_bounding_box_finder', anonymous=True)
00152
00153 cbbfs = ClusterBoundingBoxFinderServer()
00154
00155 rospy.loginfo("cluster bounding box finder is ready for queries")
00156 rospy.spin()