-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor.py
More file actions
81 lines (71 loc) · 2.25 KB
/
supervisor.py
File metadata and controls
81 lines (71 loc) · 2.25 KB
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
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import socket
from threading import Thread
from time import sleep
import pickle
from PseudoPressureSensor import PseudoPressureSensor
from flag import flag
def main():
HOST = '127.0.0.1'
PORT = 65432
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen(5)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
#data = conn.recv(1024)
sensor = PseudoPressureSensor(15, 510)
t = Thread(target=transmit, args=[conn, sensor])
t.daemon = True
t.start()
sleep(1)
stop_threads = False
if stop_threads==True:
break
def transmit(conn, sensor):
"""
Transmit new pressure measurement / warning every second.
conn is the client socket
"""
#sleep(1)
command_sent = False
while not command_sent:
measurement = sensor.measure()
msg = "Waiting for command from ground station.\n"
val = conn.recv(1024).decode('utf-8')
if val == 'True':
command_sent = True
if (command_sent):
msg = "Command Sent!\n"
print(msg)
info = [msg, measurement, warning(measurement, 50, 460)]
# Pickle converts objects into a format that can be sent via websocket.
packet = pickle.dumps(info)
conn.sendall(packet)
def warning(measurement, min, max):
"""
Generate warning if measurement outside min and max range is detected.
"""
setflag = flag()
if (measurement < min):
setflag.giveWarning = True
setflag.abortFlight = False
setflag.actuatePressureRelease = False
return setflag
if(measurement > max):
setflag.giveWarning = True
setflag.abortFlight = False
setflag.actuatePressureRelease = True
return setflag
else:
setflag.giveWarning = False
setflag.abortFlight = False
setflag.actuatePressureRelease = False
return setflag
main()