Upload
chandushar1604
View
208
Download
0
Embed Size (px)
Citation preview
PID Control Intro with the BASIC Stamp Most of us tend to enjoy the benefits of control systems without ever giving them a second though. Consider the heaters, coolers and thermostats that maintain the comfortable temperatures in our homesand workplaces. We just set the temperature on the thermostat, and the control system takes care ofthe rest. In this case, the control system's job is to regulate temperature, and when it does it's job wehardly think about it at all. Now, if it stops working during a heat wave or cold snap, well, that's anentirely different story. More examples of control systems can be found inside our cars, where they are responsible for thesmooth ride, anti-lock braking, fuel delivery and ignition just to name a few. Factories make use ofcontrol systems to maintain fluid levels in tanks, temperatures in curing ovens, and automatedplacement of parts in assembly lines. In the robotics world, control systems are used to help themmaintain balance, distance from other robots or walls, and to refine navigation. With so many projectsand products using control systems, the design techniques for control systems can be a useful additionto just about any inventor's toolbox.
Learn Lots More about Control Systems with Industrial Control.
There are many different types of control systems. Some examples include
open loop, closed loop, on/off, hysteresis, and proportional-integral-
derivative. The Industrial Control text provides a comprehensive
introduction to control systems, along with circuit and program examples
that you can apply with each of the different types of control systems.
Industrial Control is a free download from www.parallax.com -> Downloads
-> Stamps in Class Tutorials.
Closed loop control is the kind of control where information from sensors is used to determine what theactuators in the system do. It is so named because the diagrams that describe the interaction betweensensors and actuators draws a closed loop. (See Figure 1.) Some examples of sensors and actuators inclosed loop systems include distance sensors and motors, temperature sensors and heaters/coolers, andfluid/gas level sensors and valve controls. Figure 1 - Closed Loop Block Diagram
The block diagram for closed loop control in Figure 1 describes a process that is repeated over andover 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) todetermine the output that goes to the actuator. The actuator has some effect on the system. Thesensor is again checked to find out the result of the actuators influence, and the whole loop isrepeated. Monitoring the influence of an actuator on a system and adjusting its influence on thatsystem based on the sensor measurement is called feedback. The closed loop in Figure 1 is oftenreferred to as a feedback loop.
The System Block
The actuator is not usually the only thing that causes changes to the
system. Other factors change the system too, and it is the feedback loop's
job to maintain the system at the desired level. One of the examples from
the Industrial Control text involves maintaining the temperature in a
container. The temperature outside the container might vary due to doors
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
1 of 14 9/4/2010 9:36 PM
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. Oneof the most common and effective methods is called proportional-integral-derivative control, which isabbreviated PID. In fact, many of the factory, automotive, and robotic control system examplesmentioned earlier involve PID. This Chapter introduces PID control, explains how it works, and demonstrates how you can do PIDcalculations with the BASIC Stamp 2. In this chapter's activities, you will provide the BASIC Stampwith sensor feedback by sending it messages from the Debug Terminal's transmit windowpane. Youwill also monitor the PID controller’s output responses to get more familiar with the role of each of thethree types of control. In coming Chapters, you will use PID to smooth out the Boe-Bot’s followingbehavior, and improve its line following performance.
Andy LindsayEducation DepartmentParallax, Inc.
Post Edited (Andy Lindsay (Parallax)) : 8/24/2010 6:42:01 PM GMT
Image Attachment :
Control_Loop.jpg 15KB(image/pjpeg)
This image has been viewed
13031 time(s).
Back to Top
Andy Lindsay (Parallax)Forum Moderator
Date Joined Jul 2004Total Posts : 1131
Posted 3/14/2005 11:56 AM (GMT -7)
Activity #1: Proportional Control with PBASIC
The P in PID stands for proportional. This activity introduces closed loop control with just the proportionalcalculation along with a PBASIC program that performs this calculation. This activity demonstrates how aproportional calculation can be used to have an effect on the system that is "proportional" do the differencebetween the measured level and the desired level.
The Proportional Control Loop
Proportional control is sufficient for some systems, and examples of proportional control can be found inboth Industrial Control and Robotics with the Boe-Bot. As circuit schematics are used to describe circuits,block diagrams are used to describe control systems. A block diagram for proportional control is shown inFigure 2. The circle on the left is called a summing junction, and it shows how the measured sensor value issubtracted from the desired sensor value (the set point) to calculate the error. This is the error that needs tobe corrected by the control system. Proportional control attempts to correct this error by multiplying it bysome constant value (Kp). The resulting actuator output exerts a correcting influence on the system. Bydefinition, this influence is proportional to the measured error. Since the actuator output has some effect onthe system, the sensor value is checked again, and the whole process is repeated, over and over again tomaintain the level(s) in the system. Figure 2 - Proportional Control Block Diagram
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
2 of 14 9/4/2010 9:36 PM
Let's say a robot is trying to maintain a distance between itself and a target, but something keeps moving thetarget. Proportional control will detect the change in distance and apply drive to the robots wheels to makeit back away from or catch up to the target. This drive is proportional to the error in the distancemeasurement. Keep in mind that "error" doesn't mean the distance measurement is wrong, it just means thatthe measurement is different from the desired value. Figure 3 shows a graph of the change in some errormeasurements over time. Notice how the error is 3 in the sample at time = 2. Since Kp is 10, the output is30. In the second sample at time = 7, the error is -2, so the output is -20. Figure 3 - Measured Error vs. Time
Example Program - ProportionalControl.bs2
Let's look more closely at how proportional control works with ProportionalAlgorithm.bs2. This programtakes a sensor input that you enter into the Debug Terminal's transmit windowpane, performs the error andproportional calculations, and sends the drive value to the Debug Terminal's receive windowpane. (SeeFigure 4.) Figure 4 - Testing the ProportionalAlgorithm.bs2
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
3 of 14 9/4/2010 9:36 PM
√ Enter and run ProportionalAlgorthm.bs2.√ For each number you enter, the output value should be ten times the calculated error.√ Try this sequence of numbers, making sure to press the space bar once between each number tomake the DEBUGIN command accept it. 1 2 3 2 1 0 -1 -2 -3.
' ProportionalAlgorithm.bs2
' Demonstrates how proportional control influences error correction
' in a feedback loop.
' {$STAMP BS2}
' {$PBASIC 2.5}
SetPoint CON 0 ' Set point
Kp CON 10 ' Proportionality constant
Current CON 0 ' Current error
sensorInput VAR Word ' Input
error VAR Word(1) ' One element error array
p VAR Word ' Proportional term
drive VAR Word ' Output
DO
DEBUG "Enter sensor input value: "
DEBUGIN SDEC sensorInput
' Calculate error.
error(Current) = SetPoint - sensorInput
' Calculate proportional term.
p = Kp * error(current)
' Calculate output.
drive = p
' Display values.
DEBUG CR, CR, "ERROR", CR,
SDEC ? SetPoint, SDEC ? sensorInput, SDEC ? error(Current), CR,
"PROPORTIONAL", CR,
SDEC ? Kp, SDEC ? error(Current), SDEC ? p, CR,
"OUTPUT", CR,
SDEC ? drive, CR, CR
LOOP
How ProprotaionalControl.bs2 works
The DO...LOOP repeats the operations in the proportional control block over and over again. Figure 5 showshow each element in the block diagram is performed by a PBASIC statement. You are the System block andthe sensor input. The program gets the sensorInput variable from you with the command DEBUGIN SDECsensorInput. It then performs the error calculation with this statement error(Current) = SetPoint -sensorInput. The proportional block is executed with this statement p = Kp * error(current). Theactuator output, which is also an input to the system is determined by this command: drive = p. Figure 5 - BASIC Stamp 2 Commands vs. Block Diagram
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
4 of 14 9/4/2010 9:36 PM
Your Turn - Adding Offset to the Actuator Output
Let's say that you expect sensor values from -10 to 10, but you need to use these values to control a servo. Let's also say that the useful signal input range for the servo is from 1.3 ms to 1.7 ms. That translates toPULSOUT Duration augments that range from 650 to 850 for the BASIC Stamp 2. You will need to add anoffset to the output values as shown in Figure 5. Since the input is from -10 to 10, and Kp is 100, it meansthe result of the proportional block (Kp) will be -100 to 100. So, your offset value will have to be 750 togive you PULSOUT Duration arguments that range from 650 to 850. Figure 6 - Incorporating Output Offset into the Control Loop
To make the program accommodate the extra summing junction, you will have to modify this statement: ' Calculate output.
drive = p
One simple way to do it would be like this drive = p + 750. However, it's a better coding practice todeclare 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 from650 to 850.
Andy LindsayEducation DepartmentParallax, Inc.
Post Edited (Andy Lindsay (Parallax)) : 8/24/2010 6:43:09 PM GMT
Image Attachment : Image Attachment :
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
5 of 14 9/4/2010 9:36 PM
Image Attachment :
ProportionalControlLoop.jpg 13KB (image/pjpeg)
This image has been viewed
13110 time(s).ProportionalErrorVsOutput.jpg
36KB (image/pjpeg)This image has been viewed
13017 time(s).
DebugProportionalControl.jpg 97KB (image/pjpeg)
This image has been viewed
12894 time(s).
Image Attachment :
ProportionalWithOutputOffset.jpg 15KB (image/pjpeg)
This image has been viewed 12993
time(s).
Image Attachment :
ProportionalPbasicControl.jpg 30KB (image/pjpeg)
This image has been viewed12946 time(s).
Back to Top
Andy Lindsay (Parallax)Forum Moderator
Date Joined Jul 2004Total Posts : 1131
Posted 3/14/2005 12:12 PM (GMT -7)
Activity #2: Integral Control
Proportional is just one way to react to an error in the system. The problem with proportional controlis that it can't detect trends and adjust to them. This is the job of integral control. There is another example graph of the error in a system over time on the left of Figure 6. Again, itmight be the distance of a robot from an object, or it could be fluid level in a tank, or the temperaturein a factory oven. Perhaps the target the robot is following keeps on going away from the robot at aspeed that the robot isn't catching up with. Maybe the oven door seal is worn; maybe the fluid drawfrom the tank is unusually large. Regardless of the cause, since proportional is not designed to react totrends 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 tozero, the area of the error gets larger and larger. The right side of figure 6 shows how the integraloutput 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 andharder to correct the error. Figure 7 - How Integral Builds up as Error Continues to be a Problem
So what happens when the error isn't a straight line, like the curve shown in Figure 8? That's what thecalculus operation of integration determines, the area between a curve and an axis. In the case ofintegral control, as more time passes with an error, the area under the curve grows, and so does thevalue that the integral calculation will use to drive against the system error. If the error curve dropsbelow the time axis, the buildup of negative area subtracts from the buildup of positive area. Whentuned correctly, integral control can help the system home in on an error of zero. Figure 8 - Error Can Change with Time - So Does the Area
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
6 of 14 9/4/2010 9:36 PM
The BASIC Stamp can approximate the error under the curve with numerical integration. Figure 9shows how you can approximate the error under a curve by adding up the area of a bunch of littlerectangles between the error curve and the time axis. The area of each box is the error multiplied bythe time between measurements. By adding up all the box areas, you get an approximation of the areaunder the curve. Figure 9 - Numerical Integration for Determining Area
So long as your measurements are evenly spaced, you can call the width of each box a value of 1. Thismakes the math much simpler than trying to account for 20 ms between samples, 5 minutes betweensamples, or whatever your sampling rate turns out to be. Instead of multiplying error by the timeincrement between samples and then adding to the next error multiplied by time, you can just multiplyeach error sample by a time of 1. The result is that you can just keep a running total of errormeasurements for your integral calculation. Here is an example of how to do this with PBASIC: ' Calculate integral term.
error(Accumulator) = error(Accumulator) + error(Current)
i = Ki * error(Accumulator)
The next example program performs numerical integration on the error signal and adjusts the outputaccordingly. As with proportional control, there is a constant that scales the integration output to thedesired value. For simplicity's sake, we'll use 10 again for Ki. Figure 10 shows a block diagram of thecontrol loop. The term Kp ∫ edt refers Kp multiplied by the integral of the error over time. In otherwords, Kp multiplied by the accumulated area between the error curve and the time axis. Figure 10 - Integral Control
Example Program - IntegralAlgorithm.bs2
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
7 of 14 9/4/2010 9:36 PM
Example Program - IntegralAlgorithm.bs2
√ Enter, save, and run IntegralAlgorithm.bs2.√ Enter this sequence of values into the Debug Terminal's transmit windowpane: 3 3 3 3 3 3 3 33. Notice how the integral output gets larger every time the 3 is repeated. That's integral's job, todetect trends and increase the drive to correct it as needed.√ Now try this sequence: 1 2 3 4 5 4 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0. The area under this curve is 0since the negative area is the same as the positive area. That is also what the integral calculationwill arrive at when you have finished entering the sequence.
' IntegralAlgorithm.bs2
' Demonstrates how integral control influences error correction
' in a feedback loop.
' {$STAMP BS2}
' {$PBASIC 2.5}
SetPoint CON 0 ' Set point
Ki CON 10 ' Integral constant
Current CON 0 ' Array index for current
error
Accumulator CON 1 ' Array index for
accumulated error
sensorInput VAR Word ' Input
error VAR Word(2) ' Two element error array
i VAR Word ' Integral term
drive VAR Word ' Output
DO
DEBUG "Enter sensor input value: "
DEBUGIN SDEC sensorInput
' Calculate error.
error(Current) = SetPoint - sensorInput
' Calculate integral term.
error(Accumulator) = error(Accumulator) + error(Current)
i = Ki * error(Accumulator)
' Calculate output.
drive = i
' Display values.
DEBUG CR, CR, "ERROR", CR,
SDEC ? SetPoint, SDEC ? sensorInput, SDEC ? error(Current), CR,
"INTEGRAL", CR,
SDEC ? Ki, SDEC ? error(accumulator), SDEC ? i, CR,
"OUTPUT", CR,
SDEC ? i, SDEC ? drive, CR, CR
LOOP
How IntegralAlgorithm.bs2 Works
This program is a ProportionalAlgorightm.bs2 modified to perform the integral calculation instead. There are now two different types of errors to keep track of, so there the error variable array wasexpanded to two elements. The error(0) variable tracks the current error, and error(1) tracks theaccumulated error. The constants Current CON 0 and Accumulator CON 1 make the bookkeeping alittle more sensible with error(current) and error(accumulator). The other change that was made is that the error is accumulated and the integral output is calculatedwith these two statements: ' Calculate integral term.
error(Accumulator) = error(Accumulator) + error(Current)
i = Ki * error(Accumulator)
Your Turn - Clamping the Integral Output
As you may have gathered from repeatedly entering 3 into the Debug Terminal, the integral driveoutput can really start to run away. You can use the MIN and MAX operators to keep integral control
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
8 of 14 9/4/2010 9:36 PM
output can really start to run away. You can use the MIN and MAX operators to keep integral controlfrom going overboard with the output. Let's repeat the servo offset activity and limit the output from650 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 LindsayEducation DepartmentParallax, Inc.
Post Edited (Andy Lindsay (Parallax)) : 8/24/2010 6:44:09 PM GMT
Image Attachment :
IntegralAreaVsOutput.jpg 15KB (image/pjpeg)
This image has been viewed12765 time(s).
Image Attachment :
AreaUnderCurve.jpg 14KB(image/pjpeg)
This image has been viewed12788 time(s).
Image Attachment :
NumericalIntegration.jpg 16KB (image/pjpeg)
This image has been viewed12777 time(s).
Image Attachment :
IntegralBlockDiagram.jpg 14KB (image/pjpeg)
This image has been viewed
12767 time(s).
Back to Top
Andy Lindsay (Parallax)Forum Moderator
Date Joined Jul 2004Total Posts : 1131
Posted 3/14/2005 12:20 PM (GMT -7)
Activity #3: Derivative Control
Here's a situation that neither proportional nor integral control really deal with. It's rapid changes tothe system that come from an external source. Keeping the system steady when outside influences aremaking it change abruptly is the job of derivative control. In calculus, derivative is an operation thatmeasures the rate of change of a curve, such as the error curve shown in Figure 11. By taking actionbased on the rate the error is changing, the output drive can respond rapidly to disturbances to thesystem. Figure 11 - Derivative for Reacting Quickly to Changes
Like the samples in Figure 11, calculus can be used to examine any point on the curve and give you anexact slope. Circuits that involve operational amplifiers do these continuous calculations forcontinuous time control systems. When a signal is digitally sampled periodically, derivative can be analgebraic slope calculation. The BASIC Stamp can then take the difference between two sampledpoints on a curve, and divide by the distance (time) between those two points. Figure 12 - Derivative from Discrete Samples
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
9 of 14 9/4/2010 9:36 PM
In the case of the error vs. time graph, derivative would be the change in error divided by the change intime. That's ∆e/∆t. As with integration, numerical differentiation (calculating the derivative) can besimplified 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 errormeasurement from the current error measurement. In other words, derivative control can beaccomplished with simple subtraction: ' Calculate derivative term.
error(Delta) = error(Current) - error(Previous)
d = Kd * error(delta)
Although ∆t might be a small or a large value, so long as it's always the same interval, the derivativeconstant, Kd can be scaled to compensate for the fact that we made ∆t equal to 1. Keep in mind thatthe 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 -3will be dealt with by proportional and integral control, but derivative won't touch it. Why? Becausederivative 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 toresist. 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 willemulate. As with the other examples, Kd will be 10. This program will measure the rate of change ofthe error measurement (de/dt) and multiply by Kp = 10. This result will go to your Debug Terminal(actuator output). Figure 13 - Derivative in a Feedback Loop
Example Program - DerivativeAlgorithm.bs2
Try entering a series of sensor inputs that don't change, like 3 3 3 3 3 3 3 -4. Derivative will resist thefirst 3 because that's a change from 0 to 3, but all the rest of the threes will result in no correction fromthe derivative calculation. Then, with the big change from 3 to -4, the derivative will drive hard tocorrect this abrupt change.
√ Enter, save, and run DerivativeAlgorithm.bs2.√ Try 3 3 3 3 3 3 3 -4. Verify that derivative resisted the first 3 but made no attempt to correctthe others. √ Also verify that the the big change from 3 to -4 resulted in a large output to correct the changein error.
' DerivativeAlgorightm.bs2
' Demonstrates how derivative influences error correction
' in a feedback loop.
' {$STAMP BS2}
' {$PBASIC 2.5}
SetPoint CON 0 ' Set point
Kd CON 10 ' Derivative constant
Current CON 0 ' Current error
Previous CON 2 ' Previous error
Delta CON 3 ' Accumulated error
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
10 of 14 9/4/2010 9:36 PM
Delta CON 3 ' Accumulated error
sensorInput VAR Word ' Input
error VAR Word(4) ' Different types of errors
d VAR Word ' Derivative term
drive VAR Word ' Output
DO
DEBUG "Enter sensor input value: "
DEBUGIN SDEC sensorInput
' Calculate error.
error(Current) = SetPoint - sensorInput
' Calculate derivative term.
error(Delta) = error(Current) - error(Previous)
d = Kd * error(delta)
' Calculate output.
drive = d
' Display values.
DEBUG CR, CR, "ERROR", CR,
SDEC ? SetPoint, SDEC ? sensorInput, SDEC ? error(Current), CR,
"DERIVATIVE", CR,
SDEC ? Kd, SDEC ? error(Delta), SDEC ? d, CR,
"OUTPUT", CR,
SDEC ? d, SDEC ? drive, CR, CR
' Save current error to previous error before next iteration.
error(Previous) = error(Current)
LOOP
How DerivativeAlgorithm.bs2 Works
The error array variable now has four elements. Element 0 is still the current error, element 1 is theaccumulator, which is unused at present since there are not integral calculations in this program. Element 2 is used to store the previous error measurement, and element 3 is used to store thedifference between the previous and current measurements. The constant for element 2 is Previous,and the constant for element 3 is Delta. That makes the derivative calculation much easier to read. ' Calculate derivative term.
error(Delta) = error(Current) - error(Previous)
d = Kd * error(delta)
At the end of each pass through the loop, the current measurement has to be saved as the previousmeasurement. There will be a new error(current) the next time through the loop, and a newerror(delta) will be made with the updated error(Previous) value. ' Save current error to previous error before next iteration.
error(Previous) = error(Current)
Your Turn - Increasing the Reaction to Abrupt Changes
√ Try changing the derivative constant (Kd) from 10 to 20. It should react twice as strongly asbefore to abrupt changes in the error.
Andy LindsayEducation DepartmentParallax, Inc.
Post Edited (Andy Lindsay (Parallax)) : 8/24/2010 6:45:10 PM GMT
Image Attachment :
ContinuousDifferentiationSamples.jpg 11KB (image/pjpeg)
This image has been viewed 12664
time(s).
Image Attachment :
DiscreteDifferentiationSamples.jpg 13KB (image/pjpeg)
This image has been viewed 12749
time(s).
Image Attachment :
DerivativeControlLoop.jpg 14KB (image/pjpeg)
This image has been viewed12662 time(s).
Back to Top
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
11 of 14 9/4/2010 9:36 PM
Andy Lindsay (Parallax)Forum Moderator
Date Joined Jul 2004Total Posts : 1131
Posted 3/14/2005 12:25 PM (GMT -7)
Activity #4: All Together Now - Proportional, Integral, and Derivative
A PID control loop involves some contributions from each of the three kinds of control: proportional,integral, and derivative. The amount of contribution that each of the controls makes can be adjustedby changing their proportionality constants, Kp, Ki, and Kd. By making these constants larger orsmaller, you can make the contribution of one of the controls more dominant or subtle in the system. One system might need only light integral control, some proportional and strong derivative, whileanother system might need strong integral and proportional controls, but not much derivative, whilestill another system might need roughly equal measures of each. 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.
In many PID systems, the set point changes too.
Many PID systems have a variable set point. When the set point is variable,
it's just considered a system input. The goal of PID in that case is to track
the varying input. PID makes it possible to respond more quickly to the
changing input and mimic the it more faithfully at the system output.
The next example program will perform this PID control loop. To make comparisons easier, Kp, Ki,and Kd are all set to 10. Figure 14 - Block Diagram for Proportional Integral Derivative
Example Program - PidAlgorithm.bs2
Keep in mind that proportional always does some work when there's some error. However, integraland derivative are ready to do extra work to correct the error, integral to correct trends, and derivativeto correct abrupt changes.
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
12 of 14 9/4/2010 9:36 PM
√ Enter, save, and run PidAlgorithm.bs2.√ Here is a series of sensor inputs that the integral control will detect as a trend and resist muchmore strongly than either proportional or derivative: 1 2 3 4 5 5 5 5 5 5 5 5 5√ Here is a series of sensor measurements that derivative will work hard to correct whileproportional and integral don't do nearly as much: 3 -3 4 -4 5 -5 6 -6 5 -5 4 -4
' PidMathExample.bs2
' Demonstrates how a combination of proportional, integral, and
' derivative control influence error correction in a feedback loop.
' {$STAMP BS2}
' {$PBASIC 2.5}
SetPoint CON 0 ' Set point
Kp CON 10 ' Proportionality constant
Ki CON 10 ' Integral constant
Kd CON 10 ' Derivative constant
Current CON 0 ' Array index - current
error
Accumulator CON 1 ' Array index - accumulated
error
Previous CON 2 ' Array index - previous
error
Delta CON 3 ' Array index - change in
error
sensorInput VAR Word ' Sensor input variable
error VAR Word(4) ' Four different types of
errors
p VAR Word ' Proportional term
i VAR Word ' Integral term
d VAR Word ' Derivative term
drive VAR Word ' Output
DO
DEBUG "Enter sensor input value: "
DEBUGIN SDEC sensorInput
' Calculate error.
error(Current) = SetPoint - sensorInput
' Calculate proportional term.
p = Kp * error(current)
' Calculate integral term.
error(Accumulator) = error(Accumulator) + error(Current)
i = Ki * error(Accumulator)
' Calculate derivative term.
error(Delta) = error(Current) - error(Previous)
d = Kd * error(delta)
' Calculate output.
drive = p + i + d
' Display values.
DEBUG CR, CR, "ERROR", CR,
SDEC ? SetPoint, SDEC ? sensorInput, SDEC ? error(Current), CR,
"PROPORTIONAL", CR,
SDEC ? Kp, SDEC ? error(Current), SDEC ? p, CR,
"INTEGRAL", CR,
SDEC ? Ki, SDEC ? error(accumulator), SDEC ? i, CR,
"DERIVATIVE", CR,
SDEC ? Kd, SDEC ? error(Delta), SDEC ? d, CR,
"OUTPUT", CR,
SDEC ? p, SDEC ? i, SDEC ? d, SDEC ? drive, CR, CR
' Save current error to previous error before next iteration.
error(Previous) = error(Current)
LOOP
Your Turn - Tuning the PID Control Loop
Assume that your sensor inputs will range from -10 to 10. Adjust your constants (Kp, Ki, and Kd) so
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
13 of 14 9/4/2010 9:36 PM
Assume that your sensor inputs will range from -10 to 10. Adjust your constants (Kp, Ki, and Kd) sothat the maximum contribution any of the controls can make to the output ranges from 650 to 850. Toget the integral control to adhere to this requirement, you will also have to use the MIN and MAXoperators. _____________________________________________________________________________ The draft material in this Topic is part of a forthcoming Stamps in Class text by Andy Lindsay. (c) 2005 by Parallax Inc - all rights reserved.
Andy LindsayEducation DepartmentParallax, Inc.
Post Edited (Andy Lindsay (Parallax)) : 8/24/2010 6:45:44 PM GMT
Image Attachment :
PidBlockDiagram.jpg 23KB(image/pjpeg)
This image has been viewed12728 time(s).
PID Control Intro with the BASIC Stamp :: Parallax Forums http://forums.parallaxinc.com/forums/default.aspx?f=6&m=66982
14 of 14 9/4/2010 9:36 PM