From 9e7662e9eacc7aac26660b1382813a6384d85bc0 Mon Sep 17 00:00:00 2001 From: Frank Paynter Date: Sat, 15 May 2021 15:11:01 -0400 Subject: [PATCH 1/2] modified SetSampleTime() to set the internal SampleTime variable to zero for an input argument of zero, without modifying ki or kd. This forces Compute() to generate a new output value each time it is called, and allows the PID engine to be synchronized with an external timing source --- PID_v1.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/PID_v1.cpp b/PID_v1.cpp index cb6637c..f6bfb80 100644 --- a/PID_v1.cpp +++ b/PID_v1.cpp @@ -130,6 +130,8 @@ void PID::SetTunings(double Kp, double Ki, double Kd){ /* SetSampleTime(...) ********************************************************* * sets the period, in Milliseconds, at which the calculation is performed + * 15 May 2021 gfp - a value of zero will force Compute() to generate a new output every time it is called + * but avoids the 'divide-by-zero' problem ******************************************************************************/ void PID::SetSampleTime(int NewSampleTime) { @@ -139,7 +141,11 @@ void PID::SetSampleTime(int NewSampleTime) / (double)SampleTime; ki *= ratio; kd /= ratio; - SampleTime = (unsigned long)NewSampleTime; + //SampleTime = (unsigned long)NewSampleTime; + } + else if (NewSampleTime == 0) + { + SampleTime = (unsigned long)NewSampleTime; } } From a30df926fd1e041cc9059e94f0f9a1cba1acf9f5 Mon Sep 17 00:00:00 2001 From: Frank Paynter Date: Sat, 15 May 2021 15:32:31 -0400 Subject: [PATCH 2/2] Updated README.txt to include information about synching PID engine to external timing sources --- README.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.txt b/README.txt index 3f2fb63..ab13b04 100644 --- a/README.txt +++ b/README.txt @@ -9,3 +9,7 @@ http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/ - For function documentation see: http://playground.arduino.cc/Code/PIDLibrary + + - To synchronize PID::Compute with an external timing source such as a TIMER ISR, + call SetSampleTime() with an argument of zero. This will force Compute to generate a + new output value each time it is called.