mirror of
https://github.com/SHC-ASTRA/rover-ros2.git
synced 2026-02-11 09:20:40 +00:00
add an error instead of a crash when a gamepad fails to initialize
This commit is contained in:
@@ -76,12 +76,19 @@ class Headless(Node):
|
|||||||
# Initialize the gamepad
|
# Initialize the gamepad
|
||||||
id = 0
|
id = 0
|
||||||
while True:
|
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")
|
self.get_logger().fatal("Ran out of controllers to try")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
try:
|
||||||
self.gamepad = pygame.joystick.Joystick(id)
|
self.gamepad = pygame.joystick.Joystick(id)
|
||||||
self.gamepad.init()
|
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()}")
|
print(f"Gamepad Found: {self.gamepad.get_name()}")
|
||||||
|
|
||||||
if self.gamepad.get_numhats() == 0 or self.gamepad.get_numaxes() < 5:
|
if self.gamepad.get_numhats() == 0 or self.gamepad.get_numaxes() < 5:
|
||||||
@@ -138,7 +145,7 @@ class Headless(Node):
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# Check if controller is still connected
|
# Check if controller is still connected
|
||||||
if pygame.joystick.get_count() == 0:
|
if pygame.joystick.get_count() != self.num_gamepads:
|
||||||
print("Gamepad disconnected. Exiting...")
|
print("Gamepad disconnected. Exiting...")
|
||||||
# Send one last zero control message
|
# Send one last zero control message
|
||||||
self.core_publisher.publish(CORE_STOP_MSG)
|
self.core_publisher.publish(CORE_STOP_MSG)
|
||||||
|
|||||||
Reference in New Issue
Block a user