I wish I could say I had a half-million-dollar budget to put towards a Stanford-style Grand Challenge vehicle, but I'm afraid I've already earmarked that money for thermoplastic camouflage and bionic calf implants.
Still, it seems like you just can't be a self-respecting hacker these days unless you have an autonomous Humvee or two.
So, I decided it would be a worthwhile project to create a smaller-scale robotic car. After a quick treasure-hunt under the sofa cusions and a trip to Radio Shack, I had everything I needed. This project looked to be as easy as taking toys from a baby.
Here's a video of the robot car in action. Keep reading and I'll show you how you can start making your own.
This is the second part in a howto series about building a relatively cheap robot out of a radio-controlled car and an embedded microcontroller such as the Basic Stamp.
I glossed over how to interface a microcontroller with the drive and steering systems on a typical, cheap RC car. Today, we'll be looking at the circuit in a little more detail. I've also added the simple BS2 program that I used in the demo video from the previous article.
In this article, I'll go over the basic design of the robot RC car. I'm saving some of the nitty-gritty details for a second post. This will give all you readers some time to come up with a better way of doing things, and it will give me time to beg our pal robogeek to assist me with some CAD drawings of the motor control circuit.
You really just need three things to build the base platform for your robot RC car:
- Programmable microcontroller
- Cheap RC car
- Motor control circuit
If you have one of those nice, servo-controlled, proportional speed and steering cars, you can probably skip the motor control circuit and interface directly with the control servos on the car. We're going the cheap route with this howto, though, so we'll be using a very simple RC car and bypassing its circuitry, controlling its motors directly.
I chose to use the Basic Stamp for this project because, well, that's what I had handy. You could just as easily use a PICAXE, and OOPic, or maybe even a small embedded PC with a parallel port.
The design I settled on requires 4 I/O ports to control the motors. In the future you'll need more ports to handle sensor data or perhaps a GPS device. It all depends on how elaborate and complex you intend to make your control system. The number of I/O ports available and program storage space will probably determine what you choose for your robot's noggin.
The car I'm using has 2 motors: a smaller on that controls the steering, and a larger one that drives the rear wheels. They both are run off a 6 volt supply.
Since I stole this car from a baby, I wanted to make this mod as minimally invasive as possible. All we really care about are the 2 motors and the battery, so I ended up just soldering 6 wires: two to each of the motors and two to the battery connectors. The electronics are completely bypassed and it can still function as a normal RC car when the rest of our setup is disconnected.
The Messy Details
Unfortunately, the Basic Stamp can't source enough power to drive the car's motors directly. Instead, I needed to power the motors directly from the car's battery and use the microcontroller to switch the power to the motors.
Initially this had me thinking about using relays. A little current on the microcontroller side of the relay opens and closes the higher power circuit on the motor side of the relay.
One thing I wanted to be able to do, however, was control the speed of the motor. An easy way to do this is using Pulse Width Modulation, turning the I/O pin off and on quickly to limit the power delivered to the motor over time.
The problem is that I don't think the response time on a relay is fast enough to do this reliably. We need to move away from electro-mechanical switches.
I ended up going with a modified h-bridge circuit, similar to Steve Bolt's design. With this transistor based circuit, you can use 2 I/O pins to control the motor.
When you supply enough current to the base of a transistor, it becomes saturated and the resistance on the collector drops to almost nothing. In this way, you can use a relatively small amount of current on the base to enable a lot more current to flow through the collector pin.
I found that the Basic Stamp wasn't able to provide enough juice to saturate the transistors in the h-bridge. No-saturation equals increased resistance, which also equals poor motor performance and hot smoking transistors.
So, where 1 transistor is good, 2 are better. I was able to solve the problem by replacing each transistor in the circuit with a Darlington pair. Basically, each transistor gets a buddy that amplifys the input current to its base pin.
The other alteration I made was to add protection diodes. When the motor is spinning and unpowered, it can generate electricity spikes that can force current the wrong way through the transistors and damage the circuit. The protection diodes allow a path for this electricity to bypass the transistors (and the rest of the circuit).
The H Bridge
The h-bridge is a simple circuit that you can make to control power to a motor from a low power circuit such as the logical IO from a microcontroller.
Here you can see the circuit I used in my project. I/O A and I/O B connect to the I/O pins on the Basic Stamp (BS2). The +6V is the positive terminal on the RC car battery, and at the bottom VSS connects to the BS2 ground and is also grounded to the negative terminal on the battery.
Here's how it works. When A and B are both off, the PNP transistors at the top of the circuit will allow current to flow, but the NPN transistors at the bottom will not. Similarly, when A and B are both on, the situation is reversed with the NPN transistors being on and the PNP transistors not allowing current to flow. In either scenario, there is no path for electricity to get from the battery to ground.
Now if I/O A is on and B is off, the NPN pair at the bottom left and the PNP pair at the top right will both be open. This creates a circuit that allows the current to flow through from the top-right, through the motor, and down the bottom-left, and down to ground. This causes the motor to spin in one direction. Raising pin B and dropping pin A has the opposite effect, causing the motor to spin the other way.
The h-bridge effectively gives us 3 states: off, forward, and backward. On your car, you can use one of these circuits to control the drive motor, and another h-bridge to control the steering motor.
R1, R2, R3 and R4 are all 1K resistors. In my setup, Q5, Q6, Q9 and Q7 are all smaller signal transistors. You can probably get away with whatever you can find at your local electronics store.
The important thing is that Q1, Q2, Q3, and Q4 can pass enough power to meet the demands of your motors. The spec you are looking for is the collector current (Ic). 3A should be quite sufficient for most cheapo RC motors. These will be the transistors with the attached heatsink. I used the TIP31 and TIP42 models that I found at the local Radio Shack.
You'll notice that all the transistors are paired up. These are called Darlington Pairs. It allows a low current source, such as the BS2 IO, to amplify its current with one transistor, and use that output to saturate the higher power transistor which is used to drive our motor.
In this circuit, it is very important that the BS2 ground (VSS) and the battery ground (-6V) are connected. Also, the I/O pins cannot be floating, so you should have the battery disconnected if the BS2 is not running. Otherwise it will only take a few seconds to smoke your transistors.
A Note On Making Schematics
This is the first schematic I've ever made, so please pardon any sloppiness or common newbie errors. If you've never done this before and you're interested in making a schematics, I recommend trying Eagle. I used the freeware Mac version and was pretty happy with it.
If you're lucky in your hunting you may find PNP and NPN Darlington Transistors in a single package. Grab those and swap them out for the pairs above and you'll save yourself some wiring.
Robogeek also informs me that the LMD18200 is a pretty fantastic h-bridge on a single chip. It can provide 3A continuous with a 6A peak and you can avoid all the messy wiring. Wicked! I ordered 5 samples from National and they sent them free of charge and even sent me a thank you letter. How cool is that? I'm looking forward to trying these out and I'll make sure to report back.
At this point, we've got ourselves a fully functional robot platform. You'll need to add sensors, etc. to make this more autonomous, but you should have everything you need to drive a car around under the control of a teeny microcontroller.
Attached is the program I used for my demo. As you can see, there's nothing magic here. There are a few routines to turn the motors on and off, do a 2 point turn in place, etc. The main routine ties a few of these together to make the robot drive around a little bit.
I'm really excited to see other people make these. I'm working on a GPS guided version, but there are a lot of other possibilities as well. This is basically a nice little platform that you can use as a starting point for any mobile robot project.
Make sure to tell us your ideas in the comments. If you're working on an r/c car robot yourself, let me know about it and we can add it to the series.
I wrote a quick program to drive the robot around and it seems to work really well.
I'm looking forward to hearing your comments and suggestions. I'll post a part 2 soon with updates, a circuit diagram, and a starter program that you can run on your own vehicle.