Files
JackBot/main.py
T
JackM323 3531430661 Disconnection detection added
PC checks for connection
Esp32 send Heartbeat
2026-11-05 10:37:24 +01:00

88 lines
2.5 KiB
Python

"""
main.py - Entry point handling dynamic input source selection
"""
from multiprocessing import Queue, Process
import time
from config import cfg
from Robot import Robot
from gui.MainWindow import (
create_parameter_gui,
render_gui_frame,
stop_gui,
resolve_active_command,
update_robot_status,
)
from inputs.PygameController import controller_loop
robot_instance: Robot | None = None
is_robot_active = False
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
)
next_time = time.time()
try:
while True:
next_time += cfg.tick_duration
# Drain latest gamepad frame if available
gamepad_cmd = None
while not control_queue.empty():
gamepad_cmd = control_queue.get_nowait()
# Resolve motion vector based on current active dropdown mode
active_cmd = resolve_active_command(gamepad_cmd)
# 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()
if robot_instance.connection_status is not None:
update_robot_status(robot_instance.connection_status)
render_gui_frame()
sleep_time = next_time - time.time()
if sleep_time > 0:
time.sleep(sleep_time)
finally:
stop_gui()
if __name__ == "__main__":
control_queue = Queue()
controller_process = Process(target=controller_loop, args=(control_queue,))
controller_process.start()
try:
main_event_loop(control_queue)
finally:
controller_process.terminate()