Skip to content

Commit cf853be

Browse files
committed
Use enums instead of defines
1 parent d21d7e3 commit cf853be

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

PID_v1/Examples/PID_AdaptiveTunings/PID_AdaptiveTunings.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ double aggKp=4, aggKi=0.2, aggKd=1;
1919
double consKp=1, consKi=0.05, consKd=0.25;
2020

2121
//Specify the links and initial tuning parameters
22-
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, DIRECT);
22+
PID myPID(&Input, &Output, &Setpoint, consKp, consKi, consKd, PID::DIRECT);
2323

2424
void setup()
2525
{
@@ -28,7 +28,7 @@ void setup()
2828
Setpoint = 100;
2929

3030
//turn the PID on
31-
myPID.SetMode(AUTOMATIC);
31+
myPID.SetMode(PID::AUTOMATIC);
3232
}
3333

3434
void loop()

PID_v1/Examples/PID_Basic/PID_Basic.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
double Setpoint, Input, Output;
1010

1111
//Specify the links and initial tuning parameters
12-
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
12+
PID myPID(&Input, &Output, &Setpoint,2,5,1, PID::DIRECT);
1313

1414
void setup()
1515
{
@@ -18,7 +18,7 @@ void setup()
1818
Setpoint = 100;
1919

2020
//turn the PID on
21-
myPID.SetMode(AUTOMATIC);
21+
myPID.SetMode(PID::AUTOMATIC);
2222
}
2323

2424
void loop()

PID_v1/Examples/PID_RelayOutput/PID_RelayOutput.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
double Setpoint, Input, Output;
2222

2323
//Specify the links and initial tuning parameters
24-
PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);
24+
PID myPID(&Input, &Output, &Setpoint,2,5,1, PID::DIRECT);
2525

2626
int WindowSize = 5000;
2727
unsigned long windowStartTime;
@@ -36,7 +36,7 @@ void setup()
3636
myPID.SetOutputLimits(0, WindowSize);
3737

3838
//turn the PID on
39-
myPID.SetMode(AUTOMATIC);
39+
myPID.SetMode(PID::AUTOMATIC);
4040
}
4141

4242
void loop()

PID_v1/PID_v1.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* reliable defaults, so we need to have the user set them.
1919
***************************************************************************/
2020
PID::PID(double* Input, double* Output, double* Setpoint,
21-
double Kp, double Ki, double Kd, int ControllerDirection)
21+
double Kp, double Ki, double Kd, direction_t ControllerDirection)
2222
{
2323

2424
myOutput = Output;
@@ -139,11 +139,11 @@ void PID::SetOutputLimits(double Min, double Max)
139139
}
140140

141141
/* SetMode(...)****************************************************************
142-
* Allows the controller Mode to be set to manual (0) or Automatic (non-zero)
142+
* Allows the controller Mode to be set to MANUAL (0) or AUTOMATIC (1)
143143
* when the transition from manual to auto occurs, the controller is
144144
* automatically initialized
145145
******************************************************************************/
146-
void PID::SetMode(int Mode)
146+
void PID::SetMode(mode_t Mode)
147147
{
148148
bool newAuto = (Mode == AUTOMATIC);
149149
if(newAuto == !inAuto)
@@ -171,7 +171,7 @@ void PID::Initialize()
171171
* know which one, because otherwise we may increase the output when we should
172172
* be decreasing. This is called from the constructor.
173173
******************************************************************************/
174-
void PID::SetControllerDirection(int Direction)
174+
void PID::SetControllerDirection(direction_t Direction)
175175
{
176176
if(inAuto && Direction !=controllerDirection)
177177
{

PID_v1/PID_v1.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ class PID
88

99
public:
1010

11-
//Constants used in some of the functions below
12-
#define AUTOMATIC 1
13-
#define MANUAL 0
14-
#define DIRECT 0
15-
#define REVERSE 1
11+
//Parameter types for some of the functions below
12+
enum mode_t { AUTOMATIC = 1, MANUAL = 0 };
13+
enum direction_t { DIRECT = 0, REVERSE = 1 };
1614

1715
//commonly used functions **************************************************************************
1816
PID(double*, double*, double*, // * constructor. links the PID to the Input, Output, and
19-
double, double, double, int); // Setpoint. Initial tuning parameters are also set here
17+
double, double, double, // Setpoint. Initial tuning parameters are also set here
18+
direction_t);
2019

21-
void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0)
20+
void SetMode(mode_t); // * sets PID to either MANUAL (0) or AUTOMATIC (1)
2221

2322
bool Compute(); // * performs the PID calculation. it should be
2423
// called every time loop() cycles. ON/OFF and
@@ -35,8 +34,8 @@ class PID
3534
void SetTunings(double, double, // * While most users will set the tunings once in the
3635
double); // constructor, this function gives the user the option
3736
// of changing tunings during runtime for Adaptive control
38-
void SetControllerDirection(int); // * Sets the Direction, or "Action" of the controller. DIRECT
39-
// means the output will increase when error is positive. REVERSE
37+
void SetControllerDirection( // * Sets the Direction, or "Action" of the controller. DIRECT
38+
direction_t); // means the output will increase when error is positive. REVERSE
4039
// means the opposite. it's very unlikely that this will be needed
4140
// once it is set in the constructor.
4241
void SetSampleTime(int); // * sets the frequency, in Milliseconds, with which

0 commit comments

Comments
 (0)