Robot arm, Raspberry pi and wiimote

Sun 08 December 2013

Last week my 8 year old son was tasked with making a robot for a school project, but one made from toilet roll tubes, aluminium foil and possibly an LED or a wee speaker to make a noise. I said unto him, "No, you shalt make a real robot!" and he replied "But, how wise pater?" and I said "Lo! Let us go forth to Maplin and searcheth the webs and employ Raspberry of Pi."*

So we went to Maplin and bought this robot, and spent a companionable Sunday afternoon putting the robot arm together. Gear boxes needed constructing, motors needed wired to the control board and numerous small screws, fixings and nuts needed careful manipulation to bring the black and yellow mechanical beastie into this world. It certainly wasn't an easy undertaking, but the instructions were good and we worked methodically and carefully for about 2 hours with a couple of short breaks.

When we finished I installed the Windows-only software on my desktop PC running XP, got my son to plug it in and switch it on and... it worked first time! My son played about with it for a bit and we were particularly tickled by his attempts at getting the arm to dip down to the keyboard and press the key that would make it reverse its movement and move back up - yes, we'd made a self-controlling robot. Scarey.

But, he certainly wasn't going to take this PC into school, nor was it feasible to install the (closed-source) software on the teacher's PC because it'd probably require a call to some off-site IT support, incurring a cost to the school of more than the robot arm itself.

So I suggested to my son that we a) use my raspberry pi and b) use a wiimote to control the robot arm. This was met with some enthusiasm.

A quick search of the web led me to Poly's Python blog, which, although not updated in almost a year, contained an interesting record of someone hacking python on GNU/linux until it controlled the robot arm.

The last update on that blog tells of Matt Dyson's further work on developing a python class and getting an XBox controller to control the robot. I checked his python code out of his SVN repository, connected the robot arm up to my laptop (a Dell XPS 13 with Ubuntu linux 12.04 LTS) and tried to run his testRobotArm.py script:

sudo python testRobotArm.py

This failed with a message about usb.core because I'd skipped the step of installing PyUSB 1.0, which was clearly described in his blog post - RTFM! After that was corrected, the robot arm waved around in its programmed test dance.

Next up, getting the wiimote to talk to linux and python. A quick web search led me to Wii Controller + Raspberry Pi + Python = Awesome!! and all I had to do to get the python classes to give me this awesome python-powered wiimotiness was:

sudo apt-get install python-cwiid

Bluetooth seemed to be working fine on my laptop, so to prove my set-up was working, I downloaded and ran the script wiimotetest.py on the command line:

sudo python wiimotetest.py

and was able to see output in response to pressing buttons on the wiimote. This was too easy, but the work was not yet done.

Next, I procured a cheapo, never-heard-of-before-brand (signalex, I think), USB bluetooth dongle from a local electronics shop. I took it home, stuck the latest Raspbian on my pi, updated it, which took an hour or two, then shoved the bluetooth dongle in, and followed the instructions on the above-mentioned website and once again had wiimotetest.py proving the wiimote connectionworked.

I should say that my son wasn't very involved with this bit, but I did explain what was going on: he was the one that would have to take it into school, set it up and operate it in front of his class.

At this point I had all the information I needed to start hacking away in python to get the wiimote to control the robot arm. But, being very tired, overloaded with a sudden deluge of work and having various family members succumb to a flu/cold, I turned once more to a web search, and found some code by a chap called Keith Jefferies on the official Raspberry Pi forums. Once again, I just downloaded the code, ran it and bingo! I was controlling the robot arm with a wiimote. My son was immediately delighted.

But, some difficulties now presented themselves. I was using the raspberry pi connected to an old TV via HDMI. My son would have no display to use at school, so the raspberry pi would have to boot up, start the script and then he'd have to press buttons to initiate the connection between the wiimote and robot arm.

The second problem was that, despite my initial successes, the connection to the wiimote and robot arm from the pi sometimes didn't work and required two or three attempts, and sometimes both the robot arm and the pi needed to be powered on and off. He'd have to learn to do this guided only by flashing LEDs on the wiimote. After I hacked the code a wee bit and added a line to /etc/rc.local, my son learned the tricks to getting it all connected by pressing the various buttons at the right time. In fact, he quickly became more adept at it than me.

My tweaks to the code of Keith Jefferies is in this script robotarmwii.py (he didn't specify a license, but I'll get in touch with him) and here's my /etc/rc.local that gets the script running on boot.

Bottom line: one happy geeky dad, one happy proto-geek son and an impressed teacher and class, who all got a glimpse of the power of Free and Open Source software, Python and the Raspberry Pi. I do have to report that my son and his pals did use the robot to terrorise some hapless lego mini-figures, and trash the pi's lego case, as evidenced in the above photo.

*This dialogue isn't 100% accurate.