# PID control algorthm

R

can anyone please provide me with the PID control algorthm in C language,one that works and is quite simple to understand.if u can, then please provide comments as well.

J

#### jmG

I have nothing in C
The Bailey block 157 is a univeral recursive algorithm. It can filter, control ...
But, being universal and quite advanced, you will have to torture yourself for the coefficients !
Not so simple________

J

#### Johan Bengtsson

As simple as possible:

error=PV-SP; //control error
Ppart=error*gain; //P in PID part of the output
if ((Ppart>0 && out<1) || (Ppart<0 && out>0)) //antiwindup
Ipart+=Ppart*deltaT/Itime; //integrating part
Dpart=(Ppart-lastPpart)*Dtime/deltaT; //derivating part
out=Part+Ipart+Dpart; //total output
lastPpart=Ppart; //save this value for the next calculation

inputs:
PV - process value (0-1)
SP - setpoint (0-1)

gain - proportional gain
Itime - integral time (s)
Dtime - derivate time (s)

deltaT - time since last calculation (s)

outputs:
out - output value (0-1)

variables:
error - control error
Ppart - P part of the output
Ipart - I part of the output, must be retained between calculations
Dpart - D part of the output
lastPpart - last value of Ppart, must be retained between calculations
out - output value, must be retained between caclulations

This is a basic PID with antiwindup but without filters, bumpless transfer and a lot of other features you might want. The equations can be done in soem different ways and soem fo them end up with different results, some with the above result. The above is a "normal" (as normal as it can be) PID in the form called "ideal" or "parallel" (or possibly some other names) depending on who you ask.

You should really have a filter on the D part if you intend to use that one, and you should definitely have a filter on PV (but that one can be (and better is) an analog filter before the A/D converter). The antiwindup can be done in some other ways, but as long as you have one way implemented it is far better than no antiwindup.

Also check the arcives, the4 subject have been up before.

/Johan Bengtsson

Do you need education in the area of automation?
----------------------------------------
P&L, Innovation in training
Box 252, S-281 23 H{ssleholm SWEDEN
Tel: +46 451 49 460, Fax: +46 451 89 833
E-mail: [email protected]
Internet: http://www.pol.se/
----------------------------------------