-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
354 lines (299 loc) · 14.8 KB
/
main.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
'''
Simple grow controller
All values are hardcoded in to the files
Will read ph, temp, humidity and save values to individual files
Will control ph using 2 pumps
'''
import time
import datetime
import os
from growControl import Sensor_ph, Controller_ph_Pump, Sensor_humidity_temp, Controllable_Pump,Sensor_volume
from blessed import Terminal
def sensor_box(top,left,width,name,current,average):
data = term.move_xy(left,top)
data += term.on_green(name + " "*(width-len(name)))
current_value = "{:.3f}".format(current) if type(current) is float else "-"
average_value = "{:.3f}".format(average) if type(average) is float else "-"
data += term.move_xy(left+2,top+1) + "Current Value: " + current_value
data += term.move_xy(left+2,top+2) + "Average Value: " + average_value
return data
def controller_box(top,left,width,name,action,action_time,loop_time):
data = term.move_xy(left,top)
data += term.on_green(name + " "*(width-len(name)))
action_time_value = "{:.0f}".format(time.time() - action_time) if type(action_time) is float else "-"
loop_time_value = "{:.0f}".format(time.time() - loop_time) if type(loop_time) is float else "-"
data += term.move_xy(left+2,top+1) + "Last Action: {}".format(action)
data += term.move_xy(left+2,top+2) + "Time since last action: " + action_time_value + " s"
data += term.move_xy(left+2,top+3) + "Time since last loop: " + loop_time_value + " s"
return data
class MenuItem():
'''
Used to hold the menu items and what happens when they are called
'''
def __init__(self,name,key,function):
'''
name: The title of the command
key: The key to be pressed, must be lower case, only 1 character is allowed
'''
self.name = name
self.key = key.lower()[0]
self.function = function
def __call__(self):
self.function()
def __str__(self):
s = term.on_darkolivegreen("{}: {}".format(self.name,self.key))
return s
def pause():
val = ""
while val.lower() != "p":
text = "System Paused! Press 'p' to resume"
screen = term.move_xy(0,3)+ term.center(term.white_on_firebrick3(text))
print(screen)
val=term.inkey(timeout=None) # block until user presses a key
return
def quit():
val = ""
while True:
text = "Exit? Press 'y' to exit, 'n' to return to program"
screen = term.move_xy(0,3) + term.center(term.white_on_firebrick3(text))
print(screen)
if val == "y":
raise KeyboardInterrupt()
elif val == "n":
break
val = term.inkey(timeout=None)
return
def ph_calibration():
calibration_data = {}
calibration_time = 15 # seconds
calibration_samples_per_second = 5
def ph_calibration_menu():
text = "Commands"
screen = term.move_xy(term.width//2 - len(text)//2, term.height -3) + text
screen += term.move_xy(0,term.height-2) + "Quit Calibration: q"
return screen
while True:
mode = "4ph"
val = term.inkey(timeout=.5) # how often to update the screen
screen = term.clear()
screen += term.move_xy(0,5) + term.center("pH Calibration Screen")
if val == "q":
break
elif val == " ":
calibration_data.update(sensor_ph._calibration_get_point_data(mode,duration=calibration_time,sps=calibration_samples_per_second))
break
screen += term.move_xy(0,10) + term.center(term.on_hotpink("Place probe in {} soluion and press <SPACE>".format(mode)))
screen += ph_calibration_menu()
print(screen)
while True:
mode = "7ph"
val = term.inkey(timeout=.5) # how often to update the screen
screen = term.clear()
screen += term.move_xy(0,5) + term.center("pH Calibration Screen")
if val == "q":
break
elif val == " ":
calibration_data.update(sensor_ph._calibration_get_point_data(mode,duration=calibration_time,sps=calibration_samples_per_second))
break
screen += term.move_xy(0,11) + term.center(term.on_olivedrab3("Place probe in {} soluion and press <SPACE>".format(mode)))
screen += ph_calibration_menu()
print(screen)
calibration_data = sensor_ph._calibration_compute_line(calibration_data)
while True:
mode = "save"
val = term.inkey(timeout=.5) # how often to update the screen
screen = term.clear()
screen += term.move_xy(0,5) + term.center("pH Calibration Screen")
if val == "q":
break
elif val == "s":
calibration_file = sensor_ph._calibration_save_data(calibration_data)
sensor_ph._load_calibration_params(calibration_file=calibration_file)
break
screen += term.move_xy(0,10) + term.center("Determined the equation is: ph = {:.3f}*voltage + {:.3f}".format(calibration_data["m"],calibration_data["b"]))
screen += term.move_xy(0,11) + term.center(" Ideal equation is: ph = {:.3f}*voltage + {:.3f}".format(-.057,7.0))
screen += term.move_xy(0,13) + term.center("Average value for 4ph: {:.3f} volts, ideal is 0.057 volts".format(calibration_data["4ph_mean"]))
screen += term.move_xy(0,14) + term.center("Average value for 7ph: {:.3f} volts, ideal is 0.000 volts".format(calibration_data["7ph_mean"]))
screen += term.move_xy(0,16) + term.center(term.on_darkolivegreen("Press 's' to Save, or press 'q' to quit"))
screen += ph_calibration_menu()
print(screen)
class MenuItems():
'''
Hold onto all the menu items
'''
def __init__(self):
self.items = []
def addItem(self,item):
self.items.append(item)
def __iter__(self):
self.iter_idx = 0
return self
def __next__(self):
if self.iter_idx < len(self.items):
item = self.items[self.iter_idx]
self.iter_idx += 1
return item
else:
raise StopIteration
def __str__(self):
n_cols = 3
n_rows = len(self.items)//n_cols + 1
col_width = term.width//n_cols
title = "Commands"
s = term.move_xy(term.width//2-len(title)//2, term.height-n_rows-1) + term.on_darkolivegreen(title)
for idx, item in enumerate(self.items):
x = idx%n_cols * col_width
y = term.height - n_rows + idx//n_cols
s += term.move_xy(x,y) + str(item)
return s
if __name__ == "__main__":
# settings for how the UI is layed out
top = 5
left = 2
rows_per_device = 5
cols_per_device = 35
col_padding = 2
# Settings for the controller
# set to 6.0/6.4 from 5.7/6.0 on 20200422T2310 to conserve ph solution
ph_min = 6.0 # set to 5.0 from 5.7 on 20200329T1045 to see where the system wants to be
ph_max = 6.4
average_factor = 0.99
verbose = False
output_dir = "/home/pi/growControl_Data/"
sensor_ph = Sensor_ph(output_file_path=output_dir,
output_file_base="sensor_ph_bin1",
average_factor=average_factor,
read_every=10.0,
#csv="test/test_inputs/sensor_ph_sinewave01_voltage.csv",
#calibrate_on_startup=True,
#calibration_file="test/test_inputs/sensor_ph_calibration_mock.json",
calibrate_on_startup=False,
verbose=verbose)
pump_up = Controllable_Pump(27,verbose=verbose)
pump_down = Controllable_Pump(17,verbose=verbose)
controller = Controller_ph_Pump(sensor_ph,
pump_up,
pump_down,
ph_min=ph_min,
ph_max=ph_max,
output_file_path=output_dir,
output_file_base="controller_ph_pump",
ml_per_s=1.75, # ml/sec, measured 3.5ml in 2 seconds on 20200213
dispense_volume=3, # ml
control_every=30*60,
warmup_time=30.*60,
verbose=verbose)
# factors for temp and humidity:
# read_every [sec] | Factor | Time to settle from 50 to 25 (<5% error) [minutes]
# 15 .9 7.25
# 15 .8 3.50
# 30 .9 14.5
# 30 .8 7.00
sensor_ht_ambient = Sensor_humidity_temp(output_file_path=output_dir,
output_file_base="humidity_temp_ambient",
gpio_pin=18,
read_every=30.0,
average_factor_temp=0.8,
average_factor_humidity=0.8,
#csv="test/test_inputs/sensor_humidity_temp_input.csv",
verbose=False)
sensor_ht_grow = Sensor_humidity_temp(output_file_path=output_dir,
output_file_base="humidity_temp_grow",
gpio_pin=23,
#csv="test/test_inputs/sensor_humidity_temp_input.csv",
read_every=30.0,
average_factor_temp=0.8,
average_factor_humidity=0.8,
verbose=verbose)
sensor_volume = Sensor_volume(output_file_path=output_dir,
output_file_base="sensor_volume",
trigger_pin=20,
echo_pin=21,
iterations_per_reading=5,
average_factor=0.9,
read_every=30.,
calibration_file=None,
calibrate_on_startup=False,
verbose=False)
start_dt = datetime.datetime.now()
start_dt_string = start_dt.strftime("%m-%d %H:%M")
term = Terminal()
with term.cbreak(), term.hidden_cursor(), term.fullscreen():
menuItems = MenuItems()
menuItems.addItem(MenuItem(name="Quit",key="q",function=quit))
menuItems.addItem(MenuItem(name="Pause",key="p",function=pause))
menuItems.addItem(MenuItem(name="Calibrate pH",key="c",function=ph_calibration))
try:
while True:
sensor_ph()
sensor_ht_ambient()
sensor_ht_grow()
sensor_volume()
controller()
val = term.inkey(timeout=.5) # how often to update the screen
for item in menuItems:
if val.lower() == item.key:
item()
screen = term.clear
# ph sensor
sensor = sensor_box(top=top,
left=left,
width=cols_per_device,
name="pH Sensor",
current=sensor_ph.ph_raw,
average=sensor_ph.ph_avg)
screen += sensor
sensor = controller_box(top=top,
left=left + col_padding + cols_per_device,
width=cols_per_device,
name="pH Controller",
action=controller.last_action,
action_time=controller.last_action_time,
loop_time=controller.last_loop_time)
screen += sensor
sensor = sensor_box(top=top+rows_per_device,
left=left,
width=cols_per_device,
name="Ambient Temperature Sensor",
current=sensor_ht_ambient.temp_raw,
average=sensor_ht_ambient.temp_avg)
screen += sensor
sensor = sensor_box(top=top+rows_per_device,
left=left + col_padding + cols_per_device,
width=cols_per_device,
name="Ambient Humidity Sensor",
current=sensor_ht_ambient.humidity_raw,
average=sensor_ht_ambient.humidity_avg)
screen += sensor
sensor = sensor_box(top=top+2*rows_per_device,
left=left,
width=cols_per_device,
name="Chamber Temperature Sensor",
current=sensor_ht_grow.temp_raw,
average=sensor_ht_grow.temp_avg)
screen += sensor
sensor = sensor_box(top=top+2*rows_per_device,
left=left + col_padding + cols_per_device,
width=cols_per_device,
name="Chamber Humidity Sensor",
current=sensor_ht_grow.humidity_raw,
average=sensor_ht_grow.humidity_avg)
screen += sensor
sensor = sensor_box(top=top+3*rows_per_device,
left=left,
width=cols_per_device,
name="Volume Sensor",
current=sensor_volume.volume_raw,
average=sensor_volume.volume_avg)
screen += sensor
# Handle the titles and menu
uptime = datetime.datetime.now() - start_dt
uptime = str(uptime).split(".")[0] # get the delta, strip off decimal seconds
title = term.on_darkolivegreen("Grow Control")
screen += term.move_xy(0,1) + term.center(title)
title = "Started {} Uptime {}".format(start_dt_string,uptime)
screen += term.move_xy(0,2) + term.center(title)
screen += str(menuItems)
print(screen)
except KeyboardInterrupt:
pass