You are on page 1of 2

PID Algorithm Code http://www.jashaw.com/pid/code.

html

ControlSim The PID controller and process simulator Click here to get more details, buy, and
based on MS Excel spread sheets. $45.00 download
Buy and download my eBook: The PID Control
Algorithm: How It Works, How To Tune It, and How Most major credit cards accepted.
to Use It. 2nd ed. 62pp
Adobe .pdf file only $15 Get more details, buy, and download

PID Algorithm Code


Home There are many ways to implement the PID algorithm digitally. Two
will be discussed here. In each case, there will be a section of code (in Tempera
Process Control structured Basic, easily convertible to any other language) that will be Controlle
Solutions executed by the processor every second. (some other scan rate may be Sensors s
PID Control used, change the constant 60 to the number of times per minute it is temperatu
High Qual
executed.) In each code sample there is an IF statement to execute
Description Cost!
most of the code if the loop is in the auto mode. If the loop is in www.altec.cc
Tutorial manual mode only a few lines are executed in order to allow for "Learn P
Code bumpless transfer to auto. Also, while the control loop is in manual, Control"
the output (variable OutP) will be operator adjustable using the Control Yo
Multiple Loop With Conf
operator interface software.
Cascade Guide Sho
The code shown here and on the next page can be translated into any How.
Ratio www.PIDtuni
computer language. For information about process control
Override PID Cont
programming in Visual Basic and C#, some very good information can
Tutorials
Questions and be found at HT Services site HERE >>> Free Web
Answers from Galil
Simple PID code Leader in
Control.
www.GaliLmc
One method of handling the integration and bumpless transfer to
automatic mode is an algorithm that calculates the change in output Parallel A
from one pass to the next using the derivative of the PID algorithm, Stay on to
or: in parallel
Read our
dOut/dt = gain x (dError/dt + ResetRate x Error + Derivative x www.ddj.com

d2Error/dt2
Ads by Google
Pid Algorithm Bailey Su
Autotune Pid derivative of output = gain x (derivative of error + reset rate x error Net 90 Inf
Pid Controller + second derivative of error) Discount 5
Pid Loop Tuning More 1 Yr
Visual Basic This program is run every second. If the control loop is in manual, the We Repair
output is adjusted by the operator through the operator interface www.tek-sup

software. If the control loop is in Automatic, the output is computed


by the PID algorithm.

Each pass the output is changed by adding the change in output to the
previous pass output. That change is the sum of:

the change in error (Err-ErrLast)


the error multiplied by the reset rate, and
the second derivative of the error (Err-2*ErrLast+ErrLastLast)
times the derivative.

The total is then multiplied by the gain.

This simple version of the PID controller work well in most cases, and
can be tuned by the standard PID tuning methods (some of which are
discussed later). It has “Parallel” rather than “Series” reset and
derivative, and derivative is applied to the error rather than the input
only.

Variables:

1 of 2 12/31/2009 1:07 AM
PID Algorithm Code http://www.jashaw.com/pid/code.html

Process input
Process input from last pass, used in deriv. calc.
Error, Difference between input and set point
Error from last pass
Error from next to last pass
Output of PID algorithm
value is ‘AUTO’ if loop is in automatic
value is ‘DIRECT’ if loop is direct acting
Derivative Derivative value in minutes
Reset reset rate in repeats per minute
SetP Set Point, set by operator
Mode String, set to "AUTO" or "Manual" by operator
Action String, set to "REVERSE" or "DIRECT" during configuration
The PID emulation code:

1. IF Mode = ‘AUTO’ THEN

2. Err=SetP- Input Error based on reverse action


3. IF Action = ‘DIRECT’ THEN
4. Err=0 – Err Change sign of error for direct action
5. ENDIF
6. OutP=OutP+Gain*(Err-ErrLast+Reset*Err+Deriv*(Err-ErrLast*2+ErrLastLast))
Calculate the change in output using the derivative of the PID algo
then add to the previous output.
7. ErrLastLast=ErrLast
8. ErrLast=Err
9. ELSE
11. ErrLastLast=Err
12. ErrLast=Err
13. ENDIF
14. IF OutP > 100 THEN OutP=100 Limit output to between
15. IF OutP < 0 THEN OutP=0 0 and 100 percent

The only serious problem with this form of the algorithm occurs when the output has
reached an upper or lower limit. When it does, a change in the measurement can
unexpectantly pull the output away from the limit.

Next: A Better Implementation


Free Developer Version Temperature Controls Elemental Analysis System
High End .NET Graphing Component CAL Temperature Controls - In Complete Systems for the elemental
w/ Support For All Chart Types Now! Stock On Line Manuals - analysis of materials with LIBS
www.DotNetCharting.com MasterCard & Visa www.ltb-berlin.de
www.AdvIndSys.com/Cal.htm

ControlSim The PID controller and process simulator Click here to get more details, buy, and
based on MS Excel spread sheets. $45.00 download
Buy and download my eBook: The PID Control
Algorithm: How It Works, How To Tune It, and How Most major credit cards accepted.
to Use It. 2nd ed. 62pp
Adobe .pdf file only $15 Get more details, buy, and download
Apartments in Ballantyne in South Charlotte, NC.

Buy and download e-books on other subjects,


such as relationships and making money online Click Here

Mens Divorce Secrets -- Download book for men who may go through a divorce.
The Survey Zone -- Get paid for surveys - $10 to $85 per survey.

2 of 2 12/31/2009 1:07 AM

You might also like