Saturday, April 25, 2009

Robot Building Lessons

Introduction

Building real robots is hard. Robots are complex systems which rely on software, hardware and mechanical systems all working together. If you build everything yourself, all types of complex interactions occur, which you usually can't anticipate. If you buy things off-the-shelf, they may not be compatible or well suited to a robotics application.

My experience is mostly in the construction of autonomous robots. During my three years working at IS Robotics, and before that at the MIT AI Lab, I was involved in the process of building a good number of robots from the ground up. I learned a number of practical lessons related to building of these systems. This document tries to summarize some the lessons I learned the hard way by building systems that failed, or by observing other peoples' problems. As more of them come to me and I find more time, I'll update this document.

To better understand this document you might want to know a little of my background. By trade, education, and practical experience I am a Computer Scientist - I have programmed many systems from the assembly language kernel level to the highest level. By practical experience I am pretty strong digital hardware hacker - I have designed and layed out a good number of PC boards. My mechanical experience is a little below average - I am no mechanical engineer, I wouldn't dream of doing the calculations to design a mechanical system. So my recommendations mostly concern the software and electronics of robotic systems.

Electronics

Power Distribution With Motors

Power distribution is extremely important. One thing that people don't seem to understand is that all wires in a system and traces on PC boards are low ohmage resistors. This doesn't matter much when electronics are running. However, when a motor starts up and is running, it can draw a large amount of current. This large current drop will cause a voltage drop across a wire or PC board trace. So when does this cause problems? It is a problem when a wire or trace is connected to a power source like a battery or a power supply and then that wire is split and the power goes off to the electronics and then the motor. The common section of wire will act as the small resistor and when the motor turns on and draws a large surge current, the electronics will see a large current drop. Remember this applies to both to ground and +V connections. So how can this be avoided? Simple, make the shared wire as short and as thick as possible. The best solution is something called star power distribution. This simply means that separate parts of the system all connect directly to the battery (or other power supply) and all these wires form a "star" shape at the battery terminal. In practice, I have found that two sets of connections to the power supply are typically sufficient, one for the electronics and one for he motors. Another tip here is bypass capacitors. They should be as close as possible to the device they are trying to bypass. Remember that here too the PC board traces are acting as resistors and this will have a large effect at high currents. If the traces going to your caps are smaller than the ones going to the power supply or the bypass capacitor of a near by chip, more current will flow from that source and the device's neighbors will see a voltage drop. Power supply / bypass caps can and should also be used near the motor driver chips. Remember the less distance that current has to flow, the less RF there will be in the system and the less likely other components in the system are likely to see voltage drops. Also remember that not all capacitors are created equal. An important parameter of a capacitor for bypass applications is ESR or equivalent series resistance. If the internal resistance of a capacitor is high, then more current will flow from nearby traces than from the capacitor to your device. Typically ceramic and tantalum capacitors have good low ESR and are used in bypass applications. In motor power supply applications special electrolytic capacitors with low ESR are available.

Power Distribution Over Long Distances

In certain situations it may be necessary to have a long cable running from the power supply to the robot. The resistance and inductance of the long cable will introduce a voltage drop which may adversely affect the system. One way to handle this problem is to use a very thick cable. The thicker the cable, the less problems there will be. The same effect as thicker cable can be achieved by paralleling many thinner cables. But this is not always practical. Another solution is a power supply with remote sensing. In this case the power supply has additional connections so that the voltage regulator can sense the voltage at the load instead of the source and compensate for the voltage drop caused by the cable. Not all power supplies offer this option, and many which do have this capability do not have a large enough output voltage range to compensate for very large drops. Another disadvantage of this approach is the need to run additional wires. The most common approach is to run a higher voltage on the distribution cable and the regulate it at the destination. For example if five volts was needed at the target system, unregulated 12 or 18 volts could be run on the power distribution cable and is regulated to 5 volts at the target. Sometimes this is done with a simple linear regulator. The preferred way is to use a DC-DC converter or switching regulator because these are much more efficient, in the range of 70-80%. Linear regulators just convert the extra volts to heat. Of course you need to make sure that the voltage used is high enough so that the voltage drop caused by the cable will not drop the supply voltage lower than minimum voltage needed for the regulator to operate. Why not just use 110 volts? Because it is dangerous! The voltage should be just high enough, but not so high as to be dangerous. Interestingly, people also use this power distribution strategy inside of systems to avoid problems caused by voltage drops caused by internal power busses.

