PS3 controller driver -> uinput-> python? somehow?

5.6k Views Asked by At

I'm trying to read from a PS3 controller in python on Ubuntu and I'm not having much luck. I started with the ps3joy driver from Willow Garage (http://www.ros.org/wiki/ps3joy) which supposedly publishes all the important bits of the PS3 controller to something I had never heard of called "uinput". Apparently it's a linux feature that allows userspace drivers to provide system events. ...Why the WG driver requires root access given that it's supposedly a userspace driver is beyond me, but that's not my question.

Anyway, the current state of me trying to get it to work is that I've got the driver working, and I've verified that it responds to button presses on the controller, but I don't know how to pull any of that data out so I can use it.

My first guess was to use pygame to (hopefully) read from /dev/uinput (which I'm pretty sure is where the driver sends the data):

from pygame import joystick
if not joystick.get_init():
  joystick.init()
js = joystick.Joystick(0)  # there is only one joystick... even if the driver isn't running(!)
js.init()
print js.get_numbuttons()  # perhaps coincidentally correctly prints 17 which is the number of buttons on a PS3 controller
for i in range(js.get_numaxes()):
  print js.get_axis(i)   # always prints 0, no matter what I'm doing with the controller

but it didn't work. The most telling part of the problem is that it does the same thing if I don't have the WG driver running at all.

I'm sure this is something easy, that I'm just not reading the right information, but googling has not helped me find what the right information is and I'm getting tired and desperate.

5

There are 5 best solutions below

8
On

You don't need the driver. Assuming the controller exposes itself as a HID, you can use the event subsystem to read controller events directly from the device.

0
On

Try

pygame.event.pump()

before you read the joystick. I needed it to work with the 360 controller

1
On

Solving similar problem right now: communicate/receive data from PS3 bluetooth remote with python in GNU/Linux.

What i found helpful:

  1. Debugging PS3 Controller http://www.pabr.org/sixlinux/sixlinux.en.html
  2. Looks like working project, for PS3 Remote http://kitlaan.twinaxis.com/projects/bluez-ps3remote/ (it requires to patch bluez 1st) did not tested, yet.
  3. pybluez BT wrapper http://code.google.com/p/pybluez/ (checking it right now)
1
On

I know it's too late, but if anyone will ever need the code or is struggling with it, you can use mine. I've wrote a script in python that gets ps3 data from USB and sends it to specific a MAC address via PC's bluetooth (you can use ps3controller.py only for data). This was made for my quadcopter project.

https://github.com/urbanzrim/ps3controller

0
On

I believe you need the following at the very least:

from pygame import joystick, event, display
display.init()
joystick.init()
js=joystick.Joystick(0)
js.init()
...
for foo in bar:
    event.pump()
    ...

if foo:
    event.pump()
    ...

while bar:
    event.pump()
    ...

I believe that display.init() has to be there because it is needed for event handling...

Also, you can skip a lot of that with

import pygame
pygame.init()
js=pygame.joystick.Joystick(0)
js.init()
...
for foo in bar:
    pygame.event.pump()
    ...
if foo:
    pygame.event.pump()
    ...

while bar:
    pygame.event.pump()
    ....

I could be wrong, but I think your issues are: A) No event.pump in your if/while/for clauses B) No display.init()

Sources: http://webcache.googleusercontent.com/search?q=cache:I56GyE7I4CkJ:iamtherockstar.com/archive/making-hid-devices-easier-using-pygame-joysticks/+&cd=1&hl=en&ct=clnk&gl=us and http://www.pygame.org/docs/ref/event.html

"The input queue is heavily dependent on the pygame display module."