Go to the documentation of this file.00001
00002 import roslib
00003 roslib.load_manifest('rfid_demos')
00004 import rospy
00005
00006 import smach
00007 import tf
00008 from smach_ros import SimpleActionState, ServiceState, IntrospectionServer
00009 import actionlib
00010
00011 from geometry_msgs.msg import PoseStamped
00012 from move_base_msgs.msg import MoveBaseAction
00013 from rfid_behaviors.srv import RecorderSrv
00014 from rfid_behaviors.msg import RecorderReads
00015
00016 import numpy as np, math
00017
00018
00019 class BestVantage(smach.State):
00020 def __init__(self):
00021 smach.State.__init__(self,
00022 outcomes=['succeeded', 'aborted'],
00023 input_keys = ['tagid', 'rfid_reads'],
00024 output_keys = ['best_vantage', 'filtered_reads'])
00025 self.initialized = False
00026 self.orig_reads = []
00027 self.curr_reads = []
00028 self.last_pose = None
00029
00030 def execute(self, userdata):
00031 rospy.logout('BestVantage: Calculating' )
00032
00033 if not self.initialized:
00034 self.initialized = True
00035
00036 self.orig_reads = userdata.rfid_reads
00037 self.curr_reads = [ r for r in self.orig_reads
00038 if r != None and r.read.rssi != -1 and
00039 r.read.tagID == userdata.tagid ]
00040 else:
00041 rospy.logout( 'BestVantage: First try did not work, eh? Filtering and retrying a new vantage.' )
00042 return 'aborted'
00043
00044 if not self.curr_reads:
00045 rospy.logout( 'BestVantage: No more positive-read poses to consider.' )
00046 return 'aborted'
00047
00048 rssi = [ r.read.rssi for r in self.curr_reads ]
00049 ind = np.argmax( rssi )
00050 best = self.curr_reads[ ind ]
00051 best_read = best.read
00052 best_ant = best.ps_ant_map
00053 best_base = best.ps_base_map
00054
00055
00056
00057
00058
00059
00060 ps = PoseStamped()
00061 ps.header.stamp = rospy.Time.now()
00062 ps.header.frame_id = best_base.header.frame_id
00063 ps.pose.position = best_base.pose.position
00064 ps.pose.orientation = best_ant.pose.orientation
00065
00066 userdata.best_vantage = ps
00067 userdata.filtered_reads = self.curr_reads
00068
00069 return 'succeeded'
00070
00071 def sm_best_vantage():
00072 sm = smach.StateMachine( outcomes = ['succeeded', 'aborted'],
00073 input_keys = ['rfid_reads', 'tagid'])
00074 with sm:
00075 smach.StateMachine.add(
00076 'SELECT_BEST_VANTAGE',
00077 BestVantage(),
00078 remapping = { 'tagid' : 'tagid',
00079 'rfid_reads' : 'rfid_reads',
00080 'best_vantage' : 'best_vantage',
00081 'filtered_reads' : 'filtered_reads' },
00082 transitions = {'succeeded':'MOVE_BEST_VANTAGE'})
00083
00084 smach.StateMachine.add(
00085 'MOVE_BEST_VANTAGE',
00086 SimpleActionState( '/move_base',
00087 MoveBaseAction,
00088 goal_slots = [ 'target_pose' ]),
00089 remapping = { 'target_pose' : 'best_vantage' },
00090 transitions = {'aborted':'SELECT_BEST_VANTAGE',
00091 'preempted':'aborted',
00092 'succeeded':'succeeded'})
00093
00094 return sm
00095
00096
00097
00098 if __name__ == '__main__':
00099 import sm_rfid_explore
00100
00101 rospy.init_node('smach_rfid_explore')
00102
00103 sm = smach.StateMachine(outcomes=['succeeded','aborted','preempted'],
00104 input_keys = ['tagid','explore_radius'])
00105
00106
00107 with sm:
00108 sm_search = sm_rfid_explore.sm_search()
00109 smach.StateMachine.add(
00110 'RFID_SEARCH',
00111 sm_search,
00112 remapping = { 'tagid' : 'tagid',
00113 'explore_radius' : 'explore_radius',
00114 'explore_rfid_reads' : 'explore_rfid_reads' },
00115
00116 transitions={'succeeded':'BEST_VANTAGE'})
00117
00118 sm_vantage = sm_best_vantage()
00119 smach.StateMachine.add(
00120 'BEST_VANTAGE',
00121 sm_vantage,
00122 remapping = { 'tagid' : 'tagid',
00123 'rfid_reads' : 'explore_rfid_reads' },
00124 transitions = {'succeeded':'succeeded'})
00125
00126 sis = IntrospectionServer('sm_rfid_explore', sm, '/SM_ROOT_RFID_EXPLORE')
00127 sis.start()
00128 rospy.sleep(3.0)
00129
00130 sm.userdata.tagid = 'person '
00131 sm.userdata.explore_radius = 2.7
00132
00133 outcome = sm.execute()
00134
00135
00136 rospy.spin()
00137 sis.stop()
00138
00139
00140
00141