Computer Controller Design


Computer Controller Design
Controllers are sort of a black art among most engineers, believe it
or not. Many controllers used are simple on-off affairs, or gain +
bias controllers for continuous control. Computer Scientists can
sometimes be even worse, coding strange hacks to regulate variables
rather than trying to control them. Today, we'll be looking at some
general control theory, five controllers, the theory behind them, and
how to implement them in FreeBASIC. Finally, we'll look at a few minor
tricks you can use to make controllers extremely useful for
controlling things in your software.

General Control Theory
A controller takes a variable of what's going on, called a "Process
Variable" or PV, and tries to change another variable, called the
"Manipulated Variable" or M, to drive the Process Variable towards the
desired value, called "Set Point", or SP. The manipulated value M goes
to whatever is controlling the process, and that device is called the
"Control Element".
To illustrate this, let's consider a controller which controls the
amount of time a computer spends sleeping to correctly get the desired
frame rate. The frame rate itself is the Process Variable in this
case. The time the computer spends sleeping is the manipulated
variable.  The desired frame rate is the Set Point. The pause itself
is the control element.
The difference between a controller and a regulator can basically be
summed up in terms of software by the presence or absence of a set
point as part of the fundamental design. In real-life process control,
a control loop is a much more complicated and elabourate device than a
regulator. For the above example of controlling frame rate, an
effective method to regulate the frame rate is to simply set the frame
wait to 1/3 of a second. If the PC is fast enough, the rate will
regulate to its optimum value. If the PC is too slow, the regulation
will not correct properly.
One often-overlooked concept in controllers is the concept of "Fail to
safe". In terms of computers, this isn't relevant for safety reasons,
because the controller and the control element will never fail
independently of each other (In real life, lines can be cut or
pneumatics can be blown out). Where this IS important is the fact that
not every control element will act in the same direction! In the
example above, increasing the frame period will decrease the frame
rate. What if, instead, you were increasing the frequency of the frame
wait? In that case, the increase in frequency would increase the frame
rate. For this reason, we have "DIR" and "REV" outputs. The thing to
remember is that DIR outputs will increase with an increase in
error(More on this later), and REV outputs will decrease with an
increase in error.  In practice, you must ALWAYS ask yourself: "If my
PV changes, which direction of change in M will bring PV back to SP?"

Controller 1: The knife-edge on-off
The knife-edge controller functions basically by comparing the process
variable to set point, and setting M to 100% or 0%, depending on the
DIR/REV setting, when PV drops below SP, and hammering M to the
opposite value when the PV rises above SP. This results in a fairly
tightly controlled PV, oscillating at the natural frequency of the
process.

The knife-edge controller is the easiest to write in FB  basically,
it's just an IF statement!

IF DIRREV = DIR then
       IF PV < SP then M = 100 else M = 0
ELSE
       IF PV < SP then M = 0 else M = 100
ENDIF

Note that the reason this is called "knife edge" is that there is a
single sharply defined point where on becomes off. This effect can
also be created by setting the proportional gain on a P+B controller
(Covered later) to a massive value, thus sending M to max or min
depending on the PV and SP.

Controller 2: The Hysteresis on-off
The Hysteresis, or "Band gap" on-off controller is virtually the same
as above, with one important difference: Unlike the knife-edge
controller, which will turn a process on and off right around a single
point, the on and off values are different. This is used mostly in
places where hammering a control element off and on so quickly will
have a detrimental effect.

IF DIRREV = DIR then
       if M = 100 then IF PV < SP + X then M = 100 else M = 0
       if M = 0 then IF PV < SP - X then M = 100 else M = 0
ELSE
       if M = 100 then IF PV < SP + X then M = 0 else M  = 100
       if M = 0 then IF PV < SP - X then M = 0 else M = 100
ENDIF

Notice that this controller just changes the point around a bit
depending on the direction M is in. Controllers like these are used in
industrial operations when the simplicity of an on-off controller is
ideal, but the high oscillation of a control element using a
knife-edge controller will cause excess mechanical wear.

Controller 3: The P+B
The P+B controller is where things get interesting, because we're
getting into continuous control. P+B means "Proportional + Bias". To
understand what this means, you must first understand that error is
either SP-PV or PV-SP, depending on the DIR/REV setting. Therefore,
all the remainder of the controllers actually know is exactly how
different PV is from SP.
Proportional control uses this information, E, by simply multiplying
it by some factor Gp. The effect is any error in the process variable
signal vs. set point will drive M to oppose the error proportionally.
To understand this, consider you have your two hands pushing against
each other. Now, when one starts pushing harder, the other starts
pushing harder. When one lets up, the other lets up. In the end,
nothing moves. This is exactly how this controller functions. It also
has a bias function, however. The purpose of this is to eliminate the
offset caused by the Proportional signal. There is an offset because
the proportional signal REQUIRES error to act. Thus, if there is no
error, the output won't change. If the output won't change, your PV
drifts from SP. If your PV drifts from SP, you get an error, and the
controller can correct that error!
M = B + Gp*E

if DIR then E = PV  SP else E = SP  PV
M = B + Gp*E


Controller 4: The PI
We covered mathematical integrals in a previous article. This
controller supposedly take the integral of error over time, multiplies
the integral by a constant Gi, and adds that to the proportional gain
covered in the previous controller. The result is that the controller
'seeks' zero error by integrating the error over time, and acting in
the direction required to push error towards 0.  This would be tough
if we were actually planning on taking the integral mathematically,
but you can sum the error using a much better method: just keep adding
the error to a value, scaled with time!
One problem with this form of controller is  they can "wind up" the
integral and take a long time to "unwind" on some processes. This can
happen if your SP is set very high or very low, and is then set very
low or very high. There is no bias in this controller, because the
Integral action actually functions as an 'auto-bias' mechanism.

M = Gp * E + GI * E

if DIR then E = PV  SP else E = SP  PV
IntegralE += E
M = B + Gp*E + Gi*IntegralE

Controller 5: The PID
The PID controller takes the previous PI controller, and adds one more
element. A problem with many processes, such as temperature control
loops, is that the strange time constants will make a PI control loop
oscillate. This happens because a PI control loop can have no clue
when the process starts approaching the appropriate value. The PID
controller adds a derivative unit, which opposes the rate of change of
the process variable, multiplied by a constant Gd. By limiting the
rate of change, the PID controller can achieve what almost appears to
be prescient control with some processes, such as temperature. An
important thing to remember though, is processes with Gaussian
noise(that is, noise that more or less stays around the measurement
signal) will act WORSE with a pid controller.

M = GP * E + GI * integral(E) + GD * dE/dt

if DIR then E = PV  SP else E = SP  PV
IntegralE += E
DerivativeE = oldE - E
oldE = E
M = B + Gp * E + Gi * IntegralE + Gd * DerivativeE

Note that the so-called Derivative is nothing more than the difference
between the old error and the new. Again, calculus concepts so
difficult with equations are much simpler when you exist in the time
domain.

Integrating a control loop into a software process
To integrate the controller into a loop, simply plug the sensed
variable into PV, and the controlled variable into SP. You may need to
scale both, but that's more or less self-explanitory.
Tuning your control loop
There are a large variety of ways to tune a control loop, including
the Process Reaction Curve and Zeigler Nichols. Instead of trying to
explain those complex techniques, I'll just tell you to do the easy
thing: Increase proportional gain until the loop starts to oscillate,
then pull back a bit. Then, increase integral gain until the loop
starts to oscillate, then pull back a bit. Finally, do the same for
derivative. It's not pretty, but it will provide a relatively good
reaction, possibly better than quarter amplitude damping.

--

Jason