00001
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('tf')
00010 roslib.load_manifest('rfid_pf')
00011 import rospy
00012
00013 import rfid_datacapture.math_util as mu
00014 import sm_aware_home_explore as ahe
00015 import glob
00016 import yaml
00017 import tf
00018 import tf.transformations as tft
00019 import json
00020 import numpy as np, math
00021 import cPickle as pkl
00022 import template
00023 import rfid_pf.pf_stats as pfs
00024
00025 res = []
00026
00027 def process_trialobj( trial_num, obj_num, servo_yn ):
00028 obj_name = ahe.tdb[ obj_num ][0]
00029 tname = obj_name.replace( ' ', '' )
00030
00031 loc = (trial_num + obj_num) % 9
00032 loc_name = ahe.pts[loc][0]
00033 loc_pos = np.array(ahe.pts[loc][1])
00034
00035 print 'Trial %d with Object %d (%s) at Position %d (%s)' % (trial_num, obj_num, obj_name, loc, loc_name)
00036
00037 fname = 'search_aware_home/woot_150_'+str(trial_num)+'_reads.pkl'
00038 f = open( fname, 'r' )
00039 summary_search = pkl.load( f )
00040 f.close()
00041
00042 pos_readings_search = sum([ True for p in summary_search if p.read.rssi != -1 and p.read.tagID == obj_name ])
00043 tot_readings_search = len( summary_search )
00044
00045 search_positions_fname = 'search_aware_home/woot_150_' + str(trial_num) + '_tag_' + tname + '.yaml'
00046 servo_positions_fname = 'search_aware_home/woot_150_' + str(trial_num) + '_tag_' + tname + '_end.txt'
00047 servo_fname = 'search_aware_home/woot_150_' + str(trial_num) + '_tag_' + tname + '_servo.pkl'
00048
00049
00050 if pos_readings_search == 0:
00051 print '\t No results for this instance.'
00052
00053 res = { 'loc': loc,
00054 'obj_num': obj_num,
00055 'trial_num': trial_num,
00056 'pos_readings': pos_readings_search,
00057 'tot_readings':tot_readings_search,
00058 'best_pos': '--',
00059 'orient_est': '--',
00060 'dxy': '--',
00061 'dtheta': '--',
00062 'servo_yn': servo_yn,
00063 'other': { 'w_mass': 0.0,
00064 'orient_fit': 0.0 }}
00065 return False, res, None
00066
00067
00068
00069 f = open( search_positions_fname )
00070 y = yaml.load( f )
00071 f.close()
00072
00073
00074 efq = tft.euler_from_quaternion
00075 search_theta = efq( [ y['pose']['orientation']['x'],
00076 y['pose']['orientation']['y'],
00077 y['pose']['orientation']['z'],
00078 y['pose']['orientation']['w'] ])[-1]
00079
00080 search_pos = np.array([ y['pose']['position']['x'],
00081 y['pose']['position']['y'],
00082 y['pose']['position']['z'] ])
00083
00084 search_true_theta = np.arctan2( loc_pos[1] - search_pos[1],
00085 loc_pos[0] - search_pos[0] )
00086 search_theta_diff = mu.standard_rad( search_theta - search_true_theta )
00087
00088 search_pos_diff = np.linalg.norm( search_pos[0:2] - loc_pos[0:2] )
00089
00090
00091 if not servo_yn:
00092 res = { 'loc': loc,
00093 'obj_num': obj_num,
00094 'trial_num': trial_num,
00095 'pos_readings': pos_readings_search,
00096 'tot_readings':tot_readings_search,
00097 'best_pos': search_pos,
00098 'orient_est': search_theta,
00099 'dxy': search_pos_diff,
00100 'dtheta': search_theta_diff,
00101 'servo_yn': servo_yn }
00102 return True, res, None
00103
00104
00105
00106
00107 f = open( servo_fname, 'r' )
00108 summary_servo = pkl.load( f )
00109 f.close()
00110
00111 pos_readings_servo = sum([ True for p in summary_servo if p.read.rssi != -1 and p.read.tagID == obj_name ])
00112 tot_readings_servo = len( summary_servo )
00113
00114
00115 f = open( servo_positions_fname )
00116 r = f.readlines()
00117 f.close()
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 rpy = r[-1].find('RPY')+3
00128 servo_theta = json.loads( r[-1][rpy:] )[-1]
00129
00130 tion = r[-3].find('tion:')+5
00131 servo_pos = np.array(json.loads( r[-3][tion:] ))
00132
00133 servo_true_theta = np.arctan2( loc_pos[1] - servo_pos[1],
00134 loc_pos[0] - servo_pos[0] )
00135 servo_theta_diff = mu.standard_rad( servo_theta - servo_true_theta )
00136
00137 servo_pos_diff = np.linalg.norm( servo_pos[0:2] - loc_pos[0:2] )
00138
00139
00140
00141
00142
00143
00144 res = { 'loc': loc,
00145 'obj_num': obj_num,
00146 'trial_num': trial_num,
00147 'pos_readings': pos_readings_search + pos_readings_servo,
00148 'tot_readings':tot_readings_search + tot_readings_servo,
00149 'best_pos': servo_pos,
00150 'orient_est': servo_theta,
00151 'dxy': servo_pos_diff,
00152 'dtheta': servo_theta_diff,
00153 'servo_yn': servo_yn }
00154
00155 return True, res, None
00156
00157 return
00158
00159
00160
00161
00162
00163
00164 if __name__ == '__main__':
00165
00166 import optparse
00167 p = optparse.OptionParser()
00168 p.add_option('--trial', action='store', type='int', dest='trial',
00169 help='trial number (0-8)')
00170 p.add_option('--obj', action='store', type='int', dest='obj',
00171 help='object number (0-8)')
00172 p.add_option('--servo', action='store_true', dest='servo',
00173 help='Use combined search and servo?', default = False)
00174 opt, args = p.parse_args()
00175
00176 obj_num = opt.obj
00177 trial_num = opt.trial
00178 servo_yn = opt.servo
00179
00180 rospy.init_node( 'goober' )
00181
00182
00183 if trial_num < 9:
00184 print 'Publishing.'
00185 was_reads, res, p_set = process_trialobj( trial_num, obj_num, servo_yn )
00186
00187 print 'RESULTS'
00188 pfs.pprint( res )
00189
00190 f = open( 'Obj%d_Trial%d_Servo%d_rel_results.pkl' % (obj_num, trial_num, int( servo_yn )), 'w')
00191 pkl.dump( res, f )
00192 f.close()
00193
00194 else:
00195
00196
00197 for i in range( 0, 9 ):
00198 for j in range( 0, 9 ):
00199 for s in [False, True]:
00200
00201 was_reads, res, p_set = process_trialobj( trial_num = i, obj_num = j, servo_yn = s )
00202
00203 print 'RESULTS'
00204 pfs.pprint( res )
00205
00206 f = open( 'Obj%d_Trial%d_Servo%d_rel_results.pkl' % ( j, i, int( s )), 'w')
00207 pkl.dump( res, f )
00208 f.close()
00209