m-lundberg / simple-pid

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

Allow to specify last_output at PID creation #43

Closed Steinarr134 closed 1 year ago

Steinarr134 commented 3 years ago

The first output from the PID will always be zero, I don't know if this is by design or a just how the math turned out but it is a bit annoying to turn on the PID, system is at the setpoint yet the pid throws in a zero

I found a way around it by doing:

pid.set_auto_mode(False) pid.set_auto_mode(True, last_output=last_output_guess)

it's a hacky way of getting what I want but since I can read the current settings from my equipment and from there calculate what the last output from the pid would have been, I don't want the pid to output a 0 and send the system into a frenzy for no reason.

All this to say, I propose allowing optional keyword last_output in the PID.__init__ method.

edit: I've gotten the behavior I wanted by including optional keyword last_output and adding the following lines to the end of the init method:

if not last_output is None: 
    self._integral = _clamp(last_output, self.output_limits).
m-lundberg commented 1 year ago

Hi!

I've now implemented a starting_output argument when creating a PID object that sets the _integral much like you said. If you're still interested in this, any feedback on if this works for you would be greatly appreciated!