-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
116 lines (96 loc) · 2.98 KB
/
main.py
File metadata and controls
116 lines (96 loc) · 2.98 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
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
import subprocess
import eel
import os
import threading
import time
# Initialize Eel with the 'web' folder
web_folder = os.path.abspath("web")
eel.init(web_folder)
scale = 0.8
width,height = 1920*scale,1080*scale
# Start Eel frontend in fullscreen Chrome app mode
def launch_eel():
eel.start(
"index.html",
mode='chrome',
size=(width,height),
block=True
)
# Run Eel in a separate thread so Python can keep working
eel_thread = threading.Thread(target=launch_eel)
eel_thread.daemon = True
eel_thread.start()
# Define matching logic from MATLAB output to HTML element IDs
def match_line_to_status_id(line):
if "Yaw motor initialization SUCCESS" in line:
return "yaw-status"
elif "Pitch motor initialization SUCCESS" in line:
return "pitch-status"
elif "Gate motor initialization SUCCESS" in line:
return "gate-status"
elif "Interface initialization SUCCESS" in line:
return "limit-status"
return None
def match_line_to_failure_id(line):
if "Yaw motor initialization FAILED" in line:
return "yaw-status"
elif "Pitch motor initialization FAILED" in line:
return "pitch-status"
elif "Gate motor initialization FAILED" in line:
return "gate-status"
elif "Interface initialization FAILED" in line:
return "limit-status"
return None
def extract_zeroed_component(line):
if "Yaw motor zeroed" in line:
return "yaw-status"
elif "Pitch motor zeroed" in line:
return "pitch-status"
elif "Gate motor zeroed" in line:
return "gate-status"
return None
zeroing_order = ["pitch-status", "yaw-status", "gate-status"]
zeroing_index = 0
# Start MATLAB and stream its output line by line
process = subprocess.Popen(
["matlab", "-batch", "run('test.m')"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1
)
@eel.expose
def send_start_command(val):
with open("start_flag.txt", "w") as f:
f.write(str(val))
for line in process.stdout:
line = line.strip()
print("MATLAB:", line)
# Handle success case
status_id = match_line_to_status_id(line)
if status_id:
eel.updateStatus(status_id)
# Handle failure case
failure_id = match_line_to_failure_id(line)
if failure_id:
eel.updateStatusFailure(failure_id)
if "[INIT FAIL]" in line:
eel.showInitFailureUI()
if "[INIT SUCCESS]" in line:
eel.showInitSuccessUI()
zeroed_id = extract_zeroed_component(line)
if zeroed_id:
eel.markAsZeroed(zeroed_id)
# Determine next motor in the sequence
zeroing_index += 1
if zeroing_index < len(zeroing_order):
next_id = zeroing_order[zeroing_index]
eel.markAsZeroing(next_id)
if "[LOCALIZATION SUCCESS]" in line:
eel.fadeAllOut()
time.sleep(2)
eel.showMainUI()
process.wait()
print("MATLAB script finished.")
# Keep the app alive until the frontend is closed
eel_thread.join()