By Najaf Ali
I started building robots a few months ago. Here's why:
- An excuse to learn more C
- I wanted a real hobby that would have no tangible benefit towards work
- They're friggin robots!
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.
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.
<%= image_with_caption_and_attribution( 'atmega8.png', 'Atmega8 Micrcontroller', 'Peter Halasz via Wikimedia Commons', 'http://commons.wikimedia.org/wiki/File:ATmega8_01_Pengo.jpg' ) %>
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.
<%= image 'arduino-diecimila.jpg' %>
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.
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:
- moves (an actuator), like a motor or servo.
- gets data from the outside world (a sensor), like a thermistor or light dependent resistor (LDR).
- electronically switches current (a switch), like a relay or transistor.
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.
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.
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 '
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:
- Move - Preferably forward, reverse, wheel left/right and at variable speeds.
- Not fall apart - The parts need to be put together so the robot doesn't fall to pieces on colliding with a wall (which it will).
- Detect objects in front of it - And navigate away from them if possible.
Movement: Motors and Gearboxes
To get a robot moving, at the very least you're going to need a DC motor.
<%= image 'motor.gif' %>
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.
<%= image 'tamiya-gearbox.jpg' %>
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:
- Forward - L Forward, R Forward
- Reverse - L Reverse, R Reverse
- Wheel Left - L Reverse, R Forward
- Wheel Right - L Forward, R Reverse
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.
<%= code 'arduino-motor-control.c' %>
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
<%= image 'ferrero-rocher-box.jpg' %>
However you assemble the chassis, the basic requirements are as follows:
- You can attach the gearbox + ball caster to the bottom of it, with wheels.
- That it can hold the breadboard and arduino dev board securely.
If you're using the tamiya twin gearbox you have a few options for putting your robot together:
Building a chassis yourself. This is a viable option. I found that an empty box of ferrero rocher chocolates was just the right size for containing an arduino + breadbord, and attaching the tamiya gearbox to the underside.
In order to punch holes for attaching the gearbox, I had to heat a screwdriver over a flame and push it through the plastic. You might discover a safer and more elegant way of doing this, but it's difficult not to shatter the plastic without heating it first.
Since breadboards come with adhesive backings, I attached one to one of the narrower inner walls of the ferrero rocher box (the 'back' of the robot). I attached the arduino board to the one of the side walls, at right angles to the breadboard with some blu-tac.
Buying a ready-made chassis. In hindsight this would probably have resulted in a nicer robot. You can buy kits that are made to go with Tamiya parts or arduino from most robot part retailers.
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.
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.