Monday 28 September 2015

Controlling Raspberry Pi With A PS3 Controller

Lets try and get our PS3 controller talking to the raspberry pi. I started by attempting to do this on an old raspberry pi model B, but eventually realized this would require 3 available USB sockets - for the keyboard, bluetooth dongle and PS controller.  I suppose you could remove the keyboard and login in via ssh, or get a USB hub, but I decided instead to switch to using the raspberry pi 2, which has four USB ports on the board.  I am going to want the controller on ElMo in the end anyway.

There are some instructions on the diddyborg website (in the getting started tab) explaining how to do this, so lets give it a go.

First, install the joystick package

sudo apt-get -y install joystick

which installs and sets up evtest, inputattach and joystick.
I then need to look at another diddyborg page to get the details about installing QtSixA, also known as sixad

sudo apt-get -y install bluez-utils bluez-compat bluez-hcidump checkinstall

that installs a whole heap of additional packages as well as those listed.  I'm not convinced we will actually need all of those, but no matter.  Go make a cup of tea while that lot is unpacked and set up.  Then, some development libraries,

sudo apt-get -y install libusb-dev libbluetooth-dev joystick pyqt4-dev-tools

ahh - so you don't need that first install of joystick, if we include that here.  Another bunch of packages to install...

wget http://www.pabr.org/sixlinux/sixpair.c

gcc -o sixpair sixpair.c -lusb

wget http://sourceforge.net/projects/qtsixa/files/QtSixA%201.5.1/QtSixA-1.5.1-src.tar.gz

tar xfvz QtSixA-1.5.1-src.tar.gz

cd QtSixA-1.5.1/sixad 

make 

sudo mkdir -p /var/lib/sixad/profiles

sudo checkinstall

the checkinstall command creates a package that we can manage - when prompted, I called mine 'sixad'.  I didn't change any values, I just hit enter at the next prompt and it all seemed to go OK - I now have a package that could be removed at a later date.
Now, attach the bluetooth usb module - mine is a dynamode USB bluetooth adapter, bought from piborg at the same time as I bought the diddyborg

and attach the ps3 controller via it's usb cable and pair them using

~/sixpair

which says something about setting master bd_addr to some hex address - presumably that of my controller?  Now start the sixad software and press the PS3 button when prompted to do so

sudo sixad --start &
sixad-bin[8077]: sixad started, press the PS button now
sixad-bin[8077]: unable to connect to sdp session
sixad-bin[8077]: Connected Sony Computer Entertainment Wireless Controller

that looks promising?  I didn't feel it vibrate though, so lets try

jstest /dev/input/js0

Result! When I hold down buttons or move the joysticks, I get a notification that the button is on, or see that the axes coordinates change.  I think we are in business :)

I tried shutting down the pi, which also appears to turn off the ps3 controller - excellent :)

When I restart the pi, I don't need to rerun the sixpair program - that would appear to be a one off operation.  I just need to restart the sixad daemon.  I could probably do that as part of the boot up, but I would need to know when to press the ps3 button - which might be confusing if we don't have a terminal to look at?  So perhaps best to do it manually - at least until I have more of an idea of what I am actually doing :) - or maybe we could flash an led to alert us as to when we should press the button?

So I just need to remember to a) make sure the ps3 controller is charged and b) that I start the sixad program and pair the controller when I reboot the pi.

Google tells me that the ps3 controller should be good for at least 12 hours or so, so it ought to last the day at piwars without needing to worry about it.  Charging takes around 2 hours.

No comments:

Post a Comment