Try another PS3 controller i have 3 PS3 controllers and on 2 of them the PS button no longer works but every other button is fine. Also a friend of mine has had the exact same issue with one of his controllers so that could be the same problem you are having. Grab the USB cable for your PS3 controller and connect it to your computer. Once you have your controller connected, if the program was able to recognize your controller, you should see the MAC address of your controller displayed, labeled as 'Current Master:'. It sees my PS3 controller but when I hit any of the buttons or move any of the axes it does not seem to register. Its almost as if the Apple driver for the PS3 controller is not working/missing. Thanks for the suggestion though 🙂.
If you’re building a robot you will at some point probably want a way to manually drive it around. The Playstation3controller, also known as the SixAxis, makes for a great option - it connects over bluetooth, has a bundle of differentbuttons, sticks and motion sensors, and is readily available. You’ll probably google for how to make it work with theRaspberry Pi and then, if your experience is anything like mine, you’ll find that every single online guide is a) a copyof one original document and b) doesn’t work. Having solved all these problems, I thought I’d be nice and write themethod down here in the hope that no-one else has to waste the time I’ve just spent on it...
A note on pairing¶
One of the reasons the SixAxis isn’t as easy as it could be to use is how pairing works. Normal bluetooth devices willestablish a link between the device and the host once, then the host can initiate connection using this previouslystored information. In the case of the SixAxis, it’s actually the controller that initiates the process, so we have todo some setup beforehand. We need to tell the controller to which bluetooth host it should attempt to connect, and weneed to tell the host (the Pi) that it should allow the controller’s connection.
Hardware¶
This guide assumes you’re using a Raspberry Pi (I’m using a Pi 2, but there’s no reason this wouldn’t work with olderones). You’ll also need a USB bluetooth dongle and, obviously, a SixAxis controller. I’ve only tried this with genuineSony ones, many of the cheaper ones you’ll find online are clones, they should work but YMMV.
Bluetooth dongles¶
Ps3 Controller Mac Address
Some people are finding this guide does not work. I suspect this is down to the bluetooth dongle, having eliminatedeverything else in the process. The one I’m using is an Asus USB-BT400, it’s tiny and supports all the current Bluetoothstandards. If you get this to work with a different dongle can you let me know on twitter at @approx_eng_ and I’ll addit to this list:
- Asus USB-BT400
Software¶
Note 1 - this assumes you’ve set up git and installed a public key with github, you don’t have to do this but you’llneed to modify some of the git commands below if you haven’t. You can set up public keys using the instructions athttps://help.github.com/articles/generating-ssh-keys/#platform-all
Note 2 - this is also assuming you’re starting from a clean installation of the latest Jessie based Raspbian. Otherdistributions may need varying combinations of dev libraries etc. For testing I was using the minimal installation withfilename 2015-11-21-raspbian-jessie-lite.zip
but these instructions should apply to any recent version. As always,it’s not a bad idea to run sudoapt-getupdate
and sudoapt-getupgrade
to get any changes to packages sinceyour distribution was built.
You’ll need to install some packages on your Pi first, and enable the bluetooth services:
You also need to add the default user to the bluetooth
group:
You must now power cycle your Pi. Do not just reboot, actually shut down, pull the power, wait a few seconds andreconnect. This may be overkill, but it’s been the best way I’ve found to consistently have the next steps succeed.
Pairing¶
Get and build the command line pairing tool:
Firstly we need to tell the controller the address of the bluetooth dongle. To do this you need to connect thecontroller to your Pi with a mini-USB cable. Also make sure your Pi is powered from an external supply - the extrapower needed when you connect the controllers can be too much for a laptop USB socket and you’ll get random errors orthe process won’t work at all. The ‘sixpair’ command, run as root, updates the controller’s bluetooth master address:
You should see a message indicating that the bluetooth master address on the controller has been changed (you canspecify the address to which it should change, the default with no arguments is to use the first installed bluetoothadapter, which is what you want unless for some reason you’ve got more than one plugged in). The controller will nowattempt to connect to your bluetooth dongle when you press the PS button (don’t do this just yet, it won’t work). Theexample above shows that no change has been made, as this particular controller had been paired with the dongle before,but you should see two different addresses - the first is the address the controller was trusting, the second is the oneit now trusts.
Next we need to configure the bluetooth software on the Pi to accept connections from the controller.
Disconnect your controller from the USB port, and run the ‘bluetoothctl’ command as a regular user (you don’t need tobe root for this):
Now re-connect your controller with the mini-USB cable. You should see messages in the terminal indicating thatsomething has connected (but don’t worry if you don’t, as long as something useful appears in the next step!)
Type ‘devices’ in the terminal. You will see a list of possible devices, including at least your SixAxis controller.You need to take note of the MAC address of the controller for the next step:
Type ‘agent on’ and then ‘trust MAC’, replacing MAC with the MAC address you noted in the previous step (they won’tbe the same as mine!). Quit the tool once you’re done.
Disconnect your controller, you should now be able to connect wirelessly. To check this, first list everything in/dev/input:
Now press the PS button, the lights on the front of the controller should flash for a couple of seconds then stop,leaving a single light on. If you now look again at the contents of /dev/input you should see a new device, probablycalled something like ‘js0’:
If a new device has appeared here then congratulations, you have successfully paired yourdongle and SixAxis controller. This will persist across reboots, so from now on you can just connect by pressing the PSbutton on the controller. Pressing and holding this button will shut the controller down - at the moment there’s notimeout so be sure to turn the controller off when you’re not going to be using it for a while.
Accessing the SixAxis from Python¶
You now have a joystick device in /dev/input, but how do you use it in your Python code?
There are two different approaches I’ve tried. You can use PyGame - this has the advantage that you might be using italready (in which case it’s the simplest solution) and it’s already installed in the system Python on your Pi. It hasthe drawback though that it requires a display - while I’m aware there are workarounds for this they’re not reallyvery satisfactory. The second option is to use the Python bindings for evdev - this is lightweight, but has drawbackof being more complex to use and only working on linux, even if you’re on a unix-like system such as OSX you can’t useit whereas PyGame is generally suitable for cross-platform use. Because I only want to run this on the Pi and because Ireally need it to work cleanly in a headless environment I’ve gone with evdev, but there are arguments for both.
Ps3 Controller Address Labels
Actually using evdev isn’t trivial, the best documentation I have is the code I wrote to handle it. I’ve created aPython class triangula.input.SixAxis
and corresponding resource triangula.input.SixAxisResource
tomake this simpler to work with. The class uses asyncore to poll the evdev device, updating internal state within theobject. It also allows you to register button handlers which will be called, handles centering, hot zones (regions inthe axis range which clamp to 1.0 or -1.0) and dead zones (regions near the centre point which clamp to 0.0).
Ps3 Controller Bluetooth Address
By way of an example, the following code will connect to the controller (you’ll get an exception if you don’t have oneconnected) and print out the values of the two analogue sticks:
How To Get Ps3 Controller Bluetooth Address
You’re welcome to pick up Triangula’s libraries, they’re uploaded to PyPi semi-regularly (get with ‘pip installtriangula’) or from github. In either case you’ll need to install one extra package first, without which the evdevmodule won’t build:
Now you can get Triangula’s code from github and build it to acquire the triangula.input module, you can then use thisin your own code (there’s nothing particularly specific to Triangula in it)
Ps3 Controller Addresses
This will set up the libraries in develop mode, creating symbolic links into your python installation (I’m assuming herethat you’re using a virtual environment, because you should be - if you’re not you’ll need to run some of thesecommands as root)
Comments are closed.