24 from math
import cos, sin
33 rospy.init_node(
'csv_proc')
44 filename = rosparam.get_param(
"/csv_proc/file_name")
45 robot_name = rosparam.get_param(
"/csv_proc/robot_name")
46 mode = rosparam.get_param(
"/csv_proc/mode")
47 yaml_file = open(
"voltage_filter.yaml",
"w")
51 abcd = rosparam.get_param(
"/csv_proc/abcd")
54 opts, args = getopt.getopt(argv,
"hf:r:",[
"ifile=",
"irobot="])
55 except getopt.GetoptError:
56 print(
'time_volt.py -f <filename> -r <robotname>')
60 print(
'time_volt.py -f <filename> -r <robotname>')
62 elif opt
in (
"-f",
"--ifile"):
64 elif opt
in (
"-r",
"--irobot"):
72 with open(filename,
'rb')
as csvfile:
74 csvreader = csv.reader(csvfile, delimiter=
' ', quotechar=
'|')
76 row = row[0].split(
',')
77 volt_v = (float)(row[1]) * 1000.0
78 if(volt_v < 48000
and volt_v > 44000):
79 time_values.append((float)(row[0]))
80 volt_values.append(volt_v)
82 time_values[:] = [x - time_values[0]
for x
in time_values]
83 time_values = time_values[::-1]
91 pylab.plot(volt_values, time_values)
92 pylab.ylabel(
"Time elapsed(seconds)")
93 pylab.xlabel(
"Voltage(mV)")
94 pylab.title(
"Time x Volt,file="+ filename.replace(
'.csv',
''))
97 secArray = np.asarray(time_values)
98 voltArray = np.asarray(volt_values)
101 z1 = np.polyfit(voltArray, secArray,1)
102 z2 = np.polyfit(voltArray, secArray,2)
103 z3 = np.polyfit(voltArray, secArray,3)
106 xp = np.linspace(49000, 43000, 100)
113 pylab.plot(xp,
p1(xp),
'r-', xp,
p2(xp),
'g-', xp,
p3(xp),
'm-')
115 pylab.text(46000, 11900,
'p1=' + p1.__str__(), bbox=dict(facecolor=
'red', alpha=0.5))
116 pylab.text(46000, 11600,
'p2=' + p2.__str__(), bbox=dict(facecolor=
'green', alpha=0.5))
117 pylab.text(46000, 11200,
'p3=' + p3.__str__(), bbox=dict(facecolor=
'magenta', alpha=0.5))
119 pylab.savefig(filename.replace(
'.csv',
''), format=
"pdf")
128 pylab.ylabel(
"Residuals(s)")
129 pylab.xlabel(
"Voltage(mV)")
130 pylab.title(
"Residuals x Voltage,file="+ filename.replace(
'.csv',
''))
133 z1_val = np.polyval(z1, volt_values)
134 z2_val = np.polyval(z2, volt_values)
135 z3_val = np.polyval(z3, volt_values)
138 z1_res = time_values - z1_val
139 z2_res = time_values - z2_val
140 z3_res = time_values - z3_val
142 pylab.plot(time_values, z1_res, time_values, z2_res, time_values, z3_res)
146 pylab.legend((
'Residuals 1st order',
'Residuals 2nd order',
'Residuals 3rd order'))
148 pylab.savefig(filename.replace(
'.csv',
'')+
'_res', format=
"pdf")
154 values_filt = sg.filter(voltArray)
160 pylab.plot(voltArray, time_values)
163 pylab.title(
'Comparison between real and filtered data')
164 pylab.ylabel(
'Real Values(mV)')
168 pylab.plot(values_filt, time_values)
171 pylab.ylabel(
'Filtered Values(mV)')
172 pylab.xlabel(
'Time(s)')
181 pylab.plot(values_filt, time_values)
182 pylab.ylabel(
"Time elapsed(seconds)")
183 pylab.xlabel(
"Voltage(mV)")
184 pylab.title(
"Time x Volt,file="+ filename.replace(
'.csv',
''))
187 secArray = np.asarray(time_values)
190 z1 = np.polyfit(values_filt, secArray,1)
191 z2 = np.polyfit(values_filt, secArray,2)
192 z3 = np.polyfit(values_filt, secArray,3)
194 if (mode==
"initial"):
198 z3_l.append((float)(el))
206 xp = np.linspace(49000, 43000, 100)
213 pylab.plot(xp,
p1(xp),
'r-', xp,
p2(xp),
'g-', xp,
p3(xp),
'm-')
215 pylab.text(46000, 11900,
'p1=' + p1.__str__(), bbox=dict(facecolor=
'red', alpha=0.5))
216 pylab.text(46000, 11600,
'p2=' + p2.__str__(), bbox=dict(facecolor=
'green', alpha=0.5))
217 pylab.text(46000, 11200,
'p3=' + p3.__str__(), bbox=dict(facecolor=
'magenta', alpha=0.5))
225 pylab.ylabel(
"Residuals(s)")
226 pylab.xlabel(
"Voltage(mV)")
227 pylab.title(
"Residuals x Voltage,file="+ filename.replace(
'.csv',
''))
230 z1_val = np.polyval(z1, values_filt)
231 z2_val = np.polyval(z2, values_filt)
232 z3_val = np.polyval(z3, values_filt)
235 z1_res = time_values - z1_val
236 z2_res = time_values - z2_val
237 z3_res = time_values - z3_val
239 pylab.plot(time_values, z1_res, time_values, z2_res, time_values, z3_res)
243 pylab.legend((
'Residuals 1st order',
'Residuals 2nd order',
'Residuals 3rd order'))
249 if(mode ==
"update"):
253 poly_vals = np.polyval(abcd, values_filt)
256 pylab.plot(values_filt, poly_vals, values_filt, time_values)
258 pylab.legend((
'Polynomial',
'Real'))
264 pylab.ylabel(
"Time available(seconds)")
265 pylab.xlabel(
"Voltage(mV)")
266 pylab.title(
"Time x Volt,file="+ filename.replace(
'.csv',
''))
269 poly_vals = np.polyval(abcd, values_filt)
271 ss =
lambda y1, y2: ((y1-y2)**2).sum()
284 new_x = values_filt*cos(theta) - poly_vals*sin(theta)
285 new_y = values_filt*sin(theta) + poly_vals*cos(theta)
286 print(
"new_x: {}, new_y: {}".
format(new_x, new_y))
297 new_y_temp = new_y_temp + off_y
299 ss1=
ss(time_values,new_y_temp)
302 cost_values_i.append(ss1)
303 off_y_values_i.append(off_y)
308 theta_values.append(theta)
309 cost_min = min(cost_values_i)
310 cost_min_index = cost_values_i.index(cost_min)
311 cost_values.append(cost_values_i[cost_min_index])
312 off_values.append(off_y_values_i[cost_min_index])
314 cost_min = min(cost_values)
315 cost_min_index = cost_values.index(cost_min)
317 theta = theta_values[cost_min_index]
318 off_y = off_values[cost_min_index]
320 new_x = values_filt*cos(theta) - poly_vals*sin(theta)
321 new_y = values_filt*sin(theta) + poly_vals*cos(theta)
322 print(
"new_x: {}, new_y: {}".
format(new_x, new_y))
324 new_y = new_y + off_y
329 yl[
"maximum_time"] = (float)(new_y[0])
331 pylab.plot(poly_vals, values_filt, time_values, values_filt, new_y, values_filt)
333 pylab.legend((
'Poly not moving',
'Real',
'Shifted Fit'))
335 yaml.safe_dump(yl, yaml_file,default_flow_style=
False)
343 if __name__==
"__main__":