Electrical System Noise Issues

One of the big problems in electronics for robot is noise. Typically a robotic system will have digital electronics, sensitive analog sensor processing electronics and power electronics to drive the motors. The digital electronics can be interfered with with noise and power surges from the motor electronics. Analog signals can be corrupted with noise from either the digital or power electronics. The standard solution is isolation. One configuration is to have a single unregulated power source, and have separate regulation systems for the motor supply, the digital supply, and the analog supply. These systems will have a common ground only at the power source. Depending on the power source and the motors, sometimes the power supply to the motors will be unregulated. Having a common ground is nice because control signals between different parts of the system can be sent just like any other signal. However, noise can also come travel back up the control lines and corrupt otherwise clean power. The typical solution is isolation. An optoisolator is usually used in this situation. It allows a signal to be transmitted between two parts of the system even without a ground connection. Optoisolators offer great isolation but have the disadvantage of taking up extra space and some can be expensive. Another disadvantage is that many optoisolators respond slowly and therefore may not be appropriate when used to transmit a PWM signal - optoisolators designed specifically for carrying digital signals are faster. But, some of these digital out optoisolators draw a lot of current. Remember optoisolators can also be used for transmitting analog as well as digital signals, some optoisolators are designed specifically with feedback paths for handling analog signals. Be careful when choosing an optoisolator.

A more extreme form of isolation is completely isolated power sources. This can be done with separate batteries for different parts of the system. Another solution is an isolated DC-DC converter. And another solution I have used is really beefy inductors in series with the noisy supply to form a cleaner one. When isolated supplies are used with no common ground, optoisolators and the like are the only way to get signals from systems powered with one supply to systems powered by the other. To avoid the use of optoisolators, a common ground can be added between separate supplies. Often, instead of directly connecting the grounds, a small resistor is used which helps reduce noise which may travel through the ground.

Yet another issue is the type of regulator used for different power supplies. Switching regulators are much more efficient than linear regulators, which is important for battery powered systems. However, switching regulators are much more noisy than linear regulators. So linear regulators are usually preferred if a separate supply is used for the analog electronics.

Another, related issue is transmitting small signals. Small analog signals can often pick up noise if the wire carrying them is near some power wire. Standard solutions include shielded cable, twisted pair, and differential transmission. Another simple one is to just not run the signal wire near a power wire. But the problems can be more insidious. Noise can travel through the metal robot body. Traces on a PC board can also pick up noise from near by traces. In one system I worked on the motor power traces were on the opposite side of the PC board from a signal trace, and noise was induced. The same tactics that apply to wires in the robot, apply to traces on a PC board. Another solution is amplifying or digitizing a small signal near its source. An amplified signal will be less affected by noise, because typically the noise amplitude is constant. So, a larger amplitude signal will be less affected by noise. And digitizing at the source will eliminate any noise issues.

The Connector Problem

This is Anita Flynn's favorite issue. She always said that robotics was a connector problem. And she was right. Connectors can be a real pain in the ass. Actually the problem has to do with the connectors themselves and the associated cables. In a robot there is a need to bring signals from one part of the robot to the other. Each wire or cable which does this has the potential of failing in every way that Murphy imagined possible. A large number of wires can also increase the complexity of assembling the robot. Also large numbers of wires are hard to route neatly, meaning that signal wires may end up near power carrying wires and noise can become a problem. The main solution is to reduce the number of wires in the system and reduce the amount of hand wiring. What this is means is, if possible perform local processing of data. In many systems I worked on local microprocessors gathered data and a daisy chained serial bus in the robot carried the information back to the main processor. This can result in an enormous reduction in the amount of cables. Another solution is put things near where they need to be. If a particular actuator needs particular sensor data, place the two physically near each other. To reduce the amount of hand wiring, PC boards can be used as wiring harnesses. Instead of hand wiring 20 nearby sensors to a cable, a PC board can be used. In general it will have greater reliability and take less time to assemble.

