Happy Bear Software

Robots for Web Developers: Part 1

I started building robots a few months ago. Here's why:

For web developers?

I don't have a background in electrical/mechanical engineering or C, so I've had to bootstrap those skills.

Many of the guides for working with microcontrollers assume you have a good knowledge of electronics but focus in a lot of detail on basic programming syntax and concepts.

For web developers, the programming part is easy, instead we lack a lot of the latent knowledge on electronics and how they work with microcontrollers. This information in this post is based on the new things I've learned while attempting to build robots.

Microcontroller basics

A microcontroller is a little integrated circuit (IC) that you can upload compiled C programs to. It connects to a circuit via hardware pins that each perform various functions. The software you write and upload to it is linked against libraries that expose an API to these pins.

Atmega8
Atmega8 Micrcontroller - Peter Halasz via Wikimedia Commons

You can use microcontrollers to control electrical circuits, connected to components like motors and servos. You can also take input from sensors like microphones, range finders and light detectors. Put those together, and you have the beginnings of a robot.

Some microcontrollers can perform on their own, but the Atmega chips above requires an external clock among other things. Also, without special tooling, there's also no way to program the IC.

For prototyping electronics circuits then, it's useful to work with a development board that helps you to get started quickly. Arduino is an example of a development board that works with the Atmega series of chips.

Arduino have done a great job of making the development experience beginner friendly so unless you feel like doing a lot of learning up front, stick to Arduino.

Arduino diecimila Note that you can replace the microcontroller on the Arduino. If for whatever reason you destroy the chip, you can buy a replacement for a few dollars. You'll need something thin and flat to gently lever it out as it's somewhat tricky to do by hand.

Getting Started

