Skip to content

Commit be0c8e1

Browse files
committed
Use enums instead of defines
1 parent fb095d8 commit be0c8e1

File tree

5 files changed

+18
-19
lines changed

5 files changed

+18
-19
lines changed

PID_v1.cpp

+4-4
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.h

+8-9
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

examples/PID_AdaptiveTunings/PID_AdaptiveTunings.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ double aggKp=4, aggKi=0.2, aggKd=1;
2222
double consKp=1, consKi=0.05, consKd=0.25;
2323

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

2727
void setup()
2828
{
@@ -31,7 +31,7 @@ void setup()
3131
Setpoint = 100;
3232

3333
//turn the PID on
34-
myPID.SetMode(AUTOMATIC);
34+
myPID.SetMode(PID::AUTOMATIC);
3535
}
3636

3737
void loop()

examples/PID_Basic/PID_Basic.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ double Setpoint, Input, Output;
1313

1414
//Specify the links and initial tuning parameters
1515
double Kp=2, Ki=5, Kd=1;
16-
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
16+
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, PID::DIRECT);
1717

1818
void setup()
1919
{
@@ -22,7 +22,7 @@ void setup()
2222
Setpoint = 100;
2323

2424
//turn the PID on
25-
myPID.SetMode(AUTOMATIC);
25+
myPID.SetMode(PID::AUTOMATIC);
2626
}
2727

2828
void loop()

examples/PID_RelayOutput/PID_RelayOutput.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ double Setpoint, Input, Output;
2424

2525
//Specify the links and initial tuning parameters
2626
double Kp=2, Ki=5, Kd=1;
27-
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
27+
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, PID::DIRECT);
2828

2929
int WindowSize = 5000;
3030
unsigned long windowStartTime;
@@ -40,7 +40,7 @@ void setup()
4040
myPID.SetOutputLimits(0, WindowSize);
4141

4242
//turn the PID on
43-
myPID.SetMode(AUTOMATIC);
43+
myPID.SetMode(PID::AUTOMATIC);
4444
}
4545

4646
void loop()

0 commit comments

Comments
 (0)