Skip to content

Remove legacy and deprecated PID parameters #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: ros2-master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 11 additions & 214 deletions control_toolbox/include/control_toolbox/pid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,13 @@ namespace control_toolbox
* This class defines various antiwindup strategies that can be used in PID controllers.
* It allows setting the type of antiwindup strategy and validates the parameters accordingly.
*
* \param i_max Upper integral clamp.
* \param i_min Lower integral clamp.
* \param u_max Upper output clamp.
* \param u_min Lower output clamp.
* \param tracking_time_constant Specifies the tracking time constant for the 'back_calculation' strategy. If set
* to 0.0 when this strategy is selected, a recommended default value will be applied.
* \param legacy_antiwindup Anti-windup functionality. When set to true, limits
the integral error to prevent windup; otherwise, constrains the
integral contribution to the control output. i_max and
i_min are applied in both scenarios.
* \param error_deadband Error deadband is used to stop integration when the error is within the given range.
* \param type Specifies the antiwindup strategy type. Valid values are:
* - `NONE`: No antiwindup strategy applied.
* - `LEGACY`: Legacy antiwindup strategy, which limits the integral term to prevent windup (deprecated: This option will be removed in a future release).
* - `BACK_CALCULATION`: Back calculation antiwindup strategy, which uses a tracking time constant.
* - `CONDITIONAL_INTEGRATION`: Conditional integration antiwindup strategy, which integrates only when certain conditions are met.
*/
Expand All @@ -75,16 +68,12 @@ struct AntiWindupStrategy
{
UNDEFINED = -1,
NONE,
LEGACY,
BACK_CALCULATION,
CONDITIONAL_INTEGRATION
};

AntiWindupStrategy()
: type(UNDEFINED),
i_min(std::numeric_limits<double>::quiet_NaN()),
i_max(std::numeric_limits<double>::quiet_NaN()),
legacy_antiwindup(false),
tracking_time_constant(0.0),
error_deadband(std::numeric_limits<double>::epsilon())
{
Expand All @@ -100,13 +89,6 @@ struct AntiWindupStrategy
{
type = CONDITIONAL_INTEGRATION;
}
else if (s == "legacy")
{
type = LEGACY;
std::cout << "Using the legacy anti-windup technique is deprecated. This option will be "
"removed by the ROS 2 Kilted Kaiju release."
<< std::endl;
}
else if (s == "none")
{
type = NONE;
Expand All @@ -116,7 +98,7 @@ struct AntiWindupStrategy
type = UNDEFINED;
throw std::invalid_argument(
"AntiWindupStrategy: Unknown antiwindup strategy : '" + s +
"'. Valid strategies are: 'back_calculation', 'conditional_integration', 'legacy', "
"'. Valid strategies are: 'back_calculation', 'conditional_integration', "
"'none'.");
}
}
Expand All @@ -135,23 +117,8 @@ struct AntiWindupStrategy
"AntiWindupStrategy 'back_calculation' requires a valid positive tracking time constant "
"(tracking_time_constant)");
}
if (type == LEGACY && ((i_min > i_max) || !std::isfinite(i_min) || !std::isfinite(i_max)))
{
throw std::invalid_argument(
fmt::format(
"AntiWindupStrategy 'legacy' requires i_min < i_max and to be finite (i_min: {}, i_max: "
"{})",
i_min, i_max));
}
if (type != LEGACY && (std::isfinite(i_min) || std::isfinite(i_max)))
{
std::cout << "Warning: The i_min and i_max are only valid for the deprecated LEGACY "
"antiwindup strategy. Please use the AntiWindupStrategy::set_type() method to "
"set the type of antiwindup strategy you want to use."
<< std::endl;
}
if (
type != NONE && type != UNDEFINED && type != LEGACY && type != BACK_CALCULATION &&
type != NONE && type != UNDEFINED && type != BACK_CALCULATION &&
type != CONDITIONAL_INTEGRATION)
{
throw std::invalid_argument("AntiWindupStrategy has an invalid type");
Expand All @@ -171,8 +138,6 @@ struct AntiWindupStrategy
return "back_calculation";
case CONDITIONAL_INTEGRATION:
return "conditional_integration";
case LEGACY:
return "legacy";
case NONE:
return "none";
case UNDEFINED:
Expand All @@ -182,10 +147,6 @@ struct AntiWindupStrategy
}

