A couple of years ago I was given a Robotic Arm for Christmas, and more recently discovered that you can get a USB control board to allow you to hook it up to any computer. The controller that comes with the arm is pretty naff, and is very difficult to use, so I set about finding a better one! The recent release of the Raspberry Pi had inspired me to start playing around more with hardware-level things, and I’d also recently decided to learn a bit of Python (to replace my knowledge of PHP, which gets me into all kinds of trouble…!) – this seemed like a perfect project!
The controller of choice ended up being an Xbox 360 Wireless Controller for Windows, chosen mainly because I already owned one, but also as they have been around for a while, and have very good support in Linux. I blogged about my adventures getting it set up with the Rasberry Pi here, so I won’t duplicate the content – go read that post before doing anything else here!
The next challenge was getting the Robotic Arm talking to the Raspberry Pi and then creating some way of nicely interfacing with it. Turns out there’s a whole host of posts about this on the internet, the most relevant is probably this blog, which has some interesting information and source code relating to the arm. All of the examples given used PyUSB – a Python module for interfacing with USB devices. For what I needed, the version packaged with Rasbian was too old (version 0.4.something), the latest version being 1.0. In order to install PyUSB 1.0, I needed the following:
$ mkdir ~/pyusb $ cd ~/pyusb $ git clone https://github.com/walac/pyusb.git . $ sudo python setup.py install
The example code that I found all related to reading a set of instructions in from a CSV or DAT file, and then executing them in order. While this is great for defining set routines to be executed over and over, I was more interested in creating a Python class that would let me issue any instruction to the arm, and have it executed straight away. I took the ideas from the blog and created RobotArm.py. This class gives you access to methods for sending instructions to any motor or light at any point, rather than being limited to a set routine. To give it a try yourself:
$ mkdir ~/robotarmcontrol $ cd ~/robotarmcontrol $ svn co http://projects.mattdyson.org/projects/robotarm/armcontrol . $ sudo python testRobotArm.py
Your arm should then start moving! All this test file does is connect to the arm, and then issue instructions to a couple of motors and the light in sequence, to show you what it can do.
So – up next was actually tying the Xbox controller to the robotic arm! With all the above in place, this was extremely simple. All that’s needed is a single Python file that listens for events from the Xbox controller (see blog post for more information on that), and connects them to the relevant motor on the robotic arm. In order to get this fully up and running, including the lego-pi library and my RobotArm library above, you need the following:
$ mkdir ~/robotarm $ cd ~/robotarm $ svn co http://projects.mattdyson.org/projects/robotarm/ . $ git clone https://github.com/zephod/lego-pi.git legopi $ touch legopi/__init__.py $ sudo python driveRobot.py
You should then see some output similar to the following. Press Ctrl+C at any point to quit.
Starting RobotArm Controller Press Ctrl+C at any time to quit Init'ing RobotArm RobotArm now ready!
Try moving the analogue sticks on your Xbox controller – and the robotic arm should move!! Controls can be changed by modifying the getMotor(key) method near the top of driveRobot.py, and are set to the following by default:
- Left Stick X-Axis: Base rotation
- Left Stick Y-Axis: Shoulder joint
- Right Stick X-Axis: Elbow joint
- Right Stick Y-Axis: Wrist joint
- Left Trigger: Open grip
- Right Trigger: Close grip
- Left Shoulder: Toggle light
- Right Shoulder: Flash light
Here’s a video of the arm and controller in action!
Enjoy! Please feel free to submit any patches or suggestions in the comments below, and I’ll do my best to incorporate them.