sm_aware_home_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('pr2_grasp_behaviors')
00006 roslib.load_manifest('hrl_trajectory_playback')
00007 roslib.load_manifest('pr2_controllers_msgs')
00008 roslib.load_manifest('robotis')
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, PoseWithCovarianceStamped
00016 from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
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 from pr2_controllers_msgs.msg import SingleJointPositionAction, SingleJointPositionGoal
00023 from robotis.srv import MoveAng, MoveAngRequest
00024 from hrl_trajectory_playback.srv import TrajPlaybackSrv, TrajPlaybackSrvRequest
00025 
00026 import sm_rfid_delivery
00027 import string
00028 #from sm_next_best_vantage import BestVantage
00029 # import sm_fetch
00030 # import sm_rfid_explore
00031 
00032 class PrintStr(smach.State):
00033     def __init__(self, ins = 'Hand me an object [ENTER]'):
00034         smach.State.__init__(self, outcomes=['succeeded', 'aborted', 'preempted'])
00035         self.ins = ins
00036 
00037     def execute(self, userdata):
00038         rospy.logout( 'Executing PrintStr: %s' % self.ins )
00039         raw_input()
00040         return 'succeeded'
00041 
00042 class ConfirmObj(smach.State):
00043     def __init__(self, ins = 'Does the robot have the object [\'yes\' to proceed]?'):
00044         smach.State.__init__(self, outcomes=['succeeded', 'aborted', 'preempted'])
00045         self.ins = ins
00046 
00047     def execute(self, userdata):
00048         rospy.logout( 'Executing PrintStr: %s' % self.ins )
00049         f = raw_input()
00050         if string.find( f, 'yes' ) == -1:
00051             return 'aborted'
00052         else:
00053             return 'succeeded'
00054 
00055 class InitLocalization(smach.State):
00056     def __init__(self, init_pose = None):
00057         smach.State.__init__(self, outcomes=['succeeded', 'aborted'])
00058         self.init_pose = init_pose
00059                             
00060     def execute(self, userdata):
00061         rospy.logout( 'Initializing Localization' )
00062 
00063         pub = rospy.Publisher( '/initialpose', PoseWithCovarianceStamped )
00064         rospy.sleep( 0.5 )
00065 
00066         if not self.init_pose:
00067             msg = PoseWithCovarianceStamped()
00068             msg.header.frame_id = '\map'
00069 
00070             msg.pose.pose.position.x = -1.565
00071             msg.pose.pose.position.y =  0.807
00072             msg.pose.pose.position.z =  0.000
00073 
00074             msg.pose.pose.orientation.w = 1.000
00075 
00076             msg.pose.covariance = [0.25, 0.0, 0.0, 0.0,
00077                                               0.0, 0.0, 0.0, 0.25,
00078                                               0.0, 0.0, 0.0, 0.0,
00079                                               0.0, 0.0, 0.0, 0.0,
00080                                               0.0, 0.0, 0.0, 0.0,
00081                                               0.0, 0.068, 0.0, 0.0,
00082                                               0.0, 0.0, 0.0, 0.0,
00083                                               0.0, 0.0, 0.0, 0.0,
00084                                               0.0, 0.0, 0.0, 0.0]
00085 
00086             print 'RUNNING: ', msg
00087 
00088         raw_input( 'Drive the robot to the initial location (in kitchen by default).  Hit [ENTER] when done.' )
00089         msg.header.stamp = rospy.Time(0)
00090         pub.publish( msg )
00091 
00092         # for i in xrange( 5 ):
00093         #     msg.header.stamp = rospy.Time(0)
00094         #     pub.publish( msg )
00095         #     rospy.sleep( 0.05 )
00096             
00097         return 'succeeded'
00098 
00099 
00100 def init_local_test():
00101     sm = smach.StateMachine( outcomes=['succeeded','aborted'] )
00102     with sm:
00103         smach.StateMachine.add(
00104             'INIT_LOCALIZATION',
00105             InitLocalization(),
00106             transitions = { 'succeeded':'succeeded' })
00107     return sm
00108 
00109 
00110 def cousins_demo():
00111     # Create a SMACH state machine
00112     sm = smach.StateMachine(outcomes=['succeeded','aborted','preempted'],
00113                             input_keys = [ 'person_id' ])
00114 
00115     with sm:
00116 
00117         # Just a precation
00118         tgoal = SingleJointPositionGoal()
00119         tgoal.position = 0.040  # all the way up is 0.200, mostly down is 0.040
00120         tgoal.min_duration = rospy.Duration( 2.0 )
00121         tgoal.max_velocity = 1.0
00122         smach.StateMachine.add(
00123             'TORSO_SETUP',
00124             SimpleActionState( 'torso_controller/position_joint_action',
00125                                SingleJointPositionAction,
00126                                goal = tgoal),
00127             transitions = { 'succeeded': 'INIT_ARM_POSE' })
00128 
00129         
00130         smach.StateMachine.add(
00131             'INIT_ARM_POSE',
00132             ServiceState( '/traj_playback/hfa_untuck',
00133                           TrajPlaybackSrv,
00134                           request = TrajPlaybackSrvRequest( False )), 
00135             transitions = { 'succeeded':'MOVE_GET_OBJ' })
00136         
00137 
00138         go = MoveBaseGoal()
00139         
00140         go.target_pose.header.frame_id = '/map'
00141         go.target_pose.header.stamp = rospy.Time(0)
00142         go.target_pose.pose.position.x = -5.07
00143         go.target_pose.pose.position.y = 8.725
00144         go.target_pose.pose.orientation.z = 0.926
00145         go.target_pose.pose.orientation.w = 0.377
00146 
00147         smach.StateMachine.add(
00148             'MOVE_GET_OBJ',
00149             SimpleActionState( '/move_base',
00150                                MoveBaseAction,
00151                                goal = go ),
00152             transitions = {'succeeded':'READY_HANDOFF_INIT'})
00153 
00154 
00155         smach.StateMachine.add(
00156             'READY_HANDOFF_INIT',
00157             PrintStr('Hand me an object [ENTER]'), 
00158             transitions = { 'succeeded':'HANDOFF_INIT' })
00159 
00160         smach.StateMachine.add(
00161             'HANDOFF_INIT',
00162             ServiceState( 'rfid_handoff/initialize',
00163                           HandoffSrv,
00164                           request = HandoffSrvRequest()), 
00165             transitions = { 'succeeded':'CONFIRM_HANDOFF' })
00166 
00167         smach.StateMachine.add(
00168             'CONFIRM_HANDOFF',
00169             ConfirmObj('Does the robot have the object [\'yes\' to proceed]?'), 
00170             transitions = { 'succeeded':'ROTATE_AFTER_HANDOFF',
00171                             'aborted':'READY_HANDOFF_INIT'} )
00172         
00173         smach.StateMachine.add(
00174             'ROTATE_AFTER_HANDOFF',
00175             ServiceState( '/rotate_backup',
00176                           RotateBackupSrv,
00177                           request = RotateBackupSrvRequest( 3.14, 0.0)),  # Full 180-deg spin.
00178             transitions = { 'succeeded':'MOVE_DELIVERY' })
00179 
00180         gd = MoveBaseGoal()
00181         gd.target_pose.header.frame_id = '/map'
00182         gd.target_pose.header.stamp = rospy.Time(0)
00183         gd.target_pose.pose.position.x = 2.956
00184         gd.target_pose.pose.position.y = 3.047
00185         gd.target_pose.pose.orientation.z = 0.349
00186         gd.target_pose.pose.orientation.w = 0.937
00187 
00188         smach.StateMachine.add(
00189             'MOVE_DELIVERY',
00190             SimpleActionState( '/move_base',
00191                                MoveBaseAction,
00192                                goal = gd ),
00193             transitions = {'succeeded':'DELIVERY'})
00194 
00195         sm_delivery = sm_rfid_delivery.sm_delivery()
00196         smach.StateMachine.add(
00197             'DELIVERY', # outcomes: succeeded, aborted, preempted
00198             sm_delivery,
00199             remapping = { 'tagid' : 'person_id'}, #input
00200             transitions = { 'succeeded': 'TUCKL_SUCC',
00201                             'aborted': 'TUCKL_ABOR'})
00202 
00203         smach.StateMachine.add(
00204             'TUCKL_SUCC',
00205             ServiceState( '/robotis/servo_left_pan_moveangle',
00206                           MoveAng,
00207                           request = MoveAngRequest( 1.350, 0.3, 0)), 
00208             transitions = { 'succeeded':'TUCKR_SUCC' })
00209         smach.StateMachine.add(
00210             'TUCKR_SUCC',
00211             ServiceState( '/robotis/servo_right_pan_moveangle',
00212                           MoveAng,
00213                           request = MoveAngRequest( -1.350, 0.3, 0)), 
00214             transitions = { 'succeeded':'BACKUP_AFTER_DELIVERY' })
00215 
00216         smach.StateMachine.add(
00217             'TUCKL_ABOR',
00218             ServiceState( '/robotis/servo_left_pan_moveangle',
00219                           MoveAng,
00220                           request = MoveAngRequest( 1.350, 0.3, 0)), 
00221             transitions = { 'succeeded':'TUCKR_ABOR' })
00222         smach.StateMachine.add(
00223             'TUCKR_ABOR',
00224             ServiceState( '/robotis/servo_right_pan_moveangle',
00225                           MoveAng,
00226                           request = MoveAngRequest( -1.350, 0.3, 0)), 
00227             transitions = { 'succeeded':'aborted' })
00228 
00229 
00230         smach.StateMachine.add(
00231             'BACKUP_AFTER_DELIVERY',
00232             ServiceState( '/rotate_backup',
00233                           RotateBackupSrv,
00234                           request = RotateBackupSrvRequest(0.0, -0.50)), 
00235             transitions = { 'succeeded':'ROTATE_AFTER_DELIVERY' })
00236 
00237         smach.StateMachine.add(
00238             'ROTATE_AFTER_DELIVERY',
00239             ServiceState( '/rotate_backup',
00240                           RotateBackupSrv,
00241                           request = RotateBackupSrvRequest( 3.14, 0.0)), 
00242             transitions = { 'succeeded':'MOVE_FINAL' })
00243 
00244         # Kitchen
00245         # gk = MoveBaseGoal()
00246         # gk.target_pose.header.frame_id = '/map'
00247         # gk.target_pose.header.stamp = rospy.Time(0)
00248         # gk.target_pose.pose.position.x = -1.61
00249         # gk.target_pose.pose.position.y =  0.88
00250         # gk.target_pose.pose.orientation.z = 0.91
00251         # gk.target_pose.pose.orientation.w = 0.40
00252 
00253         smach.StateMachine.add(
00254             'MOVE_FINAL',
00255             SimpleActionState( '/move_base',
00256                                MoveBaseAction,
00257                                goal = go ),  # Back down the hallway
00258             transitions = {'succeeded':'succeeded'})
00259 
00260         
00261 
00262             
00263     return sm
00264 
00265 if False:
00266     rospy.init_node('localization_trial')
00267 
00268     sm = smach.StateMachine( outcomes=['succeeded','aborted','preempted'] )
00269     with sm:
00270         # Just a precation
00271         tgoal = SingleJointPositionGoal()
00272         tgoal.position = 0.040  # all the way up is 0.200
00273         tgoal.min_duration = rospy.Duration( 2.0 )
00274         tgoal.max_velocity = 1.0
00275         smach.StateMachine.add(
00276             'TORSO_SETUP',
00277             SimpleActionState( 'torso_controller/position_joint_action',
00278                                SingleJointPositionAction,
00279                                goal = tgoal),
00280             transitions = { 'succeeded': 'succeeded' })
00281 
00282     sm.execute()
00283     
00284 
00285 if __name__ == '__main__':
00286 # if False:
00287     rospy.init_node('smach_aware_home')
00288 
00289     sm = cousins_demo()
00290 
00291     sis = IntrospectionServer('sm_aware_home', sm, '/SM_AWARE_HOME')
00292     sis.start()
00293 
00294     rospy.logout( 'READY TO RUN AWARE_HOME DEMO' )
00295 
00296     sm.userdata.person_id = 'person      '
00297     # sm.userdata.explore_radius = 1.5
00298     # sm.userdata.explore_rfid_reads = []
00299     outcome = sm.execute()
00300 
00301     # rospy.spin()
00302     sis.stop()
00303 
00304     
00305 


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