Monday 9 October 2017

Arduino and the Robot

Arduino and the Robot Video (Youtube)

A few years ago, I bought a cheap plastic robot platform to play with. I bolted on two 12V geared motors, a Raspberry Pi with a WiPi WiFi USB dongle and a PiFace Digital,relay board and could drive the platform about. It trailed a 6m power cord. What I needed was a battery-powered system that actually lasted more than 5 minutes.

Unfortunately in 2011 I could not get lithium batteries easily, and so I used lead acid batteries instead. The batteries that were big enough were too heavy for the little platform and they cracked it. The few lithium batteries I had could not last long enough for the Raspberry Pi to power up.

The robot platform languished until I discovered recently power banks and Arduino parts are very cheap and you could buy them online. Enter the Arduino XL6009 5V to 12V converter:

The Arduino XL6009 costs a mere RM4.50
Together with a 20000mAh lithium power bank, which can cost as little as RM70 (I bought mine for RM87) I can now restart the robot platform project.

Rasberry Pi robot platform components (Raspi, Piface, WiPi, XL6009 and power bank) for assembly
Control and extra processing power for is from my Acer laptop. I expect to ssh via WiFi into the Raspi and execute the motor commands using a python script. The Piface relay board do nothing more than connecting 12V to the motors on command. Imaging, GPS and audio will be from an old onboard Android Nexus 1 smartphone, long put aside for a Nexus 5.

 The two motors are Cytron spg30-300k, rated at 12V 430mA (equivalent to 1032mA at 5V) each. Assuming the Raspberry Pi takes 160mA, that is 2224mA. The power bank should last 9 hours at full power. I expect the consumption to be a lot less mainly because it will be moving less mainly for the control program catches up. Anyway I do not expect the flimsy chassis to hold up for 9 hours.

But first, the XL6009 needs to be set correctly. Wire up a USB connector to it. You can cut a USB cable and find the 5V and 0V lines by measurement. Or you can use this:

USB connector pinouts
Or if you have recently discovered Arduino, you could buy an Arduino USB connector breakout PCB for RM2.50:
 Connect the USB connector to the XL6009 inputs and set the potentiometer until you get the right voltage output. My XL6009 was preset to 24V so I had to do a fair number of turns to get it down to 12V.

Setting the XL6009. For convenience I have connected voltmeters to the input and output

The power bank has 2 USB output ports, one at 2A and the other 1A. I connected the 2A output to a Raspberry Pi with a Piface Digital Cape as well as the WiPi WiFi dongle. The assembly took some 400mA at startup and between 100-300mA after.

I first tested the program 'dry', without the motor 12V power. You can tell if the relays are clicking on - besides the sound, there is a corresponding output LED.

Next I connected up the XP6009 to the power bank 1A output. The first time I tried it crashed the WiFi connection to my laptop. It rebooted OK after. Next to reduce the load on the motors, I raised the wheels off the floor so they spun free.

The first time I pulsed the motors on for only 20ms, and that was enough to drag the power bank output down and crash the Raspberry Pi.

Despite lifting the wheels clear, the motor gears must still present a considerable load. I needed to reduce the motor current. 1A seems a nice round number, and if I put a 5R 5W resistor at the 5V input to the XL6009 this should do it. But I only have a 2R7 resistor, and in the interests of expediency I used that, but swapped the motor power to the 2A power bank output.

Raspberry Pi Robot platform, assembled in glorious duct tape
The program is simple. You need the C library supplied with the Piface.

root@piface1:/home/heong/piface/piface-master/c# cat forward.c
#include <libpiface-1.0/pfio.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])
{
  int ontime=300; /* milli-seconds */

  if ( argc != 2 )
  {
    printf("Usage: %s ms\n", argv[0]);
    _exit(1);
  }
  sscanf(argv[1], "%d", &ontime);
  pfio_init();
  pfio_digital_write(0, 1);
  pfio_digital_write(1, 1);
  printf("Sleeping %d milliseconds\n", ontime);
  usleep(ontime*1000);
  pfio_digital_write(0, 0);
  pfio_digital_write(1, 0);
  pfio_deinit();
}

The command to compile is:
 gcc -L/usr/local/lib/ -lpiface-1.0 -o forward forward.c

To run, you ssh into the Raspberry Pi:
ssh -t 172.16.xx.yy

I find a 200ms pulse produces a reasonable step:

./forward 200
Sleeping 200 milliseconds
For the great finale the wheels were lowered to the ground and the and the command was executed as many times as necessary.

A very simple mod would allow you to make right.c and left.c when direction changes are required. And free Android apps are available to trigger the Pi commands.

The mechanical bit leaves much to be desired: the heavy power bank did not sit square over the wheels, so a left turn is much easier than a right turn. It needed two more wheels rather than those noisy screw heads, and it wobbled horribly, shaking lose its connectors after a few minutes.

And yet, it finally managed to lose its power cord tether and it moved fast enough to alarm Sidney the cat.

There you have it, a cheap robot platform prototype 6 years in the making, finished in one weekend.

Apart from the shakey construction, the power bank needs to be down-rated, and the PiFace Digital needs to be swapped out for a proper motor driver. Given that the processing is done from a host via a wireless link, a Raspberry Pi is overkill; a bluetooth PIC18F14K50 should do.

So, back to the drawing board, back to the pachinko.  In the meantime I see a bright future for the first prototype as a back porch prowler, scuttling back to the solar battery to recharge when low.

No comments:

Post a Comment