-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKajeMooshi_Gui_Log.py
217 lines (185 loc) · 5.88 KB
/
KajeMooshi_Gui_Log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import time, os, re
from datetime import datetime
import calendar
#import numpy as np
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import GetPaths
import Tkinter
from collections import deque
"""
KajeMooshiGui_plot_Log.py
This script plots the data from log file live
"""
#Set the filename and open the file
logfilename = GetPaths.GetMooshiLogFile_ForRead()
logfile = open(logfilename,'r')
#Find the size of the file and move to the end
st_results = os.stat(logfilename)
st_size = st_results[6]
logfile.seek(st_size)
MAX_FALLBEHIND = -5
class Cdata:
def __init__(self):
self.dates = deque()
self.x = deque()
self.y_U = deque()
self.y_I = deque()
def NewData_AppendDataset(new_timestamp,tmp_meas_A_kaje,tmp_meas_V_kaje,tmp_meas_Iraw,tmp_meas_Uraw):
timestampnow=datetime.now()
print "time Log: " + new_timestamp.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
print "time now: " + timestampnow.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
print "A_kaje %.3f A "%tmp_meas_A_kaje
print "V_Kaje %.3f V "%tmp_meas_V_kaje
print "I_raw 0x%06X "%tmp_meas_Iraw
print "U_raw 0x%06X "%tmp_meas_Uraw
#print "delta xxx: " , delta.total_seconds()
#timePlotStart=datetime.now()
mydata.dates.append(new_timestamp)
mydata.x.append(0) # to keep length persistant
mydata.y_U.append(tmp_meas_V_kaje)
mydata.y_I.append(tmp_meas_A_kaje)
return
def Dataset_CalcRealtime():
timestampnow=datetime.now()
for k in range(0, len(mydata.dates)):
mydata.x[k]=-((timestampnow - mydata.dates[k]).total_seconds())
#print "mydata.x[%i]=%.1f " % (k,mydata.x[k])
def Dataset_ChopOld():
while(len(mydata.dates)>20):
mydata.dates.popleft()
mydata.x.popleft()
mydata.y_U.popleft()
mydata.y_I.popleft()
return
if True:
while(mydata.x[0] < MAX_FALLBEHIND):
mydata.dates.popleft()
mydata.x.popleft()
mydata.y_U.popleft()
mydata.y_I.popleft()
def Plot_Update():
print "Plot_Update"
if(len(mydata.y_U)<1):
return
tmp_meas_V_kaje = mydata.y_U[-1]
tmp_meas_A_kaje = mydata.y_I[-1]
#line1.set_data([1,2],[3,tmp_meas_A_kaje]);
line1.set_data(mydata.x ,mydata.y_U);
line2.set_data(mydata.x ,mydata.y_I);
lbl1_string.set("U = %.3f V"%tmp_meas_V_kaje)
lbl2_string.set("I = %.3f A"%tmp_meas_A_kaje)
print "mydata.x=" , mydata.x
#print "mydata.y_U=" , mydata.y_U
for axFor in [ax1, ax2]:
axFor.relim()
# axFor.set_xlim(MAX_FALLBEHIND, 0)
# axFor.autoscale(True,'y',True)
axFor.autoscale(True,'both',True)
axFor.set_xlim(MAX_FALLBEHIND, 0)
def Plot_redraw():
print "Plot_redraw"
plt.draw()
canvas1.draw()
canvas2.draw()
#line1.draw()
#line2.draw()
#ax1.show()
#plt.pause(0.0001) #Note this correction
#timePlotStop=datetime.now()
#print "plotting took %.3f seconds"%((timePlotStop-timePlotStart).total_seconds())
def CheckLogForNewLine():
where = logfile.tell()
line = logfile.readline()
iLines=0
while(line):
iLines=iLines+1
print "new line"
#print line, # already has newline
#mo=re.search("this is line ([0-9]*)", line)
sRegex="([^;]*);" #time
sRegex+=" AKaje=([\+\-0-9\.]*);"
sRegex+=" VKaje=([\+\-0-9\.]*);"
sRegex+=" Iraw=([\+\-0-9\.]*);"
sRegex+=" Uraw=([\+\-0-9\.]*);"
#print sRegex
mo=re.search(sRegex, line)
if mo:
# print "match"
tmp_timestamp_string=mo.group(1)
# '%Y-%m-%d %H:%M:%S.%f'
tmp_timestamp=datetime.strptime(tmp_timestamp_string, '%Y-%m-%d %H:%M:%S.%f')
tmp_meas_A_kaje=float(mo.group(2))
tmp_meas_V_kaje=float(mo.group(3))
tmp_meas_Iraw =float(mo.group(4))
tmp_meas_Uraw =float(mo.group(5))
NewData_AppendDataset(tmp_timestamp,tmp_meas_A_kaje,tmp_meas_V_kaje,tmp_meas_Iraw,tmp_meas_Uraw)
else:
print "no RegEx match"
print "next line"
line = logfile.readline()
if(iLines<=0):
print "no line"
else:
print "there were %d lines" % iLines
Dataset_CalcRealtime()
Dataset_ChopOld()
Plot_Update()
Plot_redraw()
def MainLoopCallback():
print "MainLoopCallback: " + datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
lbl3_string.set("time = " + datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')[:-3])
CheckLogForNewLine()
rootTk.after(100, MainLoopCallback)
startTime=0
TITLE_FONT="Helvetica 50 bold"
FIGURE_DPI=60
rootTk = Tkinter.Tk()
lbl1_string = Tkinter.StringVar()
label = Tkinter.Label( rootTk, textvariable=lbl1_string, font = TITLE_FONT, relief=Tkinter.RAISED )
lbl1_string.set("U = ????")
label.pack()
mydata=Cdata()
f1 = Figure(figsize=(5,5), dpi=FIGURE_DPI)
ax1 = f1.add_subplot(111)
#line1, = ax1.plot([1,2],[8,9])
#line1, = ax1.plot(0,0)
line1 = Line2D([],[], color='black', linewidth=2 , marker='o')
ax1.add_line(line1)
canvas1 = FigureCanvasTkAgg(f1, master = rootTk)
canvas1.show()
canvas1.get_tk_widget().pack(side=Tkinter.BOTTOM, fill=Tkinter.BOTH, expand=True)
toolbar = NavigationToolbar2TkAgg(canvas1, rootTk)
toolbar.update()
canvas1._tkcanvas.pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)
lbl2_string = Tkinter.StringVar()
label = Tkinter.Label( rootTk, textvariable=lbl2_string, font = TITLE_FONT, relief=Tkinter.RAISED )
lbl2_string.set("I = ????")
label.pack()
f2 = Figure(figsize=(5,5), dpi=FIGURE_DPI)
ax2 = f2.add_subplot(111)
line2, = ax2.plot([],[])
canvas2 = FigureCanvasTkAgg(f2, master = rootTk)
canvas2.show()
canvas2.get_tk_widget().pack(side=Tkinter.BOTTOM, fill=Tkinter.BOTH, expand=True)
toolbar2 = NavigationToolbar2TkAgg(canvas2, rootTk)
toolbar2.update()
canvas2._tkcanvas.pack(side=Tkinter.TOP, fill=Tkinter.BOTH, expand=1)
lbl3_string = Tkinter.StringVar()
label = Tkinter.Label( rootTk, textvariable=lbl3_string, font = TITLE_FONT, relief=Tkinter.RAISED )
lbl3_string.set("time = ????")
label.pack()
button = Tkinter.Button(rootTk, text='Stop', width=25, command=rootTk.destroy)
button.pack()
rootTk.after(100, MainLoopCallback)
plt.show()
try:
rootTk.mainloop()
except KeyboardInterrupt:
print "quitting"
pass
print "done"