-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
78 lines (63 loc) · 1.92 KB
/
Copy pathapp.py
File metadata and controls
78 lines (63 loc) · 1.92 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
"""
Flask web server to render telemetry overlay.
"""
import subprocess
import threading
from typing import Any
from flask import Flask
import altos
from config import ALTOS_COMMAND
app = Flask(__name__)
@app.route("/")
def root():
return app.send_static_file("main.html")
@app.route("/data/<key>")
def data(key):
p = TelemetryWatcher.last_packet
d = p["sensor"]
d["tick"] = p["preamble"]["tick"]
if TelemetryWatcher.launch_at > -1:
met_tick = d["tick"] - TelemetryWatcher.launch_at
d["met"] = (
f"T+{(met_tick // 6000):02d}:{met_tick // 100 % 60:02d}.{met_tick % 100:02d}"
)
else:
d["met"] = "T+00:00.000"
state_map = {
0: "On Pad",
1: "On Pad",
2: "On Pad",
3: "Boost Phase",
4: "Fast Phase",
5: "Coast Phase",
6: "Drogue Deployed",
7: "Main Deployed",
8: "Landed",
}
d["state_c"] = state_map.get(d["state"], "Unknown State")
return str(d.get(key, "ERROR"))
class TelemetryWatcher(threading.Thread):
last_packet: dict[str, Any]
launch_at: int = -1
def run(self):
p = subprocess.Popen(ALTOS_COMMAND, stdout=subprocess.PIPE)
out = p.stdout
if out is None:
raise IOError
while True:
line = out.readline().decode(encoding="utf-8")
try:
packet = altos.parse_serial_line(line)
if packet["preamble"]["type"] == 0x0A: # sensor data
TelemetryWatcher.last_packet = packet
if (
packet["sensor"]["state"] > 0
and TelemetryWatcher.launch_at == -1
):
TelemetryWatcher.launch_at = packet["preamble"]["tick"]
except ValueError:
pass
if __name__ == "__main__":
watcher_thread = TelemetryWatcher()
watcher_thread.start()
app.run()