-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_roast_tool_01.py
273 lines (256 loc) · 12.5 KB
/
07_roast_tool_01.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# roast tool main v04 - serial communication optimization for responsiveness of app
# 4 actual data fields
# automatically updated monitor screen
# file management class, data management classes, serial data get class
# functional start, stop button, start resets time counter on arduino, proper enable/disable buttons
# pagelayout main screen
# added graph display functionality, automatic time axis adjustment
import serial
import serial.tools.list_ports
import time
import random
import datetime
import math
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.pagelayout import PageLayout
from kivy.uix.scrollview import ScrollView
from kivy.garden.graph import Graph, MeshLinePlot
from kivy.properties import StringProperty
from kivy.clock import Clock
class DataGenRandom: #data generator class
def __init__(self):
self.str1 = ''
self.i = 0
def get(self): #generates and returns data string
self.str1 = str(self.i) + ';' + str(random.randint(200,240) + round(random.random(),1)) + ';'
self.str1 += str(round(random.random()*210,1)) + ';' + str(round(random.random()*200,1)) + '\n'
self.i += 0.5
return self.str1
def start(self):
pass
def check(self): #checks available bytes in port
return 24
class DataGenLinear: #linear data generator class
def __init__(self):
self.str1 = ''
self.i = 0
self.T1 = 18
self.T2 = 20
def get(self): #generates and returns data string
self.str1 = str(self.i) + ';' + str(random.randint(200,240) + round(random.random(),1)) + ';'
self.str1 += str(round(self.T1 + random.random(),1)) + ';' + str(round(self.T2 + random.random(),1)) + '\n'
self.i += 1
self.T1 += 0.168
self.T2 += 0.2
return self.str1
def start(self):
pass
def check(self): #checks available bytes in port
return 24
class DataGetSerial: #data Serial class
def __init__(self):
self.str1 = ''
port = list(serial.tools.list_ports.comports())[0] #lists serial port
self.S1 = serial.Serial("COM11", 9600) #port.device - COM port name
print(port.device)
def start(self): #starts measurement
self.S1.write('s'.encode()) #sends start 's' string
started = False
while started == False:
if self.check() < 24: #repeatedly sends 's' string when meas did not start
self.S1.write('s'.encode())
try:
line = self.S1.readline().decode()
except ValueError:
#line = "0;0;0;0"
pass
print('bytes at port: ' + str(self.check()) + '; garbage: ' + line) #diagnostic line
if line == 's\r\n': #checks arduino response - should get 's\r\n'
started = True #measurement started
def stop(self): #currently not used
self.S1.write('e')
def get(self): #reads data/line from serial port
try:
str2 = self.S1.readline().decode() #.rstrip('\r\n') + '\n' #removes \r from string i.e. empty line in file
except ValueError:
#str2 = "0;0;0;0"
pass
print(str2) #diagnostic line
return str2
def check(self): #checks available bytes in port
return self.S1.in_waiting
class RawData: #data class - contains raw string data
def __init__(self):
self.Raw = ""
def add(self, str1):
self.Raw = str1 + self.Raw
def getRaw(self): #gets all data
return self.Raw
def getRawFirst(self, rows): #gets requested number of rows
lines = self.Raw.splitlines()
length = len(lines)
str1 = ''
if length > rows:
length = rows
for i in range(length):
str1 += lines[i] + '\n'
return str1
class DataPoint: #data class - contains one data point
def __init__(self):
self.time = 0
self.Uef = 0
self.T1 = 0
self.T2 = 0
self.Pwr = 0
def add(self, str1):
strgs = str1.split(';')
self.time = float(strgs[0])
self.Uef = float(strgs[1])
self.T1 = float(strgs[2])
self.T2 = float(strgs[3])
self.Pwr = self.Uef ** 2 / 41.5
def gettime(self):
return self.time
def gettimestr(self): #formats time for proper time display mm:ss
str1 = str(int(self.time//60)) + ':' + str(format(int(round(self.time%60, 0)), '02d'))
return str1
def getUef(self):
return self.Uef
def getT1(self):
return self.T1
def getT2(self):
return self.T2
def getPwr(self):
return self.Pwr
class DataTuplesList: #data class - contains data for garden.graph library - list of tuples
def __init__(self):
self.Uef = []
self.T1 = []
self.T2 = []
def add(self, str1):
strgs = str1.split(';')
self.Uef.append((float(strgs[0])/60,float(strgs[1])))
self.T1.append((float(strgs[0])/60,float(strgs[2])))
self.T2.append((float(strgs[0])/60,float(strgs[3])))
def getUef(self):
return self.Uef
def getT1(self):
return self.T1
def getT2(self):
return self.T2
def clear(self):
self.Uef = []
self.T1 = []
self.T2 = []
class MinuteDelta: #delta class - contains minute deltas data
def __init__(self):
self.actDelta = float('NaN')
self.Deltas = []
def update(self, DTList): #updates actDelta value
i = -1 #searches from the end of the list backwards
lasttime = DTList.T2[i][0]
ok = False
if lasttime > 1: #searches only when measurement started more than minute ago
while ok == False and abs(i) < len(DTList.T2): #searches for actual minute delta
if (lasttime - DTList.T2[i][0]) >= 1: #calculates time delta - when > 1 stops search
ok = True
self.actDelta = DTList.T2[-1][1] - DTList.T2[i][1] #calculates value delta
else:
i -= 1
#print('delta: ' + str(self.actDelta) + ' tlast: ' + str(DTList.T2[-1][1]) + ' tback: ' + str(DTList.T2[i][1])) #diagnostic print
def getAct(self): #gets actual delta value - outputs float as string
if math.isnan(self.actDelta) == False:
return str(round(self.actDelta, 1))
else:
return ''
def clear(self):
self.actDelta = float('NaN')
class FileWrp(): #file management class
def __init__(self):
self.now1 = datetime.datetime.now()
self.ffname = ''
def create(self, fname): #creates csv file including its head
self.now1 = datetime.datetime.now()
self.ffname = 'D:\\Personal\\programovanie\\python\\roastooldata\\' + self.now1.strftime("%Y-%m-%d-%H-%M-%S_") + fname +".csv"
self.file = open(self.ffname, "a")
self.file.write(self.now1.strftime("%Y-%m-%d-%H-%M-%S;") + fname + '\n')
self.file.write("time;Uef;T1;T2\n")
self.file.close()
def write(self, str1): #writes data to csv file
self.file = open(self.ffname, "a")
self.file.write(str1)
self.file.close()
def close(self): #currently not used
self.file.close()
class STWidget(BoxLayout): #root widget class - main functionality
strtodisplay = StringProperty()
dGet = DataGenLinear() #inits data generator object
#dGet = DataGetSerial() #inits serial communication
file1 = FileWrp() #inits file object
rawdata = RawData() #inits rawdata object
lastPt = DataPoint() #inits datapoint object
grphdat = DataTuplesList() #inits data list of tuples object
deltas = MinuteDelta() #inits deltas data object
def open(self): #inits plots
self.plotT1 = MeshLinePlot(color=[1, 0.2, 0.2, 1]) #creates plot
self.plotT2 = MeshLinePlot(color=[1, 0.7, 0.2, 1])
self.plotUef = MeshLinePlot(color=[0.6, 1, 0.2, 1])
self.plotT1.points = []
self.plotT2.points = []
self.plotUef.points = []
self.ids.graph.add_plot(self.plotT1) #adds plot to graph
self.ids.graph.add_plot(self.plotT2)
self.ids.graph.add_plot(self.plotUef)
def update_textbox(self, *args): #updates display; needed to add *args
S1bytes = self.dGet.check()
if S1bytes >= 24: #checks whether enough data is in serial buffer (one line = 24 bytes), greatly improves GUI responsiveness
strg = self.dGet.get() #gets string data from data generator object (e.g. serial port)
self.rawdata.add(strg) #adds get data do RawData object
a1 = self.rawdata.getRawFirst(20) #gets data to display from RawData object
self.strtodisplay = a1 #displays selected data in label
self.lastPt.add(strg) #adds data to DataPoint object
self.grphdat.add(strg) #adds data to DataTuplesList object
self.deltas.update(self.grphdat) #updates deltas object
self.ids.time.text = str(self.lastPt.gettimestr())
self.ids.Uef.text = str(int(round(self.lastPt.getUef(),0)))
self.ids.Pwr.text = str(int(round(self.lastPt.getPwr(),0)))
self.ids.T1.text = str(round(self.lastPt.getT1(),1))
self.ids.T2.text = str(round(self.lastPt.getT2(),1))
self.ids.dT2.text = self.deltas.getAct() #str(round(self.deltas.getAct(),1))
self.file1.write(strg)
self.plotT1.points = self.grphdat.getT1() #data to diplay - list of tuples - points
self.plotT2.points = self.grphdat.getT2()
self.plotUef.points = self.grphdat.getUef()
tmax = self.lastPt.gettime()/60 #data max time in minutes
tdisp = 15 #graph plot length in minutes
if tmax <= tdisp:
self.ids.graph.xmax = tdisp
self.ids.graph.xmin = 0
else:
self.ids.graph.xmax = tmax//1 + 1 #sets time axis max
self.ids.graph.xmin = tmax//1 - tdisp + 1 #sets time axis min
def start(self): #creates new file, its header and starts getting data and recording
self.grphdat.clear() #clears DataTuplesList object
self.deltas.clear() #clears MinuteDelta object
self.ids.startb.disabled = True #disables start button
self.file1.create(self.ids.sample.text) #creates data file - todo - move 1 line downwards
self.dGet.start() #starts daq
self.event = Clock.schedule_interval(self.update_textbox, 0.25)
self.ids.stopb.disabled = False #enables stop button
print("start")
def stop(self): #stops data recording
self.ids.stopb.disabled = True
Clock.unschedule(self.event)
self.ids.startb.disabled = False
print("stop")
class RoasttoolApp(App): #app class
def build(self):
r_widg = STWidget()
r_widg.open() #creates plots in graph
return r_widg
if __name__ == "__main__": #runs app
RoasttoolApp().run()