From b5be93e5f6c2dfa68754b230820870e3ce91e6b0 Mon Sep 17 00:00:00 2001 From: SHC-ASTRA <90978381+ASTRA-SHC@users.noreply.github.com> Date: Wed, 14 Jan 2026 19:49:33 -0600 Subject: [PATCH] add an error instead of a crash when a gamepad fails to initialize --- src/headless_pkg/src/headless_node.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/headless_pkg/src/headless_node.py b/src/headless_pkg/src/headless_node.py index 1d72d6e..50717c5 100755 --- a/src/headless_pkg/src/headless_node.py +++ b/src/headless_pkg/src/headless_node.py @@ -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)