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 import roslib
00032 roslib.load_manifest('pr2_overhead_grasping')
00033 roslib.load_manifest('tf')
00034 roslib.load_manifest('hrl_table_detect')
00035 roslib.load_manifest('rfid_behaviors')
00036 import rospy
00037
00038 import smach
00039 from smach_ros import SimpleActionState, ServiceState, IntrospectionServer
00040 import actionlib
00041 import tf.transformations as tft
00042
00043 from rfid_behaviors.srv import HandoffSrv
00044 from hrl_table_detect.srv import DetectTableInst, DetectTableInstRequest
00045 from pr2_overhead_grasping.msg import OverheadGraspAction, OverheadGraspGoal
00046 from pr2_overhead_grasping.msg import OverheadGraspSetupAction, OverheadGraspSetupGoal
00047 from pr2_controllers_msgs.msg import SingleJointPositionAction, SingleJointPositionGoal
00048
00049
00050
00051
00052
00053 class NTries(smach.State):
00054 def __init__(self, n):
00055 smach.State.__init__(self, outcomes=['succeeded', 'aborted'])
00056 self.counter = 0
00057 self.n = n
00058
00059 def execute(self, userdata):
00060 self.counter += 1
00061
00062 if self.counter <= self.n:
00063 rospy.logout( 'Executing NTries: On #%d of %d' % (self.counter, self.n))
00064 return 'succeeded'
00065 else:
00066 return 'aborted'
00067
00068
00069 def sm_grasp():
00070
00071 sm = smach.StateMachine(outcomes=['succeeded','aborted','preempted'])
00072
00073 with sm:
00074
00075 tgoal = SingleJointPositionGoal()
00076 tgoal.position = 0.190
00077 tgoal.min_duration = rospy.Duration( 2.0 )
00078 tgoal.max_velocity = 1.0
00079 smach.StateMachine.add(
00080 'TORSO_SETUP',
00081 SimpleActionState( 'torso_controller/position_joint_action',
00082 SingleJointPositionAction,
00083 goal = tgoal),
00084 transitions = { 'succeeded': 'THREE_TRIES' })
00085
00086
00087 smach.StateMachine.add(
00088 'THREE_TRIES',
00089 NTries( 3 ),
00090 transitions = {'succeeded':'PERCEIVE_SETUP',
00091 'aborted':'aborted'})
00092
00093
00094 smach.StateMachine.add(
00095 'PERCEIVE_SETUP',
00096 ServiceState( '/rfid_handoff/grasp', HandoffSrv ),
00097 transitions = { 'succeeded' : 'PERCEIVE_OBJECT' })
00098
00099
00100
00101 smach.StateMachine.add(
00102 'PERCEIVE_OBJECT',
00103 ServiceState( '/obj_segment_inst',
00104 DetectTableInst,
00105 request = DetectTableInstRequest( 1.0 ),
00106 response_slots = ['grasp_points']),
00107 transitions = {'succeeded':'GRASP_SETUP'},
00108 remapping = {'grasp_points':'object_poses'})
00109
00110
00111 smach.StateMachine.add(
00112 'GRASP_SETUP',
00113 SimpleActionState( 'overhead_grasp_setup',
00114 OverheadGraspSetupAction,
00115 goal = OverheadGraspSetupGoal( True )),
00116 transitions = { 'succeeded': 'GRASP' })
00117
00118
00119 def grasp_goal_cb( userdata, goal ):
00120
00121 mgp = userdata.grasp_poses.poses[0]
00122
00123 ggoal = OverheadGraspGoal()
00124 ggoal.is_grasp = True
00125 ggoal.grasp_type = OverheadGraspGoal.MANUAL_GRASP
00126
00127 ggoal.x = mgp.position.x + 0.05
00128 ggoal.y = mgp.position.y
00129
00130 o = mgp.orientation
00131 r,p,y = tft.euler_from_quaternion(( o.x, o.y, o.z, o.w ))
00132 ggoal.rot = y
00133
00134 return ggoal
00135
00136 smach.StateMachine.add(
00137 'GRASP',
00138 SimpleActionState( 'overhead_grasp',
00139 OverheadGraspAction,
00140 goal_cb = grasp_goal_cb,
00141 input_keys = ['grasp_poses']),
00142 remapping = {'grasp_poses':'object_poses'},
00143 transitions = { 'succeeded': 'succeeded',
00144 'aborted':'THREE_TRIES' })
00145
00146 return sm
00147
00148
00149
00150 if __name__ == '__main__':
00151 rospy.init_node('smach_sm_grasp')
00152
00153 sm = sm_grasp()
00154
00155 sis = IntrospectionServer('Grasping', sm, '/SM_GRASPING')
00156 sis.start()
00157
00158 outcome = sm.execute()
00159
00160 sis.stop()
00161
00162
00163