# Conceptual doubt about PID algorithm in PLC

I have to use the PID algorithm that I describe below, obtained from a PLC in the company where I work, so I want to understand it. This is used for control a valve that opens to let gas pass and reach a certain pressure (SetPoint).

There is a Timer with a preset value of "Tsample", with an output "Tsample.Q" in low level, and it become high for an scan cycle each a time "Tsample".

I have a variable that store current error value ($$e_n$$), other for the error a time Tsample before ($$e_{n-1}$$) and other to store the error 2*Tsample before ($$e_{n-2}$$). This and the next parts of algorithm are executed once every time equal to Tsample: $$e_{n-2}=e_{n-1}$$ $$e_{n-1}=e_n$$ $$e_n=SetPoint-ProcessValue$$

Aditionally, I have a variable that store the current change in the error respect the last , and the same Tsample before.

$$P_n=e_n-e_{n-1}$$ $$P_{n-1}=e_{n-1}-e_{n-2}$$

Finally a variable that store the variation of the change of the error:

$$Q_{n}=P_n-P_{n-1}=(e_n-e_{n-1})-(e_{n-1}-e_{n-2})$$

And the calculation of control signal, where $$dP_n$$ is the proportional part, $$dI_n$$ is the integral part, $$dD_n$$ is derivative part, $$K_p$$ is global gain, $$R_p$$ is proportional gain, $$T_i$$ integral time, $$T_d$$ derivative time, and $$dU_n$$ is the PID output:

$$dP_n=R_p*P_n$$ $$dI_n=T_i*e_n$$ $$dD_{n-1}=dD_{n}$$ $$dD_n=(Q_{n}*T_d+dD_{n-1})/2$$ $$dU_n=K_p*(dP_n+dI_n+dD_n)$$

After that, it calculate the direction of the correction, where PC (positive correction) is a variable with TRUE value if $$dU_n>0$$ and FALSE value if $$dU_n<0$$ and NC (negative correction) is TRUE if $$dU_n<0$$ and FALSE if $$dU_n>0$$.

After this PID algorithm is executed, the correction is made. If $$U_n$$ is current valve opening percentage and the PID output $$dU_n$$ is the change of the valve opening percentage; once every time equal to Tsample, the actual value of valve aperture is incremented or decremented:

If PC=TRUE then $$U_n$$=$$U_{n-1}$$+$$dU_n$$

If NC=TRUE then $$U_n$$=$$U_{n-1}$$-$$dU_n$$

I don't understand the reason to work with actuar error on integral control, change in the error ($$P_n$$) in proportional control, and change of the change of the error ($$Q_n$$) in the derivative control.

It is suposse that I shoud to take the integral of error to integral contror, for example, adding to the accumulated action the area of ​​a rectangle with Tsampler width and average height of the previous and current error: $$I_n=I_{n-1}+ Ki * [ Tsampler*(e_{n-1}+e_{n})/2 ]$$ Current error $$e_n$$ to proportional, and finally an aproximation of derivate of error to derivative part, for example: $$D_n=K_d * (e_{n}-e_{n-1})/Tsampler$$ where Ki and Kd are representative.

• Are you saying this is code that you have written, or is it code that you got, and you're trying to understand it? Please edit your question (probably in the first paragraph) to clear up this point. Commented Nov 16, 2022 at 19:13
• You do not say how PC or NC are calculated. Are these the sign of $dU_n$, or is the operation $U_n = U_n + dU_n$ linear in the sense that the valve closes for $dU_n < 0$ and opens for $dU_n > 0$? Commented Nov 16, 2022 at 19:16

The step where $$U_n = U_{n-1} \pm dU_n \tag 1$$ is a numerical integration - it is causing $$U_n = \sum_{k = -\infty}^n dU_k$$.

(I'm assuming you really meant the update was from $$U_{n-1}$$ to $$U_n$$; I'm further assuming that the dependence of the sign of the summation is settable in the program you're figuring out).

So what the author of that program has done is to formulate a more or less ordinary PID controller, then take the numerical derivative of every step.

This has a minor pitfall, in that if your integrator gain ($$T_i$$) is zero, then your controller has a pole-zero cancellation (if you're unfamiliar with that term then search for it). But as long as $$T_i$$ is nonzero, then the end-to-end behavior of the system will look just like a PID controller.

• Thanks, this answer clears things up. My doubt now is, what is the adventage of use this "derivate" PID?. On the other hand, with the pole-zero cancellation, do you refer to the cancelation of origin pole, which without it there would be a steady state error? Commented Nov 24, 2022 at 13:17
• Stackexchange is different from most other forums, in that it wants all the interesting bits to be in the questions and answers, with the comments just there for clarification. It looks like what you've written above is an excellent candidate for another question. Keep in mind that you can always refer back to this one for more detail. At the least, if you don't want to ask a separate question you should edit this one. Commented Nov 24, 2022 at 15:29