-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_control.hpp
447 lines (394 loc) · 17.1 KB
/
utils_control.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#ifndef UTILS_CONTROL_HPP
#define UTILS_CONTROL_HPP
#include "utils_math.hpp"
namespace utils::control {
/**
* Small, easy to use PID implementation with advanced controller capability.
*
* Minimal usage:
* setPID(p,i,d);
* ...looping code...{
* output = getOutput(sensorvalue,target);
* }
*
* \see https://github.com/tekdemo/MiniPID
* \see http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-direction/improving-the-beginners-pid-introduction
*/
class PIDController {
private:
double P = 0.0;
double I = 0.0;
double D = 0.0;
double F = 0.0;
double maxIOutput = 0.0;
double maxError = 0.0;
double errorSum = 0.0;
double maxOutput = 0.0;
double minOutput = 0.0;
double setpoint = 0.0;
double lastActual = 0.0;
bool firstRun = true;
bool reversed = false;
double outputRampRate = 0.0;
double lastOutput = 0.0;
double outputFilter = 0.0;
double setpointRange = 0.0;
/**
* \brief Check the signs of the PIDF parameters,
* and reverse them if needed.
*/
void checkSigns() {
if (this->reversed) {
// All values should be below zero
if (this->P > 0.0) this->P = -this->P;
if (this->I > 0.0) this->I = -this->I;
if (this->D > 0.0) this->D = -this->D;
if (this->F > 0.0) this->F = -this->F;
} else {
// All values should be above zero
if (this->P < 0.0) this->P = -this->P;
if (this->I < 0.0) this->I = -this->I;
if (this->D < 0.0) this->D = -this->D;
if (this->F < 0.0) this->F = -this->F;
}
}
public:
/**
* \brief Configure PID parameters.
* \param p
* \param i
* \param d
*/
PIDController(double p, double i, double d)
: P{p}, I{i}, D{d}, F{0}
{
// Empty
}
/**
* \brief Configure PID parameters with feedforward parameter.
* \param p
* \param i
* \param d
*/
PIDController(double p, double i, double d, double f)
: PIDController(p, i, d)
{
this->F = f;
}
~PIDController() = default;
/**
* \brief Configure the Proportional gain parameter.
*
* This responds quicly to changes in setpoint, and provides most
* of the initial driving force to make corrections.
* Some systems can be used with only a P gain, and many can be operated with only PI.
* For position based controllers, this is the first parameter to tune, with I second.
* For rate controlled systems, this is often the second after F.
*
* \param p
* Proportional gain. Affects output according to
* output += P * (setpoint - current_value)
*/
inline void setP(double p) {
this->P = p;
this->checkSigns();
}
/**
* \brief Configure the Integral parameter
*
* This is used for overcoming disturbances, and ensuring that the
* controller always gets to the control mode.
* Typically tuned second for "Position" based modes, and third
* for "Rate" or continuous based modes.
*
* Affects output through:
* output += previous_errors * Igain;
* previous_errors+=current_error</b>
*
* \see {\link #setMaxIOutput(double) setMaxIOutput} for how to restrict
* \param i
* New gain value for the Integral term.
*/
inline void setI(double i) {
if (this->I != 0.0) {
this->errorSum = this->errorSum * this->I / i;
}
if (this->maxIOutput != 0.0) {
this->maxError = this->maxIOutput / i;
}
this->I = i;
this->checkSigns();
}
/**
* \brief Configure the Differential parameter
*
* Responds quickly to large changes in error.
* Small values prevents P and I terms from causing overshoot.
*
* \param d
* New gain value for the Differential term.
*/
inline void setD(double d) {
this->D = d;
this->checkSigns();
}
/**
* \brief Configure the FeedForward parameter
*
* This is excellent for Velocity, rate, and other continuous control
* modes where you can expect a rough output value based solely
* on the setpoint.
* Should not be used in "position" based control modes.
*
* \param f
* Feed forward gain. Affects output according to
* output += F * Setpoint;
*/
inline void setF(double f) {
this->F = f;
this->checkSigns();
}
/**
* \brief Configure PID parameters.
*
* \param p
* Proportional gain. Large if large difference between setpoint and target.
* \param i
* Integral gain. Becomes large if setpoint cannot reach target quickly.
* \param d
* Derivative gain. Responds quickly to large changes in error.
*/
inline void setPID(double p, double i, double d) {
this->P = p;
this->I = i;
this->D = d;
this->checkSigns();
}
/**
* \brief Configure PID parameters with feedforward parameter.
*
* \param p
* Proportional gain. Large if large difference between setpoint and target.
* \param i
* Integral gain. Becomes large if setpoint cannot reach target quickly.
* \param d
* Derivative gain. Responds quickly to large changes in error.
*/
inline void setPID(double p, double i, double d, double f) {
this->P = p;
this->I = i;
this->D = d;
this->F = f;
this->checkSigns();
}
/**
* \brief Set the maximum output value contributed by the I component of the system.
* This can be used to prevent large windup issues and make tuning simpler.
*
* \param max
* Units are the same as the expected output value.
*/
inline void setMaxIOutput(double max) {
/* Internally maxError and Izone are similar, but scaled for different purposes.
* The maxError is generated for simplifying math, since calculations against
* the max error are far more common than changing the I term or Izone.
*/
this->maxIOutput = max;
if (this->I != 0.0) {
this->maxError = this->maxIOutput / this->I;
}
}
/**
* \brief Specify a maximum output to [-output, output].
*
* \param output
*/
inline void setOutputLimits(double output) {
this->setOutputLimits(-output, output);
}
/**
* \brief Specify a maximum output to [min, max].
*
* \param min
* Minimum possible output value.
* \param max
* Maximum possible output value.
*/
inline void setOutputLimits(double min, double max) {
if (max < min) return;
this->minOutput = min;
this->maxOutput = max;
const double diff = max - min;
// Ensure the bounds of the I term are within the bounds of the allowable output swing
if (this->maxIOutput == 0.0 || this->maxIOutput > diff) {
this->setMaxIOutput(diff);
}
}
/**
* \brief Set the operating direction of the PID controller.
* \param reversed
*/
inline void setDirection(bool reversed) {
this->reversed = reversed;
}
/**
* \brief Set the target value for the PID calculations.
* \param setpoint
*/
inline void setSetpoint(double setpoint) {
this->setpoint = setpoint;
}
/**
* \brief Resets the controller.
* This->erases the I term buildup, and removes D gain on the next loop.
*/
inline void reset() {
this->firstRun = true;
this->errorSum = 0.0;
}
/**
* \brief Set the maximum rate the output can increase per cycle.
*
* \param rate
*/
inline void setOutputRampRate(double rate) {
this->outputRampRate = rate;
}
/**
* \brief Set a limit on how far the setpoint can be from the current position.
*
* Can simplify tuning by helping tuning over a small range
* applies to a much larger range. This limits the reactivity of
* P term, and restricts impact of large D term during large setpoint
* adjustments. Increases lag and I term if range is too small.
*
* \param range
*/
inline void setSetpointRange(double range) {
this->setpointRange = range;
}
/**
* \brief Set a filter on the output to reduce sharp oscillations.
*
* 0.1 is likely a sane starting value. Larger values cause P and D
* oscillations, but force larger I values. Uses an exponential
* rolling sum filter, according to a simple.
*
* \param strength
* output valid between [0..1), meaning [current output only.. historical output only)
*/
inline void setOutputFilter(double strength) {
if (strength == 0.0 || utils::math::within(strength, 0.0, 1.0)) {
this->outputFilter = strength;
}
}
/**
* \brief Calculates the PID value using the last provided setpoint and actual values.
*
* \return
* Calculated output value for driving the actual to the target.
*/
inline double getOutput() {
return this->getOutput(this->lastActual, this->setpoint);
}
/**
* \brief Calculates the PID value using the last provided setpoint and actual value.
*
* \param actual
* The actual value of the system.
* \return
* Calculated output value for driving the actual to the target.
*/
inline double getOutput(double actual){
return this->getOutput(actual, this->setpoint);
}
/**
* \brief Calculate the PID value needed to hit the target setpoint.
* Automatically re-calculates the output at each call.
*
* \param actual
* The actual value of the system.
* \param setpoint
* The target value of the system.
* \return Calculated output value for driving the actual to the target.
*/
double getOutput(double actual, double setpoint) {
this->setpoint = setpoint;
// Ramp the setpoint used for calculations if user has opted to do so
if (this->setpointRange != 0.0) {
setpoint = std::clamp(setpoint,
actual - this->setpointRange,
actual + this->setpointRange);
}
// Do the simple parts of the calculations
const double error = setpoint - actual;
// Calculate P term
const double Poutput = this->P * error;
// Calculate F output. Notice, this->depends only on the setpoint, and not the error.
const double Foutput = this->F * setpoint;
// If this->is our first time running this, we don't actually _have_
// a previous input or output.
// For sensor, sanely assume it was exactly where it is now.
// For last output, we can assume it's the current time-independent outputs.
if (this->firstRun) {
this->lastActual = actual;
this->lastOutput = Poutput + Foutput;
this->firstRun = false;
}
// Interim checks
const bool min_out_nequals_max = utils::math::epsilon_equals(this->minOutput, this->maxOutput, 1e-6);
const bool outrampraqte_not_zero = this->outputRampRate != 0.0;
const bool max_iout_not_zero = this->maxIOutput != 0.0;
const double lastout_min_ramp = this->lastOutput - this->outputRampRate;
const double lastout_pls_ramp = this->lastOutput + this->outputRampRate;
// Calculate D Term
// Note, this is negative. This actually "slows" the system if it's doing
// the correct thing, and small values helps prevent output spikes and overshoot
const double Doutput = -this->D * (actual - this->lastActual);
this->lastActual = actual;
// The Iterm is more complex. There's several things to factor in to make
// it easier to deal with.
// 1. maxIoutput restricts the amount of output contributed by the Iterm.
// 2. prevent windup by not increasing errorSum if we're already running
// against our max Ioutput
// 3. prevent windup by not increasing errorSum if output is output=maxOutput
double Ioutput = this->I * this->errorSum;
if (max_iout_not_zero) {
Ioutput = std::clamp(Ioutput, -this->maxIOutput, this->maxIOutput);
}
//And, finally, we can just add the terms up
double output = Foutput + Poutput + Ioutput + Doutput;
//Figure out what we're doing with the error.
if (min_out_nequals_max && !utils::math::within(output, this->minOutput, this->maxOutput)) {
// reset the error sum to a sane level
// Setting to current error ensures a smooth transition when the P term
// decreases enough for the I term to start acting upon the controller
// From that point the I term will build up as would be expected
this->errorSum = error;
} else if ( outrampraqte_not_zero
&& !utils::math::within(output, lastout_min_ramp, lastout_pls_ramp))
{
this->errorSum = error;
} else if (max_iout_not_zero) {
// In addition to output limiting directly, we also want to prevent I term
// buildup, so restrict the error directly
this->errorSum = std::clamp(this->errorSum + error, -this->maxError, this->maxError);
} else {
this->errorSum += error;
}
//Restrict output to our specified output and ramp limits
if (outrampraqte_not_zero) {
output = std::clamp(output, lastout_min_ramp, lastout_pls_ramp);
}
if (min_out_nequals_max) {
output = std::clamp(output, this->minOutput, this->maxOutput);
}
if (this->outputFilter != 0.0) {
output = utils::math::mix(output, this->lastOutput, this->outputFilter);
}
this->lastOutput = output;
return output;
}
};
}
#endif // UTILS_CONTROL_HPP