Skip to content

Commit f130521

Browse files
authored
[PID] Cleanup saturation parameter in the methods and constructors (#390)
1 parent 04c1a2e commit f130521

File tree

7 files changed

+404
-307
lines changed

7 files changed

+404
-307
lines changed

control_toolbox/include/control_toolbox/pid.hpp

Lines changed: 52 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#define CONTROL_TOOLBOX__PID_HPP_
3535

3636
#include <chrono>
37+
#include <cmath>
38+
#include <iostream>
3739
#include <limits>
3840
#include <string>
3941

@@ -194,17 +196,9 @@ class Pid
194196
*/
195197
[[deprecated("Use constructor with AntiwindupStrategy instead.")]]
196198
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)
208202
{
209203
}
210204

@@ -224,17 +218,9 @@ class Pid
224218
*/
225219
[[deprecated("Use constructor with AntiwindupStrategy instead.")]]
226220
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)
238224
{
239225
}
240226

@@ -250,8 +236,6 @@ class Pid
250236
* \param u_min Lower output clamp.
251237
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
252238
* 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.
255239
* \param antiwindup Anti-windup functionality. When set to true, limits
256240
the integral error to prevent windup; otherwise, constrains the
257241
integral contribution to the control output. i_max and
@@ -265,7 +249,7 @@ class Pid
265249
[[deprecated("Use constructor with AntiwindupStrategy only.")]]
266250
Gains(
267251
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)
269253
: p_gain_(p),
270254
i_gain_(i),
271255
d_gain_(d),
@@ -274,10 +258,20 @@ class Pid
274258
u_max_(u_max),
275259
u_min_(u_min),
276260
trk_tc_(trk_tc),
277-
saturation_(saturation),
278261
antiwindup_(antiwindup),
279262
antiwindup_strat_(antiwindup_strat)
280263
{
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+
}
281275
}
282276

283277
/*!
@@ -290,57 +284,44 @@ class Pid
290284
* \param u_min Lower output clamp.
291285
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
292286
* 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.
295287
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
296288
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
297289
tracking_time_constant parameter to tune the anti-windup behavior.
298290
*
299291
*/
300292
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,
302294
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)
314296
{
315297
}
316298

317299
// 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()
330303
{
331304
}
332305

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. */
344325
};
345326

346327
/*!
@@ -376,8 +357,6 @@ class Pid
376357
* \param u_min Lower output clamp.
377358
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
378359
* 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.
381360
* \param antiwindup Anti-windup functionality. When set to true, limits
382361
the integral error to prevent windup; otherwise, constrains the
383362
integral contribution to the control output. i_max and
@@ -392,7 +371,7 @@ class Pid
392371
[[deprecated("Use constructor with AntiwindupStrategy only.")]]
393372
Pid(
394373
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);
396375

397376
/*!
398377
* \brief Constructor, initialize Pid-gains and term limits.
@@ -404,16 +383,14 @@ class Pid
404383
* \param u_min Lower output clamp.
405384
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
406385
* 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.
409386
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
410387
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
411388
tracking_time_constant parameter to tune the anti-windup behavior.
412389
*
413390
* \throws An std::invalid_argument exception is thrown if u_min > u_max
414391
*/
415392
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,
417394
AntiwindupStrategy antiwindup_strat);
418395

419396
/*!
@@ -458,8 +435,6 @@ class Pid
458435
* \param u_min Lower output clamp.
459436
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
460437
* 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.
463438
* \param antiwindup Anti-windup functionality. When set to true, limits
464439
the integral error to prevent windup; otherwise, constrains the
465440
integral contribution to the control output. i_max and
@@ -474,7 +449,7 @@ class Pid
474449
[[deprecated("Use initialize with AntiwindupStrategy only.")]]
475450
void initialize(
476451
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);
478453

479454
/*!
480455
* \brief Initialize Pid-gains and term limits.
@@ -486,16 +461,14 @@ class Pid
486461
* \param u_min Lower output clamp.
487462
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
488463
* 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.
491464
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
492465
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
493466
tracking_time_constant parameter to tune the anti-windup behavior.
494467
*
495468
* \note New gains are not applied if i_min_ > i_max_ or u_min > u_max
496469
*/
497470
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,
499472
AntiwindupStrategy antiwindup_strat);
500473

501474
/*!
@@ -553,8 +526,6 @@ class Pid
553526
* \param u_min Lower output clamp.
554527
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
555528
* 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.
558529
* \param antiwindup Anti-windup functionality. When set to true, limits
559530
the integral error to prevent windup; otherwise, constrains the
560531
integral contribution to the control output. i_max and
@@ -567,8 +538,7 @@ class Pid
567538
[[deprecated("Use get_gains overload with AntiwindupStrategy only.")]]
568539
void get_gains(
569540
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);
572542

573543
/*!
574544
* \brief Get PID gains for the controller (preferred).
@@ -579,15 +549,13 @@ class Pid
579549
* \param u_min Lower output clamp.
580550
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
581551
* 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.
584552
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
585553
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
586554
tracking_time_constant parameter to tune the anti-windup behavior.
587555
*/
588556
void get_gains(
589557
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);
591559

