cob_multi_pick_place_client.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('cob_pick_place_action')
00004 import rospy
00005 import actionlib
00006 import random
00007 import numpy
00008 from math import pi
00009 from tf.transformations import *
00010 
00011 from geometry_msgs.msg import PoseStamped
00012 import simple_moveit_interface as smi_
00013 import cob_pick_place_action.msg 
00014 
00015 ### Helper function 
00016 def gen_pose(frame_id="/base_link", pos=[0,0,0], euler=[0,0,0]):
00017         pose = PoseStamped()
00018         pose.header.frame_id = frame_id
00019         pose.header.stamp = rospy.Time.now()
00020         pose.pose.position.x, pose.pose.position.y, pose.pose.position.z = pos
00021         pose.pose.orientation.x, pose.pose.orientation.y, pose.pose.orientation.z, pose.pose.orientation.w = quaternion_from_euler(*euler)
00022         return pose
00023 
00024 def setup_environment():
00025         psi = smi_.get_planning_scene_interface()
00026         rospy.sleep(1.0)
00027         
00028         smi_.clear_objects()
00029         
00030         ### Add a floor
00031         smi_.add_ground()
00032         
00033         ### Add table
00034         pose = PoseStamped()
00035         pose.header.frame_id = "/base_footprint"
00036         pose.header.stamp = rospy.Time.now()
00037         pose.pose.position.x = -0.9
00038         pose.pose.position.y = 0
00039         pose.pose.position.z = 0.39
00040         pose.pose.orientation.x = 0
00041         pose.pose.orientation.y = 0
00042         pose.pose.orientation.z = 0
00043         pose.pose.orientation.w = 1
00044         psi.add_box("bookcase", pose, size=(0.5, 1.5, 0.78))
00045         rospy.sleep(1.0)
00046         
00047 
00048 
00049 def cob_pick_action_client(object_class, object_name, object_pose):
00050         pick_action_client = actionlib.SimpleActionClient('cob_pick_action', cob_pick_place_action.msg.CobPickAction)
00051         
00052         pick_action_client.wait_for_server()
00053         
00054         # Creates a goal to send to the action server.
00055         goal = cob_pick_place_action.msg.CobPickGoal()
00056         goal.object_class = object_class
00057         goal.object_name = object_name
00058         goal.object_pose = object_pose
00059         
00060         #goal.grasp_id = 21
00061         #goal.grasp_database = "KIT"
00062         goal.grasp_database = "OpenRAVE"
00063         goal.support_surface = "bookcase"
00064         
00065         #print goal
00066         # Sends the goal to the action server.
00067         pick_action_client.send_goal(goal)
00068         
00069         # Waits for the server to finish performing the action.
00070         finished_before_timeout=pick_action_client.wait_for_result(rospy.Duration(300, 0))
00071         
00072         if finished_before_timeout:
00073                 state=pick_action_client.get_state()
00074                 print "Action finished: %s"%state
00075         # Prints out the result of executing the action
00076         return state  # State after waiting for PickupAction
00077 
00078 
00079 def cob_place_action_client(object_class, object_name, destinations):
00080         # Creates the SimpleActionClient, passing the type of the action
00081         # (CobPlaceAction) to the constructor.
00082         place_action_client = actionlib.SimpleActionClient('cob_place_action', cob_pick_place_action.msg.CobPlaceAction)
00083 
00084         # Waits until the action server has started up and started
00085         # listening for goals.
00086         place_action_client.wait_for_server()
00087         
00088         # Creates a goal to send to the action server.
00089         goal = cob_pick_place_action.msg.CobPlaceGoal()
00090         goal.object_class = object_class
00091         goal.object_name = object_name
00092         goal.destinations = destinations
00093     goal.support_surface = "bookcase"
00094         
00095         #print goal
00096         # Sends the goal to the action server.
00097         place_action_client.send_goal(goal)
00098 
00099         # Waits for the server to finish performing the action.
00100         finished_before_timeout=place_action_client.wait_for_result(rospy.Duration(300, 0))
00101 
00102         if finished_before_timeout:
00103                 state=place_action_client.get_state()
00104                 print "Action finished: %s"%state
00105         # Prints out the result of executing the action
00106         return state  # State after waiting for CobPlaceAction
00107 
00108 
00109 if __name__ == '__main__':
00110         try:
00111                 # Initializes a rospy node so that the SimpleActionClient can
00112                 # publish and subscribe over ROS.
00113                 rospy.init_node('CobPickAction_client_py')
00114                 
00115                 setup_environment()
00116                 
00117                 psi = smi_.get_planning_scene_interface()
00118                 rospy.sleep(1.0)
00119                 
00120                 filename = roslib.packages.get_pkg_dir('cob_pick_place_action')+"/files/meshes/yellowsaltcube.stl"
00121                 pose1 = gen_pose(pos=[-0.7, 0.0, 0.85], euler=[random.uniform(-pi, pi), random.uniform(-pi, pi), random.uniform(-pi, pi)])
00122                 psi.add_mesh("cube1", pose1, filename)
00123                 rospy.sleep(1.0)
00124         
00125                 pose2 = gen_pose(pos=[-0.7, 0.2, 0.85], euler=[random.uniform(-pi, pi), random.uniform(-pi, pi), random.uniform(-pi, pi)])
00126                 psi.add_mesh("cube2", pose2, filename)
00127                 rospy.sleep(1.0)
00128                 
00129                 
00130                 destinations = []
00131                 for x in numpy.arange(-0.9, -0.6, 0.1):
00132                         for y in numpy.arange(-0.3, -0.2, 0.1):
00133                                 for theta in numpy.arange(-pi, pi, pi/2):
00134                                         destination = gen_pose(pos=[x,y,0.78], euler=[0,0,theta])
00135                                         destinations.append(destination)
00136                 
00137                 
00138                 
00139                 result = cob_pick_action_client(18, "cube1", pose1)
00140                 
00141                 #destinations = []
00142                 #destination1 = gen_pose(pos=[-0.7, -0.15, 0.78])
00143                 #destinations.append(destination1)
00144                 result = cob_place_action_client(18, "cube1", destinations)
00145                 
00146                 
00147                 
00148                 result = cob_pick_action_client(18, "cube2", pose2)
00149                 
00150                 #destination2 = gen_pose(pos=[-0.7, -0.30, 0.78])
00151                 #destinations.append(destination2)
00152                 #destinations.append(pose1)
00153                 #destinations.append(pose2)
00154                 result = cob_place_action_client(18, "cube2", destinations)
00155                 
00156                 
00157                 
00158                 
00159         except rospy.ROSInterruptException:
00160                 print "program interrupted before completion"


cob_pick_place_action
Author(s): Felix Messmer
autogenerated on Wed Aug 26 2015 11:01:29