sm_cousins_demo.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 import roslib
00003 roslib.load_manifest('rfid_demos')
00004 roslib.load_manifest('rfid_behaviors')
00005 roslib.load_manifest('hrl_object_fetching')
00006 roslib.load_manifest('hrl_trajectory_playback')
00007 roslib.load_manifest('pr2_overhead_grasping')
00008 roslib.load_manifest('pr2_controllers_msgs')
00009 import rospy
00010 
00011 import smach
00012 import actionlib
00013 
00014 from smach_ros import SimpleActionState, ServiceState, IntrospectionServer
00015 from geometry_msgs.msg import PoseStamped
00016 from move_base_msgs.msg import MoveBaseAction
00017 from rfid_behaviors.srv import NextBestVantage
00018 from rfid_behaviors.srv import HandoffSrv, HandoffSrvRequest
00019 from rfid_behaviors.srv import FloatFloat_Int32Request as RotateBackupSrvRequest
00020 from rfid_behaviors.srv import FloatFloat_Int32 as RotateBackupSrv
00021 from pr2_controllers_msgs.msg import PointHeadAction, PointHeadGoal
00022 
00023 import sm_rfid_delivery
00024 #from sm_next_best_vantage import BestVantage
00025 import sm_fetch
00026 import sm_rfid_explore
00027 
00028 class PrintStr(smach.State):
00029     def __init__(self, ins = 'Hello'):
00030         smach.State.__init__(self, outcomes=['succeeded', 'aborted', 'preempted'])
00031         self.ins = ins
00032 
00033     def execute(self, userdata):
00034         rospy.logout( 'Executing PrintStr: %s' % self.ins )
00035         rospy.sleep(4.0)
00036         return 'succeeded'
00037 
00038 
00039 
00040 def cousins_demo():
00041     # Create a SMACH state machine
00042     sm = smach.StateMachine(outcomes=['succeeded','aborted','preempted'],
00043                             input_keys = [ 'explore_id',  # should be ''
00044                                            'obj_id',
00045                                            'person_id',
00046                                            'explore_radius' ])
00047 
00048     with sm:
00049         sm_search = sm_rfid_explore.sm_search()
00050         smach.StateMachine.add(
00051             'RFID_SEARCH',  # outcomes: succeded, aborted, preempted
00052             sm_search,
00053             remapping = { 'tagid' : 'explore_id',  # input
00054                           'explore_radius' : 'explore_radius' },
00055             transitions={'succeeded':'BEST_VANTAGE_OBJ'})
00056 
00057         # Get best vantage for obj.
00058         # The NextBestVantage was initialized in the search.
00059         smach.StateMachine.add(
00060             'BEST_VANTAGE_OBJ',
00061             ServiceState( '/rfid_recorder/best_vantage',
00062                           NextBestVantage,
00063                           request_slots = ['tagid'],
00064                           response_slots = ['best_vantage']),
00065             remapping = {'best_vantage':'best_vantage_obj', # output
00066                          'tagid':'obj_id'}, # input
00067             transitions = {'succeeded':'MOVE_VANTAGE_OBJ'})
00068 
00069 
00070         smach.StateMachine.add(
00071             'MOVE_VANTAGE_OBJ',
00072             SimpleActionState( '/move_base',
00073                                MoveBaseAction,
00074                                goal_slots = [ 'target_pose' ]),
00075             remapping = { 'target_pose' : 'best_vantage_obj' }, # input
00076             transitions = {'aborted':'BEST_VANTAGE_OBJ',
00077                            'preempted':'aborted',
00078                            'succeeded':'FETCH'})
00079         # # FETCH
00080         # smach.StateMachine.add('FETCH',PrintStr( 'Fetching object' ),
00081         #                        transitions = { 'succeeded' : 'BEST_VANTAGE_PERSON' })
00082 
00083         # Fetch
00084         smach.StateMachine.add(
00085             'FETCH',
00086             sm_fetch.sm_fetch_object(),
00087             remapping = {'tagid':'obj_id'},
00088             transitions = {'succeeded':'POINT_HEAD',
00089                            'aborted':'BACKUP'})
00090 
00091         # Backup (60cm)
00092         smach.StateMachine.add(
00093             'BACKUP',
00094             ServiceState( '/rotate_backup',
00095                           RotateBackupSrv,
00096                           request = RotateBackupSrvRequest(0.0, -0.50)), 
00097             transitions = { 'succeeded':'PRE_STOW' })
00098 
00099         smach.StateMachine.add(
00100             'PRE_STOW',
00101             ServiceState( 'rfid_handoff/stow_grasp',
00102                           HandoffSrv,
00103                           request = HandoffSrvRequest()), 
00104             transitions = { 'succeeded':'POINT_HEAD' })
00105 
00106         # Point Head Down (eventaully roll this and perceive table into own sm?)
00107         pgoal = PointHeadGoal()
00108         pgoal.target.header.frame_id = '/torso_lift_link'
00109         pgoal.target.point.x = 0.50
00110         pgoal.target.point.z = 0.30
00111         pgoal.min_duration = rospy.Duration(0.6)
00112         pgoal.max_velocity = 1.0
00113         smach.StateMachine.add(
00114             'POINT_HEAD',
00115             SimpleActionState( '/head_traj_controller/point_head_action',
00116                                PointHeadAction,
00117                                goal = pgoal ),
00118             transitions = { 'succeeded' : 'BEST_VANTAGE_PERSON' })
00119         
00120 
00121         # Get best vantage for obj.
00122         # The NextBestVantage was initialized in the search.
00123         smach.StateMachine.add(
00124             'BEST_VANTAGE_PERSON',
00125             ServiceState( '/rfid_recorder/best_vantage',
00126                           NextBestVantage,
00127                           request_slots = ['tagid'],
00128                           response_slots = ['best_vantage']),
00129             remapping = {'best_vantage':'best_vantage_person', # output
00130                          'tagid':'person_id'}, # input
00131             transitions = {'succeeded':'MOVE_VANTAGE_PERSON'})
00132 
00133         smach.StateMachine.add(
00134             'MOVE_VANTAGE_PERSON',
00135             SimpleActionState( '/move_base',
00136                                MoveBaseAction,
00137                                goal_slots = [ 'target_pose' ]),
00138             remapping = { 'target_pose' : 'best_vantage_person' }, # input
00139             transitions = {'aborted':'BEST_VANTAGE_PERSON',
00140                            'preempted':'aborted',
00141                            'succeeded':'DELIVERY'})
00142 
00143         sm_delivery = sm_rfid_delivery.sm_delivery()
00144         smach.StateMachine.add(
00145             'DELIVERY', # outcomes: succeeded, aborted, preempted
00146             sm_delivery,
00147             remapping = { 'tagid' : 'person_id'}, #input
00148             transitions = { 'succeeded': 'succeeded' })
00149             
00150     return sm
00151 
00152 
00153 
00154 if __name__ == '__main__':
00155     rospy.init_node('smach_rfid_explore')
00156 
00157     sm = cousins_demo()
00158 
00159     sis = IntrospectionServer('sm_cousins_demo', sm, '/SM_COUSINS_DEMO')
00160     sis.start()
00161 
00162     rospy.logout( 'READY TO RUN COUSINS DEMO' )
00163 
00164     sm.userdata.explore_id = ''
00165     sm.userdata.obj_id = 'SpectrMedBot'
00166     sm.userdata.person_id = 'person      '
00167     sm.userdata.explore_radius = 1.8
00168     # sm.userdata.explore_rfid_reads = []
00169     outcome = sm.execute()
00170 
00171     rospy.spin()
00172     sis.stop()
00173 
00174     
00175 


rfid_demos
Author(s): Travis Deyle
autogenerated on Wed Nov 27 2013 11:50:51