If you’re building a robot for PiWars, or similar competitions, you’ll probably want to have some way to drive it manually – the most popular way to do this is to connect a game controller (hardware originally intended for game consoles such as the PlayStation or XBox).
The approxeng.input library, available through Pip and compatible with Python2.7 and Python3, makes this as easy as it can be, hiding all the details of how the controller sends events to the host (for example a Raspberry Pi). I’ve just released version 2.0, including the much simpler API described below, so now’s a great time to have a play.
$ sudo apt-get install python-dev python-pip gcc
$ pip install approxeng.input
from approxeng.input.selectbinder import ControllerResource
# Get a joystick
with ControllerResource() as joystick:
# Loop until disconnected
# Get a corrected value for the left stick x-axis
left_x = joystick[‘lx’]
# We can also get values as attributes
left_y = joystick.ly
# Get how long the ‘home’ button has been held, or
# None if it isn’t held
home_hold = joystick.home
# Check whether buttons were pressed since last time
# Was the triangle button pressed?
if ‘triangle’ in joystick.presses:
…and that’s it! Unlike other APIs such as PyGame, the library manages the process of reading events from the controller for you, all you need to do is query the joystick object to get whatever information you need. There are plenty of configuration options, but the defaults are almost always what you’ll need, and the simple API provides 99% of the library’s functionality.
Approxeng.input supports a number of different controllers (many pictured at the start of this post), specifically:
Adding new controllers is fairly simple, but I’m not going to talk about that here, you can read more in the docs if you want to see the details.
Not all controllers are created equal, so how do you choose? Firstly it’s worth noting that you don’t really have to choose in a final sense – one of the things about this library is that it makes it particularly easy to switch controller hardware without making any changes to your code. So, you could start coding your robot with whatever you have already without worrying whether that’s the one you want to use for the final version.
I’ll quickly rank the controllers I’ve used:
The PlayStation3 controller is what I used when I created the first version of the library. I used it because I already had two of them, but really they’re not particularly good options now.
The replacement for the PS3 controller, you will already have one of these if you’ve got a PlayStation 4. These are good controllers, but they’re relatively expensive and have a couple of drawbacks such as non-replaceable batteries.
These are super-cheap, and they feel it! Great for when budget is your main consideration though.
This is a strange beast. It was created to allow ‘tv and sofa’ gaming for games which were never designed to work with a controller. The only reason to attempt to use this is that you already bought one!
This is the ‘normal’ controller for the Wii-U, and it’s actually pretty good – if you already have one it’ll make a fine controller for your robot. That said, it’s not one I’d go out and buy if you don’t already own one.
Along with the PS4, this is the highest spec controller in this list. Note that you’ll need the most recent version of the controller, older versions don’t work over bluetooth and require a huge dongle which doesn’t work properly under linux. You can tell whether you’ve got the right one because it’ll have a ‘pair’ button on the front of the controller where the trigger buttons are.
The wiimote is very different to the other controllers here. It will work with this library thanks to support contributed by Keith Ellis, but as I’ve not personally used one I’m not going to include it in this list!
Based on my experience, you have three options:
Firstly, you should start with whatever supported controller you already have. It’ll probably be good enough, and you can always switch later.
If you’re on a budget, the Rock Candy controllers are ideal. They’re not particularly good, but they’re incredibly easy to use and incredibly cheap! You’ll lose the analogue front triggers but you’ve still got two analogue sticks and a full array of buttons.
If you’re thinking you might want to spend a bit more and get something better, the XBox One S controller is the winner. While it’s similar in functionality to the PS4 controller, its replaceable batteries are a big advantage. Personally I find it much more comfortable to use as well – the analogue sticks on the PS4 are convex and I find my fingers slide off them in a way they don’t on the XB1, but your hands may not be the same shape as mine. There’s a bit of extra configuration required on the linux side to make this controller work properly, but the hardware is excellent.
Another bonus for the XBox controller is that it’ll work with your Windows PC if you have one for gaming (I do!). In fact, most of these controllers can be made to work under Windows, but games expect an XBox controller so some things just work a bit better.