-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBackwaushomator-Threading.py
507 lines (452 loc) · 25.9 KB
/
Backwaushomator-Threading.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import RPi.GPIO as GPIO
from time import sleep
from Tkinter import *
import thread
import math
import time
import numpy as np
import spidev
from datetime import datetime, timedelta
import sys
import smtplib
import email
#from email.mime.image import MIMEImage
#from email.mime.multipart import MIMEmultipart
from email.mime.text import MIMEText
#To Do
# eventually a particle counter lable
## backwash time determined by flow past a certian point
## see if Graph can scroll with time rather than a set window
## make target data defineable in the program
## add in pressure drop over the solenoids
#______________________________________________________________________________________________________________________
#----------------------------------------------------------------------------------------------------------------------
print("start")
#Setting up Spi to read ADC
spi_0 = spidev.SpiDev()
spi_0.open(0, 0) #the second number indicates which SPI pin CE0 or CE1
#to_send = [0x01,0x02,0x03] # speed Hz,Delay, bits per word
#spi_0.xfer(to_send)
#Setting up Global Variables
ForwardPumpTarget=15
ForwardFlowCount = 0.0
oldForwardFlowCount= 0.0
forwardflow = 0.0
BackwashPumpTarget=45
BackwashFlowCount = 0.0
oldBackwashFlowCount= 0.0
backwashflow = 0.0
FlowTarget = 1.0 #lpm
PumpThreshold = 1 #psi
StartTime = datetime.now()
samplePeriod = 100 #milliseconds, time between data points written to txt file
minimumtime = 1000 #Time in miliseconds between possible backwash cycles
destination = "/home/pi/Desktop/Data/AutosavedData %s.txt" %str(StartTime)
a=open(destination,'w') #a means append to existing file, w means overwrite old data
#add column headers for perameters and data
a.write("\n\n"+ str(datetime.now())+","+str(ForwardPumpTarget)+","+str(BackwashPumpTarget)+","+str(FlowTarget)+","+str(PumpThreshold))
backwash = False
switched = False
cycles = 0
Average= 3 # taking 15 samples per second average of 15 will average over one second
flowshow = 0.0
backshow = 0.0
FPshow = ForwardPumpTarget
BPshow = BackwashPumpTarget
Diffshow= ForwardPumpTarget
toaddr = '[email protected]'
fromaddr = 'markmahlon"gmail.com'
subject = 'EMERGENCY SHUTOFF'
#Setting up GUI
class popupWindow(object):
def __init__(self,master):
top=self.top=Toplevel(master)
self.l=Label(top,text="Set Value")
self.l.pack()
self.e=Entry(top)
self.e.pack()
self.b=Button(top,text='Done',command=self.cleanup)
self.b.pack()
def cleanup(self):
self.value=self.e.get()
self.top.destroy()
class mainWindow(object):
def __init__(self,master):
self.master=master
self.b=Button(Controls,text="Set Value",command=self.popup,)
self.b.place(x=0,y=203)
self.b2=Button(Controls,text="Update Flow Target",command=lambda: FTdisplay.set((str(self.entryValue()))))
self.b2.place(x=85,y=223)
self.b3=Button(Controls,text="Update Pump Threshold",command=lambda: PTdisplay.set((str(self.entryValue()))))
self.b3.place(x=225,y=223)
self.b4=Button(Controls,text="Update Forward Target",command=lambda: FPTdisplay.set((str(self.entryValue()))))
self.b4.place(x=85,y=193)
self.b5=Button(Controls,text="Update Backwash Target",command=lambda: BPdisplay.set((str(self.entryValue()))))
self.b5.place(x=225,y=193)
def popup(self):
self.w=popupWindow(self.master)
self.master.wait_window(self.w.top)
def entryValue(self):
return self.w.value
root = Tk()
root.geometry('880x700+150+150')
root.title("Backwash Control")
#----initializing GUI------------------
background_image = PhotoImage(file='/home/pi/Desktop/Code/background.gif')
w=background_image.width()
h=background_image.height()
C = Canvas(root, bg='#333',height=(h+5),width=(w+5))
C.focus_set() # Sets the keyboard focus to the canvas
bg_label = Label(C , image=background_image)
bg_label.pack()
Ftank_text = Label(C, text='psi:', padx=5)
Ftank_text.place(x=320,y=41)
Btank_text = Label(C, text='psi:', padx=5)
Btank_text.place(x=318,y=338)
FTL= StringVar()
FTL.set('0')
forward_tank_label = Label(C, textvariable=FTL, padx=5)
forward_tank_label.place(x=350,y=41)
BTL= StringVar()
BTL.set('0')
back_tank_label = Label(C, textvariable=BTL, padx=5)
back_tank_label.place(x=350,y=338)
DL= StringVar()
DL.set('0')
differential_label = Label(C, textvariable=DL, padx=5)
differential_label.place(x=400,y=200)
Flowrate_text = Label(C, text='Flow Rate:', padx=5,pady=3)
Flowrate_text.place(x=418,y=89)
Flowrate_text2 = Label(C, text='Flow Rate:', padx=5,pady=3)
Flowrate_text2.place(x=418,y=274)
FRL= StringVar()
FRL.set('0')
Flowrate_label = Label(C, textvariable=FRL,pady=3)
Flowrate_label.place(x=489,y=89)
BFL= StringVar()
BFL.set('0')
BackFlowrate_label = Label(C, textvariable=BFL,pady=3)
BackFlowrate_label.place(x=489,y=274)
Cycle_text = Label(C,text='Cycles:', padx=5,pady=3)
Cycle_text.place(x=605,y=182)
CY= StringVar()
CY.set('0')
Cycle_label = Label(C, textvariable=CY, padx=5,pady=3)
Cycle_label.place(x=655,y=182)
Concentrate_text = Label(C,text='Liters:', padx=5)
Concentrate_text.place(x=760,y=145)
CL= StringVar()
CL.set('0')
Concentrate_label = Label(C, textvariable=CL, padx=5)
Concentrate_label.place(x=805,y=145)
Filtrate_text = Label(C,text='Liters:', padx=5)
Filtrate_text.place(x=760,y=320)
FL= StringVar()
FL.set('0')
Filtrate_label = Label(C, textvariable=FL, padx=5)
Filtrate_label.place(x=805,y=320)
frame=Frame(root)
frame.pack(anchor=NW)
#----Threshold set up-----------------
Controls= LabelFrame(root,text='Controls',height=270,width=w-450)
Controls.pack_propagate(False)
Controls.place(x=0,y=0)
FTdisplay = StringVar()
FTdisplay.set((str(FlowTarget)))
FTlabel = Label(Controls, textvariable=FTdisplay)
FTlabel.pack()
PTdisplay = StringVar()
PTdisplay.set((str(PumpThreshold)))
Plabel = Label(Controls, textvariable=PTdisplay)
Plabel.pack()
FPTdisplay = StringVar()
FPTdisplay.set((str(ForwardPumpTarget)))
Flabel = Label(Controls, textvariable=FPTdisplay)
Flabel.pack()
BPdisplay = StringVar()
BPdisplay.set((str(BackwashPumpTarget)))
Blabel = Label(Controls, textvariable=BPdisplay)
Blabel.pack()
#----Manual control switchs----------------
Switch=StringVar()
Switch.set('Forward')
ManualSwitch = Checkbutton(Controls,indicatoron=0,textvariable=Switch)
ManualSwitch.pack()
##Switch2=StringVar()
##Switch2.set('Liters')
##boolvar= IntVar()
##boolvar.set(1)
##ConversionSwitch = Checkbutton(frame,indicatoron=0,textvariable=Switch2,variable=boolvar)
##ConversionSwitch.pack()
#--- Graph settings
screenWidth = 450
resolution = 1 #number of pixels between data points, for visual purposes only
timeRange = .5 #minutes
baseTime = int(timeRange*60*1000/screenWidth)
x0Coords = []
y0Coords = []
xy0Coords = []
FlowrateAvg = []
BackflowAvg = []
FPumpAvg = []
BPumpAvg = []
DiffAvg = []
coordLength = int(screenWidth/resolution)
#---initiation of lists
for i in range(0,coordLength):
x0Coords.append(i*resolution)
y0Coords.append(249)
xy0Coords.append(0)
xy0Coords.append(0)
for i in range(0,Average):
FlowrateAvg.append(FlowTarget)
BackflowAvg.append(0)
FPumpAvg.append(ForwardPumpTarget)
BPumpAvg.append(BackwashPumpTarget)
DiffAvg.append(0)
#putting X and Y corrdinites in a list
def coordinate():
global x0Coords, y0Coords, xy0Coords
for i in range(0,coordLength*2,2):
xy0Coords[i] = x0Coords[i/2]
xy0Coords[i+1] = y0Coords[i/2]
#---End initiation of lists
Graph= LabelFrame(root, text="Flow Graph",height=250,width=screenWidth)
Graph.place(x=(w-screenWidth)+2,y=0)
GraphC=Canvas(Graph, bg = "gray", height = 249, width = screenWidth-1)
c0 = GraphC.create_rectangle(0,0,20,50)
cl0 = GraphC.create_line(xy0Coords,smooth=True)
ctar = GraphC.create_line(0,(249-FlowTarget*20),450,(249-FlowTarget*20), fill='red')
scale5 = Label(GraphC, text=' 20-', bg = "gray")
scale5.place(x=0,y=(240-20*12))
scale7 = Label(GraphC, text=' 18-', bg = "gray")
scale7.place(x=0,y=(240-18*12))
scale9 = Label(GraphC, text=' 16-', bg = "gray")
scale9.place(x=0,y=(240-16*12))
scale11 = Label(GraphC, text='14-', bg = "gray")
scale11.place(x=0,y=(240-14*12))
scale12 = Label(GraphC, text='12-', bg = "gray")
scale12.place(x=0,y=(240-12*12))
scale10 = Label(GraphC, text='10-', bg = "gray")
scale10.place(x=0,y=(240-10*12))
scale8 = Label(GraphC, text=' 8-', bg = "gray")
scale8.place(x=0,y=(240-8*12))
scale6 = Label(GraphC, text=' 6-', bg = "gray")
scale6.place(x=0,y=(240-6*12))
scale4 = Label(GraphC, text=' 4-', bg = "gray")
scale4.place(x=0,y=(240-4*12))
scale2 = Label(GraphC, text=' 2-', bg = "gray")
scale2.place(x=0,y=(240-2*12))
scale0 = Label(GraphC, text=' 0-', bg = "gray")
scale0.place(x=0,y=(240-0*20))
#setting up GPIO pins
ForwardTankValve = 14
ForwardPump = 15
BackwashPump = 18
ForwardPumpValve = 23
BackwashTankValve = 24
BackwashPumpValve = 25
ForwardFlow = 22
BackwashFlow = 17
on = GPIO.LOW
off = GPIO.HIGH
vopen = GPIO.LOW
vclose = GPIO.HIGH
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(ForwardTankValve, GPIO.OUT)
GPIO.setup(ForwardPumpValve, GPIO.OUT)
GPIO.setup(ForwardPump, GPIO.OUT)
GPIO.setup(BackwashTankValve, GPIO.OUT)
GPIO.setup(BackwashPumpValve, GPIO.OUT)
GPIO.setup(BackwashPump, GPIO.OUT)
GPIO.setup(ForwardFlow, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)#Generally reads LOW
GPIO.setup(BackwashFlow, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
#----------------------Function Definitons--------------------------------
def readadc_0(adcnum_0): #this fucntion can be used to find out the ADC value on ADC 0
if adcnum_0 > 7 or adcnum_0 < 0:
return -1
r_0 = spi_0.xfer2([1, 8 + adcnum_0 << 4, 0]) #start bit, Single/Differential mode, Don't care bit OR
adcout_0 = ((r_0[1] & 3) << 8) + r_0[2]
return adcout_0
def readadc_0diff(adcnum_0): #this fucntion can be used to find out the differential ADC value between two chanels on ADC 0
if adcnum_0 > 7 or adcnum_0 < 0:
return -1
r_0 = spi_0.xfer2([1, adcnum_0 << 4, 0]) #start bit, Single/Differential mode, Don't care bit OR
adcout_0 = ((r_0[1] & 3) << 8) + r_0[2]
return adcout_0
def callback_fflow(ForwardFlow):
global ForwardFlowCount
ForwardFlowCount+=1
def callback_bflow(BackwashFlow):
global BackwashFlowCount
BackwashFlowCount+=1
#shifts y values down in index in array to represent time moved forward
def shiftCoords(nextValue):
global y0Coords, xy0Coords
y0Coords.pop(0)
y0Coords.append(int(nextValue))
coordinate()
#updates the GUI based on the new time
def move_time():
global c0,cl0,ctar,xy0Coords,resolution,baseTime,forwardflow,backwashflow,flowshow,FPshow,Diffshow
GraphC.delete(c0)
GraphC.delete(cl0)
GraphC.delete(ctar)
c0 = GraphC.create_rectangle(0,0,20,int(backwashflow/1023*250))
shiftCoords(249-(flowshow*12))
cl0 = GraphC.create_line(xy0Coords)
ctar = GraphC.create_line(0,(249-float(FTdisplay.get())*12),450,(249-float(FTdisplay.get())*12), fill='red')
#print(float(readadc_0(0))/1023*250)
#title="V= " , str(round(3.3*float(readadc_0(2)-readadc_0(0))/1023,2)) , str(round(3.3*float(readadc_0(2))/1023,2)), str(round(3.3*float(readadc_0(0))/1023,2))
#root.title(title)
root.after(baseTime*resolution,move_time)
def SwitchB(delay):
print 'start backwash %s' %str(datetime.now())
GPIO.output(ForwardPumpValve,vclose)
GPIO.output(BackwashTankValve, vclose)
time.sleep(delay) # try time delay to see if blip will go away
GPIO.output(ForwardTankValve,vopen)
GPIO.output(BackwashPumpValve, vopen)
print 'end backwash'
def SwitchF(delay):
print 'start forward %s' %str(datetime.now())
GPIO.output(ForwardTankValve,vclose)
GPIO.output(BackwashPumpValve, vclose)
time.sleep(delay)
GPIO.output(BackwashTankValve, vopen)
GPIO.output(ForwardPumpValve,vopen)
print 'end forward'
def checkback():
global forwardflow, backwash, Switch, switched, cycles, FlowTarget, flowshow
if backwash == True:
backwash = False
Switch.set('Forward')
if switched == True:
thread.start_new_thread(SwitchF,(.2,))
cycles = cycles+1
CY.set(str(cycles))
switched = False
if flowshow < float(FTdisplay.get()) and BPshow > 40:# and flowshow > .1:
backwash = True
Switch.set('Backwash')
if switched == False:
thread.start_new_thread(SwitchB,(.2,))
switched = True
root.after(minimumtime,checkback)
def writeData():
global destination,cycles,Diffshow,switched,samplePeriod,ForwardFlowCount,oldForwardFlowCount,BackwashFlowCount,oldBackwashFlowCount,forwardflow,backwashflow,FlowrateAvg,flowshow
##Calibration of sensor: Real Pressure = reading-(-1.06+.1007xreading)
Reading=(3.3*float(readadc_0(3)-readadc_0(0))/1023)*100
ForwardPumpActual=round(Reading-(-1.06+.1007*Reading),1)
FPumpAvg.pop(0)
FPumpAvg.append(ForwardPumpActual)
FPshow=np.mean(FPumpAvg)
FTL.set(str(round(FPshow,1)))
if ForwardPumpTarget-FPshow > float(PTdisplay.get())/2:
GPIO.output(ForwardPump,on)
if FPshow-ForwardPumpTarget > float(PTdisplay.get())/2: #float(PTdisplay.get()):
GPIO.output(ForwardPump,off)
Reading=(3.3*float(readadc_0(1)-readadc_0(0))/1023)*100
BackwashPumpActual=round(Reading-(-1.06+.1007*Reading),1)
BPumpAvg.pop(0)
BPumpAvg.append(BackwashPumpActual)
BPshow=np.mean(BPumpAvg)
BTL.set(str(round(BPshow,1)))
if BackwashPumpTarget-BackwashPumpActual > float(PTdisplay.get())/2: #float(PTdisplay.get()):
GPIO.output(BackwashPump, on)
if BackwashPumpActual - BackwashPumpTarget > float(PTdisplay.get())/2:
GPIO.output(BackwashPump, off)
if BackwashPumpActual > 55 or ForwardPumpActual > 55:
try:
errormsg ='EMERGENCY SHUT OFF: Pressure too high!'
msg = errormsg
msg['Subject'] = 'EMERGENCY SHUT OFF: Pressure too high!'
msg['From'] = fromaddr
msg['To'] = toaddr
msg.preamble = str(datetime.now())
msgtext=MIMEText(errormsg, 'plain')
msg.attach(msgtext)
print errormsg
s = smtplib.SMTP('localhost')
s.set_debuglevel(1)
s.send_message(msg)
s.quit
except:
print ('Error: unable to send email')
callback_end("<End>")
if backwash==False and flowshow <0.09:
errormsg ='EMERGENCY SHUT OFF: Flow too low!'
msg = MIMEText(errormsg,'plain')
msg['Subject'] = "EMERGENCY SHUT OFF: Flow too low!"
msg['From'] = fromaddr
msg['To'] = toaddr
msg.preamble = str(datetime.now())
print errormsg
try:
s = smtplib.SMTP('localhost')
s.set_debuglevel(1)
s.send_message(msg.as_string())
s.quit
except:
print ('Error: unable to send email')
callback_end("<End>")
Reading = (3.3*float(readadc_0(2)-readadc_0(0))/1023)*100
DifferentialPressure=round(Reading-(-1.06+.1007*Reading),1)
DiffAvg.pop(0)
DiffAvg.append(DifferentialPressure)
Diffshow=np.mean(DiffAvg)
DL.set(str(round(Diffshow,1)))
## if boolvar.get() == 1:
## Switch2.set('Liters')//messes up average
## ConversionFactor = 60 # conversion factor to liters per minute
## ConversionFactor2 = 1000 # pulses per lieter
## else:
## Switch2.set('Gallons')
## ConversionFactor = 15.7
## ConversionFactor2 = 3800 # pulses per gallon
forwardflow=((ForwardFlowCount-oldForwardFlowCount)/samplePeriod)*60 #60 is a conversion factor to convert the flowrate from pulses per 100miliseconds to liters per minute
FlowrateAvg.pop(0)
FlowrateAvg.append(forwardflow)
flowshow = np.mean(FlowrateAvg)
FRL.set(str(round(flowshow,1)))
backwashflow=((BackwashFlowCount-oldBackwashFlowCount)/samplePeriod)*60
BackflowAvg.pop(0)
BackflowAvg.append(backwashflow)
BFL.set(str(round(backwashflow,1)))
CL.set(str(round(BackwashFlowCount/1000,1)))
FL.set(str(round(ForwardFlowCount/1000,1)))
#data = str(round(Diffshow,1)) + "\t" + str(round(flowshow,1)) + "\t" +str(round(backwashflow,1)) + "\t" +str(round(FPshow,1))+ "\t" +str(round(BPshow,1))+'\t'+str(cycles)+'\t'+str(backwash)#Writing averaged data
data = str(round(Diffshow,1)) + "\t" + str(round(forwardflow,1)) + "\t" +str(round(backwashflow,1)) + "\t" +str(round(ForwardPumpActual,1))+ "\t" +str(round(BackwashPumpActual,1))+'\t'+str(cycles)+'\t'+str(backwash) # writng actual data
a.write("\n"+ str(datetime.now()) + "\t" + str(data))
oldForwardFlowCount=ForwardFlowCount
oldBackwashFlowCount=BackwashFlowCount
root.after(samplePeriod,writeData)
def callback_end(event):
global FlowCount, StartTime
GPIO.output(ForwardPump, off)
GPIO.output(BackwashPump, off)
GPIO.output(ForwardPumpValve, vclose)#Keeps pressure in the tank
GPIO.output(BackwashPumpValve, vclose)
GPIO.output(ForwardTankValve, vopen)#lets pressure out into the tank
GPIO.output(BackwashTankValve, vopen)
time.sleep(10)
GPIO.output(BackwashTankValve, vclose)
GPIO.output(ForwardTankValve, vclose)
# GPIO.cleanup()#i think this would get get rid of the draining process
spi_0.close()
a.close()
quit()
#Setting up event detection
GPIO.add_event_detect(ForwardFlow, GPIO.RISING, callback=callback_fflow)
GPIO.add_event_detect(BackwashFlow, GPIO.RISING, callback=callback_bflow)
#----------------------------------Main loop----------------------------------------
thread.start_new_thread(SwitchF,(.2,))
C.bind("<End>",callback_end)
C.place(x=10,y=280)
GraphC.pack(anchor=CENTER)
root.after(baseTime,move_time)
root.after(samplePeriod,writeData)
root.after(minimumtime,checkback)
m=mainWindow(root)
root.mainloop()