process_traj_plots.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 # Basically a giant script.
00004 
00005 import roslib
00006 roslib.load_manifest( 'geometry_msgs' )  # the pickle files containe Point and Pose Stamped.
00007 roslib.load_manifest( 'sensor_msgs' )
00008 import rospy
00009 
00010 from geometry_msgs.msg import PointStamped, PoseStamped
00011 from sensor_msgs.msg import PointCloud
00012 
00013 
00014 import sys
00015 import glob
00016 import yaml
00017 import time
00018 import optparse
00019 import cPickle as pkl
00020 import numpy as np, math
00021 import pylab as pl
00022 
00023 import friis
00024 import point_cloud_utils as pcu
00025 
00026 PLOT = False
00027 
00028 # glob_files: '/home/travis/svn/robot1/src/projects/rfid_datacapture/src/rfid_datacapture/cap_360/datacap/*.pkl'
00029 # filters:
00030 #   antennas:
00031 #     PR2_Head: '/head_rfid'
00032 #   tags:
00033 #     'datacap     ':
00034 
00035 if __name__ == '__main__':
00036     p = optparse.OptionParser()
00037     p.add_option('--yaml', action='store', type='string', dest='yaml', default='',
00038                  help='yaml file that describes this run.')
00039     p.add_option('--plot', action='store_true', dest='plot',
00040                  help='Pop-up the resulting plot')
00041     opt, args = p.parse_args()
00042 
00043     yaml_fname = opt.yaml
00044     PLOT = opt.plot
00045 else:
00046     yaml_fname = ''
00047 
00048 
00049 # SCRIPT:
00050 
00051 
00052 if not yaml_fname:
00053     print 'YAML file required!'
00054     exit()
00055 else:
00056     f = open( yaml_fname )
00057     yaml_config = yaml.load( f )
00058     f.close()
00059 
00060 def add_files( d, arg ):
00061     fname, fcount = arg
00062     print 'Loading (%d of %d): %s' % (fcount, len(fnames), fname)
00063     f = open( fname, 'r' )
00064     d_new = pkl.load( f )
00065     f.close()
00066 
00067     for k in d_new.keys():
00068         if not d.has_key( k ):
00069             d[k] = []
00070         d[k] += d_new[k]
00071 
00072     return d
00073 
00074 def base_map_xy( reading ):
00075     # Model estimate of P^inc_tag.
00076     # r_rdr, theta_rdr, phi_rdr, r_tag, theta_tag, phi_tag, rssi, antname, tagid = reading
00077     d_rdr = reading[0]
00078     d_tag = reading[1]
00079     read = reading[2]
00080     d_rot = reading[3]
00081     ps = reading[4]
00082 
00083     tag_map, rdr_map, rot_map, base_map = ps
00084     return [ base_map.pose.position.x, base_map.pose.position.y ]
00085 
00086 
00087 fnames = reduce( lambda x,y: x+y, [ glob.glob(i) for i in yaml_config['glob_files'] ], [] )
00088 
00089 def pos_finish( reads_list ):
00090     d_rdr = reads_list[-1][0]
00091     d_tag = reads_list[-1][1]
00092     read = reads_list[-1][2]
00093     d_rot = reads_list[-1][3]
00094     ps = reads_list[-1][4]
00095 
00096     base_map = ps[3]
00097     bmx = base_map.pose.position.x
00098     bmy = base_map.pose.position.y
00099 
00100     if bmx < 7+2 and bmx > 7-2 and bmy > 3-2 and bmy < 3+2:
00101         rv = True
00102     else:
00103         rv = False
00104     return rv
00105 
00106 if len(glob.glob(yaml_config['use_combined'])) > 0:
00107     print 'Loading pickle: %s' % (yaml_config['use_combined'])
00108     f = open( yaml_config['use_combined'], 'r' )
00109     data = pkl.load( f )
00110     f.close()
00111     print 'Done.'
00112 else:
00113     f = open( yaml_config['use_combined'], 'w' )
00114     #d = reduce( add_files, zip(fnames,range(len(fnames))), {} )
00115 
00116     data = []
00117     # Apply Filters:
00118     for i,fname in enumerate( fnames ):
00119         print 'Loading (%d of %d): %s' % (i, len(fnames), fname)
00120         f = open( fname, 'r' )
00121         d_new = pkl.load( f )
00122         f.close()
00123 
00124         for k in d_new.keys():
00125             if dict.fromkeys(yaml_config['filters']['tags']).has_key( k ):
00126                 data += [ base_map_xy(r) + [ r[2][0], pos_finish( d_new[k] )] for r in d_new[k] ]
00127 
00128     data = np.array( data ).T # 4xN: x,y,rssi,positive finish
00129 
00130     print 'Dumping data into combined pickle file: %s ' % (yaml_config['use_combined'])
00131     f = open( yaml_config['use_combined'], 'w' )
00132     pkl.dump( data, f, -1 )
00133     f.close()
00134 
00135     print 'Done.  Re-run.'
00136     exit()
00137 
00138 
00139 # data will be 4xN => x, y, RSSI, positive finish (bool)
00140 
00141 # Calculate Useful Values
00142 
00143 xy = data[0:2,:]
00144 xyz = np.row_stack([ xy, np.zeros( xy.shape[1] )])
00145 rssi = data[2]
00146 pos_read = data[3]
00147 
00148 pp = pcu.np_points_to_ros( np.matrix(xyz[:,np.where(pos_read > 0.5)[0]]) )
00149 pp.header.frame_id = '/map'
00150 
00151 pn = pcu.np_points_to_ros( np.matrix(xyz[:,np.where(pos_read < 0.5)[0]]) )
00152 pn.header.frame_id = '/map'
00153 
00154 rospy.init_node( 'traj_pub_node' )
00155 time.sleep( 0.3 )
00156 print 'PUBLISHING'
00157 
00158 pubp = rospy.Publisher( 'traj_pub_pos', PointCloud )
00159 pubn = rospy.Publisher( 'traj_pub_neg', PointCloud )
00160 
00161 while not rospy.is_shutdown():
00162     pp.header.stamp = rospy.Time.now()
00163     pn.header.stamp = rospy.Time.now()
00164 
00165     pubp.publish( pp )
00166     pubn.publish( pn )
00167 
00168     rospy.sleep( 0.5 )


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