From d4ce7050a58b80f619f7c58390c07e78961ffa0c Mon Sep 17 00:00:00 2001 From: Brett Beauregard Date: Thu, 4 Oct 2012 20:47:12 -0400 Subject: [PATCH] fixed issue #3 and issue #4 -Changed Compute() to a bool from a void, allowing the calling function to know when the pid calculation has just been evaluated -cleaned up the initialization code in the constructor --- PID_v1/PID_v1.cpp | 20 ++++++++++++-------- PID_v1/PID_v1.h | 2 +- README.txt | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/PID_v1/PID_v1.cpp b/PID_v1/PID_v1.cpp index a5a76ae..6c95895 100644 --- a/PID_v1/PID_v1.cpp +++ b/PID_v1/PID_v1.cpp @@ -20,6 +20,12 @@ PID::PID(double* Input, double* Output, double* Setpoint, double Kp, double Ki, double Kd, int ControllerDirection) { + + myOutput = Output; + myInput = Input; + mySetpoint = Setpoint; + inAuto = false; + PID::SetOutputLimits(0, 255); //default output limit corresponds to //the arduino pwm limits @@ -29,22 +35,18 @@ PID::PID(double* Input, double* Output, double* Setpoint, PID::SetTunings(Kp, Ki, Kd); lastTime = millis()-SampleTime; - inAuto = false; - myOutput = Output; - myInput = Input; - mySetpoint = Setpoint; - } /* Compute() ********************************************************************** * This, as they say, is where the magic happens. this function should be called * every time "void loop()" executes. the function will decide for itself whether a new - * pid Output needs to be computed + * pid Output needs to be computed. returns true when the output is computed, + * false when nothing has been done. **********************************************************************************/ -void PID::Compute() +bool PID::Compute() { - if(!inAuto) return; + if(!inAuto) return false; unsigned long now = millis(); unsigned long timeChange = (now - lastTime); if(timeChange>=SampleTime) @@ -67,7 +69,9 @@ void PID::Compute() /*Remember some variables for next time*/ lastInput = input; lastTime = now; + return true; } + else return false; } diff --git a/PID_v1/PID_v1.h b/PID_v1/PID_v1.h index ef3ea4d..6f86697 100644 --- a/PID_v1/PID_v1.h +++ b/PID_v1/PID_v1.h @@ -20,7 +20,7 @@ class PID void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0) - void Compute(); // * performs the PID calculation. it should be + bool Compute(); // * performs the PID calculation. it should be // called every time loop() cycles. ON/OFF and // calculation frequency can be set using SetMode // SetSampleTime respectively diff --git a/README.txt b/README.txt index bf3b01a..e36aedd 100644 --- a/README.txt +++ b/README.txt @@ -1,5 +1,5 @@ *************************************************************** -* Arduino PID Library - Version 1.0.1 +* Arduino PID Library - Version 1.1.0 * by Brett Beauregard brettbeauregard.com