PS3 controller driver -> uinput-> python? somehow?
Asked Answered
T

5

6

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.

Topmost answered 27/5, 2012 at 3:37 Comment(0)
S
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.

Sottish answered 27/5, 2012 at 3:43 Comment(8)
How would I determine if the controller exposes itself as a HID?Topmost
Watch the system log. You should see "HID" go by as you plug it in.Sottish
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.Topmost
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.Topmost
Edit2: that file doesn't exist... I'm really confused.Topmost
All the stuff for the event subsystem is in /dev/input/event*. One of them is likely to be the device.Sottish
So I have event0 through event8, regardless of whether or not the driver is running and the controller is on.Topmost
Ok, I've read through the code and I'm sure it's writing the data to /dev/uinput . The question is, is this 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)?Topmost
B
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

Billionaire answered 26/11, 2015 at 18:23 Comment(1)
Please do read this post on offering your own OSS solutions: How to offer personal open-source libraries?. It'd be helpful if you included an example of how your library should be used and / or how it solved the problem stated.Ursi
R
1

Try

pygame.event.pump()

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

Robena answered 5/6, 2012 at 17:16 Comment(0)
L
1

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

Liddy answered 15/2, 2013 at 17:37 Comment(0)
O
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)
Outofdoors answered 5/6, 2012 at 10:49 Comment(1)
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.Topmost

© 2022 - 2024 — McMap. All rights reserved.