sm_cap_360.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 import roslib
00003 roslib.load_manifest('rfid_datacapture')
00004 roslib.load_manifest('move_base_msgs')
00005 roslib.load_manifest('pr2_controllers_msgs')
00006 roslib.load_manifest('rfid_hardware')
00007 import rospy
00008 
00009 import smach
00010 from smach_ros import SimpleActionState, ServiceState, IntrospectionServer
00011 import actionlib
00012 
00013 from move_base_msgs.msg import MoveBaseAction
00014 from pr2_controllers_msgs.msg import PointHeadAction, PointHeadGoal
00015 from rfid_datacapture.srv import BagCapture, BagCaptureRequest
00016 from hrl_rfid.srv import RfidSrv
00017 
00018 import rfid_datacapture.utils as rdut
00019 
00020 import numpy as np, math
00021 
00022 
00023 class RfidStart(smach.State):
00024     def __init__(self, srv_path ):
00025         smach.State.__init__(self,
00026                              outcomes=['succeeded', 'aborted'],
00027                              input_keys = ['tagid'])
00028         self.srv_path = srv_path
00029         self.init = False
00030 
00031     def execute(self, userdata):
00032         if not self.init:
00033             rospy.wait_for_service( self.srv_path )
00034             self.srv = rospy.ServiceProxy( self.srv_path, RfidSrv )
00035             self.init = True
00036             
00037         rv = self.srv([ 'track', userdata.tagid ])
00038         if rv:
00039             return 'succeeded'
00040         else:
00041             return 'aborted' # done!
00042 
00043 class RfidStop(smach.State):
00044     def __init__(self, srv_path ):
00045         smach.State.__init__(self, outcomes=['succeeded', 'aborted'])
00046         self.srv_path = srv_path
00047         self.init = False
00048 
00049     def execute(self, userdata):
00050         if not self.init:
00051             rospy.wait_for_service( self.srv_path )
00052             self.srv = rospy.ServiceProxy( self.srv_path, RfidSrv )
00053             self.init = True
00054             
00055         rv = self.srv([ 'stop' ])
00056         if rv:
00057             return 'succeeded'
00058         else:
00059             return 'aborted' # done!
00060 
00061 
00062 def sm_cap_360( yaml_fname ):
00063     # Create a SMACH state machine
00064     sm = smach.StateMachine( outcomes = ['succeeded','aborted','preempted'],
00065                              input_keys = ['track_mode'])
00066 
00067     # Open the container
00068     with sm:
00069 
00070         # Lots of head moving -- abstract into function
00071         def PointAdd( x, y, z, dur, state, res ):
00072             pgoal = PointHeadGoal()
00073             pgoal.target.header.frame_id = '/torso_lift_link'
00074             pgoal.target.point.x = x
00075             pgoal.target.point.y = y
00076             pgoal.target.point.z = z
00077             pgoal.min_duration = rospy.Duration( dur )
00078             pgoal.max_velocity = 1.0
00079             smach.StateMachine.add(
00080                 state,
00081                 SimpleActionState( '/head_traj_controller/point_head_action',
00082                                    PointHeadAction,
00083                                    goal = pgoal ),
00084                 transitions = { 'succeeded' : res })
00085             return
00086 
00087         PointAdd(  1.0,   0.0, 0.35,  5.0, 'INIT_HEAD', 'CAP_START' ) # Go to safe initial conditions
00088         PointAdd( -1.0, -0.25, 0.35,  7.0, 'CAP_START', 'CAPTURE_POSITIONS' ) # Prepare for lots of "neck craning"
00089 
00090         smach.StateMachine.add(
00091             'CAPTURE_POSITIONS',
00092             rdut.YAMLprocPoses( yaml_fname ),
00093             remapping = {'next_move_pose':'next_move_pose'}, # output
00094             transitions = {'aborted':'succeeded',
00095                            'succeeded':'READY_MOVE'})
00096 
00097         smach.StateMachine.add(
00098             'READY_MOVE',
00099             rdut.MoveNotify(),
00100             transitions = {'succeeded':'MOVE_POSITION'})
00101 
00102         smach.StateMachine.add(
00103             'MOVE_POSITION',
00104             SimpleActionState( '/move_base',
00105                                MoveBaseAction,
00106                                goal_slots = [ 'target_pose' ]),
00107             remapping = { 'target_pose' : 'next_move_pose' }, # input
00108             transitions = {'aborted':'MANUAL_SKIP',
00109                            'preempted':'aborted',
00110                            'succeeded':'CAPTURE_TAGS'})
00111 
00112         smach.StateMachine.add(
00113             'MANUAL_SKIP',
00114             rdut.ManualSkip(),
00115             transitions = {'succeeded':'CAPTURE_TAGS', # We already manually positioned the robot
00116                            'aborted':'CAPTURE_POSITIONS'}) # skip this position and go to next
00117 
00118         # This isn't realy necessary, but it provides a nice way to reuse code.
00119         smach.StateMachine.add(
00120             'CAPTURE_TAGS',
00121             rdut.YAMLprocMultitag( yaml_fname ),
00122             remapping = {'bagfile_name':'bagfile_name',  # output
00123                          'bagfile_topics':'bagfile_topics', # output
00124                          'panrate':'panrate',
00125                          'tagid':'tagid',
00126                          'tilt_left':'tilt_left',
00127                          'tilt_right':'tilt_right',
00128                          'tilt_rate':'tilt_rate',
00129                          'tilt_block':'tilt_block'}, # output
00130             transitions = {'aborted':'CAPTURE_POSITIONS', # move to next location
00131                            'succeeded':'START_BAG_CAPTURE'}) # capture bag
00132 
00133         smach.StateMachine.add(
00134             'START_BAG_CAPTURE',
00135             ServiceState( '/bag_cap/capture',
00136                           BagCapture,
00137                           request_slots = ['topics','dest'] ),
00138             remapping = {'topics':'bagfile_topics',
00139                          'dest':'bagfile_name'},
00140             transitions = {'succeeded':'RFID_START'})
00141 
00142         # Initialize RFID reader!
00143         #   rosservice call /rfid/head_mode -- ['track','OrangeMedBot']
00144 
00145         # Am having issues with service states with request_cb, so just making my own....
00146         smach.StateMachine.add(
00147             'RFID_START',
00148             RfidStart( '/rfid/head_mode' ),
00149             remapping = {'tagid':'tagid'},
00150             transitions = {'succeeded':'LOOK_LEFT'})
00151                           
00152         PointAdd( -1.0,  0.25, 0.35, 27.0, 'LOOK_LEFT', 'RFID_STOP' )
00153 
00154         smach.StateMachine.add(
00155             'RFID_STOP',
00156             RfidStop( '/rfid/head_mode' ),
00157             transitions = {'succeeded':'STOP_BAG_CAPTURE'})
00158         
00159         smach.StateMachine.add(
00160             'STOP_BAG_CAPTURE',
00161             ServiceState( '/bag_cap/capture',
00162                           BagCapture,
00163                           request = BagCaptureRequest('','') ),
00164             transitions = {'succeeded':'LOOK_RIGHT'})
00165 
00166         PointAdd( -1.0, -0.25, 0.35,  8.0, 'LOOK_RIGHT', 'CAPTURE_TAGS' )
00167 
00168     return sm
00169 
00170 
00171 if __name__ == '__main__':
00172     import optparse
00173     p = optparse.OptionParser()
00174     p.add_option('--yaml', action='store', type='string', dest='yaml',
00175                  help='Capture description yaml file', default='')
00176     opt, args = p.parse_args()
00177 
00178     if opt.yaml == '':
00179         print 'ERROR: Must specify YAML file.'
00180         exit()
00181 
00182     rospy.init_node('rfid_head_capture')
00183 
00184     sm = sm_cap_360( opt.yaml )
00185 
00186     sm.userdata.track_mode = 'track'
00187     outcome = sm.execute()
00188     
00189 
00190     
00191 


rfid_datacapture
Author(s): Travis Deyle
autogenerated on Wed Nov 27 2013 12:11:16