Differentiation and integration?

eeng

Member
Join Date
Sep 2016
Location
New York
Posts
21
Hi guys,

As the title suggests, I'm wondering how you would implement differentiation and integration with respect to time. Basically, I want to design a PID controller from scratch for the hell of it. Using unity pro XL, V10. Just wondering if anyone has any experience with this?

Thanks in advance
 
I used to write my own, years ago, because the pre-packaged blocks were slow to execute but these days the built in variety perform and integrate better but sometimes they can be overkill if you only need a simple PI controller. Do you really need D?

Error = Setpoint - Feedback
P = Error * KP
I = Last I + (Error * KI) (assumes a constant execution rate!)
D = ((This Error - Last Error)/(This Time - Last Time)) * KD
Out = P+I+D

I'm sure others will have much to say...

Nick
 
Its quite a slow process, so the D component helps. Thanks for that! While I understand what I need to do mathematically..my confusion is more with how do I grab the value of "this time" - "last time".
 
To trigger the execution of your PID function you will most likely do that with a timer of some sort, the preset of that timer equals (this time - last time).
 
Another thing that you can do for "I" is to trigger the time sample using a PLC interrupt. You'll have better time consistency than with a timer.
 
Error = Setpoint - Feedback
P = Error * KP
I = Last I + (Error * KI) (assumes a constant execution rate!)
D = ((This Error - Last Error)/(This Time - Last Time)) * KD
Out = P+I+D

I'm sure others will have much to say...

Nick
The integrator must be multiplied by the time between samples to keep the units straight.

I = Last I + (Error * KI*(This Time - Last Time)))

If you are cleaver and the sample time is consistent you can simplify the PID down to 3 multiply and adds once you combine some constants.

What is missing is limiting the output to +/- 100% and there is no anti integrator windup code.
 
The integrator must be multiplied by the time between samples to keep the units straight.

I = Last I + (Error * KI*(This Time - Last Time)))

Peter is, as always, absolutely correct and that is why I included the qualifying remark about the constant execution rate.

I always run PID loops on a timed interrupt and, as Peter suggests, that does simplify the maths.

What I originally posted was not intended as complete code and lacks many desirable features that you may wish to consider: Preset value? Enable signal? Integral hold? Integral disable? Derivative disable? Output limit? Feed forward?

Nick
 
D = ((This Error - Last Error)/(This Time - Last Time)) * KD

I've seen instances in which the D term is based on the Process Variable (PV) or "feedback" as Manglemender put it, rather than the error. I believe the reason for this is take changes in set point out of the equation.

I believe that output limiting and anti-windup like Peter mentioned are a must!
 
Thank you all for the replies. Still confused on that "this time" and "last time" value. Is that the actual time stamp at which the reading occurs or? If so, how do I get that value?

I guess that's my biggest problem. I'm not sure how to go about the time aspect.

I wasn't too sure what Mareka meant with his timer example up above.
 
you might be interested in the following papers on my website ...

What Is P in PID?
What Is I in PID?
What Is D in PID?

these break down the math into the step-by-step calculations used by the Allen-Bradley PID instructions ...

just click the link in my signature - and then go to the menu selection for "Sample Lessons and Videos" ... scroll down to find the links to these papers ...
 
Last edited:
I've seen instances in which the D term is based on the Process Variable (PV) or "feedback" as Manglemender put it, rather than the error. I believe the reason for this is take changes in set point out of the equation.
When the D AND P gain act on the changes in PV THEN changes in the set point don't cause a jump in the output. If the P gain acts on the error then the output will still jump when the set point makes a jump.

The PID gains add zeros to the closed loop transfer function. Zeros extend bandwidth but will also result in over shoot. A PID add 2 closed loop zeros. A PI adds one closed loop zero. When the D gain acts only on the changes in the PV I call that a PI-D. The -D because the D gain is only acting on the negative feedback from the PV. Since the PI-D only has the P and I gains in the forward path there is only one zero in the closed loop transfer function. An I-PD controller has NO zeros. The bandwidth is reduced but the PV will not over shoot.
I should make a video about this on my YouTube channel.

It should be obvious that there are many variations of PID. They all have their advantages and disadvantages. Our motion controller supports I-PD control because it has special properties like NOT overshooting. Sometime speed isn't as important as precision.

I could teach a class on this but this is not the right forum.
Monitor my YouTube channel, Peter Ponders PID, if you want to geek out. Everything can be calculated. Control can be perfect EVEN if the system in non-linear. One just needs the right techniques.

I recommend people understand what Ron has to say before tackling my mind bend stuff.
 
Thank you Ron! The content looks promising.

Peter, I wish I stumbled upon your channel earlier. Would have helped me immensely on my thesis haha.
 
I've seen instances in which the D term is based on the Process Variable (PV) or "feedback" as Manglemender put it, rather than the error. I believe the reason for this is take changes in set point out of the equation.

Taking the D part from PV only assumes that you are not using feed forward (and you should!). If you were using feed forward then the CV and PV (setpoint and feedback) might both change while the error remains zero. If D were derived from PV then an erroneous correction would result.

Nick
 

Similar Topics

Hi I am looking for kind of sensors that can measure the distance of the open box (see the attachment for details). Is there anybody with...
Replies
4
Views
1,508
Hello I want to calculate the differentiation of an analogue value in time. Simply the variable is x an I want to calculate d(x) / dt. What is...
Replies
14
Views
4,558
Hi, I am working on a machine tending application. Fanuc Robot with IR VIsion and Beckhoff PLC. I am trying to find any documentation on how to...
Replies
1
Views
100
I want to program an Exor HMI using a Rockwell controller. However, when exporting the tag list from the controller and importing it into JMobile...
Replies
0
Views
120
Hi all, I have a noob question regarding data handling from sensors. I understand configurations and I/O mapping sensor input/output variables...
Replies
2
Views
220
Back
Top Bottom