openrave_grasp_planner_test.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 '''
00003 Test client for point_cluster_grasp_planner.
00004 Requires tabletop_node.launch to be running (in tabletop_object_detector).
00005 Add Markers of topic 'grasp_markers' to see the poses being tested.
00006 '''
00007 
00008 from __future__ import division
00009 import roslib
00010 roslib.load_manifest('katana_tabletop_manipulation_launch')
00011 import rospy
00012 from object_manipulation_msgs.srv import FindClusterBoundingBox, FindClusterBoundingBoxRequest
00013 from tabletop_object_detector.srv import TabletopDetection, TabletopDetectionRequest
00014 from object_manipulation_msgs.srv import GraspPlanning, GraspPlanningRequest
00015 from visualization_msgs.msg import Marker
00016 import tf.transformations
00017 import numpy
00018 import scipy
00019 import time
00020 import katana_tabletop_manipulation_launch.draw_functions as draw_functions
00021 from katana_tabletop_manipulation_launch.convert_functions import *
00022 
00023 
00024 #call the tabletop object detector to get the table and clusters in the scene
00025 def call_object_detector():
00026 
00027     req = TabletopDetectionRequest()
00028     req.return_clusters = 1
00029     req.return_models = 0
00030     service_name = "/object_detection"
00031     rospy.loginfo("waiting for object_detection service")
00032     rospy.wait_for_service(service_name)
00033     serv = rospy.ServiceProxy(service_name, TabletopDetection)
00034     try:
00035         res = serv(req)
00036     except rospy.ServiceException, e:
00037         rospy.logerr("error when calling object_detection: %s"%e)
00038         return 0
00039     return (res.detection.table, res.detection.clusters)
00040 
00041 
00042 #call plan_point_cluster_grasp to get candidate grasps for a cluster
00043 def call_plan_point_cluster_grasp(cluster):
00044 
00045     req = GraspPlanningRequest()
00046     req.target.cluster = cluster
00047     req.arm_name = "arm"
00048     req.collision_support_surface_name = "table"
00049     service_name = "openrave_grasp_planner"
00050     rospy.loginfo("waiting for /openrave_grasp_planner service")
00051     rospy.wait_for_service(service_name)
00052     rospy.loginfo("service found")
00053     serv = rospy.ServiceProxy(service_name, GraspPlanning)
00054     try:
00055         res = serv(req)
00056     except rospy.ServiceException, e:
00057         rospy.logerr("error when calling plan_point_cluster_grasp: %s"%e)
00058         return 0
00059     if res.error_code.value != 0:
00060         return []
00061 
00062     grasp_poses = [grasp.grasp_pose for grasp in res.grasps]
00063     return grasp_poses
00064 
00065 
00066 #call find_cluster_bounding_box to get the bounding box for a cluster
00067 def call_find_cluster_bounding_box(cluster):
00068 
00069     req = FindClusterBoundingBoxRequest()
00070     req.cluster = cluster
00071     service_name = "find_cluster_bounding_box"
00072     rospy.loginfo("waiting for find_cluster_bounding_box service")
00073     rospy.wait_for_service(service_name)
00074     rospy.loginfo("service found")
00075     serv = rospy.ServiceProxy(service_name, FindClusterBoundingBox)
00076     try:
00077         res = serv(req)
00078     except rospy.ServiceException, e:
00079         rospy.logerr("error when calling find_cluster_bounding_box: %s"%e)
00080         return 0
00081     if not res.error_code:
00082         return (res.pose, res.box_dims)
00083     else:
00084         return (None, None)
00085 
00086 
00087 if __name__ == "__main__":
00088 
00089     #initialize the node, tf listener and broadcaster, and rviz drawing helper class
00090     rospy.init_node('test_point_cluster_grasp_planner', anonymous=True)
00091     tf_broadcaster = tf.TransformBroadcaster()
00092     tf_listener = tf.TransformListener()
00093     draw_functions = draw_functions.DrawFunctions('grasp_markers')
00094 
00095     #call the tabletop object detector to detect clusters on the table
00096     (table, clusters) = call_object_detector()
00097     print "table:\n", table
00098 
00099     #keep going until the user says to quit (or until Ctrl-C is pressed)
00100     while(not rospy.is_shutdown()):
00101 
00102         #for each cluster in view in turn
00103         for cluster in clusters:
00104             draw_functions.clear_grasps(num=1000)
00105 
00106             #get the bounding box for this cluster and draw it in blue
00107             (box_pose, box_dims) = call_find_cluster_bounding_box(cluster)
00108             if box_pose == None:
00109                 continue
00110             box_mat = pose_to_mat(box_pose.pose)
00111             box_ranges = [[-box_dims.x/2, -box_dims.y/2, -box_dims.z/2],
00112                           [box_dims.x/2, box_dims.y/2, box_dims.z/2]]
00113             draw_functions.draw_rviz_box(box_mat, box_ranges, 'katana_base_link', \
00114                                          ns = 'bounding box', \
00115                                          color = [0,0,1], opaque = 0.25, duration = 60)
00116 
00117             print "Run the blue cluster? Enter y to run, enter to go to the next, q to exit"
00118             c = raw_input()
00119 
00120             if c == 'y':
00121                 draw_functions.clear_rviz_points('grasp_markers')
00122 
00123                 #call the grasp planner
00124                 grasps = call_plan_point_cluster_grasp(cluster)
00125 
00126                 #draw the resulting grasps (all at once, or one at a time)
00127                 print "number of grasps returned:", len(grasps)
00128                 print "drawing grasps: press p to pause after each one in turn, enter to show all grasps at once"
00129                 c = raw_input()
00130                 if c == 'p':
00131                     draw_functions.draw_grasps(grasps, cluster.header.frame_id, pause = 1)
00132                 else:
00133                     draw_functions.draw_grasps(grasps, cluster.header.frame_id, pause = 0)
00134                 print "done drawing grasps, press enter to continue"
00135                 raw_input()
00136 
00137                 #clear out the grasps drawn
00138                 draw_functions.clear_grasps(num = 1000)
00139 
00140             elif c == 'q':
00141                 break
00142         else:
00143             continue
00144         break
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


katana_tabletop_manipulation_launch
Author(s): Martin Günther, Henning Deeken
autogenerated on Tue May 28 2013 15:14:24