m-lundberg / simple-pid

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

I was wondering if the derivative gain should be negative #62

Closed erickmartinez closed 2 years ago

erickmartinez commented 2 years ago

Hi guys,

First of all, great job putting this together. I was wondering though, why in line 127:

https://github.com/m-lundberg/simple-pid/blob/7cd080473e582e4de29b49f9d002d2bd62ef33ca/simple_pid/pid.py#L127

you set the derivative to negative. Is this intended? If so, why? The code you reference:

http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/

does not do this:

double dErr = (error - lastErr) / timeChange; /*Compute PID Output*/ Output = kp * error + ki * errSum + kd * dErr;

Maybe I am wrong, but I would much appreciate it if you could took a look at it. I am currently implementing it with a power supply and a temperature readout. I was trying to tune the kp, ki and kd using the Ziegler-Nichols method and I noticed that setting kd != 0 introduces some steady overshoot.

erickmartinez commented 2 years ago

It looks like this has to do with the fact that you are using d_input and not d_error:

https://github.com/m-lundberg/simple-pid/blob/7cd080473e582e4de29b49f9d002d2bd62ef33ca/simple_pid/pid.py#L108-L109

so, effectively, d_err = -d_input.

I guess this answers my question and closes the issue.