-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcs.py
537 lines (483 loc) · 17.2 KB
/
pcs.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
## PYTHEAS CONTROL SYSTEM
##
## Primary control interface for the Pytheas
## Underwater Sensor Platform (USP).
from prettytable import PrettyTable
from time import strftime, sleep, time
import os, subprocess, sys
import logging
import io
import picamera
import threading
import socket
import math
import csv
import RPi.GPIO as gpio
import ms5837
# Quick Values: Settings you might need to change quickly
fileroot = "/mnt" # Root file path where data gets saved
auxPath = "/backups/" # Root path for back up files (ADD TRAILING SLASH!)
logfile = "/mnt/pcs.log" # Location of the PCS log file
camrot = 270 # Default rotation for the camera
# Function declarations
def header():
# Prints header
os.system('clear')
print ("+------------------------------------------------------------------------------------+")
print ("| PYTHEAS CONTROL SYSTEM |")
def subheader(menuname):
# Prints the header for submenus
titlelen = 84
leftpadlen = math.floor((titlelen - len(menuname))/2)
rightpadlen = math.ceil((titlelen - len(menuname))/2)
titleline = "|" + " "*leftpadlen + menuname + " "*rightpadlen + "|"
hborder = "+------------------------------------------------------------------------------------+"
header()
print(hborder)
print(titleline)
print(hborder)
print()
def refreshUI():
# Prints the main menu & adds entry to the passive capture log
headertable.clear_rows()
ltime, pressure, depth, etemp, itemp = writeLog("")
pressure = round(pressure, 2)
depth = round(depth, 2)
headertable.add_row([ltime, str(pressure) + " mbar", str(depth) + " m", str(etemp) + " C", str(itemp) + " C"])
if gpio.input(lamp):
lampStatus = "ON"
else:
lampStatus = "OFF"
if isAcap():
acapStatus = "ACTIVE"
else:
acapStatus = "inactive"
header()
print (headertable)
print ()
print ("Streaming video at tcp/h264://pytheas-usp:19212")
print ("ACAP is currently {}.".format(acapStatus))
print ()
print ("1. Capture Picture")
print ("2. Automatic Data Capture")
print ("3. Change Camera Settings")
print ("4. Write Note to Log")
print ("5. Toggle Lamp (Currently {})".format(lampStatus))
print ("-----")
print ("9. Refresh Display")
print ("0. Quit MCD")
print ()
def getReadings():
# Gathers sensor data
ltime = strftime("%H:%M:%S")
pressure = baro.pressure()
depth = baro.depth()
etemp = round(baro.temperature(), 2)
itemp = round(float(subprocess.getoutput('cat /sys/class/thermal/thermal_zone0/temp')) / 1000, 2)
return ltime, pressure, depth, etemp, itemp
def writeLog(note):
# Writes an entry in the passive log
ltime, pressure, depth, etemp, itemp = getReadings()
with open(sessionFullFile, mode='a') as file:
dx = csv.writer(file)
dx.writerow([ltime, pressure, depth, etemp, itemp, note])
return ltime, pressure, depth, etemp, itemp
def capture(capmode):
# Captures a single picture
lampstatus = gpio.input(lamp)
gpio.output(lamp, flash)
try:
cam.resolution = (3280, 2464) # 3280x2462 == max resolution of picamera v2
cam.annotate_text_size = 50
except picamera.exc.PiCameraRuntimeError:
logging.info("Preview stream is open - camera capturing at reduced resolution.")
filename = "{}_{}_{}.png".format(sessionName, capmode, strftime("%Y%m%d-%H%M%S"))
buildAnnotate()
cam.capture("{}/{}".format(sessionPath, filename))
logging.info("Picture captured as "+ filename)
gpio.output(lamp, lampstatus)
return filename
def camsettings():
# Allows adjustment of the camera settings
def setbrightness():
print()
print("Camera brightness can be any integer from 0 to 100.")
print("It is currently {}. The default is 50.".format(cam.brightness))
print()
choice = int(input("What do you want to set the brightness to? "))
if not choice:
return
if choice < 0:
choice = 0
if choice > 100:
choice = 100
cam.brightness = choice
return
def setcontrast():
print()
print("Camera contrast can be any integer from -100 to 100.")
print("It is currently {}. The default is 0.".format(cam.contrast))
print()
choice = int(input("What do you want to set the contrast to? "))
if not choice:
return
if choice < -100:
choice = -100
if choice > 100:
choice = 100
cam.contrast = choice
return
def setexposure():
print()
print("Camera exposure can be any of the following:")
print("1. auto")
print("2. night")
print("3. nightpreview")
print("4. backlight")
print("5. spotlight")
print("6. sports")
print("7. snow")
print("8. beach")
print("9. fixedfps")
print("10. antishake")
print("11. fireworks")
print()
print("It is currently {}. The default is auto".format(cam.exposure_mode))
print()
choice = int(input("What do you want to set the exposure mode to? "))
if not choice:
return
if choice < 1 or choice > 11:
return
if choice == 1:
cam.exposure_mode = 'auto'
elif choice == 2:
cam.exposure_mode = 'night'
elif choice == 3:
cam.exposure_mode = 'nightpreview'
elif choice == 4:
cam.exposure_mode = 'backlight'
elif choice == 5:
cam.exposure_mode = 'spotlight'
elif choice == 6:
cam.exposure_mode = 'sports'
elif choice == 7:
cam.exposure_mode = 'snow'
elif choice == 8:
cam.exposure_mode = 'beach'
elif choice == 9:
cam.exposure_mode = 'fixedfps'
elif choice == 10:
cam.exposure_mode = 'antishake'
elif choice == 11:
cam.exposure_mode = 'fireworks'
return
def setwb():
print()
print("Camera white balance can be any of the following:")
print("1. auto")
print("2. sunlight")
print("3. cloudy")
print("4. shade")
print("5. tungsten")
print("6. fluorescent")
print("7. incandescent")
print("8. flash")
print("9. horizon")
print()
print("It is currently {}. The default is auto".format(cam.awb_mode))
print()
choice = int(input("What do you want to set the exposure mode to? "))
if not choice:
return
if choice < 1 or choice > 9:
return
if choice == 1:
cam.awb_mode = 'auto'
elif choice == 2:
cam.awb_mode = 'sunlight'
elif choice == 3:
cam.awb_mode = 'cloudy'
elif choice == 4:
cam.awb_mode = 'shade'
elif choice == 5:
cam.awb_mode = 'tungsten'
elif choice == 6:
cam.awb_mode = 'fluorescent'
elif choice == 7:
cam.awb_mode = 'incandescent'
elif choice == 8:
cam.awb_mode = 'flash'
elif choice == 9:
cam.awb_mode = 'horizon'
return
global flash
while True:
if flash:
flashStatus = "ON"
else:
flashStatus = "OFF"
subheader("Adjust Camera Settings")
print ("1. Brightness (currently {})".format(cam.brightness))
print ("2. Contrast (currently {})".format(cam.contrast))
print ("3. Exposure Mode (currently {})".format(cam.exposure_mode))
print ("4. White Balance Mode (currently {})".format(cam.awb_mode))
print ("5. Use Lamp During Capture (currently {})".format(flashStatus))
print ("-----")
print ("9. Reset everything to defaults")
print ("0. Go back to main menu")
print()
choice = int(input("Please choose a command: "))
if choice == 1:
setbrightness()
elif choice == 2:
setcontrast()
elif choice == 3:
setexposure()
elif choice == 4:
setwb()
elif choice == 5:
if flash:
flash = False
else:
flash = True
elif choice == 9:
## Write code to set all defaults here
cam.brightness = 50
cam.contrast = 0
cam.exposure_mode = 'auto'
cam.awb_mode = 'auto'
flash = False
print()
print("All values set back to their defaults!")
sleep(2)
elif choice == 0:
return
else:
print("Invalid choice!")
sleep(1)
def acap_menu():
# Generates the menu to kick off automatic data capture
subheader("Automatic Data Capture")
pi = input("Polling interval tick time in seconds (default=1)? ")
bi = input("Back up every X ticks (0 for none, default=50)? ")
ci = input("Capture image every X ticks (0 for none [default])? ")
pp = input("Total polling period is X ticks long (default=3600)? ")
poll_interval = 1
bk_interval = 50
cap_interval = 0
poll_period = 3600
if pi:
poll_interval = float(pi)
if bi:
bk_interval = int(bi)
if ci:
cap_interval = int(ci)
if pp:
poll_period = int(pp)
acapSession = threading.Thread(target=acap, args=(poll_interval, cap_interval, poll_period, bk_interval))
acaps.append(acapSession)
acapSession.start()
def acap(poll_interval, cap_interval, poll_period, bk_interval):
# Performs automatic capture of sensor data & pictures
filename = "{}_ACAP_{}.csv".format(sessionName, strftime("%Y%m%d-%H%M%S"))
subheader("Automatic Data Capture")
acapThreads = []
with open("{}/{}".format(sessionPath, filename), mode='w') as file:
dx = csv.writer(file)
dx.writerow(["T", "LTime", "Pressure", "Depth", "ETemp", "ITemp"])
logging.info("Automatic data capture under file name " + filename + " has begun.")
for tick in range(1, poll_period + 1):
startTime = time()
with open("{}/{}".format(sessionPath, filename), mode='a') as file:
dx = csv.writer(file)
ltime, pressure, depth, etemp, itemp = getReadings()
dx.writerow([tick, ltime, pressure, depth, etemp, itemp])
if cap_interval != 0 and tick % cap_interval == 0:
autocap = threading.Thread(target=capture, args=("auto",), daemon=False)
acapThreads.append(autocap)
autocap.start()
if bk_interval != 0 and tick % bk_interval == 0:
backups = threading.Thread(target=bkACAP, args=(filename,))
acapThreads.append(backups)
backups.start()
# This block ensures that the requested tick rate is accurate by
# factoring in how long it took for the tick to process before
# sleeping.
if (time() - startTime) >= poll_interval:
logging.debug("ACAP tick took longer than prescribed tick time!")
else:
sleep(poll_interval-(time()-startTime))
tick += 1
logging.info("Automatic data capture has ended.")
return
def bkACAP(filename):
# Backs up ACAP capture files to guard against data loss.
srcPath = "{}/{}".format(sessionPath, filename)
destPath = "{}AUX_{}".format(auxPath, filename)
os.system("cp {} {}".format(srcPath, destPath))
logging.debug("Copied {} to {}".format(srcPath, destPath))
def isAcap():
for t in acaps:
if t.is_alive():
return True
return False
def quitMCD():
# Gracefully closes the program
global pollActive
logging.info("User-invoked QUIT")
if isAcap():
print()
print("ACAP is currently active! Quitting is not recommended.")
sleep(2)
return
pollActive = False
logging.debug("Sent kill signal to baro poller.")
gpio.output(lamp, False)
bpThread.join()
logging.debug("Baro poller shut down successfully.")
sys.exit()
def netvidHandler():
# Drives the network video stream for live viewing by the SCU.
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 19212))
server_socket.listen(5)
while True:
sessions = []
connection = server_socket.accept()[0].makefile('wb')
session = threading.Thread(target=netStream, args=(connection,), daemon=True)
sessions.append(session)
session.start()
server_socket.close()
def netStream(connection):
# Sustains a live streaming session when SCU connects to video feed.
def closeConnect(reason):
logging.debug("Attempting to close netStream connection ({})...".format(reason))
try:
cam.stop_recording()
except:
pass
try:
connection.close()
except:
pass
try:
cam.resolution = (1280, 720)
cam.annotate_text_size = 30
except:
closeConnect("Failed Previous Closeout")
cam.framerate = 24
cam.start_recording(connection, format='h264')
run = True
while run:
buildAnnotate()
try:
cam.wait_recording(1)
except BrokenPipeError:
closeConnect("Broken Pipe")
run = False
except ConnectionResetError:
closeConnect("Connection Reset")
run = False
def toggleLamp():
# Toggles the lamp state on or off
if gpio.input(lamp):
gpio.output(lamp, False)
else:
gpio.output(lamp, True)
def buildAnnotate():
# Builds the annotation string for the camera
ltime, pressure, depth, etemp, itemp = getReadings()
cam.annotate_background = picamera.Color('black')
pressure = round(pressure, 2)
depth = round(depth, 2)
cam.annotate_text = "Time: {} | Pressure: {}mbar | Depth: {}m | ETemp: {}C | ITemp {}C".format(ltime, pressure, depth, etemp, itemp)
def baroPoller():
# Polls the baro sensor at an orderly interval
while pollActive:
try:
baro.read()
except:
logging.debug("Tried to read baro sensor, but couldn't!")
sleep(0.5) # Wait before polling again
logging.debug("Baro poller attempting to shut down...")
# INITIALIZATION
# Set up header table
headertable = PrettyTable()
headertable.field_names = ["Local Time", "Pressure (millibars)", "Depth (meters)", "External Temp", "Internal Temp"]
# Set up logging
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler(logfile)
]
)
# Initialize camera & lamp
cam = picamera.PiCamera()
cam.rotation = camrot
streamThread = threading.Thread(target=netvidHandler, daemon=True)
streamThread.start()
acaps = []
lamp = 4 # GPIO pin for the lamp controller
flash = False # Determines if lamp should be lit when capturing pictures
gpio.setmode(gpio.BCM)
gpio.setwarnings(False)
gpio.setup(lamp, gpio.OUT)
gpio.output(lamp, False)
# Initialize pressure/temp sensor
baro = ms5837.MS5837_30BA()
pollActive = True
baroInit = False
while not baroInit:
try:
baro.init()
logging.debug("Pressure sensor initialized successfully.")
baroInit = True
except:
logging.debug("Pressure sensor could not be initialized!")
sleep(1)
#setFluidDensity(ms5837.DENSITY_SALTWATER) # Un-comment if operating in saltwater, defaults to freshwater
bpThread = threading.Thread(target=baroPoller)
bpThread.start()
logging.info("PCS has started.")
# Set up the session
print()
sessionName = input("What is the name of this session? ")
sessionPath = "{}/sessions/{}".format(fileroot, sessionName)
os.system("mkdir {}".format(sessionPath))
sessionFile = "{}_passiveCap_{}.csv".format(sessionName, strftime("%Y%m%d-%H%M%S"))
sessionFullFile = "{}/{}".format(sessionPath, sessionFile)
with open(sessionFullFile, mode='a') as file:
dx = csv.writer(file)
dx.writerow(["LTime", "Pressure", "Depth", "ETemp", "ITemp", "Notes"])
logging.info("PCS session " + sessionName + " has been initialized.")
# MAIN
while True:
refreshUI()
choice = int(input("Please choose a command: "))
if choice == 1:
print("Capturing picture...")
capfile = capture("manual")
print("Captured as {}".format(capfile))
sleep(2)
refreshUI()
elif choice == 2:
acap_menu()
elif choice == 3:
camsettings()
elif choice == 4:
print()
note = input("Write your note here: ")
writeLog(note)
logging.info("User-written note: " + note)
refreshUI()
elif choice ==5:
toggleLamp()
elif choice == 9:
pass
elif choice == 0:
quitMCD()
else:
print ("Invalid choice!")
sleep(1)