m-lundberg / simple-pid

A simple and easy to use PID controller in Python
MIT License
799 stars 217 forks source link

Tracking and output of PID terms to PV #2

Closed G00364778 closed 5 years ago

G00364778 commented 5 years ago

I would like to track to individual contributions of the PID terms to the PV output in order to facilitate manual tuning of hard to tune loops. Can this be facilitated in the current implementation?

m-lundberg commented 5 years ago

Hello!

This is not exactly supported at the moment but as a workaround you can get the P and I terms by doing something like this in your loop, after calling the PID update:

p, i = pid._proportional, pid._error_sum

Unfortunately you can't get the D term as easily since it's not stored. You should be able to calculate it like this though:

d = -(pid.Kd * (PV - pid._last_input))

(written off the top of my head so I might have made a mistake)

I might add a more convenient way of doing this though as it seems like a useful feature to have.

m-lundberg commented 5 years ago

I added a components-property to the PID controller to do this, it can be used like this:

while controlling:
    cv = pid(pv)
    p, i, d = pid.components  # the separate terms are now in p, i, d

You can install the latest version and try it out. Let me know what you think!

G00364778 commented 5 years ago

Thanks for the quick update and support on the product.