sm_rfid_delivery.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 import roslib
00003 roslib.load_manifest('rfid_demos')
00004 import rospy
00005 
00006 import smach
00007 from smach_ros import SimpleActionState, ServiceState, IntrospectionServer
00008 import actionlib
00009 
00010 from rfid_servoing.msg import ServoAction, ServoGoal
00011 from rfid_artoolkit.msg import UpCloseAction, UpCloseGoal
00012 from pr2_controllers_msgs.msg import PointHeadAction, PointHeadGoal
00013 from explore_hrl.msg import ExploreAction, ExploreActionGoal
00014 from rfid_behaviors.srv import HandoffSrv
00015 from rfid_behaviors.srv import FlapEarsSrv
00016 from rfid_behaviors.srv import OrientSrv, OrientSrvRequest
00017 from robotis.srv import MoveAng, MoveAngRequest
00018 
00019 # import rfid_search
00020 import yaml
00021 
00022 
00023 
00024 def sm_delivery():
00025     # Create a SMACH state machine
00026     sm = smach.StateMachine( outcomes = ['succeeded','aborted','preempted'],
00027                              input_keys = ['tagid'])
00028 
00029     # Open the container
00030     with sm:
00031 
00032         # smach.StateMachine.add(
00033         #     'EXPLORE',
00034         #     Explore(),
00035         #     transitions = { 'succeeded' : 'FLAP_EARS',
00036         #                     'aborted' : 'RECOVER_ONCE' })
00037 
00038         # smach.StateMachine.add(
00039         #     'RECOVER_ONCE',
00040         #     RecoverOnce(),
00041         #     transitions = { 'succeeded' : 'EXPLORE',
00042         #                     'aborted' : 'aborted' })
00043 
00044         # Initial RFID Ear Flapping
00045         smach.StateMachine.add(
00046             'FLAP_EARS',
00047             ServiceState( '/rfid_orient/flap', FlapEarsSrv ),
00048             transitions = { 'succeeded' : 'ORIENT' })
00049 
00050         # Orient towards tag
00051         smach.StateMachine.add(
00052             'ORIENT',
00053             ServiceState( '/rfid_orient/orient',
00054                           OrientSrv,
00055                           request_slots = ['data'], 
00056                           input_keys=['data']), # tagid (string)
00057             transitions = { 'succeeded' : 'SERVO' },
00058             remapping = {'data':'tagid'}) # input
00059 
00060         # Servoing is a basic state machine.  Success means servoing finished @ obs.
00061         smach.StateMachine.add(
00062             'SERVO',
00063             SimpleActionState( '/rfid_servo/servo_act',
00064                                ServoAction,
00065                                goal_slots = ['tagid']), #goal = ServoGoal( 'person      ' ),
00066             transitions = { 'succeeded': 'TUCK_LEFT' },
00067             remapping = {'tagid':'tagid'}) # input
00068 
00069 
00070         # # ARTag scanner.  Result is actually stored in result message "status"
00071         # # Remap based on upclose_action result
00072         # def ar_detect_cb( userdata, status, result ):
00073         #     if result.status == 'SUCCEEDED':  # Actionlib Succeeded: Found the tag!
00074         #         userdata.tag_pos = result.ps
00075         #         return 'succeeded'
00076         #     elif result.status == 'FAILED':   # Actionlib Succeed, but no tag found.
00077         #         return 'recover'
00078         #     elif result.status == 'PREEMPTED': # Actionlib was preempted (higher level)
00079         #         return 'preempted'
00080         #     else: # result.status == 'RESERVO' # Obstacle from servo cleared.  Self-preempt to reservo.
00081         #         return 'reservo'
00082             
00083         # smach.StateMachine.add(
00084         #     'ARTAG',
00085         #     SimpleActionState( '/rfid_artoolkit/upclose_act',
00086         #                        UpCloseAction,
00087         #                        goal_slots = ['tagid'], #goal = UpCloseGoal( 'person      ' )
00088         #                        output_keys = ['tag_pos'],
00089         #                        outcomes = ['succeeded', 'recover', 'preempted', 'reservo'],
00090         #                        result_cb = ar_detect_cb ),
00091         #     transitions = { 'recover' : 'aborted',
00092         #                     'reservo' : 'HEAD_REPOS',
00093         #                     'succeeded': 'TUCK_LEFT' },
00094         #     remapping = {'tagid':'tagid'}) # input
00095 
00096 
00097         # # Reposition the head:
00098         # goal = PointHeadGoal()
00099         # goal.target.header.frame_id = '/torso_lift_link'
00100         # goal.target.point.x = 0.54
00101         # goal.target.point.z = 0.35
00102         # goal.min_duration = rospy.Duration(0.6)
00103         # smach.StateMachine.add(
00104         #     'HEAD_REPOS',
00105         #     SimpleActionState( '/head_traj_controller/point_head_action',
00106         #                        PointHeadAction,
00107         #                        goal = goal ),
00108         #     transitions = { 'succeeded' : 'SERVO' })
00109 
00110 
00111         # Tuck Left (non-blocking)
00112         smach.StateMachine.add(
00113             'TUCK_LEFT',
00114             ServiceState( 'robotis/servo_left_pan_moveangle',
00115                           MoveAng,
00116                           request = MoveAngRequest( 1.350, 0.2, 0 )), # ang (float), angvel (float), blocking (bool)
00117             transitions = {'succeeded':'TUCK_RIGHT'})
00118 
00119         # Tuck Right (non-blocking)
00120         smach.StateMachine.add(
00121             'TUCK_RIGHT',
00122             ServiceState( 'robotis/servo_right_pan_moveangle',
00123                           MoveAng,
00124                           request = MoveAngRequest( -1.350, 0.2, 0 )), # ang (float), angvel (float), blocking (bool)
00125             transitions = {'succeeded':'HANDOFF'})
00126 
00127         # Handoff if tag found
00128         smach.StateMachine.add(
00129             'HANDOFF',
00130             ServiceState( '/rfid_handoff/handoff', HandoffSrv ),
00131             transitions = { 'succeeded' : 'succeeded' })
00132                 
00133 
00134     # Execute SMACH plan
00135     return sm
00136 
00137 
00138 if __name__ == '__main__':
00139     rospy.init_node('smach_example_state_machine')
00140 
00141     sm = sm_delivery()
00142 
00143     sis = IntrospectionServer('RFID_delivery', sm, '/SM_ROOT')
00144     sis.start()
00145 
00146     sm.userdata.tagid = 'person      '
00147     outcome = sm.execute()
00148     
00149     rospy.spin()
00150     sis.stop()
00151 
00152     
00153 


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