process_servo_reads.py
Go to the documentation of this file.
00001 #! /usr/bin/python
00002 import roslib
00003 roslib.load_manifest('smach_ros')
00004 roslib.load_manifest('actionlib')
00005 roslib.load_manifest('rfid_datacapture')
00006 roslib.load_manifest('rfid_demos')
00007 roslib.load_manifest('rfid_behaviors')
00008 roslib.load_manifest('hrl_lib')
00009 roslib.load_manifest('rosbag')
00010 import rospy
00011 
00012 import tf
00013 
00014 from rfid_behaviors.srv import RecorderSrv, RecorderSrvResponse
00015 from rfid_behaviors.srv import NextBestVantage
00016 from rfid_behaviors.msg import RecorderReads
00017 import hrl_rfid.ros_M5e_client as rmc
00018 from geometry_msgs.msg import PoseStamped
00019 from hrl_rfid.msg import RFIDread
00020 from hrl_lib import util
00021 
00022 import process_bags_utils as pbut
00023 from rfid_behaviors import recorder
00024 import rosbag
00025 import glob
00026 import numpy as np,math
00027 
00028 SERVO_FNAMES = 'search_cap/search_aware_home/*_servo.bag'
00029 
00030 
00031 # This is a modified version of rfid_behaviors.recorder.py
00032 class TmpRecorder( ):
00033     def __init__( self, serv_name = 'rfid_recorder', node_name = 'rfid_recorder_py' ):
00034         rospy.logout( 'rfid_recorder: initializing' )
00035         try:
00036             rospy.init_node(node_name)
00037         except:
00038             pass
00039 
00040         self.name = 'ears'
00041         
00042         self.should_rec = False
00043 
00044         self.listener = tf.TransformListener()
00045         # rospy.logout( 'RFID Recorder: Waiting on transforms' )
00046         # self.listener.waitForTransform('/ear_antenna_left', '/map',
00047         #                                rospy.Time(0), timeout = rospy.Duration(100) )
00048         # self.listener.waitForTransform('/ear_antenna_right', '/map',
00049         #                                rospy.Time(0), timeout = rospy.Duration(100) )
00050 
00051         self.data = []
00052         self._sub = rospy.Subscriber( '/rfid/' + self.name + '_reader', RFIDread, self.add_datum)
00053 
00054         rospy.logout( 'rfid_recorder: ready' )
00055 
00056     def process_datum( self, datum ):
00057         # Hooray for lexical scope (listener)!
00058         ant_lookup = { 'EleLeftEar': '/ear_antenna_left',
00059                        'EleRightEar': '/ear_antenna_right' }
00060 
00061         ps_ant = PoseStamped()
00062         ps_ant.header.stamp = rospy.Time( 0 )
00063         ps_ant.header.frame_id = ant_lookup[ datum.antenna_name ]
00064 
00065         ps_base = PoseStamped()
00066         ps_base.header.stamp = rospy.Time( 0 )
00067         ps_base.header.frame_id = '/base_link'
00068 
00069         try:
00070             ps_ant_map = self.listener.transformPose( '/map', ps_ant )
00071             ps_base_map = self.listener.transformPose( '/map', ps_base )
00072             rv = RecorderReads()
00073             rv.read = datum
00074             rv.ps_ant_map = ps_ant_map
00075             rv.ps_base_map = ps_base_map 
00076         except:
00077             rospy.logout( 'RFID Recorder: TF failed. Ignoring read.' )
00078             rv = None
00079         return rv
00080 
00081     def add_datum( self, datum ):
00082         # Hooray for lexical scope (data)!
00083         pd = self.process_datum( datum )
00084         if pd != None:
00085             self.data.append( pd )
00086 
00087 
00088 
00089 def order_bagfiles( fnames ):
00090     # I'm too lazy to figure out how to reset time and prevent "TF_OLD_DATA" errors / warnings.
00091     #   Instead, we're just going to order the bag playback in wall-clock order.
00092 
00093     rospy.logout( 'Ordering the bagfiles in increasing order of start time.' )
00094     def gettime( fname ):
00095         # returns the timestamp of the first message
00096         b = rosbag.Bag( fname )
00097         msg = b.read_messages().next()
00098         tt = msg[-1].to_sec()
00099         b.close()
00100         return tt
00101 
00102     start_times = [ gettime( f ) for f in fnames ]
00103     rospy.logout( 'Done ordering.' )
00104     return [ fnames[ind] for ind in np.argsort( start_times ) ]
00105 
00106 
00107 if __name__ == '__main__':
00108     import optparse
00109     p = optparse.OptionParser()
00110     p.add_option('--fname', action='store', dest='fname',
00111                  help='filename', default = '')
00112     opt, args = p.parse_args()
00113 
00114     
00115     rospy.init_node( 'process_servo_reads' )
00116 
00117     ordered_fnames = order_bagfiles( glob.glob(SERVO_FNAMES) )
00118 
00119     print 'starting recorder.'
00120     rec = TmpRecorder( serv_name = 'temp_recorder', node_name = 'temp_recorder_py' )
00121     print 'done starting'
00122 
00123     for i,fname in enumerate( ordered_fnames ):
00124         rospy.logout( 'Processing [ %d of %d ]: %s' % (i+1, len(ordered_fnames), fname) )
00125 
00126         rec.data = []
00127 
00128         # Start the new bagplay
00129         bp = pbut.bagplay( fname )
00130         bp.run()
00131 
00132         while not bp.is_finished():
00133             print 'Still waiting...'
00134             pbut.sim_safe_sleep( 1.0 )  # Cannot use rostime, since it will stall when bag stops
00135         print 'Done Waiting.'
00136 
00137         print 'Saving recorder pickle data.'
00138         util.save_pickle( rec.data, fname.replace('.bag','.pkl') )


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