From cf853be5951900753eaf81ac2d5ceac21a71eb3c Mon Sep 17 00:00:00 2001 From: Dan Rice Date: Tue, 2 Sep 2014 13:54:33 -0400 Subject: [PATCH] Use enums instead of defines --- .../PID_AdaptiveTunings/PID_AdaptiveTunings.ino | 4 ++-- PID_v1/Examples/PID_Basic/PID_Basic.ino | 4 ++-- .../PID_RelayOutput/PID_RelayOutput.ino | 4 ++-- PID_v1/PID_v1.cpp | 8 ++++---- PID_v1/PID_v1.h | 17 ++++++++--------- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/PID_v1/Examples/PID_AdaptiveTunings/PID_AdaptiveTunings.ino b/PID_v1/Examples/PID_AdaptiveTunings/PID_AdaptiveTunings.ino index 450c4fa..271434e 100644 --- a/PID_v1/Examples/PID_AdaptiveTunings/PID_AdaptiveTunings.ino +++ b/PID_v1/Examples/PID_AdaptiveTunings/PID_AdaptiveTunings.ino @@ -19,7 +19,7 @@ double aggKp=4, aggKi=0.2, aggKd=1; double consKp=1, consKi=0.05, consKd=0.25; //Specify the links and initial tuning parameters -PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT); +PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, PID::DIRECT); void setup() { @@ -28,7 +28,7 @@ void setup() Setpoint = 100; //turn the PID on - myPID.SetMode(AUTOMATIC); + myPID.SetMode(PID::AUTOMATIC); } void loop() diff --git a/PID_v1/Examples/PID_Basic/PID_Basic.ino b/PID_v1/Examples/PID_Basic/PID_Basic.ino index ed44396..03e3ea6 100644 --- a/PID_v1/Examples/PID_Basic/PID_Basic.ino +++ b/PID_v1/Examples/PID_Basic/PID_Basic.ino @@ -9,7 +9,7 @@ double Setpoint, Input, Output; //Specify the links and initial tuning parameters -PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT); +PID myPID(&Input, &Output, &Setpoint,2,5,1, PID::DIRECT); void setup() { @@ -18,7 +18,7 @@ void setup() Setpoint = 100; //turn the PID on - myPID.SetMode(AUTOMATIC); + myPID.SetMode(PID::AUTOMATIC); } void loop() diff --git a/PID_v1/Examples/PID_RelayOutput/PID_RelayOutput.ino b/PID_v1/Examples/PID_RelayOutput/PID_RelayOutput.ino index ed67e03..97ffcd0 100644 --- a/PID_v1/Examples/PID_RelayOutput/PID_RelayOutput.ino +++ b/PID_v1/Examples/PID_RelayOutput/PID_RelayOutput.ino @@ -21,7 +21,7 @@ double Setpoint, Input, Output; //Specify the links and initial tuning parameters -PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT); +PID myPID(&Input, &Output, &Setpoint,2,5,1, PID::DIRECT); int WindowSize = 5000; unsigned long windowStartTime; @@ -36,7 +36,7 @@ void setup() myPID.SetOutputLimits(0, WindowSize); //turn the PID on - myPID.SetMode(AUTOMATIC); + myPID.SetMode(PID::AUTOMATIC); } void loop() diff --git a/PID_v1/PID_v1.cpp b/PID_v1/PID_v1.cpp index 6c95895..f021272 100644 --- a/PID_v1/PID_v1.cpp +++ b/PID_v1/PID_v1.cpp @@ -18,7 +18,7 @@ * reliable defaults, so we need to have the user set them. ***************************************************************************/ PID::PID(double* Input, double* Output, double* Setpoint, - double Kp, double Ki, double Kd, int ControllerDirection) + double Kp, double Ki, double Kd, direction_t ControllerDirection) { myOutput = Output; @@ -139,11 +139,11 @@ void PID::SetOutputLimits(double Min, double Max) } /* SetMode(...)**************************************************************** - * Allows the controller Mode to be set to manual (0) or Automatic (non-zero) + * Allows the controller Mode to be set to MANUAL (0) or AUTOMATIC (1) * when the transition from manual to auto occurs, the controller is * automatically initialized ******************************************************************************/ -void PID::SetMode(int Mode) +void PID::SetMode(mode_t Mode) { bool newAuto = (Mode == AUTOMATIC); if(newAuto == !inAuto) @@ -171,7 +171,7 @@ void PID::Initialize() * know which one, because otherwise we may increase the output when we should * be decreasing. This is called from the constructor. ******************************************************************************/ -void PID::SetControllerDirection(int Direction) +void PID::SetControllerDirection(direction_t Direction) { if(inAuto && Direction !=controllerDirection) { diff --git a/PID_v1/PID_v1.h b/PID_v1/PID_v1.h index 6f86697..ec5bbb9 100644 --- a/PID_v1/PID_v1.h +++ b/PID_v1/PID_v1.h @@ -8,17 +8,16 @@ class PID public: - //Constants used in some of the functions below - #define AUTOMATIC 1 - #define MANUAL 0 - #define DIRECT 0 - #define REVERSE 1 + //Parameter types for some of the functions below + enum mode_t { AUTOMATIC = 1, MANUAL = 0 }; + enum direction_t { DIRECT = 0, REVERSE = 1 }; //commonly used functions ************************************************************************** PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and - double, double, double, int); // Setpoint. Initial tuning parameters are also set here + double, double, double, // Setpoint. Initial tuning parameters are also set here + direction_t); - void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0) + void SetMode(mode_t); // * sets PID to either MANUAL (0) or AUTOMATIC (1) bool Compute(); // * performs the PID calculation. it should be // called every time loop() cycles. ON/OFF and @@ -35,8 +34,8 @@ class PID void SetTunings(double, double, // * While most users will set the tunings once in the double); // constructor, this function gives the user the option // of changing tunings during runtime for Adaptive control - void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT - // means the output will increase when error is positive. REVERSE + void SetControllerDirection( // * Sets the Direction, or "Action" of the controller. DIRECT + direction_t); // means the output will increase when error is positive. REVERSE // means the opposite. it's very unlikely that this will be needed // once it is set in the constructor. void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which