WORKING WITH
DC MOTORS
DC motors are the mainstay of robotics. A surprisingly small motor, when connected to
wheels through a gear reduction system, can power a 25-, 50-, even 100-lb robot with
ease. A flick of a switch, a click of a relay, or a tick of a transistor, and the motor stops in
its tracks and turns the other way. A simple electronic circuit enables you to gain quick and
easy control over speed—from a slow crawl to a fast sprint.
This chapter shows you how to apply open-loop continuous DC motors (as opposed to
servo DC motors, which use position feedback) to drive your robots. The emphasis is on
using motors to propel a robot across your living room floor, but you can use the same control
techniques for any motor application, including gripper closure, elbow flexion, and sensor
positioning.
20.1 The Fundamentals of DC Motors
There are many ways to build a DC motor. By their nature, all DC motors are powered by
direct current—hence the name DC—rather than the alternating current (AC) used by most
motorized household appliances. By and large, AC motors are less expensive to manufacture
than DC motors, and because their construction is simpler they tend to last longer than
DC motors.
The most common form of DC motor is the permanent magnet type, so-called because
it uses two or more permanent magnet pole pieces (called the stator). The turning shaft of the motor, or the rotor, is composed of windings that are connected to a mechanical commutator.
Internally, metal brushes (which can wear out!) supply the contact point for the current
that turns the motor. Fig. 20-1 provides a side view of the motor while Fig. 20-2 shows
the different parts of a motor that has been taken apart.
There are normally three sets of windings to a DC motor as illustrated in Fig. 20-3. The
brushes can only come into contact with two of the windings’ commutators, resulting in two
rotors being magnetized at any given time. These magnetized rotors create an unbalanced
force in the DC motor, causing them to be pulled toward their respective permanent magnet
poles, and torque on the motor shaft. The placement of the commutators and the
brushes causes the motor windings to change polarity as the rotor turns (again shown in
Fig. 20-3).
Other types of DC motors exist as well, including the series wound (or universal) and the
shunt wound DC motors. These differ from the permanent magnet motor in that no magnets are used. Instead, the stator is composed of windings that, when supplied with current,
become electromagnets.
One of the prime benefits of most, but not all, DC motors is that they are inherently
reversible. Apply current in one direction (the “and” on the battery terminals, for example),
and the motor may spin clockwise. Apply current in the other direction, and the motor
spins counterclockwise. This capability makes DC motors well suited for robotics, where it
is often desirable to have the motors reverse direction, such as to back a robot away from
an obstacle or to raise or lower a mechanical arm.
Not all DC motors are reversible, and those that are typically exhibit better performance
(though often just slightly better) in one direction over the other. For example, the motor
may turn a few revolutions per minute faster in one direction. Normally, this is not observable
in the typical motor application, but robotics isn’t typical. In a robot with the common
two-motor drive (see Chapter 18, “Principles of Robot Locomotion”), the motors will be
facing opposite directions, so one will turn clockwise while the other turns counterclockwise.
If one motor is slightly faster than the other, it can cause the robot to steer off course.
Fortunately, this effect isn’t usually seen when the robot just travels short distances, and in
any case, it can often be corrected by the control circuitry used in the robot.
20.2 Reviewing DC Motor Ratings
Motor ratings, such as voltage and current, were introduced in Chapter 19, “Choosing the
Right Motor for the Job.” Here are some things to keep in mind when considering a DC
motor for your robot:
-
DC motors can often be effectively operated at voltages above and below their specified
rating. If the motor is rated for 12 V, and you run it at 6 V, the odds are the motor will
still turn but at reduced speed and torque. Conversely, if the motor is run at 18 to 24 V,
the motor will turn faster and will have increased torque. This does not mean that you
should intentionally under- or overdrive the motors you use. Significantly overdriving a
motor may cause it to wear out much faster than normal. However, it’s usually fairly safe
to run a 10-V motor at 12 V or a 6-V motor at 5 V.
- DC motors draw the most current when they are stalled. Stalling occurs if the motor is
supplied current, but the shaft does not rotate. If there is no stall detection circuitry built
into the motor driver, the battery, control electronics, and drive circuitry you use with the
motor must be able to deliver the current at stall, or they (along with the motor) could
burn out.
- DC motors vary greatly in efficiency. Many of the least expensive motors you may find
are meant to be used in applications (such as automotive) where brute strength, rather
than the conservation of electricity, is the most important factor. Since the typical mobile
robot is powered by a battery, strive for the most efficient motors you can get. It’s best
to stay away from automotive starter, windshield wiper, power window, and power seat
motors since these are notoriously inefficient.
- The rotational speed of a DC motor is usually too fast to be directly applied in a robot.
Gear reduction of some type is necessary to slow down the speed of the motor shaft.
Gearing down the output speed has the positive side effect of increasing torque.
As noted earlier, it’s fairly easy to change the rotational direction of a DC motor. Simply
switch the power lead connections to the battery, and the motor turns in reverse. The small
robots discussed in earlier chapters performed this feat by using a double-pole, double-throw
(DPDT) switch. Two such switches were used, one for each of the drive motors. The
wiring diagram for these robot motors is duplicated in Fig. 20-4 for your convenience. The
DPDT switches used here have a center-off position. When they are in the center position,
the motors receive no power so the robot does not move.
You can use the direction control switch for experimenting, but you’ll soon want to graduate
to more automatic control of your robot. There are a number of ways to accomplish
the electronic or electrically assisted direction control of motors. All have their advantages
and disadvantages.
Perhaps the most straightforward approach to the automatic control of DC motors is to use
relays. It may seem rather daft to install something as old-fashioned and cumbersome as
relays in a high-tech robot, but it is still a useful technique. You’ll find that while relays may
wear out in time (after a few hundred thousand switchings), they are fairly inexpensive and
easy to use.
You can accomplish basic on/off motor control with a single-pole relay. Rig up the relay so that current is broken when the relay is not activated. Turn on the relay, and the switch
closes, thus completing the electrical circuit. The motor turns.
How you activate the relay is something you’ll want to consider carefully. You could control
it with a push-button switch, but that’s no better than the manual switch method just
described. Relays can easily be driven by digital signals. Fig. 20-5 shows the complete driver
circuit for a relay-controller motor. Logical 0 (LOW) turns the relay off; logical 1 (HIGH)
turns it on (refer to the parts list in Table 20-1). The relay can be operated from any digital
gate, including a computer or microprocessor port.
TABLE 20-1 Parts List for On/Off Relay Control |
RL1 |
SPDT relay, 5 V coil, contacts rated 2 A or more |
Q1 |
2N2222 NPN transistor |
R1 |
1K resistor |
D1 |
1N4003 diode |
|
Controlling the direction of the motor is only a little more difficult. This requires a double-pole,
double-throw (DPDT) relay, wired in series after the on/off relay just described (see
Fig. 20-6; refer to the parts list in Table 20-2). With the contacts in the relay in one position,
the motor turns clockwise. Activate the relay, and the contacts change positions, turning
the motor counterclockwise. Again, you can easily control the direction relay with digital
signals. Logical 0 makes the motor turn in one direction (let’s say forward), and logical 1
makes the motor turn in the other direction. Both on/off and direction relay controls are
shown combined in Fig. 20-7.
TABLE 20-2 Parts List for Direction Relay Control |
RL1 |
DPDT relay, 5V coil, contacts rated 2 A or more |
Q1 |
2N2222 NPN transistor |
R1 |
1K resistor |
D1 |
1N4003 diode |
|
You can quickly see how to control the operation and direction of a motor using just two
data bits from a computer. Since most robot designs incorporate two drive motors, you can
control the movement and direction of your robot with just four data bits. When selecting
relays, make sure the contacts are rated for the motors you are using. All relays carry contact
ratings, and they vary from a low of about 0.5 A to over 10 A, at 125 V. Higher-capacity
relays are larger and may require bigger transistors to trigger them (the very small
reed relays can often be triggered by digital control without adding the transistor). For most
applications, you don’t need a relay rated higher than 2 or 3 A.
20.3.2 BIPOLAR TRANSISTOR CONTROL
Bipolar transistors provide true solid-state control of motors. For the purpose of motor control,
you use the bipolar transistor as a simple switch. By the way, note that when a transistor
is referred to in this section it is a bipolar transistor. There are many kinds of
transistors you can use, including the field effect transistor, or FET. In fact, FETs will be discussed
in the next section.
There are two common ways to implement the transistor control of motors. One way is
shown in Fig. 20-8 (see the parts list in Table 20-3). Here, two transistors do all the work.
The motor is connected so that when one transistor is switched on, the shaft turns clockwise.
When the other transistor is turned on, the shaft turns counterclockwise. When both
transistors are off, the motor stops turning. Notice that this setup requires a dual-polarity power supply. The schematic calls for a 6-V motor and a 6-V and 6-V power source. This
is known as a split power supply.
TABLE 20-3 Parts List for Two-Transistor Motor Direction Control |
Q1 |
TIP31 NPN power transistor |
Q2 |
TIP32 PNP power transistor |
R1, R2 |
1–3K resistor |
D1, D2 |
1N4002 diode |
Misc. |
Heat sinks for transistors |
|
Perhaps the most common way to control DC motors is to use the H-bridge network,
as shown in Fig. 20-9 (see the parts list in Table 20-4). The figure shows a simplified H-bridge;
some designs get quite complicated. However, this one will do for most hobby
robot applications. The H-bridge is wired in such a way that only two transistors are on at
a time. When transistors 1 and 4 are on, the motor turns in one direction. When transistors
2 and 3 are on, the motor spins the other way. When all transistors are off, the motor
remains still.
TABLE 20-4 Parts List for H-Bridge Bipolar Transistor Motor Direction Control |
Q1–Q4 |
TIP41 NPN power transistor |
R1–R4 |
1–3K resistor |
D1–D4 |
1N4002 diode |
Misc. |
Heat sinks for transistors |
|
Note that the resistor is used to bias the base of each transistor. These are necessary
to prevent the transistor from pulling excessive current from the gate controlling it (computer
port, logic gate, whatever). Without the resistor, the gate would overheat and be destroyed. The actual value of the bias resistor depends on the voltage and current draw
of the motor, as well as the characteristics of the particular transistors used. For ballpark
computations, the resistor is usually in the 1K- to 3K-Ω range. You can calculate the exact
value of the resistor using Ohm’s law, taking into account the gain and current output of
the transistor, or you can experiment until you find a resistor value that works. Start high
and work down, noting when the controlling electronics seem to get too hot. Don’t go
below 1K.
The transistors you choose should comply with some general guidelines. First, they must
be capable of handling the current draw demanded by the motors, but which specific transistor
you finally choose will largely depend on your application and your design preference.
Most large drive motors draw about 1 to 3 A continuously, so the transistors you choose
should be able to handle this. This immediately rules out the small signal transistors, which
are rated for no more than a few hundred milliamps.
A good NPN transistor for medium-duty applications is the TIP31, which comes in a
TO-220 style case. Its PNP counterpart is the TIP32. Both of these transistors are universally
available. Use them with suitable heat sinks. For high-power applications, the NPN
transistor that’s almost universally used is the 2N3055 (get the version in the TO-3 case;
it handles more power). Its close PNP counterpart is the MJ2955 (or 2N2955). Both transistors
can handle up to 10 A (115 W) when used with a heat sink, such as the one in
Fig. 20-10.
Another popular transistor to use in H-bridges is the TIP120, which is known as a Darlington
transistor. Internally, it’s actually two transistors: a smaller “booster” transistor and
a larger power transistor. The TIP120 is preferred because it’s often easier to interface it
with control electronics. Some transistors, like the 2N3055, require a hefty amount of current
in order to switch, and not all computer ports can supply this current. If you’re not
using a Darlington like the TIP120, it’s sometimes necessary to use small-signal transistors
(the 2N2222 is common) between the computer port and the power transistor.
The driving transistors should be located off the main circuit board—ideally directly on a
large heat sink or at least on a heavy board with clip-on or bolt-on heat sinks attached to
the transistors (as in Fig. 20-10). Use the proper mounting hardware when attaching transistors
to heat sinks.
Remember that with most power transistors, the case is the collector terminal. This is
particularly important when there is more than one transistor on a common heat sink and
they aren’t supposed to have their collectors connected together. It’s also important when that heat sink is connected to the grounded metal frame of the robot. You can avoid any
extra hassle by using the insulating washer provided in most transistor mounting kits.
The power leads from the battery and to the motor should be 12- to 16-gauge wire. Use
solder lugs or crimp-on connectors to attach the wire to the terminals of T0-3-style transistors.
Don’t tap off power from the electronics for the driver transistors; get it directly from
the battery or main power distribution rail. See Chapter 17, “All about Batteries and Robot
Power Supplies,” for more detail about robot power distribution systems.
20.3.3 POWER MOSFET CONTROL
Wouldn’t it be nice if you could use a transistor without bothering with current limiting resistors?
Well, you can as long as you use a special brand of transistor, the power MOSFET.
The MOSFET part stands for “metal oxide semiconductor field effect transistor.” The
“power” part means you can use it for motor control without worrying about it or the controlling
circuitry going up in smoke.
Physically, MOSFETs look a lot like bipolar transistors, but there are a few important differences.
First, like CMOS ICs, it is entirely possible to damage a MOSFET device by zapping
it with static electricity. When handling it, always keep the protective foam around the
terminals. Further, the names of the terminals are different from transistors. Instead of base, emitter, and collector, MOSFETs have a gate, source, and drain. You can easily damage a
MOSFET by connecting it in the circuit improperly. Always refer to the pinout diagram
before wiring the circuit, and double-check your work.
When MOSFETs are on, the resistance between the drain and source (usually referred to
as “RDS” in data sheets) is given as a parameter. The lower the drain and source resistance,
the higher performing the transistor. This resistance represents power loss and the lower
the RDS the lower the power loss in the MOSFETS and the smaller a heat sink that can be
used with them.
A commonly available power MOSFET is the IRF-5XX series (such as the IRF-520, IRF-
530, etc.) from International Rectifier, one of the world’s leading manufacturers of power
MOSFET components. These N-channel MOSFETs come in a T0-220-style transistor case
and can control several amps of current (when on a suitable heat sink). A current limiting
circuit that uses MOSFETs is shown in Fig. 20-11 (see the parts list in Table 20-5). Note the
similarity between this design and the transistor design on Fig. 20-9.
TABLE 20-5 Parts List for N-Channel Power MOSFET Motor Control Bridge |
IC1 |
4011 CMOS Quad NAND Gate IC |
Q1–Q4 |
IRF-5XX series (e.g., IRF-530 or equiv.) N-channel power MOSFET |
D1–D4 |
1N4002 diode |
Misc. |
Heat sinks for transistors |
|
An even better H-bridge with power MOSFETs uses two N-channel MOSFETs for the
low side of the bridge and two complementary P-channel MOSFETs for the high side. The
use of complementary MOSFETs allows all four transistors in the H-bridge to turn completely
on, thereby supplying the motor with full voltage. Fig. 20-12
shows a revised
schematic (refer to the parts list in Table 20-6).
TABLE 20-6 Parts List for N-Channel Power MOSFET Motor Control Bridge |
IC1 |
4011 CMOS Quad NAND Gate IC |
Q1, Q3 |
IRF-9530 (or equiv.) P-channel power MOSFET |
Q2, Q4 |
IRF-5XX series (e.g., IRF-530 or equiv.) N-channel power MOSFET |
D1–D4 |
1N4002 diode |
Misc. |
Heat sinks for transistors |
|
In both circuits, logic gates provide positive-action control. When the control signal is
LOW, the motor turns clockwise. When the control signal is HIGH, the motor turns counterclockwise.
20.3.4 MOTOR BRIDGE CONTROL
The control of motors is big business, and it shouldn’t come as a surprise that dozens of
companies offer all-in-one solutions for controlling motors through fully electronic means.
These products range from inexpensive $2 integrated circuits to sophisticated modules
costing tens of thousands of dollars. Of course, the discussion will be confined to the low
end of this scale!
The basic motor control is an H-bridge, as discussed earlier—an all-in-one integrated circuit
package. Bridges for high-current motors tend to be physically large, and they may
come with heat fins or have connections to a heat sink. A good example of a motor bridge
is the Allegro Microsystems 3952, which provides in one single package a much improved
version of the circuit shown in Fig. 20-13.
Motor control bridges have two or more pins on them for connection to control electronics.
Typical functions for the pins are:
-
Motor enable. When enabled, the motor turns on. When disabled, the motor turns off.
Some bridges let the motor float when disabled; that is, the motor coasts to a stop. On
other bridges, disabling the motor causes a full or partial short across the motor terminals,
which acts as a brake to stop the motor very quickly.
- Direction. Setting the direction pin changes the direction of the motor.
- Brake. On bridges that allow the motor to float when the enable pin is disengaged, a
separate brake input is used to specifically control the braking action of the motor.
- PWM. Most H-bridge motor control ICs are used not only to control the direction and
power of the motor, but its speed as well. The typical means used to vary the speed of a
motor is with pulse width modulation, or PWM. This topic is described more fully in the
next section.
The better motor control bridges incorporate overcurrent protection circuitry, which prevents
them from being damaged if the motor pulls too much current and overheats the chip.
Some even provide for current sense, an output that can be fed back to the control electronics
in order to monitor the amount of current being drawn from the motor. This can be
useful when you need to determine if the robot has become stuck and the motors have
stalled. Recall from earlier in this chapter that DC motors will draw the most current when
they are stalled. If the robot gets caught on something and can’t budge, the motors will stop,
and the current draw will increase.
Some of the available motor control bridges include the L293D and L298N from
SGSThomson; the 754410, an improved version of the L293 from Texas Instruments; and
the LM18293 from National Semiconductor. Be sure to also check the listings in Appendix
B, “Sources,” and Appendix C, “Robot Information on the Internet,” for additional sources
of information on motor control bridges.
20.3.5 RELAY VERSUS BIPOLAR VERSUS FET MOTOR DRIVERS
When making the decision on what type of motor driver to use in a robot, you should consider
the advantages of the different types that have been described so far. Some of the
comments may be surprising and will hopefully give you some things to think about when
you are designing your own robot.
Relay motor control is probably the least efficient and least reliable. Relays are quite
costly and large (especially compared to power bipolar and MOSFET transistors) and will
fail after a certain amount of time. To make it easier for you, remember to always socket
your relays and provide easy access in your robot so that they can be replaced easily. Where
relays have an advantage over the transistor motor controllers/drivers is their low motor
power loss and voltage drop. It may seem to be a paradox, but relays are often the most
effective method of control in small robots that run from radio battery power supplies.
Bipolar transistors are probably the cheapest solution but they can be very inefficient in
terms of power usage. Remember that there is a definite voltage drop through the transistor,
which translates to a power loss (which is the product of the current flowing through the
motor and the voltage drop), which can be very significant in low-voltage situations.
MOSFET transistors tend to be the most efficient type of motor driver in terms of power
loss, but they can be fairly expensive depending on the transistors that are chosen for the
application. They must always be handled carefully to ensure they are not damaged by ESD.
Despite the limitations, the high efficiency of MOSFETs makes them the preferred motor
driver for DC motors.
There will be plenty of times when you’ll want the motors in your robot to go a little slower,
or perhaps track at a predefined speed. Speed control with continuous DC motors is a science
in its own, and there are literally dozens of ways to do it. Some of the more popular
methods will be covered in this and later chapters.
20.4.1 NOT THE WAY TO DO IT
Before exploring the right ways to control the speed of motors, it is important to understand
how not to do it. Many robot experimenters first attempt to vary the speed of a motor by
using a potentiometer. While this scheme certainly works, it wastes a lot of energy. Turning
up the resistance of the pot decreases the speed of the motor, but it also causes excess current
to flow through the pot. That current creates heat and draws off precious battery
power.
Another similar approach is shown in Fig. 20-14. Here, the potentiometer controls the
base current to a bipolar transistor controlling the operation of the motor. Like the previous
case, excess current flows through the transistor, and the energy is dissipated as lost heat.
There are, fortunately, far better ways of doing it. Read on.
20.4.2 BASIC PWM SPEED CONTROL
The best way to control the speed of a DC motor is to chop the power being passed to it
into different sized chunks. The most common way of doing this is to set up a recurring signal;
the time for power to be passed to the motor should be a set fraction of this period.
This method is known as pulse width modulation or PWM motor control and its basics can
be seen in Fig. 20-15. The time the power signal is active is known as the pulse width, and
the duty cycle is the percentage of the pulse width as part of the PWM signal’s period.
One issue that you should always be aware of is that when a motor’s speed is controlled
using a PWM, it will vibrate at the speed of the PWM (due to the coils being turned on and
off according to the speed control), which could result in an annoying hum. To avoid this,
you should run your PWM either at subaudible (60 Hz or less) or superaudible (18 kHz or
more) frequencies. Subaudible frequencies are best for small motors that are usually found
in toys, while superaudible frequencies should be used for high-performance or efficiency
motors (basically anything that wouldn’t be considered in a toy).
It is interesting to note that the actual power delivered to the motors follows a square law
with respect to the duty cycle instead of the expected linear relationship. The reason for
power to drop off as the square of the duty cycle can be seen from going back to the DC
electricity power equation:
Power = V × i = V2/R
By halving the time the voltage is applied (a 50 percent duty cycle), the actual power
available to the motor is only one-quarter of that available when the duty cycle is 100 percent
(or full on). As noted previously, throttling the PWM is not a linear process. When you
are setting up your robot, you will have to make sure that the actual power-to-duty cycle
relationship is understood as you will discover that if a linear range is used your robot will
probably only be active when the PWM duty cycle is 50 percent or more.
Creating a PWM signal is not as easy as you would expect. In the previous versions of
this book, a PWM generator circuit was built from a relaxation oscillator, not understanding
that while the pulse width would change, so would the period, resulting in a constant PWM
duty cycle for a varying frequency. What is desired is a varying PWM pulse width for a constant
frequency.
The first demonstration circuit (Fig. 20-16 with parts list in Table 20-7) uses a 555 to
allow a potentiometer to brighten or dim an LED.
TABLE 20-7 Parts List for 555 PWM Circuit |
IC1 |
555 timer chip |
LED |
Visible light LED |
R1 |
1k resistor |
R2, R3 |
10k resistors |
R4 |
100k potentiometer |
C1 |
0.1 μF capacitor |
C2 |
0.01 μF capacitor |
Misc. |
Breadboard, breadboard wiring, 9-V battery clip, 9-V battery |
|
You will find that the circuit works reasonably well, although it is impossible to completely
turn off the LED. This is due to the 555’s inability to provide less than a 50 percent
duty cycle signal. In Fig. 20-17, the operating signals are shown for different potentiometer
settings. The left side of the oscilloscope trace shows the LED dimmed, a 17 percent
duty cycle (the time the signal is low, which is when current can flow through the LED) versus
the LED at its brightest, a 49 percent duty cycle.
The oscilloscope pictures in Fig. 20-15 probably do not look like what you would expect
for two reasons. First, the LED was turned on when the 555 output was low. This allowed
the greatest dynamic duty cycle range, which provided the most visible changes in the LED.
Second, the PWM pulse width remained constant while the frequency changed: this is a
function of the operation of the 555 timer and must be taken into account if this chip is
used as a PWM generator.
While the 555-based PWM generator does not look all that acceptable as a PWM
generator, it can be used successfully for motor control. R4’s resistance can be changed
digitally using a selection of resistors that are allowed to pass current using transistors
(as is done in the IBM PC gamepad/joystick interface discussed in Chapter 16). If it is
to be used, you may have to switch between using the high and low outputs of the 555
to control the motor drivers, depending on the application and the characteristics of the
motor.
The BS2 has a PWM function, which can be used to produce a PWM on each of its pins
to control a motor. Fig. 20-18 shows the circuit (Table 20-8 lists the parts) for a BS2 PWM
control of the brightness of an LED. The software used for the application is:
TABLE 20-8 Parts List for Using A BS2 to Vary the Brightness of an LED |
BS2 |
Parallax BASIC Stamp 2 |
LED1 |
Visible light LED |
R1 |
470 ω resistor |
Misc. |
BS2 communications/programming interface, breadboard, breadboard
wiring, 4x AA battery clip, 4x AA alkaline batteries |
|
The BS2 PWM function is a very effective way to control a motor’s speed, and it can also
be used to output an analog voltage using a resistor and capacitor, as is shown in the Parallax
PBASIC reference manuals. The problem with the PWM function is that it does not
run in the background, which is to say that the BS2 cannot do other operations (such as
running more than one motor or polling sensors) while the PWM is active. In the next section,
a circuit that will allow the BS2 to specify the PWM duty cycle for multiple motors as
well as execute other tasks will be presented.
20.4.3 COUNTER-BASED PWM SPEED CONTROL
As mentioned in the previous section, the ideal motor speed control is a PWM in which the
period stays constant and the pulse width (and therefore the duty cycle) is variable. Ideally
the pulse width would be controlled by a digital value rather than an analog potentiometer
or other components, which are difficult to interface digital signals and should not require
constant monitoring by the robot’s controller. The PWM controller described in this section
should meet these criteria and use parts that are readily available
The PWM controller circuit could be blocked out using the diagram in Fig. 20-19. A
clock signal is used to increment a four-bit counter and the output of the counter is continually
compared against an expected value. When the counter value is less than the expected
value, the output from the digital magnitude comparator is a logic high and when the
counter output is equal to or less than the compare value the comparator output is a logic
low. When the counter overflows, the process starts over.
The circuit that implements this function is shown in Fig. 20-20 with the parts listed in
Table 20-9.
TABLE 20-9 Parts List for Four BIT PWM Motor Control Circuit |
IC1 |
555 timer chip |
IC2 |
74LS191 four-bit binary counter chip |
IC3 |
74LS85 four-bit magnitude comparator chip |
LED1 |
Visible light LED |
R1–R2 |
4.7k resistors |
R3 |
470 ω resistor |
C1–C5 |
0.01 μF capacitor |
DIP Switch |
Breadboard mountable four-bit DIP switch |
Misc. |
Breadboard, breadboard wiring, 4x AA battery clip, 4x AA batteries |
|
This circuit can be replicated surprisingly easily for controlling multiple motors. You do
not have to replicate the entire circuit, you just have to add additional 74LS85 chips for
each motor to be controlled. The clock oscillator and counter can be common for multiple
PWM circuits. The controller must dedicate four digital outputs for each motor controller for
the PWM control as well as any specific outputs for motor direction control. Using this circuit
to control two motors, a minimum of 10 lines of the microcontroller must be dedicated
to it.
As with any counter-based PWM, the counter clock frequency must be the maximum
number value of the counter multiplied by the desired PWM frequency. For the circuit
shown in Fig. 20-20, the clock period is 10 kHz; the actual PWM frequency is approximately 667 Hz (10 kHz divided by 15), which is very audible. For a 20 kHz PWM, the PWM
clock would have to be 300 kHz. Similarly for a 30 Hz (subaudible) PWM, the PWM clock
would have to be 450 Hz.
When more bits are used for the PWM, the clock frequency can become very high. Fortunately
for most robot motor applications a four-bit PWM provides adequate resolution.
Finally, this circuit is available in a number of microcontrollers with built-in PWM outputs.
Unfortunately, there are very few microcontrollers on the market with more than one built-in PWM. Most only have one PWM generation circuit and must either rely on an external
circuit, similar to the one demonstrated in Fig. 20-20, or two microcontrollers must be used
in the application (one to control each motor’s PWM).
20.5 Odometry: Measuring Distance of Travel
Odometry is the science of measuring the movement of a mobile robot by keeping track of
the number of revolutions each motor makes and in which direction. The amount of movement
is a simple calculation: the number of revolutions multiplied by the diameter of the
wheel times pi. As will be explained in this chapter, the odometry measurements are normally
carried out using shaft encoders directly connected to the wheels and assume that
they can accurately track the position of the robot by how much the wheels have turned.
20.5.1 ANATOMY OF A SHAFT ENCODER
The typical shaft encoder is a disc that has numerous holes or slots along its outside edge.
An infrared LED is placed on one side of the disc so that its light shines through the holes.
The number of holes or slots is not a consideration here, but for increased speed resolution,
there should be as many holes around the outer edge of the disc as possible. An infrared-sensitive
phototransistor is positioned directly opposite the LED (see Fig. 20-21) so that
when the motor and disc turn, the holes pass the light intermittently. The result, as seen by
the phototransistor, is a series of flashing light.
Instead of mounting the shaft encoders on the motor shafts, mount them on the wheel
shafts (if they are different). The number of slots in the disc determines the maximum accuracy
of the travel circuit. The more slots the disc has, the better the accuracy.
If the encoder disc has 50 slots around its circumference, that represents a minimum
sensing angle of 7.2°. As the wheel rotates, it provides a signal to the counting circuit every
7.2°. Stated another way, if the robot is outfitted with a 7-in wheel (circumference = 21.98
in), the maximum travel resolution is approximately 0.44 linear inches. This figure was calculated
by taking the circumference of the wheel and dividing it by the number of slots in
the shaft encoder.
The outputs of the phototransistor are conditioned by Schmitt triggers. This smooths out
the wave shape of the light pulses so only voltage inputs above or below a specific threshold
are accepted (this helps prevent spurious triggers). The output of the triggers is applied
to the control circuitry of the robot.
20.5.2 THE DISTANCE COUNTER
The pulses from a shaft encoder do not in themselves carry distance measurement. The
pulses must be counted and the count converted to distance. Counting and conversion are
ideal tasks for a computer. Most single-chip computers and microprocessors, or their interface
adapters, are equipped with counters. If your robot lacks a computer or microprocessor with a timer, you can add one using a 4040 12-stage binary ripple counter (see Fig. 20-22). This CMOS chip has 12 binary weighted outputs and can count to 4096. It would
probably be easiest if you just used the first eight outputs to count to 256.
Any counter with a binary or BCD output can be used with a 7485 magnitude comparator.
A pinout of this versatile chip is shown in Fig. 20-23, and a basic hookup diagram
in Fig. 20-24. In operation, the chip will compare the binary weighted number at its A and
B inputs. One of the three LEDs will then light up, depending on the result of the difference
between the two numbers. In a practical circuit, you’d replace the DIP switches (in the dotted
box) with a computer port.
You can cascade comparators to count to just about any number. If counting in BCD,
three packages can be used to count to 999, which should be enough for most distance
recording purposes. Using a disc with 25 slots in it and a 7-in drive wheel, the travel resolution
is 0.84 linear inches. Therefore, the counter system will stop the robot within 0.84
in of the desired distance (allowing for coasting and slip between the wheels and ground) up
to a maximum working range of 69.93 ft. You can increase the distance by building a
counter with more BCD stages or decreasing the number of slots in the encoder disc.
20.5.3 MAKING THE SHAFT ENCODER
By far, the hardest part about odometry is making or adapting the shaft encoders. (You can
also buy shaft encoders ready-made.) The shaft encoder you make may not have the fine
resolution of a commercially made disc, which often has 256 or 360 slots in it, but the
homemade versions will be more than adequate. You may even be able to find already machined parts that closely fit the bill, such as the encoder wheels in a discarded computer
mouse. Fig. 20-25 shows the encoder wheels from a surplus $5 mouse. The mouse contains
two encoders, one for each wheel of the robot.
You can also make your own shaft encoder by taking a 1- to 2-in disc of plastic or metal
and drilling holes in it. Remember that the disc material must be opaque to infrared light.
Some things that may look opaque to you may actually pass infrared light. When in doubt,
add a coat or two of flat black or dark blue paint. That should block stray infrared light from
reaching the phototransistor. Mark the disc for at least 20 holes, with a minimum size of about
in. The more holes the better. Use a compass to scribe an exact circle for drilling.
The infrared light will only pass through holes that are on this scribe line.
20.5.4 MOUNTING THE HARDWARE
Secure the shaft encoder to the shaft of the drive motor or wheel. Using brackets, attach
the LED so that it fits snugly on the back side of the disc. You can bend the lead of the
LED a bit to line it up with the holes. Do the same for the phototransistor. You must mask
the phototransistor so it doesn’t pick up stray light or reflected light from the LED, as
shown in Fig. 20-26. You can increase the effectiveness of the phototransistor placing an
infrared filter (a dark red filter will do in a pinch) between the lens of the phototransistor
and the disc. You can also use the type of phototransistor that has its own built-in infrared
filter.
If you find that the circuit isn’t sensitive enough, check whether stray light is hitting the
phototransistor. Baffle it with a piece of black construction paper if necessary. Or, if you
prefer, you can use a striped disc of alternating white and black spokes as well as a
reflectance IR emitter and detector. Reflectance discs are best used when you can control or
limit the amount of ambient light that falls on the detector.
20.5.5 QUADRATURE ENCODING
So far we’ve investigated shaft encoders that have just one output. This output pulses as the
shaft encoder turns. By using two LEDs and phototransistors, positioned 90° out of phase
(see Fig. 20-27), you can construct a system that not only tells you the amount of travel, but
the direction as well. This can be useful if the wheels of your robot may slip. You can determine
if the wheels are moving when they aren’t supposed to be, and you can determine the direction of travel. This so-called two-channel system uses quadrature encoding—the channels
are out of phase by 90 degrees (one quarter of a circle).
Use the flip-flop circuit in Fig. 20-28 to separate the distance pulses from the direction
pulses. Note that this circuit will only work when you are using quadrature encoding, where
the pulses are in the following format:
off/off
on/off
on/on
off/on (… and repeat.)
To learn more about . . . |
|
Read |
Selecting the right motors for your robot |
|
Chapter 19, “Choosing the Right Motor for the
Job” |
Using stepper motors |
|
Chapter 21, “Working with Stepper Motors” |
Interfacing motors to computers and microcontrollers |
|
Chapter 14, “Computer Peripherals” |
More on odometry and measuring the distance of travel of a robot |
|
|