Added Gui (unfinished)

config into dataclass and enums
new Gui that includes settings
deleted GlobalVariables

small fixes (import, names...)
This commit is contained in:
2026-07-30 22:49:53 +02:00
parent b537677277
commit 9c31de3c38
13 changed files with 383 additions and 230 deletions
+65 -47
View File
@@ -1,67 +1,85 @@
"""
main.py - Entry point using object-oriented Robot and Input abstractions
main.py - Entry point handling dynamic input source selection
"""
from threading import Thread
from multiprocessing import Queue
from multiprocessing import Queue, Process
import time
import config as cfg
from Robot import Robot, HardwareBackend, PyBulletBackend
from simulation import Simulation
from EspCommunication import ESP32Communication
from ArduinoCommunication import ArduinoCommunication
from config import cfg
from Robot import Robot
from gui.MainWindow import (
create_parameter_gui,
render_gui_frame,
stop_gui,
resolve_active_command
)
from inputs.PygameController import controller_loop
def create_robot_instance() -> Robot:
if cfg.sim:
sim_instance = Simulation()
backend = PyBulletBackend(sim_instance)
else:
comm = ArduinoCommunication() if cfg.arduinoConnection else ESP32Communication()
comm.start()
backend = HardwareBackend(comm)
return Robot(backend=backend)
robot_instance: Robot | None = None
is_robot_active = False
def robot_control_loop(robot: Robot, control_queue: Queue):
robot.reset_to_init()
time.sleep(1)
def start_robot():
global robot_instance, is_robot_active
print(f"[Main] Starting Robot with Backend: {cfg.backend.value.upper()}")
robot_instance = Robot(backend_type=cfg.backend)
robot_instance.reset_to_init()
is_robot_active = True
def stop_robot():
global robot_instance, is_robot_active
print("[Main] Stopping Robot...")
is_robot_active = False
if robot_instance is not None:
robot_instance.cleanup()
robot_instance = None
def main_event_loop(control_queue: Queue):
global robot_instance, is_robot_active
create_parameter_gui(
on_start_callback=start_robot,
on_stop_callback=stop_robot
)
tick = 0.05
next_time = time.time()
while True:
next_time += tick
try:
while True:
next_time += cfg.tick_duration
# Drain the queue to get the latest command frame
latest_cmd = None
while not control_queue.empty():
latest_cmd = control_queue.get_nowait()
# Drain latest gamepad frame if available
gamepad_cmd = None
while not control_queue.empty():
gamepad_cmd = control_queue.get_nowait()
if latest_cmd is not None:
robot.robot_state = latest_cmd["state"]
robot.vector_dirmov = latest_cmd["dirmov"]
# Resolve motion vector based on current active dropdown mode
active_cmd = resolve_active_command(gamepad_cmd)
# Run state machine tick
robot.tick()
# Pass inputs to robot and tick state machine
if is_robot_active and robot_instance is not None:
robot_instance.robot_state = active_cmd["state"]
robot_instance.vector_dirmov = active_cmd["dirmov"]
robot_instance.tick()
sleep_time = next_time - time.time()
if sleep_time > 0:
time.sleep(sleep_time)
render_gui_frame()
sleep_time = next_time - time.time()
if sleep_time > 0:
time.sleep(sleep_time)
finally:
stop_gui()
if __name__ == "__main__":
controller_queue = Queue()
control_queue = Queue()
my_robot = create_robot_instance()
controller_process = Process(target=controller_loop, args=(control_queue,))
controller_process.start()
# Start Pygame input thread
input_thread = Thread(target=controller_loop, args=(controller_queue,), daemon=True)
robot_thread = Thread(target=robot_control_loop, args=(my_robot, controller_queue), daemon=True)
input_thread.start()
robot_thread.start()
robot_thread.join()
try:
main_event_loop(control_queue)
finally:
controller_process.terminate()