5

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.

teryret
  • 557
  • 1
  • 5
  • 15

5 Answers5

3

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.

Ignacio Vazquez-Abrams
  • 699,552
  • 132
  • 1,235
  • 1,283
  • How would I determine if the controller exposes itself as a HID? – teryret May 27 '12 at 03:49
  • Watch the system log. You should see "HID" go by as you plug it in. – Ignacio Vazquez-Abrams May 27 '12 at 03:49
  • I'm not sure it's that simple. The controller is bluetooth but it behaves strangely. When I run bluetooth-wizard it repeatedly flashes on and off the device list and I can't connect to it. Nothing appeared in dmesg when I powered the controller on, nor when I ran the wizard, nor when I used the WG driver to successfully connect. – teryret May 27 '12 at 03:59
  • Edit: check that, one thing did appear but I didn't notice it. Each time I use the WG driver I get a new input: [11270.347066] input: Sony Playstation SixAxis/DS3 as /devices/virtual/input/input19 It's just a matter of figuring out how to read that file. – teryret May 27 '12 at 04:04
  • Edit2: that file doesn't exist... I'm really confused. – teryret May 27 '12 at 04:19
  • All the stuff for the event subsystem is in `/dev/input/event*`. One of them is likely to be the device. – Ignacio Vazquez-Abrams May 27 '12 at 04:20
  • So I have event0 through event8, regardless of whether or not the driver is running and the controller is on. – teryret May 27 '12 at 14:38
  • Ok, I've read through the code and I'm sure it's writing the data to /dev/uinput . The question is, is [this](http://docs.python.org/library/fcntl.html) the way I want to consume the data? Does anyone know of a more newb friendly introduction to doing this in python (more newb friendly than reading the methods reference that is)? – teryret May 27 '12 at 15:10
2

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

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
spaceman
  • 88
  • 9
  • Please do read this post on offering your own OSS solutions: [How to offer personal open-source libraries?](https://meta.stackexchange.com/q/229085). It'd be helpful if you included an example of how your library should be used and / or how it solved the problem stated. – Martijn Pieters Nov 27 '15 at 08:00
1

Try

pygame.event.pump()

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

Meozaa
  • 93
  • 8
0

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)
okobaka
  • 508
  • 3
  • 8
  • One thing you might try is opening up the ps3joy driver and adding a print line to dump the list of values being sent into uinput, then you can press a button and see the value change. For the time being that's what I've done; I just incorporated the driver into my program so I can bypass uinput completely. It's a terrible hack, but at least it works. – teryret Jun 05 '12 at 14:28
0

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."

Mike M
  • 1