Value type = UNDEFINED;
double i_min = std::numeric_limits<double>::quiet_NaN(); /**< Minimum allowable integral term. */
double i_max = std::numeric_limits<double>::quiet_NaN(); /**< Maximum allowable integral term. */

bool legacy_antiwindup = false; /**< Use legacy anti-windup strategy. */

// tracking_time_constant Specifies the tracking time constant for the 'back_calculation'
// strategy. If set to 0.0 a recommended default value will be applied.
Expand Down Expand Up @@ -286,64 +247,6 @@ class Pid
*/
struct Gains
{
/*!
* \brief Optional constructor for passing in values without antiwindup and saturation
*
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max Upper integral clamp.
* \param i_min Lower integral clamp.
*
*/
[[deprecated("Use constructor with AntiWindupStrategy instead.")]]
Gains(double p, double i, double d, double i_max, double i_min)
: p_gain_(p),
i_gain_(i),
d_gain_(d),
i_max_(i_max),
i_min_(i_min),
u_max_(std::numeric_limits<double>::infinity()),
u_min_(-std::numeric_limits<double>::infinity()),
antiwindup_(false)
{
antiwindup_strat_.type = AntiWindupStrategy::LEGACY;
antiwindup_strat_.i_max = i_max;
antiwindup_strat_.i_min = i_min;
antiwindup_strat_.legacy_antiwindup = true;
}

/*!
* \brief Optional constructor for passing in values without saturation
*
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max Upper integral clamp.
* \param i_min Lower integral clamp.
* \param antiwindup Anti-windup functionality. When set to true, limits
the integral error to prevent windup; otherwise, constrains the
integral contribution to the control output. i_max and
i_min are applied in both scenarios.
*
*/
[[deprecated("Use constructor with AntiWindupStrategy instead.")]]
Gains(double p, double i, double d, double i_max, double i_min, bool antiwindup)
: p_gain_(p),
i_gain_(i),
d_gain_(d),
i_max_(i_max),
i_min_(i_min),
u_max_(std::numeric_limits<double>::infinity()),
u_min_(-std::numeric_limits<double>::infinity()),
antiwindup_(antiwindup)
{
antiwindup_strat_.type = AntiWindupStrategy::LEGACY;
antiwindup_strat_.i_max = i_max;
antiwindup_strat_.i_min = i_min;
antiwindup_strat_.legacy_antiwindup = antiwindup;
}

/*!
* \brief Constructor for passing in values.
*
Expand All @@ -363,11 +266,8 @@ class Pid
: p_gain_(p),
i_gain_(i),
d_gain_(d),
i_max_(antiwindup_strat.i_max),
i_min_(antiwindup_strat.i_min),
u_max_(u_max),
u_min_(u_min),
antiwindup_(antiwindup_strat.legacy_antiwindup),
antiwindup_strat_(antiwindup_strat)
{
if (std::isnan(u_min) || std::isnan(u_max))
Expand All @@ -385,12 +285,7 @@ class Pid

bool validate(std::string & error_msg) const
{
if (i_min_ > i_max_)
{
error_msg = fmt::format("Gains: i_min ({}) must be less than i_max ({})", i_min_, i_max_);
return false;
}
else if (u_min_ >= u_max_)
if (u_min_ >= u_max_)
{
error_msg = fmt::format("Gains: u_min ({}) must be less than u_max ({})", u_min_, u_max_);
return false;
Expand All @@ -412,53 +307,21 @@ class Pid
return true;
}

// Default constructor
[[deprecated(
"Use constructor with AntiWindupStrategy only. The default constructor might be deleted in "
"future")]] Gains()
{
}

void print() const
{
std::cout << "Gains: p: " << p_gain_ << ", i: " << i_gain_ << ", d: " << d_gain_
<< ", i_max: " << i_max_ << ", i_min: " << i_min_ << ", u_max: " << u_max_
<< ", u_min: " << u_min_ << ", antiwindup: " << antiwindup_
<< ", u_max: " << u_max_ << ", u_min: " << u_min_
<< ", antiwindup_strat: " << antiwindup_strat_.to_string() << std::endl;
}

double p_gain_ = 0.0; /**< Proportional gain. */
double i_gain_ = 0.0; /**< Integral gain. */
double d_gain_ = 0.0; /**< Derivative gain. */
double i_max_ = 0.0; /**< Maximum allowable integral term. */
double i_min_ = 0.0; /**< Minimum allowable integral term. */
double p_gain_ = 0.0; /**< Proportional gain. */
double i_gain_ = 0.0; /**< Integral gain. */
double d_gain_ = 0.0; /**< Derivative gain. */
double u_max_ = std::numeric_limits<double>::infinity(); /**< Maximum allowable output. */
double u_min_ = -std::numeric_limits<double>::infinity(); /**< Minimum allowable output. */
bool antiwindup_ = false; /**< Anti-windup. */
AntiWindupStrategy antiwindup_strat_; /**< Anti-windup strategy. */
};

