34
34
#define CONTROL_TOOLBOX__PID_HPP_
35
35
36
36
#include < chrono>
37
+ #include < cmath>
38
+ #include < iostream>
37
39
#include < limits>
38
40
#include < string>
39
41
@@ -194,17 +196,9 @@ class Pid
194
196
*/
195
197
[[deprecated(" Use constructor with AntiwindupStrategy instead." )]]
196
198
Gains (double p, double i, double d, double i_max, double i_min)
197
- : p_gain_(p),
198
- i_gain_ (i),
199
- d_gain_(d),
200
- i_max_(i_max),
201
- i_min_(i_min),
202
- u_max_(0.0 ),
203
- u_min_(0.0 ),
204
- trk_tc_(0.0 ),
205
- saturation_(false ),
206
- antiwindup_(true ),
207
- antiwindup_strat_(AntiwindupStrategy::NONE)
199
+ : Gains(
200
+ p, i, d, i_max, i_min, std::numeric_limits<double >::infinity(),
201
+ -std::numeric_limits<double >::infinity(), 0.0 , true , AntiwindupStrategy::NONE)
208
202
{
209
203
}
210
204
@@ -224,17 +218,9 @@ class Pid
224
218
*/
225
219
[[deprecated(" Use constructor with AntiwindupStrategy instead." )]]
226
220
Gains (double p, double i, double d, double i_max, double i_min, bool antiwindup)
227
- : p_gain_(p),
228
- i_gain_(i),
229
- d_gain_(d),
230
- i_max_(i_max),
231
- i_min_(i_min),
232
- u_max_(0.0 ),
233
- u_min_(0.0 ),
234
- trk_tc_(0.0 ),
235
- saturation_(false ),
236
- antiwindup_(antiwindup),
237
- antiwindup_strat_(AntiwindupStrategy::NONE)
221
+ : Gains(
222
+ p, i, d, i_max, i_min, std::numeric_limits<double >::infinity(),
223
+ -std::numeric_limits<double >::infinity(), 0.0 , antiwindup, AntiwindupStrategy::NONE)
238
224
{
239
225
}
240
226
@@ -250,8 +236,6 @@ class Pid
250
236
* \param u_min Lower output clamp.
251
237
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
252
238
* to 0.0 when this strategy is selected, a recommended default value will be applied.
253
- * \param saturation Enables output saturation. When true, the controller output is
254
- clamped between u_max and u_min.
255
239
* \param antiwindup Anti-windup functionality. When set to true, limits
256
240
the integral error to prevent windup; otherwise, constrains the
257
241
integral contribution to the control output. i_max and
@@ -265,7 +249,7 @@ class Pid
265
249
[[deprecated(" Use constructor with AntiwindupStrategy only." )]]
266
250
Gains (
267
251
double p, double i, double d, double i_max, double i_min, double u_max, double u_min,
268
- double trk_tc, bool saturation, bool antiwindup, AntiwindupStrategy antiwindup_strat)
252
+ double trk_tc, bool antiwindup, AntiwindupStrategy antiwindup_strat)
269
253
: p_gain_(p),
270
254
i_gain_ (i),
271
255
d_gain_(d),
@@ -274,10 +258,20 @@ class Pid
274
258
u_max_(u_max),
275
259
u_min_(u_min),
276
260
trk_tc_(trk_tc),
277
- saturation_(saturation),
278
261
antiwindup_(antiwindup),
279
262
antiwindup_strat_(antiwindup_strat)
280
263
{
264
+ if (std::isnan (u_min) || std::isnan (u_max))
265
+ {
266
+ throw std::invalid_argument (" Gains: u_min and u_max must not be NaN" );
267
+ }
268
+ if (u_min > u_max)
269
+ {
270
+ std::cout << " Received invalid u_min and u_max values: " << " u_min: " << u_min
271
+ << " , u_max: " << u_max << " . Setting saturation to false." << std::endl;
272
+ u_max_ = std::numeric_limits<double >::infinity ();
273
+ u_min_ = -std::numeric_limits<double >::infinity ();
274
+ }
281
275
}
282
276
283
277
/* !
@@ -290,57 +284,44 @@ class Pid
290
284
* \param u_min Lower output clamp.
291
285
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
292
286
* to 0.0 when this strategy is selected, a recommended default value will be applied.
293
- * \param saturation Enables output saturation. When true, the controller output is
294
- clamped between u_max and u_min.
295
287
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
296
288
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
297
289
tracking_time_constant parameter to tune the anti-windup behavior.
298
290
*
299
291
*/
300
292
Gains (
301
- double p, double i, double d, double u_max, double u_min, double trk_tc, bool saturation,
293
+ double p, double i, double d, double u_max, double u_min, double trk_tc,
302
294
AntiwindupStrategy antiwindup_strat)
303
- : p_gain_(p),
304
- i_gain_(i),
305
- d_gain_(d),
306
- i_max_(0.0 ),
307
- i_min_(0.0 ),
308
- u_max_(u_max),
309
- u_min_(u_min),
310
- trk_tc_(trk_tc),
311
- saturation_(saturation),
312
- antiwindup_(false ),
313
- antiwindup_strat_(antiwindup_strat)
295
+ : Gains(p, i, d, 0.0 , 0.0 , u_max, u_min, trk_tc, false , antiwindup_strat)
314
296
{
315
297
}
316
298
317
299
// Default constructor
318
- Gains ()
319
- : p_gain_(0.0 ),
320
- i_gain_(0.0 ),
321
- d_gain_(0.0 ),
322
- i_max_(0.0 ),
323
- i_min_(0.0 ),
324
- u_max_(0.0 ),
325
- u_min_(0.0 ),
326
- trk_tc_(0.0 ),
327
- saturation_(false ),
328
- antiwindup_(false ),
329
- antiwindup_strat_(AntiwindupStrategy::NONE)
300
+ [[deprecated(
301
+ " Use constructor with AntiwindupStrategy only. The default constructor might be deleted in "
302
+ " future" )]] Gains()
330
303
{
331
304
}
332
305
333
- double p_gain_; /* *< Proportional gain. */
334
- double i_gain_; /* *< Integral gain. */
335
- double d_gain_; /* *< Derivative gain. */
336
- double i_max_; /* *< Maximum allowable integral term. */
337
- double i_min_; /* *< Minimum allowable integral term. */
338
- double u_max_; /* *< Maximum allowable output. */
339
- double u_min_; /* *< Minimum allowable output. */
340
- double trk_tc_; /* *< Tracking time constant. */
341
- bool saturation_; /* *< Saturation. */
342
- bool antiwindup_; /* *< Anti-windup. */
343
- AntiwindupStrategy antiwindup_strat_; /* *< Anti-windup strategy. */
306
+ void print () const
307
+ {
308
+ std::cout << " Gains: p: " << p_gain_ << " , i: " << i_gain_ << " , d: " << d_gain_
309
+ << " , i_max: " << i_max_ << " , i_min: " << i_min_ << " , u_max: " << u_max_
310
+ << " , u_min: " << u_min_ << " , trk_tc: " << trk_tc_
311
+ << " , antiwindup: " << antiwindup_
312
+ << " , antiwindup_strat: " << antiwindup_strat_.to_string () << std::endl;
313
+ }
314
+
315
+ double p_gain_ = 0.0 ; /* *< Proportional gain. */
316
+ double i_gain_ = 0.0 ; /* *< Integral gain. */
317
+ double d_gain_ = 0.0 ; /* *< Derivative gain. */
318
+ double i_max_ = 0.0 ; /* *< Maximum allowable integral term. */
319
+ double i_min_ = 0.0 ; /* *< Minimum allowable integral term. */
320
+ double u_max_ = std::numeric_limits<double >::infinity(); /* *< Maximum allowable output. */
321
+ double u_min_ = -std::numeric_limits<double >::infinity(); /* *< Minimum allowable output. */
322
+ double trk_tc_ = 0.0 ; /* *< Tracking time constant. */
323
+ bool antiwindup_ = false ; /* *< Anti-windup. */
324
+ AntiwindupStrategy antiwindup_strat_ = AntiwindupStrategy::NONE; /* *< Anti-windup strategy. */
344
325
};
345
326
346
327
/* !
@@ -376,8 +357,6 @@ class Pid
376
357
* \param u_min Lower output clamp.
377
358
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
378
359
* to 0.0 when this strategy is selected, a recommended default value will be applied.
379
- * \param saturation Enables output saturation. When true, the controller output is
380
- clamped between u_max and u_min.
381
360
* \param antiwindup Anti-windup functionality. When set to true, limits
382
361
the integral error to prevent windup; otherwise, constrains the
383
362
integral contribution to the control output. i_max and
@@ -392,7 +371,7 @@ class Pid
392
371
[[deprecated(" Use constructor with AntiwindupStrategy only." )]]
393
372
Pid (
394
373
double p, double i, double d, double i_max, double i_min, double u_max, double u_min,
395
- double trk_tc, bool saturation, bool antiwindup, AntiwindupStrategy antiwindup_strat);
374
+ double trk_tc, bool antiwindup, AntiwindupStrategy antiwindup_strat);
396
375
397
376
/* !
398
377
* \brief Constructor, initialize Pid-gains and term limits.
@@ -404,16 +383,14 @@ class Pid
404
383
* \param u_min Lower output clamp.
405
384
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
406
385
* to 0.0 when this strategy is selected, a recommended default value will be applied.
407
- * \param saturation Enables output saturation. When true, the controller output is
408
- clamped between u_max and u_min.
409
386
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
410
387
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
411
388
tracking_time_constant parameter to tune the anti-windup behavior.
412
389
*
413
390
* \throws An std::invalid_argument exception is thrown if u_min > u_max
414
391
*/
415
392
Pid (
416
- double p, double i, double d, double u_max, double u_min, double trk_tc, bool saturation,
393
+ double p, double i, double d, double u_max, double u_min, double trk_tc,
417
394
AntiwindupStrategy antiwindup_strat);
418
395
419
396
/* !
@@ -458,8 +435,6 @@ class Pid
458
435
* \param u_min Lower output clamp.
459
436
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
460
437
* to 0.0 when this strategy is selected, a recommended default value will be applied.
461
- * \param saturation Enables output saturation. When true, the controller output is
462
- clamped between u_max and u_min.
463
438
* \param antiwindup Anti-windup functionality. When set to true, limits
464
439
the integral error to prevent windup; otherwise, constrains the
465
440
integral contribution to the control output. i_max and
@@ -474,7 +449,7 @@ class Pid
474
449
[[deprecated(" Use initialize with AntiwindupStrategy only." )]]
475
450
void initialize (
476
451
double p, double i, double d, double i_max, double i_min, double u_max, double u_min,
477
- double trk_tc, bool saturation, bool antiwindup, AntiwindupStrategy antiwindup_strat);
452
+ double trk_tc, bool antiwindup, AntiwindupStrategy antiwindup_strat);
478
453
479
454
/* !
480
455
* \brief Initialize Pid-gains and term limits.
@@ -486,16 +461,14 @@ class Pid
486
461
* \param u_min Lower output clamp.
487
462
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
488
463
* to 0.0 when this strategy is selected, a recommended default value will be applied.
489
- * \param saturation Enables output saturation. When true, the controller output is
490
- clamped between u_max and u_min.
491
464
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
492
465
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
493
466
tracking_time_constant parameter to tune the anti-windup behavior.
494
467
*
495
468
* \note New gains are not applied if i_min_ > i_max_ or u_min > u_max
496
469
*/
497
470
void initialize (
498
- double p, double i, double d, double u_max, double u_min, double trk_tc, bool saturation,
471
+ double p, double i, double d, double u_max, double u_min, double trk_tc,
499
472
AntiwindupStrategy antiwindup_strat);
500
473
501
474
/* !
@@ -553,8 +526,6 @@ class Pid
553
526
* \param u_min Lower output clamp.
554
527
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
555
528
* to 0.0 when this strategy is selected, a recommended default value will be applied.
556
- * \param saturation Enables output saturation. When true, the controller output is
557
- clamped between u_max and u_min.
558
529
* \param antiwindup Anti-windup functionality. When set to true, limits
559
530
the integral error to prevent windup; otherwise, constrains the
560
531
integral contribution to the control output. i_max and
@@ -567,8 +538,7 @@ class Pid
567
538
[[deprecated(" Use get_gains overload with AntiwindupStrategy only." )]]
568
539
void get_gains (
569
540
double & p, double & i, double & d, double & i_max, double & i_min, double & u_max,
570
- double & u_min, double & trk_tc, bool & saturation, bool & antiwindup,
571
- AntiwindupStrategy & antiwindup_strat);
541
+ double & u_min, double & trk_tc, bool & antiwindup, AntiwindupStrategy & antiwindup_strat);
572
542
573
543
/* !
574
544
* \brief Get PID gains for the controller (preferred).
@@ -579,15 +549,13 @@ class Pid
579
549
* \param u_min Lower output clamp.
580
550
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
581
551
* to 0.0 when this strategy is selected, a recommended default value will be applied.
582
- * \param saturation Enables output saturation. When true, the controller output is
583
- clamped between u_max and u_min.
584
552
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
585
553
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
586
554
tracking_time_constant parameter to tune the anti-windup behavior.
587
555
*/
588
556
void get_gains (
589
557
double & p, double & i, double & d, double & u_max, double & u_min, double & trk_tc,
590
- bool & saturation, AntiwindupStrategy & antiwindup_strat);
558
+ AntiwindupStrategy & antiwindup_strat);
591
559
592
560
/* !
593
561
* \brief Get PID gains for the controller.
@@ -623,8 +591,6 @@ class Pid
623
591
* \param u_min Lower output clamp.
624
592
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
625
593
* to 0.0 when this strategy is selected, a recommended default value will be applied.
626
- * \param saturation Enables output saturation. When true, the controller output is
627
- clamped between u_max and u_min.
628
594
* \param antiwindup Anti-windup functionality. When set to true, limits
629
595
the integral error to prevent windup; otherwise, constrains the
630
596
integral contribution to the control output. i_max and
@@ -639,7 +605,7 @@ class Pid
639
605
[[deprecated(" Use set_gains with AntiwindupStrategy only." )]]
640
606
void set_gains (
641
607
double p, double i, double d, double i_max, double i_min, double u_max, double u_min,
642
- double trk_tc = 0.0 , bool saturation = false , bool antiwindup = false ,
608
+ double trk_tc = 0.0 , bool antiwindup = false ,
643
609
AntiwindupStrategy antiwindup_strat = AntiwindupStrategy::NONE);
644
610
645
611
/* !
@@ -652,16 +618,14 @@ class Pid
652
618
* \param u_min Lower output clamp.
653
619
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
654
620
* to 0.0 when this strategy is selected, a recommended default value will be applied.
655
- * \param saturation Enables output saturation. When true, the controller output is
656
- clamped between u_max and u_min.
657
621
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
658
622
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
659
623
tracking_time_constant parameter to tune the anti-windup behavior.
660
624
*
661
625
* \note New gains are not applied if i_min_ > i_max_ or u_min > u_max
662
626
*/
663
627
void set_gains (
664
- double p, double i, double d, double u_max, double u_min, double trk_tc, bool saturation,
628
+ double p, double i, double d, double u_max, double u_min, double trk_tc,
665
629
AntiwindupStrategy antiwindup_strat);
666
630
667
631
/* !
0 commit comments