recorder.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 import roslib
00003 roslib.load_manifest('rfid_behaviors')
00004 import rospy
00005 
00006 import tf
00007 
00008 from rfid_behaviors.srv import RecorderSrv, RecorderSrvResponse
00009 from rfid_behaviors.srv import NextBestVantage
00010 from rfid_behaviors.msg import RecorderReads
00011 import hrl_rfid.ros_M5e_client as rmc
00012 from geometry_msgs.msg import PoseStamped
00013 
00014 import numpy as np, math
00015 
00016 class Recorder( ):
00017     def __init__( self, serv_name = 'rfid_recorder', node_name = 'rfid_recorder_py' ):
00018         rospy.logout( 'rfid_recorder: initializing' )
00019         try:
00020             rospy.init_node(node_name)
00021         except:
00022             pass
00023 
00024         self.should_rec = False
00025 
00026         self._servrec = rospy.Service( serv_name + '/record',
00027                                        RecorderSrv,
00028                                        self.process_service )
00029 
00030         self._servbest = rospy.Service( serv_name + '/best_vantage',
00031                                         NextBestVantage,
00032                                         self.bestvantage )
00033 
00034         self.listener = tf.TransformListener()
00035         rospy.logout( 'RFID Recorder: Waiting on transforms' )
00036         self.listener.waitForTransform('/ear_antenna_left', '/map',
00037                                        rospy.Time(0), timeout = rospy.Duration(100) )
00038         self.listener.waitForTransform('/ear_antenna_right', '/map',
00039                                        rospy.Time(0), timeout = rospy.Duration(100) )
00040         
00041 
00042         rospy.logout( 'rfid_recorder: ready' )
00043 
00044     def process_service( self, req ):
00045         self.should_rec = not self.should_rec  # toggle state.  (bad way to do this...)
00046 
00047         if self.should_rec == True:
00048             self.data = []
00049             self.rec = rmc.ROS_M5e_Client('ears', callbacks = [self.add_datum])
00050             rospy.logout( 'RFID Recorder: Logging Reads.' )
00051             rv = RecorderSrvResponse()
00052             rv.rfid_reads = []
00053         else:
00054             rospy.logout( 'RFID Recorder: Halting recorder.' )
00055             self.rec.unregister() # Stop processing new reads
00056             rospy.sleep( 0.5 ) # Give it some time to settle
00057             rv = RecorderSrvResponse()
00058             rv.rfid_reads = list(self.data) # Save the data.
00059 
00060         self.recorder_data = list( self.data )
00061         return rv
00062 
00063     def process_datum( self, datum ):
00064         # Hooray for lexical scope (listener)!
00065         ant_lookup = { 'EleLeftEar': '/ear_antenna_left',
00066                        'EleRightEar': '/ear_antenna_right' }
00067 
00068         ps_ant = PoseStamped()
00069         ps_ant.header.stamp = rospy.Time( 0 )
00070         ps_ant.header.frame_id = ant_lookup[ datum.antenna_name ]
00071 
00072         ps_base = PoseStamped()
00073         ps_base.header.stamp = rospy.Time( 0 )
00074         ps_base.header.frame_id = '/base_link'
00075 
00076         try:
00077             ps_ant_map = self.listener.transformPose( '/map', ps_ant )
00078             ps_base_map = self.listener.transformPose( '/map', ps_base )
00079             rv = RecorderReads()
00080             rv.read = datum
00081             rv.ps_ant_map = ps_ant_map
00082             rv.ps_base_map = ps_base_map 
00083         except:
00084             rospy.logout( 'RFID Recorder: TF failed. Ignoring read.' )
00085             rv = None
00086         return rv
00087 
00088     def add_datum( self, datum ):
00089         # Hooray for lexical scope (data)!
00090         pd = self.process_datum( datum )
00091         if pd != None:
00092             self.data.append( pd )
00093 
00094     def bestvantage(self, req):
00095         rospy.logout('Recorder: Calculating best vantage for tag \'%s\'' % req.tagid)
00096 
00097         d = {}
00098         for rr in self.recorder_data:  # rr is RecorderRead
00099             if not d.has_key( rr.read.tagID ):
00100                 d[rr.read.tagID] = []
00101             d[rr.read.tagID].append( rr )
00102 
00103         pos_reads = []
00104         if d.has_key( req.tagid ):
00105             pos_reads = [ q for q in d[ req.tagid ] if q.read.rssi != -1 ]  # list of RecorderReads
00106 
00107         if not pos_reads: # check at least one positive reading
00108             rospy.warn( 'Recorder: Desired tag had no readings.' )
00109             rv = PoseStamped()
00110             rv.header.frame_id = 'base_link'
00111             rv.header.stamp = rospy.Time.now()
00112             rv.pose.orientation.w = 1.0
00113             return rv
00114 
00115         # Select the RecorderRead with greatest RSSI
00116         rssi = [ r.read.rssi for r in pos_reads ]
00117         ind = np.argmax( rssi )
00118 
00119         best = pos_reads[ ind ] # RecorderRead
00120         best_read = best.read
00121         best_ant = best.ps_ant_map
00122         best_base = best.ps_base_map
00123 
00124         #print best_read, best_ant, best_base
00125 
00126         # We're going to keep the <x,y> location from the baselink (mapframe),
00127         # but keep <ang> (mapframe) from the antenna.
00128 
00129         rv = PoseStamped()
00130         rv.header.stamp = rospy.Time.now()
00131         rv.header.frame_id = best_base.header.frame_id
00132         rv.pose.position = best_base.pose.position
00133         rv.pose.orientation = best_ant.pose.orientation
00134 
00135         return rv
00136     
00137     
00138         
00139 if __name__ == '__main__':
00140     rec = Recorder()
00141     rospy.spin()


rfid_behaviors
Author(s): Travis Deyle
autogenerated on Wed Nov 27 2013 11:50:14