PID control Algorithm

  • Thread starter GAMATEC Instrumentación
  • Start date
G

Thread Starter

GAMATEC Instrumentación

I'm required to carry out an PID controller in Visual BASIC, but I have not been able to find an algorithm easy to implement.
Somebody could help me.

Thanks.
Daniel Marin Valencia.
GAMATEC Instrumentación.
 
T

Tobin Harsono

The simple way is something like this:

1). Type: PID Position controller:
Out(k) = BIAS + Kp*Err(k) + Ki*(Err(k)+Err(k-1))+Kd*(Err(k)-Err(k-1))

2). Type: PID Velocity controller:
Out(k) = Out(k-1) + Kp*Err(k) + Ki*(Err(k)+Err(k-1))+Kd*(Err(k)-Err(k-1))

where:
Out = output to actuator
Kp = Proportional Gain
Ki = Integration Gain
Kd = Differentiator Gain
k = sampling at (k) iteration
k-1 = sampling at (k-1) iteration

If the actuator should stay at some position and output of the PID controller is not 0 (zero), than use Velocity Type.

If you need more information, let me know. I can give you more information.
[email protected]
 
T
A really simple PID can be done by implementing the following psuedocode:

Error = setpoint - process variable //for reverse acting controls like heating
or
Error = process variable - setpoint //for forward acting controls like cooling

PTERM = Error * PGAIN //proportional

ITERM = ITERM + (Error* Igain) //integral
IF ITERM>100% ITERM=100% //anti-windup
IF ITERM<0% ITERM=0%

DTERM = (Error - LastError)*DGAIN //differential

LastError = Error

CV = PTERM + ITERM + DTERM //P+I+D
IF CV> 100% CV=100% //output limiting
IF CV< 0% CV=0%

RETURN CV

This algorithim must be executed at a consistent guarenteed periodic rate (such as once/second) to function correctly. Correctly tuned it will function well and it is a rudimentary implementation of the equations posted above. As you understand more about PID you can add more to the code. Download the Puffin PLC tarball, there's a more sophisticated PID in there if you want to get into it.
 
L
Daniel:
Below is source code that you can use in you VB program. Place code in a module file. Remember that process and setpoint are normalized 0 to 1.0.The code should be called from timer whose interval time is used to set deltaT. The algorithm
below does not have auto/manual switching. It does have integral windup protection and will turn off integration for times greater than 9999 secs.

Len Klochek
Seneca College
Toronto

'-------------------------------------------------------------------------------------------------------------------

Public Function PID_output(ByVal process!, ByVal setpoint!, ByVal Gain!, ByVal
Integ!, ByVal deriv!, ByVal deltaT)

Dim Er!,Derivative!,Proportional!

Static Olderror!, Cont!, Integral!
Static Limiter_Switch%
Limiter_Switch% = 1

Er = setpoint - process

If ((Cont >= 1 And Er > 0) Or (Cont <= 0 And Er < 0) Or (Integ >= 9999)) Then
Limiter_Switch = 0
Else
Limiter_Switch = 1
End If
Integral = Integral + Gain / Integ * Er * deltaT * Limiter_Switch

Derivative = Gain * deriv * (Er - Olderror) / deltaT

Proportional = Gain * Er

Cont = Proportional + Integral + Derivative
Olderror = Er

If (Cont > 1) Then
Cont = 1
End If
If (Cont < 0) Then
Cont = 0
End If
'------------------------------------------------------------------------------------------------------------------
 
L
The integral action in this implementation is incorrect. Integral limiting or anti reset windup should switch off the inegration whenever the controller output hits a limit (100% or 0%). In addition, this implementation doesn't allow integral action to reduce the controller output because it is limited to +ve values. The integral contribution must be allowed to go positive or negative.

Len Klochek
 
T
As I said, its a "really simple" independent gains implementation. Correctly tuned, it will work fine for single loop heating/cooling appliciations. And this implementation will reduce controller output when the error goes negative, even though the integral term itself does not ever go negative. . Look at it again. .I posted C code for a full featured PID here some very long time ago and was immediately innudated with permission requests about using it. Anyone asking how to program a PID needs first to understand the basics of it in an obvious manner, otherwise they wouldnt be asking. The simple psuedocode is a place to start. As I said, when he learns more he can add to the code.

>>Integral limiting or anti reset windup should switch off the inegration whenever the controller output hits a limit (100% or 0%).
---This is not the case for an independent gains controller. The total controller output can hit 100% for large errors long before the integral increases to a value sufficient to sustain control action, leading to unstable control.
 
Top