13
13
14
14
#include " PID_v1.h"
15
15
16
- /* Constructor (...)*********************************************************
17
- * The parameters specified here are those for for which we can't set up
18
- * reliable defaults, so we need to have the user set them.
19
- ***************************************************************************/
16
+ // The parameters specified here are those for for which we can't set up
17
+ // reliable defaults, so we need to have the user set them.
20
18
PID::PID (double *Input, double *Output, double *Setpoint, double Kp, double Ki,
21
19
double Kd, PIDProportionalOn POn,
22
20
PIDControllerDirection ControllerDirection) {
@@ -25,8 +23,9 @@ PID::PID(double *Input, double *Output, double *Setpoint, double Kp, double Ki,
25
23
mySetpoint = Setpoint;
26
24
mode = MANUAL;
27
25
28
- PID::SetOutputLimits (0 , 255 ); // default output limit corresponds to
29
- // the arduino pwm limits
26
+ // default output limit corresponds to
27
+ PID::SetOutputLimits (0 , 255 );
28
+ // the arduino pwm limits
30
29
31
30
SampleTime = 100 ; // default Controller Sample Time is 0.1 seconds
32
31
@@ -36,23 +35,18 @@ PID::PID(double *Input, double *Output, double *Setpoint, double Kp, double Ki,
36
35
lastTime = millis () - SampleTime;
37
36
}
38
37
39
- /* Constructor (...)*********************************************************
40
- * To allow backwards compatability for v1.1, or for people that just want
41
- * to use Proportional on Error without explicitly saying so
42
- ***************************************************************************/
43
-
38
+ // To allow backwards compatability for v1.1, or for people that just want to
39
+ // use Proportional on Error without explicitly saying so
44
40
PID::PID (double *Input, double *Output, double *Setpoint, double Kp, double Ki,
45
41
double Kd, PIDControllerDirection ControllerDirection)
46
42
: PID::PID(Input, Output, Setpoint, Kp, Ki, Kd, P_ON_E,
47
43
ControllerDirection) {}
48
44
49
- /* Compute()
50
- *********************************************************************** This,
51
- *as they say, is where the magic happens. this function should be called every
52
- *time "void loop()" executes. the function will decide for itself whether a
53
- *new pid Output needs to be computed. returns true when the output is
54
- *computed, false when nothing has been done.
55
- **********************************************************************************/
45
+ // This, as they say, is where the magic happens.
46
+ // This function should be called every time "void loop()" executes.
47
+ // The function will decide for itself whether a new pid Output needs to be
48
+ // computed. returns true when the output is computed, false when nothing has
49
+ // been done.
56
50
bool PID::Compute () {
57
51
if (mode != AUTOMATIC) {
58
52
return false ;
@@ -100,11 +94,9 @@ bool PID::Compute() {
100
94
return false ;
101
95
}
102
96
103
- /* SetTunings(...)*************************************************************
104
- * This function allows the controller's dynamic performance to be adjusted.
105
- * it's called automatically from the constructor, but tunings can also
106
- * be adjusted on the fly during normal operation
107
- ******************************************************************************/
97
+ // This function allows the controller's dynamic performance to be adjusted.
98
+ // it's called automatically from the constructor, but tunings can also
99
+ // be adjusted on the fly during normal operation
108
100
void PID::SetTunings (double Kp, double Ki, double Kd, PIDProportionalOn POn) {
109
101
if (Kp < 0 || Ki < 0 || Kd < 0 )
110
102
return ;
@@ -127,16 +119,12 @@ void PID::SetTunings(double Kp, double Ki, double Kd, PIDProportionalOn POn) {
127
119
}
128
120
}
129
121
130
- /* SetTunings(...)*************************************************************
131
- * Set Tunings using the last-rembered POn setting
132
- ******************************************************************************/
122
+ // Set Tunings using the last-rembered POn setting
133
123
void PID::SetTunings (double Kp, double Ki, double Kd) {
134
124
SetTunings (Kp, Ki, Kd, pOn);
135
125
}
136
126
137
- /* SetSampleTime(...) *********************************************************
138
- * sets the period, in Milliseconds, at which the calculation is performed
139
- ******************************************************************************/
127
+ // sets the period, in Milliseconds, at which the calculation is performed
140
128
void PID::SetSampleTime (int NewSampleTime) {
141
129
if (NewSampleTime > 0 ) {
142
130
double ratio = (double )NewSampleTime / (double )SampleTime;
@@ -146,14 +134,12 @@ void PID::SetSampleTime(int NewSampleTime) {
146
134
}
147
135
}
148
136
149
- /* SetOutputLimits(...)****************************************************
150
- * This function will be used far more often than SetInputLimits. while
151
- * the input to the controller will generally be in the 0-1023 range (which is
152
- * the default already,) the output will be a little different. maybe they'll
153
- * be doing a time window and will need 0-8000 or something. or maybe they'll
154
- * want to clamp it from 0-125. who knows. at any rate, that can all be done
155
- * here.
156
- **************************************************************************/
137
+ // This function will be used far more often than SetInputLimits. while
138
+ // the input to the controller will generally be in the 0-1023 range (which is
139
+ // the default already,) the output will be a little different. maybe they'll
140
+ // be doing a time window and will need 0-8000 or something. or maybe they'll
141
+ // want to clamp it from 0-125. who knows. at any rate, that can all be done
142
+ // here.
157
143
void PID::SetOutputLimits (double Min, double Max) {
158
144
if (Min >= Max)
159
145
return ;
@@ -173,11 +159,9 @@ void PID::SetOutputLimits(double Min, double Max) {
173
159
}
174
160
}
175
161
176
- /* SetMode(...)****************************************************************
177
- * Allows the controller Mode to be set to manual (0) or Automatic (non-zero)
178
- * when the transition from manual to auto occurs, the controller is
179
- * automatically initialized
180
- ******************************************************************************/
162
+ // Allows the controller Mode to be set to manual (0) or Automatic (non-zero)
163
+ // when the transition from manual to auto occurs, the controller is
164
+ // automatically initialized
181
165
void PID::SetMode (PIDControllerMode Mode) {
182
166
/* we just went from manual to auto*/
183
167
if (Mode == AUTOMATIC && mode != AUTOMATIC) {
@@ -187,10 +171,8 @@ void PID::SetMode(PIDControllerMode Mode) {
187
171
mode = Mode;
188
172
}
189
173
190
- /* Initialize()****************************************************************
191
- * does all the things that need to happen to ensure a bumpless transfer
192
- * from manual to automatic mode.
193
- ******************************************************************************/
174
+ // does all the things that need to happen to ensure a bumpless transfer
175
+ // from manual to automatic mode.
194
176
void PID::Initialize () {
195
177
outputSum = *myOutput;
196
178
lastInput = *myInput;
@@ -200,12 +182,10 @@ void PID::Initialize() {
200
182
outputSum = outMin;
201
183
}
202
184
203
- /* SetControllerDirection(...)*************************************************
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.
208
- ******************************************************************************/
185
+ // The PID will either be connected to a PIDControllerDirection::DIRECT acting
186
+ // process (+Output leads to +Input) or a REVERSE acting process(+Output leads
187
+ // to -Input.) we need to know which one, because otherwise we may increase the
188
+ // output when we should be decreasing. This is called from the constructor.
209
189
void PID::SetControllerDirection (PIDControllerDirection Direction) {
210
190
if (mode == AUTOMATIC && Direction != controllerDirection) {
211
191
kp = (0 - kp);
@@ -215,11 +195,9 @@ void PID::SetControllerDirection(PIDControllerDirection Direction) {
215
195
controllerDirection = Direction;
216
196
}
217
197
218
- /* Status Funcions*************************************************************
219
- * Just because you set the Kp=-1 doesn't mean it actually happened. these
220
- * functions query the internal state of the PID. they're here for display
221
- * purposes. this are the functions the PID Front-end uses for example
222
- ******************************************************************************/
198
+ // Just because you set the Kp=-1 doesn't mean it actually happened. these
199
+ // functions query the internal state of the PID. they're here for display
200
+ // purposes. this are the functions the PID Front-end uses for example
223
201
double PID::GetKp () { return dispKp; }
224
202
double PID::GetKi () { return dispKi; }
225
203
double PID::GetKd () { return dispKd; }
0 commit comments