Figure 2. MOSFET and
relay motor driver.
higher voltage than the Vgs rating of the device. Ringing
can occur on any MOSFET that is driving a high Vds (Voltage
across the drain and source of the MOSFET). Even if the
MOSFET has a voltage rating of 60V, the Vgs will typically be
much less than that. The resistor R4 is used to bleed off the
voltage at the gate of the MOSFET which will speed up its
switching time. The PWM frequency here is limited by the
opto-isolator transition speeds. The advantage of this type of
MOSFET/relay motor driver compared to the relay only driver
is that you can PWM the MOSFET and get variable speeds. If
you are driving large currents, however, it is a good idea to
turn the PWM off before switching motor directions to avoid
arcing on your relay contacts.
Q. I’ve heard that PID is hard to implement but that it
makes your motors run better. Is this true?
A. Hmmm, I guess this depends on your definition of
difficult. There have been many articles written about
PID algorithms, and if you have read some of them
you could come away with the idea that PID is horrifically
complex and difficult to implement. Really, this is not
generally true. Sure, some of the very fancy algorithms that
need to run super fast for super accurate motor controls can
be very hairy indeed. But most of that complexity comes
from the need to have high accuracy for special applications.
We can implement PID in our robot motors with a minimum
of complexity because we would be happy with just getting
constant speeds that don’t depend much on battery levels.
Before we continue, some definitions are in order for the
elements of a PID algorithm.
Proportional: We will apply a correction that is
proportional to the difference between the speed we are
going now and the speed we want to be going. In other
words, if we want to be going 500 RPM and we are going
100 RPM, we will have a larger P term than if we wanted to
go 500 RPM and we were already going 400 RPM.
Integral: We will add a correction every time we are not
going the speed that we want to be going. This element of
the PID algorithm is usually the one that is misunderstood.
With this element, we will accumulate an error term every
cycle of the PID where our speed is not where we want it to
be. Sometimes the P error term will not be large enough to
reach our terminal speed. Over time, the I error term will get
larger and force a greater error correction to eventually
occur. This is a very handy error term for making smooth
corrections to large error terms. This term should be kept
small relative to the other PID elements.
Derivative: The P and I elements have been driving us
onward to our terminal speed goal. As we approach our
goal, someone needs to start applying the brakes so that we
don’t overshoot the target. This is what the D term will do.
As the motor speed gets closer to the terminal value that we
want, the D term will start supplying a negative correction to
slow the acceleration down so that we won’t overshoot our
target speed (by much). This term will be larger than the I
term, but still smaller than the P term. This term is arrived
at by subtracting the last error from the current error.
Eventually (we hope), the current error will be smaller than
the last error and this term will get increasingly more
Error: This is the name given to the difference between
the terminal (target) speed and the current speed.
Correction: This is the PWM value that we will give the
motors to tell them to speed up or slow down.
So, here is how a PID loop is calculated and used.
SERVO 11.2008 15