The connectors themselves are the other part of the story. Connectors are used so a system can be disassembled and repaired. Don't fool yourself - every piece of your system is prone to failure. It is much easier to remove a device by unplugging a connector rather than unsoldering it. So you might be tempted to add more connectors. But the problem with connectors is that they can take up a lot of space and add a failure point. And every possible failure can and will occur. The most common is a faulty or flaky connection between the cable and the connector - try to track that down in a system with hundreds of connections. (One way is to swap cables around if that is possible in your system.) The other most common problem is cables coming loose. Your robot works for a while and then it stops. Why? Because the connectors have shaken loose. The solution here is locking connectors. They may take up more space and be more expensive - but they are a god send. The other major problem is that some connectors can be plugged in backwards. This can be a disaster - high voltage can be fed into low voltage systems and those systems can be damaged. One solution is design the layout of signals in connectors so that this possibility is taken into account. A symmetrical layout for high voltage power or signals can solve it. But this is often difficult in practice and a better solution is a connector which is polarized - which can only be plugged in one way. Again they may be physically bigger or more expensive, but it is worth the money.

Nice Things to Have

I highly recommend adding an ability for the system to check its battery voltage. If you have an A/D port open you can use that. It saves having to pull out a voltmeter every time.

Software

Making PID Control Work

One would think that getting a PID controller to work is simple. You just copy the formula out of a book and your done. I found this not to be the case. Getting a PID controller to really work well is very hard. Tuning the parameters just right is difficult. I had a lot of problems with a controller I was writing. A magic fix which I got from Pavlo Rudakevych was encoder filtering. In this system the entire PID loop was digital and under controlled by a microcontroller. I had a lot of problems with instability. A fix was to low pass filter the encoder counts in software before using them in the PID loop. The problem was that the controller was trying to compensate for gear jitter and backlash, which it can't do. Filtering the encoder counts, removed this noise signal so it would not be amplified by the PID loop. The resulting system was stable. If your careful, you can maintain the low order bits during filtering and increase the apparent resolution (to the control loop) of the encoder.

Another nice addition to the standard PID loop is a cross term. I found it useful to add an additional term to the PID loops of two wheeled robots which was proportional to the error between the two wheels. For example, if a robot was supposed to be driving straight and one wheel was not keeping up, this term would help compensate.

Diagnostics

One of the most useful pieces of software you can write for a system is diagnostics. It is amazingly useful to have some code to run, which you know works, and can be used to test the hardware. This often helps save much debugging time to decide whether the software or the hardware is at fault. It is so useful that I would recommend burning a copy into ROM so it is always available.

Mechanics

Problems With Treads

Treads are trickier than they may seem. I have seen more success with four wheeled systems than treaded systems. For treaded systems to work the treads must have a good internal suspension system. Also, treaded vehicles without good suspension can tip over backwards when climbing over relatively short obstacles.

Motors

Don't skimp on the motors you use. I have seen too many robotics projects fail because the motors were too junky and didn't work, or generated too much electrical noise, or were too weak. Better motors are more expensive, but well worth it. Also, choose motors well suited for the task at hand - Does the robot need to move fast or slow and what kind of payload (how heavy) does it need to carry?

A Last Remark

Building a robot requires expertise in many fields. Simple robots can be built by a single person. But to truly build a robust robotic system, it is good to have an expert in each field on the team and good communication between them to ensure compatibility and tight integration between the various pieces.