piborg / Gamepad

A simple library for getting game controller and joystick inputs into Python in Linux.
MIT License
101 stars 27 forks source link

Key Error #1

Closed mtc-20 closed 2 years ago

mtc-20 commented 4 years ago

Hello! I tried using the Gamepad.py but kept running into a Key error, any idea how to solve it?

We have a Logitech F710 Gamepad, that we'd like to use for event based callbacks, so thought of using this library.

I tried running the Gamepad.pyon python3 with our controller to find the mappings to make my own class, so started with the PS4 class, ran into a key error, thought it was because I used the wrong class for my gamepad.

But after a few trials, found out that the problem wasn't with the Gamepad class. The Gamepad.py generates an output for only 2 buttons (R1 and B) and 2 axes, for every other button or axis event, it throws a Key error. I haven't changed anything in the Gamepad.py. Would be great if I could receive some insight into how to solve this issue.

Screenshots

Available device names:
    Gamepad - Generic (numbers only)
    Logitech - Logitech Rumblepad
    MMP1251 - ModMyPi Raspberry Pi Wireless USB Gamepad
    PS3 - PlayStation 3 controller
    PS4 - PlayStation 4 controller
    Xbox360 - Xbox 360 controller
    example - Enter the human readable name of the device here

What device name are you using (leave blank if not in the list)
? Logitech

Gamepad connected
Logitech Rumblepad
BUTTON,   R1,   True
BUTTON,   R1,   False
BUTTON,   R1,   True
BUTTON,   R1,   False
BUTTON,   R1,   True
BUTTON,   R1,   False
BUTTON,   R1,   True
BUTTON,   B,    False
BUTTON,   B,    True
BUTTON,   B,    False
BUTTON,   B,    True
BUTTON,   B,    False
BUTTON,   B,    True
BUTTON,   B,    False
BUTTON,   B,    True
BUTTON,   B,    False
BUTTON,   B,    True
BUTTON,   B,    False
Traceback (most recent call last):
  File "./Gamepad.py", line 613, in <module>
    eventType, index, value = gamepad.getNextEvent()
  File "./Gamepad.py", line 244, in getNextEvent
    return self.getNextEvent()
  File "./Gamepad.py", line 211, in getNextEvent
    for callback in self.movedEventMap[index]:
KeyError: 3

Hardware setup

Thanks in advance

mtc-20 commented 4 years ago

Tiny update: The scripts work fine on a RaspberryP, so it works for our purpose. So, thanks a lot for this library!

But, I would still like some insight into why it doesn't behave the same way on an Ubuntu workstation, hope somebody could direct me.

Hardware setup

millerda commented 4 years ago

Did you happen to ever make any headway on this? I'm having the same issue on Ubuntu 18.04.

mtc-20 commented 4 years ago

Unfortunately no. Since we wanted very limited functionality, we just went ahead with our own class using threading and inputs library.

They work on Raspberry Pi (3 and 4), but not on Jetson Nano or Ubuntu workstations.

millerda commented 4 years ago

Thanks for the quick response. I actually managed to fix this by just excepting KeyErrors in the relevant locations and then next time the button is pressed it gets added appropriately. I started with inputs but reading the gamepad is a blocking call which is causing us all kinds of issues.

We need to read the gamepad to send its state over a network connection and the server is expecting the output at a set rate so blocking reads isn't working. Found a few potential avenues to fix this but if you had any immediate thoughts to share it would be appreciated.

mtc-20 commented 4 years ago

Thanks for the quick response. I actually managed to fix this by just excepting KeyErrors in the relevant locations and then next time the button is pressed it gets added appropriately.

That worked for you? That's good to know! We didn't pursue that approach when we realized that KeyErrors were being raised at too many locations

I started with inputs but reading the gamepad is a blocking call which is causing us all kinds of issues.

Same here, that's why I wanted to use this library

We need to read the gamepad to send its state over a network connection and the server is expecting the output at a set rate so blocking reads isn't working. Found a few potential avenues to fix this but if you had any immediate thoughts to share it would be appreciated.

Sorry, no. The workaround we came up with was to make sure the gamepad calls from the inputs library is always on a new thread, so that it doesn't exactly block anything else...

nophysicalbody commented 2 years ago

I tried this library on Ubuntu 18.04 and had the same issue. I made Poohl's change and now it is working :) Thank you Poohl!