sm_overhead_grasp.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 #
00003 # Copyright (c) 2009, Georgia Tech Research Corporation
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are met:
00008 #     * Redistributions of source code must retain the above copyright
00009 #       notice, this list of conditions and the following disclaimer.
00010 #     * Redistributions in binary form must reproduce the above copyright
00011 #       notice, this list of conditions and the following disclaimer in the
00012 #       documentation and/or other materials provided with the distribution.
00013 #     * Neither the name of the Georgia Tech Research Corporation nor the
00014 #       names of its contributors may be used to endorse or promote products
00015 #       derived from this software without specific prior written permission.
00016 #
00017 # THIS SOFTWARE IS PROVIDED BY GEORGIA TECH RESEARCH CORPORATION ''AS IS'' AND
00018 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 # DISCLAIMED. IN NO EVENT SHALL GEORGIA TECH BE LIABLE FOR ANY DIRECT, INDIRECT,
00021 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023 # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
00025 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00026 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 #
00028 
00029 #  \author Travis Deyle (Healthcare Robotics Lab, Georgia Tech.)
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 # Overhead grasping requres:
00050 #   run: hrl_pr2_gains/change_gains_grasp.py
00051 #   roslaunch pr2_overhead_grasping overhead_grasping_server.launch
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     # Create a SMACH state machine
00071     sm = smach.StateMachine(outcomes=['succeeded','aborted','preempted'])
00072 
00073     with sm:
00074         # Setup arm pose (out of way for perception)
00075         tgoal = SingleJointPositionGoal()
00076         tgoal.position = 0.190  # all the way up is 0.200
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         # We will run the grasper at most 3 times.
00087         smach.StateMachine.add(
00088             'THREE_TRIES',
00089             NTries( 3 ),
00090             transitions = {'succeeded':'PERCEIVE_SETUP',
00091                            'aborted':'aborted'})
00092 
00093         # get hand out of face
00094         smach.StateMachine.add(
00095             'PERCEIVE_SETUP',
00096             ServiceState( '/rfid_handoff/grasp', HandoffSrv ),
00097             transitions = { 'succeeded' : 'PERCEIVE_OBJECT' })
00098             
00099 
00100         # Setment objects
00101         smach.StateMachine.add(
00102             'PERCEIVE_OBJECT',
00103             ServiceState( '/obj_segment_inst',
00104                           DetectTableInst,
00105                           request = DetectTableInstRequest( 1.0 ),
00106                           response_slots = ['grasp_points']), # PoseArray
00107             transitions = {'succeeded':'GRASP_SETUP'},
00108             remapping = {'grasp_points':'object_poses'}) #output
00109 
00110         # Setup arm pose (out of way for perception)
00111         smach.StateMachine.add(
00112             'GRASP_SETUP',
00113             SimpleActionState( 'overhead_grasp_setup',
00114                                OverheadGraspSetupAction,
00115                                goal = OverheadGraspSetupGoal( True )), # disable new look
00116             transitions = { 'succeeded': 'GRASP' })
00117 
00118         # Actually perform grasp of some object in front of robot on table
00119         def grasp_goal_cb( userdata, goal ):
00120             # grasp_poses is PoseArray in base_link
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 # Converts base_link -> torso_lift_link (only for x,y)
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 


kelsey_sandbox
Author(s): kelsey
autogenerated on Wed Nov 27 2013 11:52:04