Skip to content

Commit 5f2c7a0

Browse files
committed
Implement controller direction as an enum
1 parent 516ffad commit 5f2c7a0

File tree

5 files changed

+79
-72
lines changed

5 files changed

+79
-72
lines changed

PID_v1.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@
1111
#include "WProgram.h"
1212
#endif
1313

14-
#include <PID_v1.h>
14+
#include "PID_v1.h"
1515

1616
/*Constructor (...)*********************************************************
1717
* The parameters specified here are those for for which we can't set up
1818
* reliable defaults, so we need to have the user set them.
1919
***************************************************************************/
2020
PID::PID(double *Input, double *Output, double *Setpoint, double Kp, double Ki,
21-
double Kd, int POn, int ControllerDirection) {
21+
double Kd, PIDProportionalOn POn,
22+
PIDControllerDirection ControllerDirection) {
2223
myOutput = Output;
2324
myInput = Input;
2425
mySetpoint = Setpoint;
25-
inAuto = false;
26+
mode = MANUAL;
2627

2728
PID::SetOutputLimits(0, 255); // default output limit corresponds to
2829
// the arduino pwm limits
@@ -41,7 +42,7 @@ PID::PID(double *Input, double *Output, double *Setpoint, double Kp, double Ki,
4142
***************************************************************************/
4243

4344
PID::PID(double *Input, double *Output, double *Setpoint, double Kp, double Ki,
44-
double Kd, int ControllerDirection)
45+
double Kd, PIDControllerDirection ControllerDirection)
4546
: PID::PID(Input, Output, Setpoint, Kp, Ki, Kd, P_ON_E,
4647
ControllerDirection) {}
4748

@@ -53,8 +54,10 @@ PID::PID(double *Input, double *Output, double *Setpoint, double Kp, double Ki,
5354
*computed, false when nothing has been done.
5455
**********************************************************************************/
5556
bool PID::Compute() {
56-
if (!inAuto)
57+
if (mode != AUTOMATIC) {
5758
return false;
59+
}
60+
5861
unsigned long now = millis();
5962
unsigned long timeChange = (now - lastTime);
6063
if (timeChange >= SampleTime) {
@@ -65,7 +68,7 @@ bool PID::Compute() {
6568
outputSum += (ki * error);
6669

6770
/*Add Proportional on Measurement, if P_ON_M is specified*/
68-
if (!pOnE)
71+
if (pOn == P_ON_M)
6972
outputSum -= kp * dInput;
7073

7174
if (outputSum > outMax)
@@ -75,7 +78,7 @@ bool PID::Compute() {
7578

7679
/*Add Proportional on Error, if P_ON_E is specified*/
7780
double output;
78-
if (pOnE)
81+
if (pOn == P_ON_E)
7982
output = kp * error;
8083
else
8184
output = 0;
@@ -102,12 +105,11 @@ bool PID::Compute() {
102105
* it's called automatically from the constructor, but tunings can also
103106
* be adjusted on the fly during normal operation
104107
******************************************************************************/
105-
void PID::SetTunings(double Kp, double Ki, double Kd, int POn) {
108+
void PID::SetTunings(double Kp, double Ki, double Kd, PIDProportionalOn POn) {
106109
if (Kp < 0 || Ki < 0 || Kd < 0)
107110
return;
108111

109112
pOn = POn;
110-
pOnE = POn == P_ON_E;
111113

112114
dispKp = Kp;
113115
dispKi = Ki;
@@ -158,7 +160,7 @@ void PID::SetOutputLimits(double Min, double Max) {
158160
outMin = Min;
159161
outMax = Max;
160162

161-
if (inAuto) {
163+
if (mode == AUTOMATIC) {
162164
if (*myOutput > outMax)
163165
*myOutput = outMax;
164166
else if (*myOutput < outMin)
@@ -176,12 +178,13 @@ void PID::SetOutputLimits(double Min, double Max) {
176178
* when the transition from manual to auto occurs, the controller is
177179
* automatically initialized
178180
******************************************************************************/
179-
void PID::SetMode(int Mode) {
180-
bool newAuto = (Mode == AUTOMATIC);
181-
if (newAuto && !inAuto) { /*we just went from manual to auto*/
181+
void PID::SetMode(PIDControllerMode Mode) {
182+
/*we just went from manual to auto*/
183+
if (Mode == AUTOMATIC && mode != AUTOMATIC) {
182184
PID::Initialize();
183185
}
184-
inAuto = newAuto;
186+
187+
mode = Mode;
185188
}
186189

187190
/* Initialize()****************************************************************
@@ -198,13 +201,13 @@ void PID::Initialize() {
198201
}
199202

200203
/* SetControllerDirection(...)*************************************************
201-
* The PID will either be connected to a DIRECT acting process (+Output leads
202-
* to +Input) or a REVERSE acting process(+Output leads to -Input.) we need to
203-
* know which one, because otherwise we may increase the output when we should
204-
* be decreasing. This is called from the constructor.
204+
* The PID will either be connected to a PIDControllerDirection::DIRECT acting
205+
*process (+Output leads to +Input) or a REVERSE acting process(+Output leads to
206+
*-Input.) we need to know which one, because otherwise we may increase the
207+
*output when we should be decreasing. This is called from the constructor.
205208
******************************************************************************/
206-
void PID::SetControllerDirection(int Direction) {
207-
if (inAuto && Direction != controllerDirection) {
209+
void PID::SetControllerDirection(PIDControllerDirection Direction) {
210+
if (mode == AUTOMATIC && Direction != controllerDirection) {
208211
kp = (0 - kp);
209212
ki = (0 - ki);
210213
kd = (0 - kd);
@@ -220,5 +223,5 @@ void PID::SetControllerDirection(int Direction) {
220223
double PID::GetKp() { return dispKp; }
221224
double PID::GetKi() { return dispKi; }
222225
double PID::GetKd() { return dispKd; }
223-
int PID::GetMode() { return inAuto ? AUTOMATIC : MANUAL; }
224-
int PID::GetDirection() { return controllerDirection; }
226+
PIDControllerMode PID::GetMode() { return mode; }
227+
PIDControllerDirection PID::GetDirection() { return controllerDirection; }

PID_v1.h

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,40 @@
22
#define PID_v1_h
33
#define LIBRARY_VERSION 1.2.1
44

5+
enum PIDControllerDirection { DIRECT, REVERSE };
6+
enum PIDControllerMode { MANUAL, AUTOMATIC };
7+
enum PIDProportionalOn { P_ON_M, P_ON_E };
8+
59
class PID {
610

711
public:
8-
// Constants used in some of the functions below
9-
#define AUTOMATIC 1
10-
#define MANUAL 0
11-
#define DIRECT 0
12-
#define REVERSE 1
13-
#define P_ON_M 0
14-
#define P_ON_E 1
15-
1612
// commonly used functions
1713
// **************************************************************************
1814
PID(double *, double *,
1915
double *, // * constructor. links the PID to the Input, Output, and
20-
double, double, double, int,
21-
int); // Setpoint. Initial tuning parameters are also set here.
22-
// (overload for specifying proportional mode)
16+
double, double, double, PIDProportionalOn,
17+
PIDControllerDirection); // Setpoint. Initial tuning parameters are
18+
// also set here. (overload for specifying
19+
// proportional mode)
2320

2421
PID(double *, double *,
2522
double *, // * constructor. links the PID to the Input, Output, and
2623
double, double, double,
27-
int); // Setpoint. Initial tuning parameters are also set here
24+
PIDControllerDirection); // Setpoint. Initial tuning parameters are
25+
// also set here
2826

29-
void SetMode(int Mode); // * sets PID to either Manual (0) or Auto (non-0)
27+
void SetMode(PIDControllerMode Mode); // * sets PID to either Manual (0) or Auto (non-0)
3028

31-
bool Compute(); // * performs the PID calculation. it should be
32-
// called every time loop() cycles. ON/OFF and
33-
// calculation frequency can be set using SetMode
34-
// SetSampleTime respectively
29+
// * performs the PID calculation. it should be
30+
// called every time loop() cycles. ON/OFF and
31+
// calculation frequency can be set using SetMode
32+
// SetSampleTime respectively
33+
bool Compute();
3534

36-
void SetOutputLimits(
37-
double,
38-
double); // * clamps the output to a specific range. 0-255 by default, but
39-
// it's likely the user will want to change this depending on
40-
// the application
35+
// * clamps the output to a specific range. 0-255 by default, but
36+
// it's likely the user will want to change this depending on
37+
// the application
38+
void SetOutputLimits(double, double);
4139

4240
// available but not commonly used functions
4341
// ********************************************************
@@ -46,24 +44,25 @@ class PID {
4644
double); // constructor, this function gives the user the option
4745
// of changing tunings during runtime for Adaptive control
4846
void SetTunings(double, double, // * overload for specifying proportional mode
49-
double, int);
47+
double, PIDProportionalOn);
5048

51-
void SetControllerDirection(
52-
int); // * Sets the Direction, or "Action" of the controller. DIRECT
53-
// means the output will increase when error is positive. REVERSE
54-
// means the opposite. it's very unlikely that this will be
55-
// needed once it is set in the constructor.
56-
void
57-
SetSampleTime(int); // * sets the frequency, in Milliseconds, with which
58-
// the PID calculation is performed. default is 100
49+
// * Sets the Direction, or "Action" of the controller. DIRECT
50+
// means the output will increase when error is positive. REVERSE
51+
// means the opposite. it's very unlikely that this will be
52+
// needed once it is set in the constructor.
53+
void SetControllerDirection(PIDControllerDirection);
54+
55+
// * sets the frequency, in Milliseconds, with which
56+
// the PID calculation is performed. default is 100
57+
void SetSampleTime(int);
5958

6059
// Display functions
6160
// ****************************************************************
62-
double GetKp(); // These functions query the pid for interal values.
63-
double GetKi(); // they were created mainly for the pid front-end,
64-
double GetKd(); // where it's important to know what is actually
65-
int GetMode(); // inside the PID.
66-
int GetDirection(); //
61+
double GetKp(); // These functions query the pid for interal values.
62+
double GetKi(); // they were created mainly for the pid front-end,
63+
double GetKd(); // where it's important to know what is actually
64+
PIDControllerMode GetMode(); // inside the PID.
65+
PIDControllerDirection GetDirection(); //
6766

6867
private:
6968
void Initialize();
@@ -76,20 +75,21 @@ class PID {
7675
double ki; // * (I)ntegral Tuning Parameter
7776
double kd; // * (D)erivative Tuning Parameter
7877

79-
int controllerDirection;
80-
int pOn;
78+
PIDControllerDirection controllerDirection;
79+
PIDProportionalOn pOn;
80+
PIDControllerMode mode;
8181

8282
double *myInput; // * Pointers to the Input, Output, and Setpoint variables
8383
double *myOutput; // This creates a hard link between the variables and the
84-
double
85-
*mySetpoint; // PID, freeing the user from having to constantly tell us
86-
// what these values are. with pointers we'll just know.
84+
85+
// PID, freeing the user from having to constantly tell us
86+
// what these values are. with pointers we'll just know.
87+
double *mySetpoint;
8788

8889
unsigned long lastTime;
8990
double outputSum, lastInput;
9091

9192
unsigned long SampleTime;
9293
double outMin, outMax;
93-
bool inAuto, pOnE;
9494
};
9595
#endif

examples/PID_Basic/PID_Basic.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Reading analog input 0 to control analog PWM output 3
44
********************************************************/
55

6-
#include <PID_v1.h>
6+
#include "PID_v1.h"
77

88
#define PIN_INPUT 0
99
#define PIN_OUTPUT 3
@@ -14,6 +14,7 @@ double Setpoint, Input, Output;
1414
// Specify the links and initial tuning parameters
1515
double Kp = 2, Ki = 5, Kd = 1;
1616
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);
17+
>>>>>>> 6b7c36a (Implement controller direction as an enum)
1718

1819
void setup() {
1920
// initialize the variables we're linked to

examples/PID_PonM/PID_PonM.ino

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66
* in certain processes like sous-vides.
77
********************************************************/
88

9-
#include <PID_v1.h>
9+
#include "PID_v1.h"
1010

1111
// Define Variables we'll be connecting to
1212
double Setpoint, Input, Output;
1313

1414
// Specify the links and initial tuning parameters
15-
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, P_ON_M,
16-
DIRECT); // P_ON_M specifies that Proportional on Measurement be used
17-
// P_ON_E (Proportional on Error) is the default behavior
15+
PID myPID(
16+
&Input, &Output, &Setpoint, 2, 5, 1, P_ON_M,
17+
PIDControllerDirection::DIRECT); // P_ON_M specifies that Proportional on
18+
// Measurement be used P_ON_E (Proportional
19+
// on Error) is the default behavior
1820

1921
void setup() {
2022
// initialize the variables we're linked to
2123
Input = analogRead(0);
2224
Setpoint = 100;
2325

2426
// turn the PID on
25-
myPID.SetMode(AUTOMATIC);
27+
myPID.SetMode(PIDControllerMode::AUTOMATIC);
2628
}
2729

2830
void loop() {

examples/PID_RelayOutput/PID_RelayOutput.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ 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,
28+
PIDControllerDirection::DIRECT);
2829

2930
int WindowSize = 5000;
3031
unsigned long windowStartTime;
@@ -39,7 +40,7 @@ void setup() {
3940
myPID.SetOutputLimits(0, WindowSize);
4041

4142
// turn the PID on
42-
myPID.SetMode(AUTOMATIC);
43+
myPID.SetMode(PIDControllerMode::AUTOMATIC);
4344
}
4445

4546
void loop() {

0 commit comments

Comments
 (0)