Control your Python!

By tom, July 30, 2017

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.

Installation:

[code lang=”bash”]
$ sudo apt-get install python-dev python-pip gcc
$ pip install approxeng.input
[/code]

Usage:

[code lang=”python”]
from approxeng.input.selectbinder import ControllerResource

# Get a joystick
with ControllerResource() as joystick:
# Loop until disconnected
while joystick.connected:
# 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
joystick.check_presses()
# Was the triangle button pressed?
if ‘triangle’ in joystick.presses:
print(‘Triangle pressed!’)
[/code]

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

Choosing a controller:

Approxeng.input supports a number of different controllers (many pictured at the start of this post), specifically:

  • DualShock3 (Playstation 3)
  • DualShock4 (Playstation 4)
  • Rock Candy (super cheap PS3 clones)
  • Wii Remote Pro (the conventional controller for the Wii-U)
  • Wiimote and nunchuck
  • XBox One S (only the most recent with bluetooth)
  • Steam Controllers

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:

PS3

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.

Pros:

  • You already have one if you have a PS3
  • Comfortable for smaller hands
  • Bluetooth, so no dongle required to work with recent Pi versions

Cons:

  • Only genuine controllers are supported. It’s often hard to find out whether the controller you’re buying has the right chipset, and ones with different bluetooth chips simply won’t work.
  • Pairing process is more annoying than with others, there are instructions in the docs for this library but it’s still not ideal.
  • Can’t change the battery, and the battery will only charge when connected to a computer or console – you can’t charge this up with a regular USB charger.
  • Feels cheap and low quality compared to more recent controllers, but is actually relatively expensive to get genuine ones.
  • Front triggers are buttons rather than analogue axes.
  • Older system, can be hard to obtain genuine controllers.

PS4

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.

Pros:

  • Good build quality. These are solid controllers, they feel solid and well made.
  • Full set of controls, including analogue front triggers.
  • Bluetooth, easy to set up and doesn’t require dongles with modern Pi versions.
  • Charges from standard USB chargers.
  • Current system, so easy to get hold of genuine controllers.

Cons:

  • Non-replaceable battery, so you don’t have the option to quickly swap batteries if your controller dies mid-competition.
  • Relatively expensive.
  • Subjective, but I don’t really like the caps on the analogue sticks, I find my fingers tend to slide off them!

Rock Candy

These are super-cheap, and they feel it! Great for when budget is your main consideration though.

Pros:

  • Cheap cheap cheap!
  • Dongle rather than bluetooth, so literally no configuration required, just plug the dongle in and go
  • Runs off regular batteries, so easy to swap out if your controller dies mid-competition.

Cons:

  • Cheap, and you can tell. Light, and not particularly ergonomic, analogue sticks are twitchy, low quality materials.
  • Dongle adds hardware, and for Pi Zero you’ll need a micro to full-size USB adapter. If you lose the dongle you’ve in effect lost the controller.
  • Front triggers are buttons rather than analogue axes.
  • A bit hit and miss whether the one you buy will actually work, but they’re so cheap this isn’t really a deal-breaker.

Steam Controller

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!

Pros:

  • Good build quality, but that’s about it.
  • Interchangeable batteries.

Cons:

  • Expensive.
  • Only one actual analogue stick, the other ‘stick’ is a touchpad which is a bit odd.
  • Very large – I’m an adult with large hands and it still feels quite bulky.
  • Requires third party daemon running to get events.
  • Dongle based rather than bluetooth.

Wii Remote Pro

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.

Pros:

  • Good quality, light but feels good.
  • Bluetooth, simple pairing process.

Cons:

  • Batteries not replaceable.
  • Front triggers are buttons rather than analogue axes.

XBox One S

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.

Pros:

  • High build quality, materials etc.
  • Bluetooth pairing.
  • Analogue front triggers.
  • Replaceable batteries, and third party support for battery packs, charging stands etc.

Cons:

  • Expensive, along with the PS4 you can expect to spend fifty pounds or so on one of these.
  • Some system configuration tweaks needed before pairing.
  • Requires access to a Windows PC to update controller firmware.
  • May not suit smaller hands.

Wiimote

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!

Conclusion – Selecting your Controller

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.

 

What do you think?

You must be logged in to post a comment.