/*!
* \brief Constructor, zeros out Pid values when created and
* initialize Pid-gains and integral term limits.
*
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max Upper integral clamp.
* \param i_min Lower integral clamp.
* \param antiwindup Anti-windup functionality. When set to true, limits
the integral error to prevent windup; otherwise, constrains the
integral contribution to the control output. i_max and
i_min are applied in both scenarios.
*
* \throws An std::invalid_argument exception is thrown if i_min > i_max
*/
[[deprecated("Use constructor with AntiWindupStrategy only.")]]
Pid(
double p = 0.0, double i = 0.0, double d = 0.0, double i_max = 0.0, double i_min = -0.0,
bool antiwindup = false);

/*!
* \brief Constructor, initialize Pid-gains and term limits.
*
Expand All @@ -474,8 +337,10 @@ class Pid
* \throws An std::invalid_argument exception is thrown if u_min > u_max
*/
Pid(
double p, double i, double d, double u_max, double u_min,
const AntiWindupStrategy & antiwindup_strat);
double p = 0.0, double i = 0.0, double d = 0.0,
double u_max = std::numeric_limits<double>::infinity(),
double u_min = -std::numeric_limits<double>::infinity(),
const AntiWindupStrategy & antiwindup_strat = AntiWindupStrategy());

/*!
* \brief Copy constructor required for preventing mutexes from being copied
Expand All @@ -488,25 +353,6 @@ class Pid
*/
~Pid();

/*!
* \brief Zeros out Pid values and initialize Pid-gains and term limits
*
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max Upper integral clamp.
* \param i_min Lower integral clamp.
* \param antiwindup Anti-windup functionality. When set to true, limits
the integral error to prevent windup; otherwise, constrains the
integral contribution to the control output. i_max and
i_min are applied in both scenarios.
* \return True if all parameters are successfully set, False otherwise.
* \note New gains are not applied if i_min_ > i_max_
*/
[[deprecated("Use initialize with AntiWindupStrategy instead.")]]
bool initialize(
double p, double i, double d, double i_max, double i_min, bool antiwindup = false);

/*!
* \brief Initialize Pid-gains and term limits.
*
Expand Down Expand Up @@ -544,36 +390,6 @@ class Pid
*/
void clear_saved_iterm();

/*!
* \brief Get PID gains for the controller.
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max Upper integral clamp.
* \param i_min Lower integral clamp.
*
* \note This method is not RT safe
*/
void get_gains(double & p, double & i, double & d, double & i_max, double & i_min);

/*!
* \brief Get PID gains for the controller.
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max Upper integral clamp.
* \param i_min Lower integral clamp.
* \param antiwindup Anti-windup functionality. When set to true, limits
the integral error to prevent windup; otherwise, constrains the
integral contribution to the control output. i_max and
i_min are applied in both scenarios.
*
* \note This method is not RT safe
*/
[[deprecated("Use get_gains overload with AntiWindupStrategy argument.")]]
void get_gains(
double & p, double & i, double & d, double & i_max, double & i_min, bool & antiwindup);

/*!
* \brief Get PID gains for the controller (preferred).
* \param p The proportional gain.
Expand Down Expand Up @@ -607,25 +423,6 @@ class Pid
*/
Gains get_gains_rt() { return gains_; }

/*!
* \brief Set PID gains for the controller.
* \param p The proportional gain.
* \param i The integral gain.
* \param d The derivative gain.
* \param i_max Upper integral clamp.
* \param i_min Lower integral clamp.
* \param antiwindup Anti-windup functionality. When set to true, limits
the integral error to prevent windup; otherwise, constrains the
integral contribution to the control output. i_max and
i_min are applied in both scenarios.
* \return True if all parameters are successfully set, False otherwise.
*
* \note New gains are not applied if i_min > i_max
* \note This method is not RT safe
*/
[[deprecated("Use set_gains with AntiWindupStrategy instead.")]]
bool set_gains(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);

/*!
* \brief Set PID gains for the controller.
*
Expand Down
Loading