00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 import pickle
00019 import csv
00020 import pylab
00021 import sys
00022 import getopt
00023
00024 from math import *
00025 from scipy import *
00026 from numpy import *
00027 import numpy as np
00028
00029 import rospy
00030 import rosparam
00031 import yaml
00032 import savitzky
00033
00034 def main(argv):
00035 rospy.init_node('csv_proc')
00036
00037 volt_values=[]
00038 time_values=[]
00039
00040 sg = savitzky.savitzky_golay(window_size=901, order=3)
00041
00042
00043
00044
00045
00046 filename = rosparam.get_param("/csv_proc/file_name")
00047 robot_name = rosparam.get_param("/csv_proc/robot_name")
00048 mode = rosparam.get_param("/csv_proc/mode")
00049 yaml_file = open("voltage_filter.yaml", "w")
00050 yl = {}
00051
00052 if(mode=="update"):
00053 abcd = rosparam.get_param("/csv_proc/abcd")
00054
00055 try:
00056 opts, args = getopt.getopt(argv,"hf:r:",["ifile=", "irobot="])
00057 except getopt.GetoptError:
00058 print 'time_volt.py -f <filename> -r <robotname>'
00059 sys.exit(2)
00060 for opt, arg in opts:
00061 if opt == '-h':
00062 print 'time_volt.py -f <filename> -r <robotname>'
00063 sys.exit()
00064 elif opt in ("-f", "--ifile"):
00065 filename = arg
00066 elif opt in ("-r", "--irobot"):
00067 robot_name = arg
00068
00069
00070
00071
00072
00073
00074 with open(filename, 'rb') as csvfile:
00075
00076 csvreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
00077 for row in csvreader:
00078 row = row[0].split(',')
00079 volt_v = (float)(row[1]) * 1000.0
00080 if(volt_v < 48000 and volt_v > 44000):
00081 time_values.append((float)(row[0]))
00082 volt_values.append(volt_v)
00083
00084 time_values[:] = [x - time_values[0] for x in time_values]
00085 time_values = time_values[::-1]
00086
00087
00088
00089
00090
00091 pylab.figure(1)
00092
00093 pylab.plot(volt_values, time_values)
00094 pylab.ylabel("Time elapsed(seconds)")
00095 pylab.xlabel("Voltage(mV)")
00096 pylab.title("Time x Volt,file="+ filename.replace('.csv',''))
00097 pylab.grid(True)
00098
00099 secArray = np.asarray(time_values)
00100 voltArray = np.asarray(volt_values)
00101
00102
00103 z1 = np.polyfit(voltArray, secArray,1)
00104 z2 = np.polyfit(voltArray, secArray,2)
00105 z3 = np.polyfit(voltArray, secArray,3)
00106
00107
00108 xp = np.linspace(49000, 43000, 100)
00109
00110
00111 p1 = np.poly1d(z1)
00112 p2 = np.poly1d(z2)
00113 p3 = np.poly1d(z3)
00114
00115 pylab.plot(xp, p1(xp), 'r-', xp, p2(xp), 'g-', xp, p3(xp), 'm-')
00116
00117 pylab.text(46000, 11900, 'p1=' + p1.__str__(), bbox=dict(facecolor='red', alpha=0.5))
00118 pylab.text(46000, 11600, 'p2=' + p2.__str__(), bbox=dict(facecolor='green', alpha=0.5))
00119 pylab.text(46000, 11200, 'p3=' + p3.__str__(), bbox=dict(facecolor='magenta', alpha=0.5))
00120
00121 pylab.savefig(filename.replace('.csv',''), format="pdf")
00122
00123
00124
00125
00126
00127
00128 pylab.figure(2)
00129
00130 pylab.ylabel("Residuals(s)")
00131 pylab.xlabel("Voltage(mV)")
00132 pylab.title("Residuals x Voltage,file="+ filename.replace('.csv',''))
00133
00134
00135 z1_val = np.polyval(z1, volt_values)
00136 z2_val = np.polyval(z2, volt_values)
00137 z3_val = np.polyval(z3, volt_values)
00138
00139
00140 z1_res = time_values - z1_val
00141 z2_res = time_values - z2_val
00142 z3_res = time_values - z3_val
00143
00144 pylab.plot(time_values, z1_res, time_values, z2_res, time_values, z3_res)
00145
00146 pylab.grid()
00147
00148 pylab.legend(('Residuals 1st order', 'Residuals 2nd order', 'Residuals 3rd order'))
00149
00150 pylab.savefig(filename.replace('.csv','')+'_res', format="pdf")
00151
00152
00153
00154
00155
00156 values_filt = sg.filter(voltArray)
00157
00158 pylab.figure(3)
00159
00160 pylab.subplot(211)
00161
00162 pylab.plot(voltArray, time_values)
00163
00164 pylab.grid(True)
00165 pylab.title('Comparison between real and filtered data')
00166 pylab.ylabel('Real Values(mV)')
00167
00168 pylab.subplot(212)
00169
00170 pylab.plot(values_filt, time_values)
00171
00172 pylab.grid(True)
00173 pylab.ylabel('Filtered Values(mV)')
00174 pylab.xlabel('Time(s)')
00175
00176
00177
00178
00179
00180
00181 pylab.figure(4)
00182
00183 pylab.plot(values_filt, time_values)
00184 pylab.ylabel("Time elapsed(seconds)")
00185 pylab.xlabel("Voltage(mV)")
00186 pylab.title("Time x Volt,file="+ filename.replace('.csv',''))
00187 pylab.grid(True)
00188
00189 secArray = np.asarray(time_values)
00190
00191
00192 z1 = np.polyfit(values_filt, secArray,1)
00193 z2 = np.polyfit(values_filt, secArray,2)
00194 z3 = np.polyfit(values_filt, secArray,3)
00195
00196 if (mode=="initial"):
00197 z3_l = []
00198
00199 for el in z3:
00200 z3_l.append((float)(el))
00201
00202 abcd = z3_l
00203 yl["abcd"] = z3_l
00204 yl["theta"] = 0.
00205 yl["off_y"] = 0.
00206
00207
00208 xp = np.linspace(49000, 43000, 100)
00209
00210
00211 p1 = np.poly1d(z1)
00212 p2 = np.poly1d(z2)
00213 p3 = np.poly1d(z3)
00214
00215 pylab.plot(xp, p1(xp), 'r-', xp, p2(xp), 'g-', xp, p3(xp), 'm-')
00216
00217 pylab.text(46000, 11900, 'p1=' + p1.__str__(), bbox=dict(facecolor='red', alpha=0.5))
00218 pylab.text(46000, 11600, 'p2=' + p2.__str__(), bbox=dict(facecolor='green', alpha=0.5))
00219 pylab.text(46000, 11200, 'p3=' + p3.__str__(), bbox=dict(facecolor='magenta', alpha=0.5))
00220
00221
00222
00223
00224
00225 pylab.figure(5)
00226
00227 pylab.ylabel("Residuals(s)")
00228 pylab.xlabel("Voltage(mV)")
00229 pylab.title("Residuals x Voltage,file="+ filename.replace('.csv',''))
00230
00231
00232 z1_val = np.polyval(z1, values_filt)
00233 z2_val = np.polyval(z2, values_filt)
00234 z3_val = np.polyval(z3, values_filt)
00235
00236
00237 z1_res = time_values - z1_val
00238 z2_res = time_values - z2_val
00239 z3_res = time_values - z3_val
00240
00241 pylab.plot(time_values, z1_res, time_values, z2_res, time_values, z3_res)
00242
00243 pylab.grid()
00244
00245 pylab.legend(('Residuals 1st order', 'Residuals 2nd order', 'Residuals 3rd order'))
00246
00247
00248
00249
00250
00251 if(mode == "update"):
00252 pylab.figure(6)
00253
00254
00255 poly_vals = np.polyval(abcd, values_filt)
00256
00257
00258 pylab.plot(values_filt, poly_vals, values_filt, time_values)
00259
00260 pylab.legend(('Polynomial', 'Real'))
00261
00262 pylab.grid()
00263
00264 pylab.figure(7)
00265
00266 pylab.ylabel("Time available(seconds)")
00267 pylab.xlabel("Voltage(mV)")
00268 pylab.title("Time x Volt,file="+ filename.replace('.csv',''))
00269 pylab.grid(True)
00270
00271 poly_vals = np.polyval(abcd, values_filt)
00272
00273 ss = lambda y1, y2: ((y1-y2)**2).sum()
00274
00275
00276
00277
00278
00279 theta = -0.2
00280 theta_values = []
00281 cost_values = []
00282 off_values = []
00283
00284 while theta < 0.2:
00285 theta +=0.01
00286 new_x = values_filt*cos(theta) - poly_vals*sin(theta)
00287 new_y = values_filt*sin(theta) + poly_vals*cos(theta)
00288
00289
00290 off_y = -6000
00291
00292 cost_values_i =[]
00293 off_y_values_i=[]
00294
00295 while off_y < 6000:
00296 off_y +=200
00297 new_y_temp = new_y
00298 new_y_temp = new_y_temp + off_y
00299
00300 ss1=ss(time_values,new_y_temp)
00301 print ss1, off_y
00302
00303 cost_values_i.append(ss1)
00304 off_y_values_i.append(off_y)
00305
00306
00307
00308
00309 theta_values.append(theta)
00310 cost_min = min(cost_values_i)
00311 cost_min_index = cost_values_i.index(cost_min)
00312 cost_values.append(cost_values_i[cost_min_index])
00313 off_values.append(off_y_values_i[cost_min_index])
00314
00315 cost_min = min(cost_values)
00316 cost_min_index = cost_values.index(cost_min)
00317
00318 theta = theta_values[cost_min_index]
00319 off_y = off_values[cost_min_index]
00320
00321 new_x = values_filt*cos(theta) - poly_vals*sin(theta)
00322 new_y = values_filt*sin(theta) + poly_vals*cos(theta)
00323
00324 new_y = new_y + off_y
00325
00326 yl["abcd"] = abcd
00327 yl["theta"] = theta
00328 yl["off_y"] = off_y
00329 yl["maximum_time"] = (float)(new_y[0])
00330
00331 pylab.plot(poly_vals, values_filt, time_values, values_filt, new_y, values_filt)
00332
00333 pylab.legend(('Poly not moving', 'Real', 'Shifted Fit'))
00334
00335 yaml.safe_dump(yl, yaml_file,default_flow_style=False)
00336
00337 yaml_file.close()
00338
00339 pylab.show()
00340
00341
00342
00343 if __name__=="__main__":
00344 main(sys.argv[1:])