sm_head_capture.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 import roslib
00003 roslib.load_manifest('pr2_controllers_msgs')
00004 roslib.load_manifest('smach_ros')
00005 roslib.load_manifest('actionlib')
00006 roslib.load_manifest('rfid_datacapture')
00007 import rospy
00008 
00009 import smach
00010 import actionlib
00011 
00012 from smach_ros import SimpleActionState, ServiceState, IntrospectionServer
00013 from pr2_controllers_msgs.msg import PointHeadAction, PointHeadGoal
00014 from rfid_datacapture.srv import BagCapture, BagCaptureRequest
00015 
00016 def head_capture( ):
00017     # Create a SMACH state machine
00018     sm = smach.StateMachine(outcomes=['succeeded','aborted','preempted'],
00019                             input_keys = [ 'bagfile_name', 'bagfile_topics' ])
00020     with sm:
00021 
00022         def PointAdd( x, y, z, dur, state, res ):
00023             pgoal = PointHeadGoal()
00024             pgoal.target.header.frame_id = '/torso_lift_link'
00025             pgoal.target.point.x = x
00026             pgoal.target.point.y = y
00027             pgoal.target.point.z = z
00028             pgoal.min_duration = rospy.Duration( dur )
00029             pgoal.max_velocity = 1.0
00030             smach.StateMachine.add(
00031                 state,
00032                 SimpleActionState( '/head_traj_controller/point_head_action',
00033                                    PointHeadAction,
00034                                    goal = pgoal ),
00035                 transitions = { 'succeeded' : res })
00036             return
00037 
00038         PointAdd( 0.00, -1.00, -0.60, 5.0, 'PH1', 'START_BAG_CAPTURE' )
00039 
00040         smach.StateMachine.add(
00041             'START_BAG_CAPTURE',
00042             ServiceState( '/bag_cap/capture',
00043                           BagCapture,
00044                           request_slots = ['topics','dest'] ),
00045             remapping = {'topics':'bagfile_topics',
00046                          'dest':'bagfile_name'},
00047             transitions = {'succeeded':'PH2'})
00048         
00049         
00050         PointAdd( 0.00, 1.00, -0.60, 15.0, 'PH2', 'PH3' )
00051         PointAdd( 0.00, 1.00, -0.20, 3.0, 'PH3', 'PH4' )
00052         PointAdd( 0.00, -1.00, -0.20, 15.0, 'PH4', 'PH5' )
00053         PointAdd( 0.00, -1.00, 0.30, 3.0, 'PH5', 'PH6' )
00054         PointAdd( 0.00, 1.00, 0.30, 15.0, 'PH6', 'PH7' )
00055         PointAdd( 1.00, 0.00, 0.00, 7.5, 'PH7', 'STOP_BAG_CAPTURE' )
00056 
00057         smach.StateMachine.add(
00058             'STOP_BAG_CAPTURE',
00059             ServiceState( '/bag_cap/capture',
00060                           BagCapture,
00061                           request = BagCaptureRequest('','') ),
00062             transitions = {'succeeded':'succeeded'})
00063 
00064         return sm
00065 
00066 
00067 class DelayState( smach.State ):
00068     def __init__( self, delay = 3.0 ):
00069         smach.State.__init__(self,outcomes=['succeeded', 'aborted'])
00070         self.delay = delay
00071 
00072     def execute( self, userdata ):
00073         rospy.sleep( self.delay )
00074         return 'succeeded'
00075     
00076 
00077 def cam_capture( ):
00078     # Create a SMACH state machine
00079     sm = smach.StateMachine(outcomes=['succeeded','aborted','preempted'],
00080                             input_keys = [ 'bagfile_name', 'bagfile_topics' ])
00081     with sm:
00082 
00083         smach.StateMachine.add(
00084             'START_BAG_CAPTURE',
00085             ServiceState( '/bag_cap/capture',
00086                           BagCapture,
00087                           request_slots = ['topics','dest'] ),
00088             remapping = {'topics':'bagfile_topics',
00089                          'dest':'bagfile_name'},
00090             transitions = {'succeeded':'DELAY'})
00091 
00092         smach.StateMachine.add(
00093             'DELAY',
00094             DelayState(),
00095             transitions = {'succeeded':'STOP_BAG_CAPTURE'})
00096 
00097         smach.StateMachine.add(
00098             'STOP_BAG_CAPTURE',
00099             ServiceState( '/bag_cap/capture',
00100                           BagCapture,
00101                           request = BagCaptureRequest('','') ),
00102             transitions = {'succeeded':'succeeded'})
00103 
00104         return sm
00105     
00106 
00107 if __name__ == '__main__':
00108     import optparse
00109     p = optparse.OptionParser()
00110     p.add_option('--fname', action='store', type='string', dest='fname',
00111                  help='File name.  Should be without extension. [eg. \'trial\']', default='')
00112 
00113     opt, args = p.parse_args()
00114 
00115     if opt.fname == '':
00116         print 'Fname required'
00117         exit()
00118     
00119     rospy.init_node('smach_head_capture')
00120 
00121     sm = head_capture()
00122 
00123     sis = IntrospectionServer('sm_head_capture', sm, '/SM_HEAD_CAPTURE')
00124     sis.start()
00125 
00126     fname_base = '/u/travis/svn/robot1/src/projects/rfid_datacapture/src/rfid_datacapture/search_cap/search_aware_home/'
00127     fname = fname_base + opt.fname
00128     
00129     sm.userdata.bagfile_name = fname
00130     #sm.userdata.bagfile_topics = '/tf /kinect_head/rgb/points_throttled /kinect_head/rgb/image_color'
00131     sm.userdata.bagfile_topics = '/tf /kinect_head/rgb/points_throttled'
00132 
00133     outcome = sm.execute()
00134 
00135     #raw_input( 'Hit [ENTER] to begin capturing camera.' )
00136 
00137     sm_cam = cam_capture()
00138     sm_cam.userdata.bagfile_name = fname + '_cam'
00139     sm_cam.userdata.bagfile_topics = '/tf /kinect_head/rgb/image_color'
00140 
00141     sm_cam.execute()
00142 
00143     sis.stop()
00144 
00145     
00146 


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