Professional Documents
Culture Documents
f=6&m=66982
More examples of control systems can be found inside our cars, where they are responsible for the
smooth ride, anti-lock braking, fuel delivery and ignition just to name a few. Factories make use of
control systems to maintain fluid levels in tanks, temperatures in curing ovens, and automated
placement of parts in assembly lines. In the robotics world, control systems are used to help them
maintain balance, distance from other robots or walls, and to refine navigation. With so many projects
and products using control systems, the design techniques for control systems can be a useful addition
to just about any inventor's toolbox.
Closed loop control is the kind of control where information from sensors is used to determine what the
actuators in the system do. It is so named because the diagrams that describe the interaction between
sensors and actuators draws a closed loop. (See Figure 1.) Some examples of sensors and actuators in
closed loop systems include distance sensors and motors, temperature sensors and heaters/coolers, and
fluid/gas level sensors and valve controls.
The block diagram for closed loop control in Figure 1 describes a process that is repeated over and
over again. A sensor measurement is compared to the desired measurement (called the set point),
resulting in an error calculation. Some math is performed on this error value (control calculations) to
determine the output that goes to the actuator. The actuator has some effect on the system. The
sensor is again checked to find out the result of the actuators influence, and the whole loop is
repeated. Monitoring the influence of an actuator on a system and adjusting its influence on that
system based on the sensor measurement is called feedback. The closed loop in Figure 1 is often
referred to as a feedback loop.
1 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
container. The temperature outside the container might vary due to doors
to the outside being opened or closed, drafts from ventilation, etc. The
control system that you build and tune correct for these outside influences.
There are lots of different control calculations that can be made to determine the actuator output. One
of the most common and effective methods is called proportional-integral-derivative control, which is
abbreviated PID. In fact, many of the factory, automotive, and robotic control system examples
mentioned earlier involve PID.
This Chapter introduces PID control, explains how it works, and demonstrates how you can do PID
calculations with the BASIC Stamp 2. In this chapter's activities, you will provide the BASIC Stamp
with sensor feedback by sending it messages from the Debug Terminal's transmit windowpane. You
will also monitor the PID controller’s output responses to get more familiar with the role of each of the
three types of control. In coming Chapters, you will use PID to smooth out the Boe-Bot’s following
behavior, and improve its line following performance.
Andy Lindsay
Education Department
Parallax, Inc.
Image Attachment :
Control_Loop.jpg 15KB
(image/pjpeg)
This image has been viewed
13031 time(s).
Back to Top
Proportional control is sufficient for some systems, and examples of proportional control can be found in
both Industrial Control and Robotics with the Boe-Bot. As circuit schematics are used to describe circuits,
Date Joined Jul 2004
Total Posts : 1131 block diagrams are used to describe control systems. A block diagram for proportional control is shown in
Figure 2. The circle on the left is called a summing junction, and it shows how the measured sensor value is
subtracted from the desired sensor value (the set point) to calculate the error. This is the error that needs to
be corrected by the control system. Proportional control attempts to correct this error by multiplying it by
some constant value (Kp). The resulting actuator output exerts a correcting influence on the system. By
definition, this influence is proportional to the measured error. Since the actuator output has some effect on
the system, the sensor value is checked again, and the whole process is repeated, over and over again to
maintain the level(s) in the system.
2 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
Let's say a robot is trying to maintain a distance between itself and a target, but something keeps moving the
target. Proportional control will detect the change in distance and apply drive to the robots wheels to make
it back away from or catch up to the target. This drive is proportional to the error in the distance
measurement. Keep in mind that "error" doesn't mean the distance measurement is wrong, it just means that
the measurement is different from the desired value. Figure 3 shows a graph of the change in some error
measurements over time. Notice how the error is 3 in the sample at time = 2. Since Kp is 10, the output is
30. In the second sample at time = 7, the error is -2, so the output is -20.
3 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
DO
LOOP
4 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
To make the program accommodate the extra summing junction, you will have to modify this statement:
One simple way to do it would be like this drive = p + 750. However, it's a better coding practice to
declare a constant at the beginning of the program and then use it in the drive statement, so try this:
√ Add this constant declaration to the beginning of the program Offset CON 750.
√ Change the drive calculation to drive = p + Offset.
√ Run the modified version of the program and verify that inputs from 10 to -10 result in outputs from
650 to 850.
Andy Lindsay
Education Department
Parallax, Inc.
5 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
ProportionalControlLoop.jpg ProportionalWithOutputOffset.jpg
13KB (image/pjpeg) 15KB (image/pjpeg)
This image has been viewed ProportionalErrorVsOutput.jpg DebugProportionalControl.jpg This image has been viewed 12993
13110 time(s). 36KB (image/pjpeg) 97KB (image/pjpeg) time(s).
This image has been viewed This image has been viewed
13017 time(s). 12894 time(s).
Image Attachment :
ProportionalPbasicControl.jpg
30KB (image/pjpeg)
This image has been viewed
12946 time(s).
Back to Top
There is another example graph of the error in a system over time on the left of Figure 6. Again, it
might be the distance of a robot from an object, or it could be fluid level in a tank, or the temperature
in a factory oven. Perhaps the target the robot is following keeps on going away from the robot at a
Date Joined Jul 2004 speed that the robot isn't catching up with. Maybe the oven door seal is worn; maybe the fluid draw
Total Posts : 1131
from the tank is unusually large. Regardless of the cause, since proportional is not designed to react to
trends it can't detect and correct the problem. That's where integral control comes into the picture.
Integral measures the area between the error values and the time axis. If the error doesn't return to
zero, the area of the error gets larger and larger. The right side of figure 6 shows how the integral
output can react to this kind of trend. As the area between the error curve and the time axis increases,
the output increases proportional to this area. As a result, the output drives the actuator harder and
harder to correct the error.
So what happens when the error isn't a straight line, like the curve shown in Figure 8? That's what the
calculus operation of integration determines, the area between a curve and an axis. In the case of
integral control, as more time passes with an error, the area under the curve grows, and so does the
value that the integral calculation will use to drive against the system error. If the error curve drops
below the time axis, the buildup of negative area subtracts from the buildup of positive area. When
tuned correctly, integral control can help the system home in on an error of zero.
6 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
The BASIC Stamp can approximate the error under the curve with numerical integration. Figure 9
shows how you can approximate the error under a curve by adding up the area of a bunch of little
rectangles between the error curve and the time axis. The area of each box is the error multiplied by
the time between measurements. By adding up all the box areas, you get an approximation of the area
under the curve.
So long as your measurements are evenly spaced, you can call the width of each box a value of 1. This
makes the math much simpler than trying to account for 20 ms between samples, 5 minutes between
samples, or whatever your sampling rate turns out to be. Instead of multiplying error by the time
increment between samples and then adding to the next error multiplied by time, you can just multiply
each error sample by a time of 1. The result is that you can just keep a running total of error
measurements for your integral calculation. Here is an example of how to do this with PBASIC:
The next example program performs numerical integration on the error signal and adjusts the output
accordingly. As with proportional control, there is a constant that scales the integration output to the
desired value. For simplicity's sake, we'll use 10 again for Ki. Figure 10 shows a block diagram of the
control loop. The term Kp ∫ edt refers Kp multiplied by the integral of the error over time. In other
words, Kp multiplied by the accumulated area between the error curve and the time axis.
7 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
' IntegralAlgorithm.bs2
' Demonstrates how integral control influences error correction
' in a feedback loop.
DO
LOOP
The other change that was made is that the error is accumulated and the integral output is calculated
with these two statements:
8 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
output can really start to run away. You can use the MIN and MAX operators to keep integral control
from going overboard with the output. Let's repeat the servo offset activity and limit the output from
650 to 850.
√ Change the drive = i statement to drive = i + Offset MIN 650 MAX 850.
√ Run the program and verify that the integral output is limited to values between 650 and 850.
Try entering 2 repeatedly, then -2 repeatedly.
Andy Lindsay
Education Department
Parallax, Inc.
IntegralAreaVsOutput.jpg IntegralBlockDiagram.jpg
15KB (image/pjpeg) AreaUnderCurve.jpg 14KB NumericalIntegration.jpg
(image/pjpeg) 16KB (image/pjpeg) 14KB (image/pjpeg)
This image has been viewed This image has been viewed
12765 time(s). This image has been viewed This image has been viewed
12788 time(s). 12777 time(s). 12767 time(s).
Back to Top
Like the samples in Figure 11, calculus can be used to examine any point on the curve and give you an
exact slope. Circuits that involve operational amplifiers do these continuous calculations for
continuous time control systems. When a signal is digitally sampled periodically, derivative can be an
algebraic slope calculation. The BASIC Stamp can then take the difference between two sampled
points on a curve, and divide by the distance (time) between those two points.
9 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
In the case of the error vs. time graph, derivative would be the change in error divided by the change in
time. That's ∆e/∆t. As with integration, numerical differentiation (calculating the derivative) can be
simplified for control purposes by always considering the time between samples to be a value of 1.
Then, numerical differentiation can be accomplished by subtracting the value of the previous error
measurement from the current error measurement. In other words, derivative control can be
accomplished with simple subtraction:
Although ∆t might be a small or a large value, so long as it's always the same interval, the derivative
constant, Kd can be scaled to compensate for the fact that we made ∆t equal to 1. Keep in mind that
the rate that the errors are sampled also has to be faster than the error signal's tendency to change.
The job of derivative control is to react to changes in the system. For example, a constant error of -3
will be dealt with by proportional and integral control, but derivative won't touch it. Why? Because
derivative only reacts to changes. With a series of errors of the same value, there's no change.
However, any kind of change, such as the error going from -3 to 4, will cause derivative control to
resist. Let's try an example program and observe how this works.
Figure 13 shows a block diagram for the derivative control loop the next example program will
emulate. As with the other examples, Kd will be 10. This program will measure the rate of change of
the error measurement (de/dt) and multiply by Kp = 10. This result will go to your Debug Terminal
(actuator output).
' DerivativeAlgorightm.bs2
' Demonstrates how derivative influences error correction
' in a feedback loop.
10 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
DO
LOOP
At the end of each pass through the loop, the current measurement has to be saved as the previous
measurement. There will be a new error(current) the next time through the loop, and a new
error(delta) will be made with the updated error(Previous) value.
Andy Lindsay
Education Department
Parallax, Inc.
DerivativeControlLoop.jpg
ContinuousDifferentiationSamples.jpg DiscreteDifferentiationSamples.jpg 14KB (image/pjpeg)
11KB (image/pjpeg) 13KB (image/pjpeg) This image has been viewed
This image has been viewed 12664 This image has been viewed 12749 12662 time(s).
time(s). time(s).
Back to Top
11 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
Here are several important things to keep in mind when working with PID control loops:
• Error is the difference between the level you want and the level that is measured, and control
loops work to correct error.
• Proportional control resists error by applying an opposing influence that is proportional to the
error.
• Integral control detects and corrects trends in error over time.
• Derivative control detects and resists abrupt changes in the system.
The next example program will perform this PID control loop. To make comparisons easier, Kp, Ki,
and Kd are all set to 10.
12 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
' PidMathExample.bs2
' Demonstrates how a combination of proportional, integral, and
' derivative control influence error correction in a feedback loop.
DO
LOOP
13 of 14 9/4/2010 9:36 PM
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
Assume that your sensor inputs will range from -10 to 10. Adjust your constants (Kp, Ki, and Kd) so
that the maximum contribution any of the controls can make to the output ranges from 650 to 850. To
get the integral control to adhere to this requirement, you will also have to use the MIN and MAX
operators.
_____________________________________________________________________________
The draft material in this Topic is part of a forthcoming Stamps in Class text by Andy Lindsay.
Andy Lindsay
Education Department
Parallax, Inc.
Image Attachment :
PidBlockDiagram.jpg 23KB
(image/pjpeg)
This image has been viewed
12728 time(s).
14 of 14 9/4/2010 9:36 PM