m-lundberg / simple-pid

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

non-monotonic time issue #1

Closed deniz195 closed 5 years ago

deniz195 commented 5 years ago

Hi martin,

i'm using a similar PID implementation on a raspberry pi in a production environement (PID code is also based Brett Beauregard's code at ).

Turns out that both time.time() and datetime.datetime.now() are non-monotonic time signals. I.e. if a system service or the user turns the time back, the PID will see negative time differences. This happens on many systems (in particular the raspberry pi) regularly which has given me rare, but quite erratic behavior of the machine.

Fixed the problem using time.monotonic() which is available since python 3.3. Maybe this is helpful to your code too.

Best, Deniz

m-lundberg commented 5 years ago

Hello,

Thanks for the feedback!

This sounds like a tricky bug to track down if you are unaware of this. I will definitely look into using time.monotonic() instead when I get the chance. It might save me or someone else a headache in the future.

m-lundberg commented 5 years ago

I changed to using time.monotonic() for calculating time instead as you suggested if the python version is > 3.3. If time.monotonic() is not available it will fall back to using time.time() instead, with a warning to the user.

Thank you again for the feedback @deniz195!