Complete Restructered Robot Code

Robot into its own Class instead of lose Global Variables that cause circular imports

StateClass usage instead of the old RobotState.py

New Input Class for Controller and randome intputs
This commit is contained in:
2026-07-30 21:14:50 +02:00
parent 5448335b11
commit b537677277
19 changed files with 955 additions and 916 deletions
+43 -36
View File
@@ -1,49 +1,51 @@
"""
main.py - Entry point using object-oriented Robot and Input abstractions
"""
from threading import Thread
from multiprocessing import Process, Manager, Queue
from multiprocessing import Queue
import time
# Selfmade Libraries
import Controller as ctr
import RobotState as rs
# Global Variables
import GlobalVariables as gv
import config as cfg
from Robot import Robot, HardwareBackend, PyBulletBackend
from simulation import Simulation
from EspCommunication import ESP32Communication
from ArduinoCommunication import ArduinoCommunication
from inputs.PygameController import controller_loop
def robot_control():
# Connection
if gv.robotCommunication != None:
gv.robotCommunication.start()
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)
# Start Position
rs.initPos()
return Robot(backend=backend)
def robot_control_loop(robot: Robot, control_queue: Queue):
robot.reset_to_init()
time.sleep(1)
############################## Main Loop ##############################
tick = 0.05
next_time = time.time()
while gv.control_pause == False:
while True:
next_time += tick
if gv.emote == "wave":
rs.initPos()
time.sleep(0.3)
rs.wave_emote()
gv.emote = None
gv.robot_state = "idle"
continue
# Drain the queue to get the latest command frame
latest_cmd = None
while not control_queue.empty():
latest_cmd = control_queue.get_nowait()
# Robot
if gv.robot_state == "idle":
rs.initPos()
time.sleep(0.2)
elif gv.robot_state == "walking":
rs.walking()
if latest_cmd is not None:
robot.robot_state = latest_cmd["state"]
robot.vector_dirmov = latest_cmd["dirmov"]
# Simulation
if gv.shared_sim != None:
gv.shared_sim.step()
# Run state machine tick
robot.tick()
sleep_time = next_time - time.time()
if sleep_time > 0:
@@ -52,9 +54,14 @@ def robot_control():
if __name__ == "__main__":
controller_queue = Queue()
robot_queue = Queue()
my_robot = create_robot_instance()
controller_thread = Thread(target=ctr.controller)
controller_thread.start()
robot_control_thread = Thread(target=robot_control)
robot_control_thread.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()