592560
/*!
593561
* \brief Get PID gains for the controller.
@@ -623,8 +591,6 @@ class Pid
623591
* \param u_min Lower output clamp.
624592
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
625593
* 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.
628594
* \param antiwindup Anti-windup functionality. When set to true, limits
629595
the integral error to prevent windup; otherwise, constrains the
630596
integral contribution to the control output. i_max and
@@ -639,7 +605,7 @@ class Pid
639605
[[deprecated("Use set_gains with AntiwindupStrategy only.")]]
640606
void set_gains(
641607
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,
643609
AntiwindupStrategy antiwindup_strat = AntiwindupStrategy::NONE);
644610

645611
/*!
@@ -652,16 +618,14 @@ class Pid
652618
* \param u_min Lower output clamp.
653619
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
654620
* 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.
657621
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
658622
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
659623
tracking_time_constant parameter to tune the anti-windup behavior.
660624
*
661625
* \note New gains are not applied if i_min_ > i_max_ or u_min > u_max
662626
*/
663627
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,
665629
AntiwindupStrategy antiwindup_strat);
666630

667631
/*!

control_toolbox/include/control_toolbox/pid_ros.hpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ class PidROS
133133
* \param u_min Lower output clamp.
134134
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
135135
* to 0.0 when this strategy is selected, a recommended default value will be applied.
136-
* \param saturation Enables output saturation. When true, the controller output is
137-
clamped between u_max and u_min.
138136
* \param antiwindup Anti-windup functionality. When set to true, limits
139137
the integral error to prevent windup; otherwise, constrains the
140138
integral contribution to the control output. i_max and
@@ -155,8 +153,7 @@ class PidROS
155153
[[deprecated("Use initialize_from_args with AntiwindupStrategy only.")]]
156154
void initialize_from_args(
157155
double p, double i, double d, double i_max, double i_min, double u_max, double u_min,
158-
double trk_tc, bool saturation, bool antiwindup, AntiwindupStrategy antiwindup_strat,
159-
bool save_i_term);
156+
double trk_tc, bool antiwindup, AntiwindupStrategy antiwindup_strat, bool save_i_term);
160157

161158
/*!
162159
* \brief Initialize the PID controller and set the parameters.
@@ -168,8 +165,6 @@ class PidROS
168165
* \param u_min Lower output clamp.
169166
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
170167
* to 0.0 when this strategy is selected, a recommended default value will be applied.
171-
* \param saturation Enables output saturation. When true, the controller output is
172-
clamped between u_max and u_min.
173168
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
174169
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
175170
tracking_time_constant parameter to tune the anti-windup behavior.
@@ -178,7 +173,7 @@ class PidROS
178173
* \note New gains are not applied if u_min_ > u_max_.
179174
*/
180175
void initialize_from_args(
181-
double p, double i, double d, double u_max, double u_min, double trk_tc, bool saturation,
176+
double p, double i, double d, double u_max, double u_min, double trk_tc,
182177
AntiwindupStrategy antiwindup_strat, bool save_i_term);
183178

184179
/*!
@@ -260,8 +255,6 @@ class PidROS
260255
* \param u_min Lower output clamp.
261256
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
262257
* to 0.0 when this strategy is selected, a recommended default value will be applied.
263-
* \param saturation Enables output saturation. When true, the controller output is
264-
clamped between u_max and u_min.
265258
* \param antiwindup Anti-windup functionality. When set to true, limits
266259
the integral error to prevent windup; otherwise, constrains the
267260
integral contribution to the control output. i_max and
@@ -282,7 +275,7 @@ class PidROS
282275
[[deprecated("Use set_gains with AntiwindupStrategy only.")]]
283276
void set_gains(
284277
double p, double i, double d, double i_max, double i_min, double u_max, double u_min,
285-
double trk_tc = 0.0, bool saturation = false, bool antiwindup = false,
278+
double trk_tc = 0.0, bool antiwindup = false,
286279
AntiwindupStrategy antiwindup_strat = AntiwindupStrategy::NONE);
287280

288281
/*!
@@ -295,16 +288,14 @@ class PidROS
295288
* \param u_min Lower output clamp.
296289
* \param trk_tc Specifies the tracking time constant for the 'back_calculation' strategy. If set
297290
* to 0.0 when this strategy is selected, a recommended default value will be applied.
298-
* \param saturation Enables output saturation. When true, the controller output is
299-
clamped between u_max and u_min.
300291
* \param antiwindup_strat Specifies the anti-windup strategy. Options: 'back_calculation',
301292
'conditional_integration', or 'none'. Note that the 'back_calculation' strategy use the
302293
tracking_time_constant parameter to tune the anti-windup behavior.
303294
*
304295
* \note New gains are not applied if u_min_ > u_max_.
305296
*/
306297
void set_gains(
307-
double p, double i, double d, double u_max, double u_min, double trk_tc, bool saturation,
298+
double p, double i, double d, double u_max, double u_min, double trk_tc,
308299
AntiwindupStrategy antiwindup_strat);
309300

310301
/*!

0 commit comments

Comments
 (0)