add an error instead of a crash when a gamepad fails to initialize

This commit is contained in:
SHC-ASTRA
2026-01-14 19:49:33 -06:00
parent 0e775c65c6
commit b5be93e5f6

View File

@@ -76,12 +76,19 @@ class Headless(Node):
# Initialize the gamepad
id = 0
while True:
if id >= pygame.joystick.get_count():
self.num_gamepads = pygame.joystick.get_count()
if id >= self.num_gamepads:
self.get_logger().fatal("Ran out of controllers to try")
sys.exit(1)
self.gamepad = pygame.joystick.Joystick(id)
self.gamepad.init()
try:
self.gamepad = pygame.joystick.Joystick(id)
self.gamepad.init()
except Exception as e:
self.get_logger().error("Error when initializing gamepad")
self.get_logger().error(e)
id += 1
continue
print(f"Gamepad Found: {self.gamepad.get_name()}")
if self.gamepad.get_numhats() == 0 or self.gamepad.get_numaxes() < 5:
@@ -138,7 +145,7 @@ class Headless(Node):
sys.exit(0)
# Check if controller is still connected
if pygame.joystick.get_count() == 0:
if pygame.joystick.get_count() != self.num_gamepads:
print("Gamepad disconnected. Exiting...")
# Send one last zero control message
self.core_publisher.publish(CORE_STOP_MSG)