Thursday 25 September 2014

Raspberry Pi Robotic Arm

Build Your Own Robot Arm
I came across this bargain a while ago in a local charity shop. Brand new, unopened and just £2.99! I have seen many examples of this kit being hooked up to the Raspberry Pi, so I thought it would be a good opportunity to get physical with my own RPi.

However, the majority of tutorials are based on the USB model. The one I have comes with a simple switch box control, not USB.

So, time to control the motors directly. All five of them.

I read a few tutorials before getting going and soon realised that connecting the motors willy nilly to the GPIO pins was probably not a good idea. There appear to be a number of ways of connecting the motors, but which was going to be best was a bit of a mystery. I plumped for getting started with a L298N H Bridge driver for no reason other than I had seen this working in another blog post and it seemed to be safe and foolproof.

I bought the L298N driver board from ebay but it didn't come with a datasheet. It looks a bit like this one, so I used this as a basis for connecting it up.

L298N motor driver

Although, it is capable of driving two motors, I started out by just trying out the motor controlling the robotic arm jaws.

I hooked up the +ve and -ve leads from the robotic arm battery pack (containing 4 D cells ~ 6V) to the +12V power and power GND terminals. The +5V power terminal was left unconnected - I believe this is an output. I then connected the yellow and black leads from the jaws motor to the output A terminals.

Next, the raspberry pi was connected. One GND pin was connected to the same GND as the robotic arm battery pack. Two GPIO pins (23 & 24) were connected to the two input pins on the driver board corresponding to 'output A'.

Some simple python code was then written to enable the pins, turn the motor clockwise, anti-clockwise and stop. This verified that all connections were working ok.

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

# set pins as output
GPIO.setup(23,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)

# spin motor one way for 0.2 seconds
GPIO.output(23,True)
GPIO.output(24,False)
time.sleep(0.2)

# stop the motors for 1 second
GPIO.output(23,False)
GPIO.output(24,False)
time.sleep(1)

# spin motor the other way for 0.2 seconds
GPIO.output(23,False)
GPIO.output(24,True)
time.sleep(0.2)

# stop the motors
GPIO.output(23,False)
GPIO.output(24,False)

After this success, the other four motors were connected up. One to the other side of the existing L298N driver, two to a separate L298N driver and the final one to a L293D driver. The L293D driver came as just a chip and is not mounted on a board with other components. It looks like this:

L293D motor driver
It is also an H bridge and is capable of controlling 2 motors. I needed a breadboard to mount it and provide easy access to its pins. It was simple to connect up, guided by datasheets readily available online.

I then wrote a python script which allowed the robotic arm to be controlled via keyboard presses (via pygame). The code is available on github robotarm.py

And here is the robotic arm in action:


Great, so I have replaced the perfectly adequate control box that came with the arm with a wiring monstrosity! But the project has lead to a better understanding of interfacing and the research required has pointed me towards many new resources to learn from.

Questions which I now wish to pursue are:

  1. What alternative interfaces could be used to connected a DC motor?
  2. What is an H Bridge?
  3. What are the main differences between the L298N and L293D?
  4. What other motor drivers are popular among robotic enthusiasts?
  5. What other motors are possible?








Sunday 17 August 2014

Lego WeDo and the Raspberry Pi

After some initial fun with Lego WeDo and Scratch, our enthusiasm waned, mainly due to the necessity to tether any model to our computer. Somehow it didn't seem quite so exciting as Lego Mindstorms where the microcontroller is part of your model giving it the freedom to roam.
Inspired by this Lego / Raspberry Pi model built by another geeky parent I decided to see if we could do something similar. As we already had a WeDo hub, it seemed that would be simpler than getting involved with all that fiddly looking GPIO stuff.

First, the 'car'.

Lego WeDo + Raspberry Pi

Pretty neat eh? The WeDo hub is sitting under the RPi. The black box at the back is not functional; it houses a ridiculously long USB cable which is the only one I have with a slim enough micro USB connector to fit in the PiBow case (my only gripe with this case - others have the same problem). The ribbon cable is not in use.

It was inspired by:

Lego 9719 Robotics Invention System

Lego's early robotic system which housed the processor (RCX) in a box of a similar size to my cased Raspberry Pi. There are lots of archived building instructions for this forerunner to the latest Mindstorms products and we got quite excited about the prospect of adapting more models for the Raspberry Pi.

The Pi itself is housed in a PiBow which is a great case and has the added advantage of a few holes in the bottom which fit perfectly onto Lego bricks. The power source is a Powergen battery pack (as used by 'Geek Dad').

Feeling pleased with myself I knocked together the software. At the time, Scratch only officially supported a single motor, so I decided it was time for my son and me to move onto Python. This was already installed on our Raspberry Pi, but we needed some extra stuff to get it to work with the WeDo hub.
After a few teething problems I managed to install this on our Raspberry Pi. But then we ran into other problems.

First, there are only two USB ports on our RPi. Ideally we needed ports for all of the following:
  • wifi adapter - to login remotely
  • bluetooth adapter - we planned to use the Wii Remote as a controller
  • Lego WeDo hub
I plugged in an unpowered USB hub hoping that would solve the problem. It didn't. Some things didn't work at all, and others were intermittent. Plus, the hub was an added weight for the 'car' to be carrying around.

So, we ditched the Wii Remote idea. 

After booting up the RPi I connected to it remotely over the wifi using Putty from my laptop (I had already noted its IP address from an earlier session). Then I ran the python script remotely. Controlling the car was with a simple character entry interface via Putty. And here we see it in action:




Successfully controlled, but rather slow. The hub can only deliver 5V as opposed to the 9V required by the motors. In addition, even under quite small loads (a ruck in the carpet for instance) the motors sometimes would just stop. The only way to recover was to unplug the Lego WeDo hub, put it back in and restart everything. Not the experience we were hoping for.

I have since tested the Lego WeDo hub on different computers both with Python and Scratch. Whenever two motors are connected it is very easy to stop them functioning by putting them under quite small loads. This has never happened with just one motor attached. I've not seen any reports of this elsewhere, but then not many people are using two motors with it. Perhaps I just have a faulty hub?

Sadly, it is farewell to the simplicity of the Lego WeDo hub for us. Next step is to get to grips with the GPIO pins on the RPi and to get some real power to our motors!