The first thing you should do is buy yourself an Arduino starter kit from any of the main poviders (sparkfun, adafruit or oomlout if you're in Europe) and work through all of the demo circuits. They're tedious but you'll learn about things like pulse width modulation that are more or less required for working with robot parts.

In particular you'll need to control something that:

You can still follow along with this post without getting too stuck into the details of those components, you'll need to be able to wire up basic circuits if you want to build a robot.

Development Environment

The default Arduino development environment is great if you're new to programming but somewhat cumbersome if you're used to a slicker experience in say vim or sublime. Whatever editor you prefer to sling code in, getting arduino sketches built and uploaded from the command line is a prerequisite for building a custom development environment.

Ino is a tool written in Python for just that. It requires that you have the arduino software tools installed and may be a little finicky to set up, but once you've got it working you can initialize, build and upload arduino projects with a few short commands.

Soldering

When you're just getting started it's not worth splashing out on a on an expensive soldering set. Instead a cheap $20 soldering iron should be enough to get you started.

Before you do, it's worth going over a basic guide to soldering. It took me a few tries to get soldering right. It boils down to keeping your soldering tip clean. A nice shiny soldering tip, tinned correctly, attracts solder and is easy to use. It takes about five seconds for the tip to oxidise, so you'll need to work fast or be continually cleaning the tip.

Cleaning the iron on a wet sponge works well enough while you're soldering. If you manage to totally cover the tip in oxidized solder, one quick cheat is to brush the end of the iron with some sandpaper. This will take off any coating that helps prevent oxidisation and probably isn't very good for the iron in the long term, but since they're so cheap I didn't worry myself too much.

On Sourcing Parts and Datasheets

With programming if you're missing a library or a bit of information, there's not much you can't google for.

If you're building robots, not having the right parts means sending orders and waiting days or weeks for them to arrive. This is compounded by the fact that when you're starting out, you're not quite sure what you're going to need or when you're going to need it.

For those reasons, if your budget can stretch to it, it's best to buy a little more than you need when it comes to parts. I've found that having spare parts lying around also serves to inspire ideas for fun mini-projects.

All parts that you buy will make a datasheet available online. This is like a manpage for the part that tells you its operating limits for voltage and current, how to use them and example circuits. When you're building projects, it's good to have a computer on nearby with the relevant datasheets at hand.

To find a datasheet online, googling for ' datasheet' normally finds what you need.

Iteration 1: A moving Robot

After getting to grips with the starter kit experiments, my first robot was an extremely basic obstacle avoider. To avoid obstacles a robot needs to be able to:

Movement: Motors and Gearboxes

To get a robot moving, at the very least you're going to need a DC motor.

Motor In the image above you can see two connectors. If you put an electric current through the connectors in one direction, the motor spins. If you put the current through in the opposite direction (i.e. "reverse the polarity") the motor spins the other way.

In order to transfer torque to a wheel, you're going to need some sort of gearbox attached to the motor. I opted to go with the relatively cheap Tamiya twin gearbox.

Handily the Tamiya gearbox comes with a couple of hobby DC motors. Downsides are that its a little fiddly to assemble, and you may need to solder jumper wire to the connectors in order to hook it up to your circuit.

Tamiya gearbox The gearbox is cheaply available on most robotics parts sites. Alternatively you could go with gearmotors attached to your robot.

In either case, you'll need to buy the wheels to go with the gearbox/gearmotors separately.

To keep things simple, rather than building a four wheeled robot, you can use two motors + wheels along with a single ball caster. For the small-sized robots we're going to be building, the only approriately sized casters I could find were available from pololu.com.

Driving two Motors with an H-Bridge

If we have two motors (L and R) powering the robot, they need to rotate in the following ways to carry out the basic movements we mentioned above:

To control the motors like this from a microcontroller, we need a way of switching the direction of the motors electronically. Based on what we said above about current going through the motors in different direction to change the spin direction, this means we need a way of switching each motors polarity simply.

There's a common pattern in electrical circuits that allows you to apply a voltage accross a motor (or any 'load') in either direction called a H-Bridge.

While you can put together an h-bridge by hand using transistors and wires, I opted for using an integrated circuit. You can pick up a H-Bridge chip for a few dollars.

For detailed instructions on getting a motor working with a H-Bridge chip, I found this guide on the NYU Physical Computing site to be extremely useful.

Note: I went with the IC mostly because it's less work, and I'm not planning on pushing a lot of power through the motors. Putting a H-bridge together by hand would however have been a good exercise, so I'll probably give it a try at some point.

The following code is an example of how you might abstract away the details of controlling each motor. Loads of repetition, but a quick sampling will make the general idea clear. There's probably a way to cleverly rewrite this with C macros.

const int h1Pin     = 2;
const int h2Pin     = 4;
const int h3Pin     = 7;
const int h4Pin     = 8;
const int enablePin1 = 12;
const int enablePin2 = 13;

void writeHigh(int pin) {
  digitalWrite(pin, HIGH);
}

void writeLow(int pin) {
  digitalWrite(pin, LOW);
}

void setup()
{
  // Setup H-Bridge control pins as output pins
  pinMode(h1Pin, OUTPUT);
  pinMode(h2Pin, OUTPUT);
  pinMode(h3Pin, OUTPUT);
  pinMode(h4Pin, OUTPUT);
  pinMode(enablePin1, OUTPUT);
  pinMode(enablePin2, OUTPUT);
  writeHigh(enablePin1);
  writeHigh(enablePin2);
}

void main() {
  //test sketch to make sure everythings working as expected
  forward();
  sleep(1000);
  wheelLeft();
  sleep(1000);
  forward();
  sleep(1000);
  wheelRight();
  reverse();
  sleep(1000);
  stop();
  sleep(5000);
}

// right wheel control
void rightStop() {
  writeLow(h1Pin);
  writeLow(h2Pin);
}

void rightForward() {
  writeHigh(h1Pin);
  writeLow(h2Pin);
}

void rightReverse() {
  writeLow(h1Pin);
  writeHigh(h2Pin);
}

// same again for left
void leftStop() {
  writeLow(h3Pin);
  writeLow(h4Pin);
}

void leftForward() {
  writeHigh(h3Pin);
  writeLow(h4Pin);
}

void leftReverse() {
  writeLow(h3Pin);
  writeHigh(h4Pin);
}

//composite movements
void forward() {
  leftForward();
  rightForward();
}

void stop() {
  leftStop();
  rightStop();
}

void reverse() {
  leftReverse();
  rightReverse();
}

void wheelLeft() {
  leftReverse();
  rightForward();
}

void wheelRight() {
  rightReverse();
  leftForward();
}

Once you've got a gearbox assembled and are controlling the motors with an H-bridge, all you need next is to put it together on a chassis with a ball caster and we have our first moving robot.

Chassis + Assembly

Ferrero rocher box However you assemble the chassis, the basic requirements are as follows:

If you're using the tamiya twin gearbox you have a few options for putting your robot together:

Depending on the sort of chassis you end up using, you may need a ball-caster (like the wheels on office chairs) to go along with the two-wheeled gear motor. As linked earlier, the only one I found of roughly the right size was available at pololu.com.

Wrap Up

So far we've got from scratch to a moving robot. In the follow up to this I'll cover how we might get the robot to measure distance to a nearby object and then bring the code for making it roam around avoiding objects.

Related links