feat: add control by key through recorder

This commit is contained in:
2025-10-24 19:03:28 +02:00
parent 8ad97785b8
commit 62de92e7a2

View File

@@ -6,6 +6,7 @@ from typing import Optional
from PyQt6 import uic
from PyQt6.QtCore import QObject, QThread, QTimer, pyqtSignal, pyqtSlot
from PyQt6.QtGui import QKeyEvent
from PyQt6.QtWidgets import QMainWindow
from src.command import CarControl, Command, ControlCommand, RecordingCommand
@@ -105,6 +106,13 @@ class RecorderWindow(Ui_Recorder, QMainWindow):
SAVE_DIR: Path = Path(__file__).parent.parent / "records"
COMMAND_DIRECTIONS: dict[str, CarControl] = {
"w": CarControl.FORWARD,
"s": CarControl.BACKWARD,
"d": CarControl.RIGHT,
"a": CarControl.LEFT,
}
def __init__(self, host: str, port: int) -> None:
super().__init__()
@@ -120,13 +128,6 @@ class RecorderWindow(Ui_Recorder, QMainWindow):
uic.load_ui.loadUi("src/recorder.ui", self)
self.command_directions = {
"w": CarControl.FORWARD,
"s": CarControl.BACKWARD,
"d": CarControl.RIGHT,
"a": CarControl.LEFT,
}
self.forwardButton.pressed.connect(
lambda: self.on_car_controlled(CarControl.FORWARD, True)
)
@@ -173,6 +174,25 @@ class RecorderWindow(Ui_Recorder, QMainWindow):
def on_car_controlled(self, control: CarControl, active: bool):
self.send_command(ControlCommand(control, active))
def keyPressEvent(self, event): # type: ignore
if event.isAutoRepeat():
return
if isinstance(event, QKeyEvent):
key_text = event.text()
ctrl: Optional[CarControl] = self.COMMAND_DIRECTIONS.get(key_text)
if ctrl is not None:
self.on_car_controlled(ctrl, True)
def keyReleaseEvent(self, event): # type: ignore
if event.isAutoRepeat():
return
if isinstance(event, QKeyEvent):
key_text = event.text()
ctrl: Optional[CarControl] = self.COMMAND_DIRECTIONS.get(key_text)
if ctrl is not None:
self.on_car_controlled(ctrl, False)
def toggle_record(self):
self.recording = not self.recording
self.recordDataButton.setText(