concurrent_hri_move_base.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import roslib; roslib.load_manifest('tibi_dabo_smachs')
00004 import rospy
00005 import smach
00006 import smach_ros
00007 import tf
00008 
00009 from tf.transformations import euler_from_quaternion
00010 from geometry_msgs.msg import Point, Pose, Quaternion
00011 from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
00012 from tibi_dabo_msgs.msg import sequenceAction, sequenceGoal
00013 from iri_perception_msgs.msg import peopleTrackAction
00014 from actionlib import *
00015 from actionlib.msg import *
00016 from smach_ros import SimpleActionState, ServiceState
00017 
00018 # hri goal callback
00019 def hri_goal_cb(userdata, goal):
00020   rospy.loginfo('hri_goal_cb')
00021   tts_goal = sequenceGoal()
00022   tts_goal.sequence_file = [None]*5
00023   tts_goal.sequence_file[0] = userdata.tts_in
00024   tts_goal.sequence_file[1] = ""
00025   tts_goal.sequence_file[2] = userdata.head_in
00026   tts_goal.sequence_file[3] = ""
00027   tts_goal.sequence_file[4] = ""
00028   return tts_goal
00029 
00030 # move base goal callback
00031 def move_base_goal_cb(userdata, goal):
00032   rospy.loginfo('move_base_goal_cb')
00033   nav_goal = MoveBaseGoal()
00034   nav_goal.target_pose.header.stamp    = rospy.Time.now()
00035   nav_goal.target_pose.header.frame_id = userdata.frame_id_in
00036   p = Point(userdata.goal_x_in, userdata.goal_y_in, 0)
00037   q = tf.transformations.quaternion_from_euler(0, 0, userdata.goal_theta_in)
00038   nav_goal.target_pose.pose = Pose(position=p, orientation=Quaternion(*q))
00039   return nav_goal
00040 
00041 # people tracking result callback
00042 def pt_result_cb(userdata, state, result):
00043   rospy.loginfo('result!! num_persons=%d',len(result.ps.peopleSet))
00044   if len(result.ps.peopleSet)<1:
00045     return 'aborted'
00046   else:
00047     userdata.frame_id_pt_out = result.ps.header.frame_id
00048     userdata.x_pt_out        = result.ps.peopleSet[0].x
00049     userdata.y_pt_out        = result.ps.peopleSet[0].y
00050     userdata.id_pt_out       = result.ps.peopleSet[0].targetId
00051     userdata.theta_pt_out    = 0
00052     return 'succeeded'
00053 
00054 # Check Distance  between robot and person
00055 
00056 
00057 
00058 # main
00059 def main():
00060     rospy.init_node('concurrent_hri_move_base')
00061 
00062     # Create a SMACH state machine
00063     sm = smach.StateMachine(['succeeded','aborted','preempted'])
00064     
00065     # Define userdata
00066     #sm.userdata.sm_frame_id   = "/tibi/base_link"
00067     #sm.userdata.sm_goal_x     = 2.0
00068     #sm.userdata.sm_goal_y     = 0.0
00069     #sm.userdata.sm_goal_theta = 0.0
00070 
00071     # gets called when ALL child states are terminated
00072     def hri_mb_out_cb(outcome_map):
00073       if outcome_map['CC_HRI'] == 'succeeded' and outcome_map['CC_MOVE_BASE'] == 'succeeded':
00074           return 'succeeded'
00075       else:
00076           return 'aborted'
00077 
00078     # Concurrence State: HRI + Move Base
00079     hri_mb_cc = smach.Concurrence(outcomes=['succeeded', 'aborted', 'preempted'],
00080                                   default_outcome='aborted',
00081                                   input_keys=['cc_hri_tts', 'cc_hri_head', 'cc_frame_id', 
00082                                               'cc_goal_x', 'cc_goal_y', 'cc_goal_theta'],
00083                                   child_termination_cb = lambda so: False,
00084                                   outcome_cb = hri_mb_out_cb)
00085     with hri_mb_cc:
00086         smach.Concurrence.add('CC_HRI',
00087                               smach_ros.SimpleActionState('hri',
00088                                                           sequenceAction,
00089                                                           goal_cb=hri_goal_cb,
00090                                                           input_keys=['tts_in','head_in']),
00091                               remapping={'tts_in':'cc_hri_tts',
00092                                          'head_in':'cc_hri_head'})
00093 
00094         smach.Concurrence.add('CC_MOVE_BASE',
00095                               smach_ros.SimpleActionState('MoveBase',
00096                                                           MoveBaseAction,
00097                                                           goal_cb=move_base_goal_cb,
00098                                                           input_keys=['frame_id_in', 'goal_x_in', 
00099                                                                       'goal_y_in', 'goal_theta_in']),
00100                               remapping={'frame_id_in':'cc_frame_id',
00101                                          'goal_x_in':'cc_goal_x',
00102                                          'goal_y_in':'cc_goal_y',
00103                                          'goal_theta_in':'cc_goal_theta'})
00104 
00105 
00106     # Add states to the State Machine
00107     with sm:
00108         # HRI Presentation
00109         sm.userdata.sm_hri_tts_1  = "I am looking for a person in the room"
00110         sm.userdata.sm_hri_head_1 = "head_random_4.xml"
00111         smach.StateMachine.add('HRI_BEFORE_CC',
00112                                smach_ros.SimpleActionState('hri',
00113                                                            sequenceAction,
00114                                                            goal_cb=hri_goal_cb,
00115                                                            input_keys=['tts_in', 'head_in']),
00116                                transitions={'succeeded':'AC_PT'},
00117                                remapping={'tts_in':'sm_hri_tts_1',
00118                                           'head_in':'sm_hri_head_1'})
00119                                
00120         # People Tracking Action Client
00121         smach.StateMachine.add('AC_PT',
00122                                smach_ros.SimpleActionState('people_tracking',
00123                                                            peopleTrackAction,
00124                                                            result_cb=pt_result_cb,
00125                                                            output_keys=['frame_id_pt_out','x_pt_out',
00126                                                                         'y_pt_out','id_pt_out','theta_pt_out']),
00127                                transitions={'succeeded':'CC_HRI_MB',
00128                                             'aborted':'HRI_FAIL'},
00129                                remapping={'frame_id_pt_out':'sm_frame_id',
00130                                           'x_pt_out':'sm_goal_x',
00131                                           'y_pt_out':'sm_goal_y',
00132                                           'id_pt_out':'sm_target_id',
00133                                           'theta_pt_out':'sm_goal_theta'})
00134 
00135         # HRI Fail
00136         sm.userdata.sm_hri_tts_2  = "I could not find anyone in this room"
00137         sm.userdata.sm_hri_head_2 = "head_random_4.xml"
00138         smach.StateMachine.add('HRI_FAIL',
00139                                smach_ros.SimpleActionState('hri',
00140                                                            sequenceAction,
00141                                                            goal_cb=hri_goal_cb,
00142                                                            input_keys=['tts_in', 'head_in']),
00143                                transitions={'succeeded':'HRI_BEFORE_CC'},
00144                                remapping={'tts_in':'sm_hri_tts_2',
00145                                           'head_in':'sm_hri_head_2'})
00146 
00147 
00148         # First concurrent state
00149         sm.userdata.sm_hri_tts_3  =  "I found a person close to me. I am approaching the target"
00150         sm.userdata.sm_hri_head_3 = "head_random_4.xml"
00151         smach.StateMachine.add('CC_HRI_MB',
00152                                hri_mb_cc,
00153                                transitions={'succeeded':'HRI_AFTER_CC'},
00154                                remapping={'cc_hri_tts':'sm_hri_tts_3',
00155                                           'cc_hri_head':'sm_hri_head_3',
00156                                           'cc_frame_id':'sm_frame_id', 
00157                                           'cc_goal_x':'sm_goal_x',  #puc posar 0 pq aixi no es moura?
00158                                           'cc_goal_y':'sm_goal_y',  #puc posar 0 pq aixi no es moura?
00159                                           'cc_goal_theta':'sm_goal_theta'})
00160 
00161         ## Second concurrent state
00162         #sm.userdata.sm_hri_tts_3 = "More concurrent text"
00163         #sm.userdata.sm_goal_x_2  = 1.0
00164         #smach.StateMachine.add('CC_HRI_MB_2',
00165                                #hri_mb_cc,
00166                                #transitions={'succeeded':'HRI_AFTER_CC'},
00167                                #remapping={'cc_hri_tts':'sm_hri_tts_3',
00168                                           #'cc_frame_id':'sm_frame_id', 
00169                                           #'cc_goal_x':'sm_goal_x_2',
00170                                           #'cc_goal_y':'sm_goal_y',
00171                                           #'cc_goal_theta':'sm_goal_theta'})
00172 
00173         # HRI After Concurrence State
00174         sm.userdata.sm_hri_tts_4  = "I wish I have someone standing in front of me"
00175         sm.userdata.sm_hri_head_4 = "head_random_4.xml"
00176         smach.StateMachine.add('HRI_AFTER_CC',
00177                                smach_ros.SimpleActionState('hri',
00178                                                            sequenceAction,
00179                                                            goal_cb=hri_goal_cb,
00180                                                            input_keys=['tts_in', 'head_in']),
00181                                transitions={'succeeded':'succeeded'},
00182                                remapping={'tts_in':'sm_hri_tts_4',
00183                                           'head_in':'sm_hri_head_4'})
00184 
00185     # Create and start the introspection server
00186     sis = smach_ros.IntrospectionServer('server_name', sm, '/SM_ROOT')
00187     sis.start()
00188 
00189     # Execute the state machine
00190     outcome = sm.execute()
00191 
00192     # Wait for ctrl-c to stop the application
00193     rospy.spin()
00194     sis.stop()
00195 
00196 
00197 if __name__ == '__main__':
00198     main()
00199 


tibi_dabo_smachs
Author(s): Joan Perez, jnperez at iri.upc.edu
autogenerated on Fri Dec 6 2013 20:04:35