11
11
#include " WProgram.h"
12
12
#endif
13
13
14
- #include < PID_v1.h>
14
+ #include " PID_v1.h"
15
15
16
16
/* Constructor (...)*********************************************************
17
17
* The parameters specified here are those for for which we can't set up
18
18
* reliable defaults, so we need to have the user set them.
19
19
***************************************************************************/
20
20
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) {
22
23
myOutput = Output;
23
24
myInput = Input;
24
25
mySetpoint = Setpoint;
25
- inAuto = false ;
26
+ mode = MANUAL ;
26
27
27
28
PID::SetOutputLimits (0 , 255 ); // default output limit corresponds to
28
29
// the arduino pwm limits
@@ -41,7 +42,7 @@ PID::PID(double *Input, double *Output, double *Setpoint, double Kp, double Ki,
41
42
***************************************************************************/
42
43
43
44
PID::PID (double *Input, double *Output, double *Setpoint, double Kp, double Ki,
44
- double Kd, int ControllerDirection)
45
+ double Kd, PIDControllerDirection ControllerDirection)
45
46
: PID::PID(Input, Output, Setpoint, Kp, Ki, Kd, P_ON_E,
46
47
ControllerDirection) {}
47
48
@@ -53,8 +54,10 @@ PID::PID(double *Input, double *Output, double *Setpoint, double Kp, double Ki,
53
54
*computed, false when nothing has been done.
54
55
**********************************************************************************/
55
56
bool PID::Compute () {
56
- if (!inAuto)
57
+ if (mode != AUTOMATIC) {
57
58
return false ;
59
+ }
60
+
58
61
unsigned long now = millis ();
59
62
unsigned long timeChange = (now - lastTime);
60
63
if (timeChange >= SampleTime) {
@@ -65,7 +68,7 @@ bool PID::Compute() {
65
68
outputSum += (ki * error);
66
69
67
70
/* Add Proportional on Measurement, if P_ON_M is specified*/
68
- if (!pOnE )
71
+ if (pOn == P_ON_M )
69
72
outputSum -= kp * dInput;
70
73
71
74
if (outputSum > outMax)
@@ -75,7 +78,7 @@ bool PID::Compute() {
75
78
76
79
/* Add Proportional on Error, if P_ON_E is specified*/
77
80
double output;
78
- if (pOnE )
81
+ if (pOn == P_ON_E )
79
82
output = kp * error;
80
83
else
81
84
output = 0 ;
@@ -102,12 +105,11 @@ bool PID::Compute() {
102
105
* it's called automatically from the constructor, but tunings can also
103
106
* be adjusted on the fly during normal operation
104
107
******************************************************************************/
105
- void PID::SetTunings (double Kp, double Ki, double Kd, int POn) {
108
+ void PID::SetTunings (double Kp, double Ki, double Kd, PIDProportionalOn POn) {
106
109
if (Kp < 0 || Ki < 0 || Kd < 0 )
107
110
return ;
108
111
109
112
pOn = POn;
110
- pOnE = POn == P_ON_E;
111
113
112
114
dispKp = Kp;
113
115
dispKi = Ki;
@@ -158,7 +160,7 @@ void PID::SetOutputLimits(double Min, double Max) {
158
160
outMin = Min;
159
161
outMax = Max;
160
162
161
- if (inAuto ) {
163
+ if (mode == AUTOMATIC ) {
162
164
if (*myOutput > outMax)
163
165
*myOutput = outMax;
164
166
else if (*myOutput < outMin)
@@ -176,12 +178,13 @@ void PID::SetOutputLimits(double Min, double Max) {
176
178
* when the transition from manual to auto occurs, the controller is
177
179
* automatically initialized
178
180
******************************************************************************/
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 ) {
182
184
PID::Initialize ();
183
185
}
184
- inAuto = newAuto;
186
+
187
+ mode = Mode;
185
188
}
186
189
187
190
/* Initialize()****************************************************************
@@ -198,13 +201,13 @@ void PID::Initialize() {
198
201
}
199
202
200
203
/* 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.
205
208
******************************************************************************/
206
- void PID::SetControllerDirection (int Direction) {
207
- if (inAuto && Direction != controllerDirection) {
209
+ void PID::SetControllerDirection (PIDControllerDirection Direction) {
210
+ if (mode == AUTOMATIC && Direction != controllerDirection) {
208
211
kp = (0 - kp);
209
212
ki = (0 - ki);
210
213
kd = (0 - kd);
@@ -220,5 +223,5 @@ void PID::SetControllerDirection(int Direction) {
220
223
double PID::GetKp () { return dispKp; }
221
224
double PID::GetKi () { return dispKi; }
222
225
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; }
0 commit comments