6
6
**********************************************************************************************/
7
7
8
8
#if ARDUINO >= 100
9
- #include " Arduino.h"
9
+ #include " Arduino.h"
10
10
#else
11
- #include " WProgram.h"
11
+ #include " WProgram.h"
12
12
#endif
13
13
14
14
#include < PID_v1.h>
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
- PID::PID (double * Input, double * Output, double * Setpoint,
21
- double Kp, double Ki, double Kd, int POn, int ControllerDirection)
22
- {
23
- myOutput = Output;
24
- myInput = Input;
25
- mySetpoint = Setpoint;
26
- inAuto = false ;
20
+ PID::PID (double *Input, double *Output, double *Setpoint, double Kp, double Ki,
21
+ double Kd, int POn, int ControllerDirection) {
22
+ myOutput = Output;
23
+ myInput = Input;
24
+ mySetpoint = Setpoint;
25
+ inAuto = false ;
27
26
28
- PID::SetOutputLimits (0 , 255 ); // default output limit corresponds to
29
- // the arduino pwm limits
27
+ PID::SetOutputLimits (0 , 255 ); // default output limit corresponds to
28
+ // the arduino pwm limits
30
29
31
- SampleTime = 100 ; // default Controller Sample Time is 0.1 seconds
30
+ SampleTime = 100 ; // default Controller Sample Time is 0.1 seconds
32
31
33
- PID::SetControllerDirection (ControllerDirection);
34
- PID::SetTunings (Kp, Ki, Kd, POn);
32
+ PID::SetControllerDirection (ControllerDirection);
33
+ PID::SetTunings (Kp, Ki, Kd, POn);
35
34
36
- lastTime = millis ()- SampleTime;
35
+ lastTime = millis () - SampleTime;
37
36
}
38
37
39
38
/* Constructor (...)*********************************************************
40
39
* To allow backwards compatability for v1.1, or for people that just want
41
40
* to use Proportional on Error without explicitly saying so
42
41
***************************************************************************/
43
42
44
- PID::PID (double * Input, double * Output, double * Setpoint,
45
- double Kp, double Ki, double Kd, int ControllerDirection)
46
- :PID::PID(Input, Output, Setpoint, Kp, Ki, Kd, P_ON_E, ControllerDirection)
47
- {
48
-
49
- }
50
-
51
-
52
- /* Compute() **********************************************************************
53
- * This, as they say, is where the magic happens. this function should be called
54
- * every time "void loop()" executes. the function will decide for itself whether a new
55
- * pid Output needs to be computed. returns true when the output is computed,
56
- * false when nothing has been done.
43
+ PID::PID (double *Input, double *Output, double *Setpoint, double Kp, double Ki,
44
+ double Kd, int ControllerDirection)
45
+ : PID::PID(Input, Output, Setpoint, Kp, Ki, Kd, P_ON_E,
46
+ ControllerDirection) {}
47
+
48
+ /* Compute()
49
+ *********************************************************************** This,
50
+ *as they say, is where the magic happens. this function should be called every
51
+ *time "void loop()" executes. the function will decide for itself whether a
52
+ *new pid Output needs to be computed. returns true when the output is
53
+ *computed, false when nothing has been done.
57
54
**********************************************************************************/
58
- bool PID::Compute ()
59
- {
60
- if (!inAuto) return false ;
61
- unsigned long now = millis ();
62
- unsigned long timeChange = (now - lastTime);
63
- if (timeChange>=SampleTime)
64
- {
65
- /* Compute all the working error variables*/
66
- double input = *myInput;
67
- double error = *mySetpoint - input;
68
- double dInput = (input - lastInput);
69
- outputSum+= (ki * error);
70
-
71
- /* Add Proportional on Measurement, if P_ON_M is specified*/
72
- if (!pOnE) outputSum-= kp * dInput;
73
-
74
- if (outputSum > outMax) outputSum= outMax;
75
- else if (outputSum < outMin) outputSum= outMin;
76
-
77
- /* Add Proportional on Error, if P_ON_E is specified*/
78
- double output;
79
- if (pOnE) output = kp * error;
80
- else output = 0 ;
81
-
82
- /* Compute Rest of PID Output*/
83
- output += outputSum - kd * dInput;
84
-
85
- if (output > outMax) output = outMax;
86
- else if (output < outMin) output = outMin;
87
- *myOutput = output;
88
-
89
- /* Remember some variables for next time*/
90
- lastInput = input;
91
- lastTime = now;
92
- return true ;
93
- }
94
- else return false ;
55
+ bool PID::Compute () {
56
+ if (!inAuto)
57
+ return false ;
58
+ unsigned long now = millis ();
59
+ unsigned long timeChange = (now - lastTime);
60
+ if (timeChange >= SampleTime) {
61
+ /* Compute all the working error variables*/
62
+ double input = *myInput;
63
+ double error = *mySetpoint - input;
64
+ double dInput = (input - lastInput);
65
+ outputSum += (ki * error);
66
+
67
+ /* Add Proportional on Measurement, if P_ON_M is specified*/
68
+ if (!pOnE)
69
+ outputSum -= kp * dInput;
70
+
71
+ if (outputSum > outMax)
72
+ outputSum = outMax;
73
+ else if (outputSum < outMin)
74
+ outputSum = outMin;
75
+
76
+ /* Add Proportional on Error, if P_ON_E is specified*/
77
+ double output;
78
+ if (pOnE)
79
+ output = kp * error;
80
+ else
81
+ output = 0 ;
82
+
83
+ /* Compute Rest of PID Output*/
84
+ output += outputSum - kd * dInput;
85
+
86
+ if (output > outMax)
87
+ output = outMax;
88
+ else if (output < outMin)
89
+ output = outMin;
90
+ *myOutput = output;
91
+
92
+ /* Remember some variables for next time*/
93
+ lastInput = input;
94
+ lastTime = now;
95
+ return true ;
96
+ } else
97
+ return false ;
95
98
}
96
99
97
100
/* SetTunings(...)*************************************************************
98
101
* This function allows the controller's dynamic performance to be adjusted.
99
102
* it's called automatically from the constructor, but tunings can also
100
103
* be adjusted on the fly during normal operation
101
104
******************************************************************************/
102
- void PID::SetTunings (double Kp, double Ki, double Kd, int POn)
103
- {
104
- if (Kp<0 || Ki<0 || Kd<0 ) return ;
105
-
106
- pOn = POn;
107
- pOnE = POn == P_ON_E;
108
-
109
- dispKp = Kp; dispKi = Ki; dispKd = Kd;
110
-
111
- double SampleTimeInSec = ((double )SampleTime)/1000 ;
112
- kp = Kp;
113
- ki = Ki * SampleTimeInSec;
114
- kd = Kd / SampleTimeInSec;
115
-
116
- if (controllerDirection ==REVERSE)
117
- {
118
- kp = (0 - kp);
119
- ki = (0 - ki);
120
- kd = (0 - kd);
121
- }
105
+ void PID::SetTunings (double Kp, double Ki, double Kd, int POn) {
106
+ if (Kp < 0 || Ki < 0 || Kd < 0 )
107
+ return ;
108
+
109
+ pOn = POn;
110
+ pOnE = POn == P_ON_E;
111
+
112
+ dispKp = Kp;
113
+ dispKi = Ki;
114
+ dispKd = Kd;
115
+
116
+ double SampleTimeInSec = ((double )SampleTime) / 1000 ;
117
+ kp = Kp;
118
+ ki = Ki * SampleTimeInSec;
119
+ kd = Kd / SampleTimeInSec;
120
+
121
+ if (controllerDirection == REVERSE) {
122
+ kp = (0 - kp);
123
+ ki = (0 - ki);
124
+ kd = (0 - kd);
125
+ }
122
126
}
123
127
124
128
/* SetTunings(...)*************************************************************
125
129
* Set Tunings using the last-rembered POn setting
126
130
******************************************************************************/
127
- void PID::SetTunings (double Kp, double Ki, double Kd){
128
- SetTunings (Kp, Ki, Kd, pOn);
131
+ void PID::SetTunings (double Kp, double Ki, double Kd) {
132
+ SetTunings (Kp, Ki, Kd, pOn);
129
133
}
130
134
131
135
/* SetSampleTime(...) *********************************************************
132
136
* sets the period, in Milliseconds, at which the calculation is performed
133
137
******************************************************************************/
134
- void PID::SetSampleTime (int NewSampleTime)
135
- {
136
- if (NewSampleTime > 0 )
137
- {
138
- double ratio = (double )NewSampleTime
139
- / (double )SampleTime;
140
- ki *= ratio;
141
- kd /= ratio;
142
- SampleTime = (unsigned long )NewSampleTime;
143
- }
138
+ void PID::SetSampleTime (int NewSampleTime) {
139
+ if (NewSampleTime > 0 ) {
140
+ double ratio = (double )NewSampleTime / (double )SampleTime;
141
+ ki *= ratio;
142
+ kd /= ratio;
143
+ SampleTime = (unsigned long )NewSampleTime;
144
+ }
144
145
}
145
146
146
147
/* SetOutputLimits(...)****************************************************
@@ -151,47 +152,49 @@ void PID::SetSampleTime(int NewSampleTime)
151
152
* want to clamp it from 0-125. who knows. at any rate, that can all be done
152
153
* here.
153
154
**************************************************************************/
154
- void PID::SetOutputLimits (double Min, double Max)
155
- {
156
- if (Min >= Max) return ;
157
- outMin = Min;
158
- outMax = Max;
159
-
160
- if (inAuto)
161
- {
162
- if (*myOutput > outMax) *myOutput = outMax;
163
- else if (*myOutput < outMin) *myOutput = outMin;
164
-
165
- if (outputSum > outMax) outputSum= outMax;
166
- else if (outputSum < outMin) outputSum= outMin;
167
- }
155
+ void PID::SetOutputLimits (double Min, double Max) {
156
+ if (Min >= Max)
157
+ return ;
158
+ outMin = Min;
159
+ outMax = Max;
160
+
161
+ if (inAuto) {
162
+ if (*myOutput > outMax)
163
+ *myOutput = outMax;
164
+ else if (*myOutput < outMin)
165
+ *myOutput = outMin;
166
+
167
+ if (outputSum > outMax)
168
+ outputSum = outMax;
169
+ else if (outputSum < outMin)
170
+ outputSum = outMin;
171
+ }
168
172
}
169
173
170
174
/* SetMode(...)****************************************************************
171
175
* Allows the controller Mode to be set to manual (0) or Automatic (non-zero)
172
176
* when the transition from manual to auto occurs, the controller is
173
177
* automatically initialized
174
178
******************************************************************************/
175
- void PID::SetMode (int Mode)
176
- {
177
- bool newAuto = (Mode == AUTOMATIC);
178
- if (newAuto && !inAuto)
179
- { /* we just went from manual to auto*/
180
- PID::Initialize ();
181
- }
182
- inAuto = newAuto;
179
+ void PID::SetMode (int Mode) {
180
+ bool newAuto = (Mode == AUTOMATIC);
181
+ if (newAuto && !inAuto) { /* we just went from manual to auto*/
182
+ PID::Initialize ();
183
+ }
184
+ inAuto = newAuto;
183
185
}
184
186
185
187
/* Initialize()****************************************************************
186
188
* does all the things that need to happen to ensure a bumpless transfer
187
189
* from manual to automatic mode.
188
190
******************************************************************************/
189
- void PID::Initialize ()
190
- {
191
- outputSum = *myOutput;
192
- lastInput = *myInput;
193
- if (outputSum > outMax) outputSum = outMax;
194
- else if (outputSum < outMin) outputSum = outMin;
191
+ void PID::Initialize () {
192
+ outputSum = *myOutput;
193
+ lastInput = *myInput;
194
+ if (outputSum > outMax)
195
+ outputSum = outMax;
196
+ else if (outputSum < outMin)
197
+ outputSum = outMin;
195
198
}
196
199
197
200
/* SetControllerDirection(...)*************************************************
@@ -200,25 +203,22 @@ void PID::Initialize()
200
203
* know which one, because otherwise we may increase the output when we should
201
204
* be decreasing. This is called from the constructor.
202
205
******************************************************************************/
203
- void PID::SetControllerDirection (int Direction)
204
- {
205
- if (inAuto && Direction !=controllerDirection)
206
- {
207
- kp = (0 - kp);
208
- ki = (0 - ki);
209
- kd = (0 - kd);
210
- }
211
- controllerDirection = Direction;
206
+ void PID::SetControllerDirection (int Direction) {
207
+ if (inAuto && Direction != controllerDirection) {
208
+ kp = (0 - kp);
209
+ ki = (0 - ki);
210
+ kd = (0 - kd);
211
+ }
212
+ controllerDirection = Direction;
212
213
}
213
214
214
215
/* Status Funcions*************************************************************
215
216
* Just because you set the Kp=-1 doesn't mean it actually happened. these
216
217
* functions query the internal state of the PID. they're here for display
217
218
* purposes. this are the functions the PID Front-end uses for example
218
219
******************************************************************************/
219
- double PID::GetKp (){ return dispKp; }
220
- double PID::GetKi (){ return dispKi;}
221
- double PID::GetKd (){ return dispKd;}
222
- int PID::GetMode (){ return inAuto ? AUTOMATIC : MANUAL;}
223
- int PID::GetDirection (){ return controllerDirection;}
224
-
220
+ double PID::GetKp () { return dispKp; }
221
+ double PID::GetKi () { return dispKi; }
222
+ double PID::GetKd () { return dispKd; }
223
+ int PID::GetMode () { return inAuto ? AUTOMATIC : MANUAL; }
224
+ int PID::GetDirection () { return controllerDirection; }
0 commit comments