-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI5.py.backup
More file actions
477 lines (400 loc) · 17.7 KB
/
GUI5.py.backup
File metadata and controls
477 lines (400 loc) · 17.7 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
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
import sys
import os
import subprocess
from pathlib import Path
from PySide6.QtWidgets import QApplication
from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtCore import QObject, Signal, Slot, QProcess, QUrl
from pdf2image import convert_from_path
from djitellopy import Tello
import random
import pandas as pd
import time
import io
import urllib.parse
import contextlib
from brainflow.board_shim import BoardShim, BrainFlowInputParams, BoardIds
# Add the parent directory to the Python path for file-shuffler
sys.path.append(str(Path(__file__).resolve().parent / "file-shuffler"))
sys.path.append(str(Path(__file__).resolve().parent / "file-unify-labels"))
sys.path.append(str(Path(__file__).resolve().parent / "file-remove8channel"))
import unifyTXT
import run_file_shuffler
import remove8channel
class TabController(QObject):
def __init__(self):
super().__init__()
self.nao_process = None
class BrainwavesBackend(QObject):
# Define signals to update QML components
flightLogUpdated = Signal(list)
predictionsTableUpdated = Signal(list)
imagesReady = Signal(list)
logMessage = Signal(str)
naoStarted = Signal()
naoEnded = Signal()
@Slot()
def startNaoManual(self):
print("Nao6 Manual session started")
self.naoStarted.emit("Nao6 Manual session started")
@Slot()
def stopNaoManual(self):
print("Nao6 Manual session ended")
self.naoEnded.emit("Nao6 Manual session ended")
@Slot()
def connectNao(self):
# Mock function to simulate drone connection
self.flight_log.insert(0, "Nao connected.")
self.flightLogUpdated.emit(self.flight_log)
def __init__(self):
super().__init__()
self.flight_log = [] # List to store flight log entries
self.predictions_log = [] # List to store prediction records
self.current_prediction_label = ""
self.image_paths = [] # Store converted image paths
self.plots_dir = os.path.abspath("plotscode/plots") # Base plots directory
self.current_dataset = "refresh" # Default dataset to display
try:
self.tello = Tello()
except Exception as e:
print(f"Warning: Failed to initialize Tello drone: {e}")
self.logMessage.emit(f"Warning: Failed to initialize Tello drone: {e}")
@Slot(str)
def selectModel(self, model_name):
""" Select the machine learning model """
print(f"Model selected: {model_name}")
self.current_model = model_name
self.flight_log.insert(0, f"Selected Model: {model_name}")
self.flightLogUpdated.emit(self.flight_log)
@Slot()
def readMyMind(self):
""" Runs the selected model and processes the brainwave data. """
if self.current_model == "Random Forest":
prediction = self.run_random_forest()
else:
prediction = self.run_deep_learning()
# Set current prediction
self.current_prediction_label = prediction
# Log the prediction
self.predictions_log.append({
"count": str(len(self.predictions_log) + 1),
"server": "Brainwave AI",
"label": prediction
})
self.predictionsTableUpdated.emit(self.predictions_log)
# Update Flight Log
self.flight_log.insert(0, f"Executed: {prediction} (Model: {self.current_model})")
self.flightLogUpdated.emit(self.flight_log)
def run_random_forest(self):
""" Simulated Random Forest model processing """
print("Running Random Forest Model...")
time.sleep(1) # Simulate processing delay
return random.choice(["Move Forward", "Turn Left", "Turn Right", "Land"])
def run_deep_learning(self):
""" Simulated Deep Learning model processing """
print("Running Deep Learning Model...")
time.sleep(2) # Simulate slightly longer DL processing time
return random.choice(["Move Forward", "Turn Left", "Turn Right", "Land", "Hover"])
@Slot(str)
def notWhatIWasThinking(self, manual_action):
# Handle manual action input
self.predictions_log.append({
"count": "manual",
"server": "manual",
"label": manual_action
})
self.predictionsTableUpdated.emit(self.predictions_log)
# Also update flight log
self.flight_log.insert(0, f"Manual Action: {manual_action}")
self.flightLogUpdated.emit(self.flight_log)
@Slot()
def executeAction(self):
# Execute the current prediction
if self.current_prediction_label:
self.flight_log.insert(0, f"Executed: {self.current_prediction_label}")
self.flightLogUpdated.emit(self.flight_log)
@Slot()
def connectDrone(self):
# Mock function to simulate drone connection
self.flight_log.insert(0, "Drone connected.")
self.flightLogUpdated.emit(self.flight_log)
@Slot()
def keepDroneAlive(self):
# Mock function to simulate sending keep-alive signal
self.flight_log.insert(0, "Keep alive signal sent.")
self.flightLogUpdated.emit(self.flight_log)
@Slot(str)
def getDroneAction(self, action):
try:
if action == 'connect':
self.tello.connect()
self.logMessage.emit("Connected to Tello Drone")
elif action == 'up':
self.tello.move_up(30)
self.logMessage.emit("Moving up")
elif action == 'down':
self.tello.move_down(30)
self.logMessage.emit("Moving down")
elif action == 'forward':
self.tello.move_forward(30)
self.logMessage.emit("Moving forward")
elif action == 'backward':
self.tello.move_back(30)
self.logMessage.emit("Moving backward")
elif action == 'left':
self.tello.move_left(30)
self.logMessage.emit("Moving left")
elif action == 'right':
self.tello.move_right(30)
self.logMessage.emit("Moving right")
elif action == 'turn_left':
self.tello.rotate_counter_clockwise(45)
self.logMessage.emit("Rotating left")
elif action == 'turn_right':
self.tello.rotate_clockwise(45)
self.logMessage.emit("Rotating right")
elif action == 'takeoff':
self.tello.takeoff()
self.logMessage.emit("Taking off")
elif action == 'land':
self.tello.land()
self.logMessage.emit("Landing")
elif action == 'go_home':
self.go_home()
else:
self.logMessage.emit("Unknown action")
except Exception as e:
self.logMessage.emit(f"Error during {action}: {e}")
# Method for returning to home (an approximation)
def go_home(self):
# Assuming the home action means moving backward and upwards
self.tello.move_back(50) # Move back to home point (adjust distance as needed)
self.tello.move_up(50) # Move up to avoid obstacles
self.logMessage.emit("Returning to home")
@Slot()
def check_plots_exist(self):
"""
Check if all necessary plot PDFs exist in both Rollback and Refresh directories.
If not, run controller.py to generate them.
"""
print("\n=== CHECKING IF PLOTS EXIST ===")
# Create plots base directory if it doesn't exist
plots_base_dir = Path(self.plots_dir)
if not plots_base_dir.exists():
print(f"Creating plots base directory: {plots_base_dir}")
plots_base_dir.mkdir(parents=True, exist_ok=True)
# List of datasets to check
datasets = ["rollback", "refresh"]
# List of PDF files that should exist for each dataset
pdf_files = [
"takeoff_plots.pdf", "forward_plots.pdf", "right_plots.pdf",
"land_plots.pdf", "backward_plots.pdf", "left_plots.pdf"
]
# Check if all directories and PDFs exist
missing_pdfs = False
for dataset in datasets:
dataset_dir = plots_base_dir / dataset
if not dataset_dir.exists():
print(f"Creating dataset directory: {dataset_dir}")
dataset_dir.mkdir(parents=True, exist_ok=True)
missing_pdfs = True
continue
print(f"Checking PDFs in {dataset_dir}...")
for pdf_file in pdf_files:
pdf_path = dataset_dir / pdf_file
if not pdf_path.exists():
print(f"Missing file: {pdf_path}")
missing_pdfs = True
break
# If any PDFs are missing, run the controller.py script
if missing_pdfs:
print("Some plot files are missing. Running controller.py to generate them...")
# Get the path to controller.py in the plotscode directory
controller_path = Path(self.plots_dir).parent / "controller.py" # plotscode/controller.py
print(f"Controller path: {controller_path}")
print(f"Controller exists: {controller_path.exists()}")
if controller_path.exists():
try:
# Change to the plotscode directory before running the script
original_dir = os.getcwd()
os.chdir(controller_path.parent)
# Run the controller.py script to generate plots for both datasets
print(f"Executing: {sys.executable} {controller_path}")
result = subprocess.run(
[sys.executable, str(controller_path)],
check=True,
capture_output=True,
text=True
)
# Go back to the original directory
os.chdir(original_dir)
# Print output for debugging
print(f"Output: {result.stdout}")
if result.stderr:
print(f"Errors: {result.stderr}")
print("Successfully generated plot files.")
return True
except subprocess.CalledProcessError as e:
print(f"Error running controller.py: {e}")
if hasattr(e, 'stderr'):
print(f"Error output: {e.stderr}")
return False
except Exception as e:
print(f"Unexpected error: {str(e)}")
return False
else:
print(f"Controller script not found: {controller_path}")
return False
return True # All files exist
@Slot(str)
def setDataset(self, dataset_name):
"""
Set the current dataset to display (refresh or rollback).
:param dataset_name: Name of the dataset ('refresh' or 'rollback')
"""
if dataset_name.lower() in ["refresh", "rollback"]:
self.current_dataset = dataset_name.lower()
print(f"Switched to {self.current_dataset} dataset")
# Update the displayed images
self.convert_pdfs_to_images()
else:
print(f"Invalid dataset name: {dataset_name}")
@Slot()
def convert_pdfs_to_images(self):
"""
Convert PDF files from the current dataset to images and send to QML.
"""
print(f"\n=== STARTING CONVERT PDFS TO IMAGES FOR {self.current_dataset.upper()} ===")
# First check if all plot PDFs exist, and generate them if needed
success = self.check_plots_exist()
print(f"Result of check_plots_exist: {success}")
# Current dataset directory
dataset_dir = Path(self.plots_dir) / self.current_dataset
# Convert PDF files to images and send image paths + graph names to QML.
self.image_paths = []
graph_titles = ["Takeoff", "Forward", "Right",
"Landing", "Backward", "Left"]
# Load files in the correct order
pdf_files = [
"takeoff_plots.pdf", "forward_plots.pdf", "right_plots.pdf",
"land_plots.pdf", "backward_plots.pdf", "left_plots.pdf"
]
for i, pdf_file in enumerate(pdf_files):
pdf_path = dataset_dir / pdf_file
if not pdf_path.exists():
print(f"Missing file: {pdf_path}") # Debugging: Check missing PDFs
continue # Skip if file does not exist
images = convert_from_path(str(pdf_path), dpi=150) # Convert PDF to image
image_path = dataset_dir / f"{pdf_file.replace('.pdf', '.png')}"
images[0].save(str(image_path), "PNG") # Save first page as an image
# Debugging: Print the generated image path
print(f"Generated image: {image_path}")
self.image_paths.append({
"graphTitle": graph_titles[i],
"imagePath": QUrl.fromLocalFile(str(image_path)).toString()
})
# Debugging: Print final list of image paths
print("Final Image Paths Sent to QML:", self.image_paths)
self.imagesReady.emit(self.image_paths) # Send data to QML
@Slot()
def launch_file_shuffler_gui(self):
# Launch the file shuffler GUI program
file_shuffler_path = Path(__file__).resolve().parent / "file-shuffler/file-shuffler-gui.py"
subprocess.Popen(["python", str(file_shuffler_path)])
@Slot(str, result=str)
def run_file_shuffler_program(self, path):
# Need to parse the path as the FolderDialog appends file:// in front of the selection
path = path.replace("file://", "")
if path.startswith("/C:"):
path = 'C' + path[2:]
response = run_file_shuffler.main(path)
return response
# Adding Synthetic Data and Live Data Logic (Row 327 to 355) as part of Ticket 186
@Slot(str, result=str)
def unify_thoughts(self, base_dir):
"""
Called from QML when the user picks a directory.
"""
# strip file:/// if necessary
path = base_dir.replace("file://", "")
if base_dir.startswith("file:///"):
base_dir = urllib.parse.unquote(base_dir.replace("file://", ""))
if os.name == 'nt' and base_dir.startswith("/"):
base_dir = base_dir[1:]
print("Unify Thoughts on directory:", base_dir)
output = io.StringIO()
try:
with contextlib.redirect_stdout(output), contextlib.redirect_stderr(output):
unifyTXT.move_any_txt_files(base_dir)
print("Unify complete.")
except Exception as e:
print("Error during unify:", e)
return output.getvalue()
@Slot(str, result=str)
def remove_8_channel(self, base_dir):
"""
Called from QML when the user picks a directory to remove 8 channel data.
"""
# Decode URL path
if base_dir.startswith("file:///"):
base_dir = urllib.parse.unquote(base_dir.replace("file://", ""))
if os.name == 'nt' and base_dir.startswith("/"):
base_dir = base_dir[1:]
print("Removing 8 Channel data form:", base_dir)
output = io.StringIO()
try:
with contextlib.redirect_stdout(output), contextlib.redirect_stderr(output):
remove8channel.file_remover(base_dir)
print("8 Channel Data Removal complete.")
except Exception as e:
print("Error during cleanup: ", e)
@Slot(str)
def setDataMode(self, mode):
"""
Set data mode to either synthetic or live based on radio button selection.
"""
if mode == "synthetic":
self.init_synthetic_board()
print("Switched to Synthetic Data Mode")
elif mode == "live":
self.init_live_board()
print("Switched to Live Data Mode")
else:
print(f"Unknown data mode: {mode}")
def init_synthetic_board(self):
""" Initialize BrainFlow with synthetic board for testing """
params = BrainFlowInputParams()
self.board = BoardShim(BoardIds.SYNTHETIC_BOARD.value, params)
print("\nSynthetic board initialized.")
def init_live_board(self):
""" Initialize BrainFlow with a real headset """
params = BrainFlowInputParams()
params.serial_port = "/dev/cu.usbserial-D200PMA1" # Update if different on your system
self.board = BoardShim(BoardIds.CYTON_DAISY_BOARD.value, params)
print("\nLive headset board initialized.")
if __name__ == "__main__":
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Fusion"
app = QApplication(sys.argv)
engine = QQmlApplicationEngine()
# Create our controllers
tab_controller = TabController()
print("TabController created")
# Initialize backend before loading QML
backend = BrainwavesBackend()
engine.rootContext().setContextProperty("tabController", tab_controller)
engine.rootContext().setContextProperty("backend", backend)
engine.rootContext().setContextProperty("imageModel", []) # Initialize empty model
engine.rootContext().setContextProperty("fileShufflerGui", backend) # For file shuffler
print("Controllers exposed to QML")
engine.rootContext().setContextProperty("fileShufflerGui", backend) # For file shuffler
# Load QML
qml_file = Path(__file__).resolve().parent / "main.qml"
engine.load(str(qml_file))
# Convert PDFs after engine load
try:
backend.convert_pdfs_to_images()
except Exception as e:
print(f"Error converting PDFs: {str(e)}")
# Ensure image model updates correctly
backend.imagesReady.connect(lambda images: engine.rootContext().setContextProperty("imageModel", images))
sys.exit(app.exec())