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...
- Ps3 Controller Bluetooth Address
- Ps3 Controller Mac Address
- How To Get Ps3 Controller Bluetooth Address
- Ps3 Controller Addresses
A note on pairing¶
Configuring Playstation 3 Controllers¶. If you’re building a robot you will at some point probably want a way to manually drive it around. The Playstation3 controller, also known as the SixAxis, makes for a great option - it connects over bluetooth, has a bundle of different buttons, sticks and motion sensors, and is readily available.
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.
- As input, we need to pass the Bluetooth address stored on the PS3 controller. This address can be retrieved by following the procedure illustrated here. Note that the mentioned tutorial covers the connection of a PS4 controller but the procedure and the software to find out the Bluetooth address is exactly the same for a PS3 controller.
- Before we get started, it’s important to understand that a controller paired with a PS3 console has the Bluetooth MAC address of the console stored. Consequently, this is the only device to which the controller will connect.
- I’d be remiss if I didn’t address the elephant in the room, though: Bluetooth can be. Just do a Google search for “retropie ps3 controller” and you’ll come across pages of results showing RetroPie users who just can’t seem to connect their PS3 controllers to their Raspberry Pi. Personally, I didn’t have any issues.
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¶
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.
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).
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:
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)
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)
Ps3 Controller Bluetooth Address
Settings > Accessory Settings > Manage Bluetooth® Devices
Register, or pair, Bluetooth®-compatible devices on your PS3™ system. You also can manage the Bluetooth® devices that connect to your system.
Ps3 Controller Mac Address
Registering a Bluetooth®-compatible device
3. | Select [Manage Bluetooth® Devices]. |
---|---|
5. | Select [Start Scanning]. |
Hints
- When you are using the maximum number of wireless controllers, delete any devices you are not using from the list of registered devices to make room for the new Bluetooth® device.
- For details on the Bluetooth®-compatible device, refer to the instructions supplied with the device.
How To Get Ps3 Controller Bluetooth Address
Managing Bluetooth® devices
Ps3 Controller Addresses
Check information about Bluetooth®-compatible devices that are connected to your PS3™ system, or connect and disconnect devices. After selecting the device you want to manage, press the button, and then select options from the options menu.