33#ifndef CONTROL_TOOLBOX__PID_HPP_
34#define CONTROL_TOOLBOX__PID_HPP_
42#include "fmt/format.h"
43#include "rclcpp/duration.hpp"
44#include "realtime_tools/realtime_thread_safe_box.hpp"
80 CONDITIONAL_INTEGRATION
85 i_min(-std::numeric_limits<double>::infinity()),
86 i_max(std::numeric_limits<double>::infinity()),
93 void set_type(
const std::string &
s)
95 if (
s ==
"back_calculation")
97 type = BACK_CALCULATION;
99 else if (
s ==
"conditional_integration")
101 type = CONDITIONAL_INTEGRATION;
103 else if (
s ==
"legacy")
106 std::cout <<
"Using the legacy anti-windup technique is deprecated. This option will be "
107 "removed by the ROS 2 Kilted Kaiju release."
110 else if (
s ==
"none")
117 throw std::invalid_argument(
118 "AntiWindupStrategy: Unknown antiwindup strategy : '" +
s +
119 "'. Valid strategies are: 'back_calculation', 'conditional_integration', 'legacy', "
124 void validate()
const
126 if (type == UNDEFINED)
128 throw std::invalid_argument(
"AntiWindupStrategy is UNDEFINED. Please set a valid type");
131 type == BACK_CALCULATION &&
134 throw std::invalid_argument(
135 "AntiWindupStrategy 'back_calculation' requires a valid positive tracking time constant "
136 "(tracking_time_constant)");
140 throw std::invalid_argument(
141 fmt::format(
"PID requires i_min to be smaller or equal to 0 (i_min: {})",
i_min));
145 throw std::invalid_argument(
146 fmt::format(
"PID requires i_max to be greater or equal to 0 (i_max: {})",
i_max));
149 type != NONE && type != UNDEFINED && type != LEGACY && type != BACK_CALCULATION &&
150 type != CONDITIONAL_INTEGRATION)
152 throw std::invalid_argument(
"AntiWindupStrategy has an invalid type");
158 std::cout <<
"antiwindup_strat: " << to_string() <<
"\ti_max: " <<
i_max <<
", i_min: " <<
i_min
163 operator std::string()
const {
return to_string(); }
165 constexpr bool operator==(Value
other)
const {
return type ==
other; }
166 constexpr bool operator!=(Value
other)
const {
return type !=
other; }
168 std::string to_string()
const
172 case BACK_CALCULATION:
173 return "back_calculation";
174 case CONDITIONAL_INTEGRATION:
175 return "conditional_integration";
186 Value type = UNDEFINED;
187 double i_min = -std::numeric_limits<double>::infinity();
188 double i_max = std::numeric_limits<double>::infinity();
197 std::numeric_limits<double>::epsilon();
201inline bool is_zero(T value, T
tolerance = std::numeric_limits<T>::epsilon())
301 [[deprecated(
"Use constructor with AntiWindupStrategy instead.")]]
302 Gains(
double p,
double i,
double d,
double i_max,
double i_min)
332 [[deprecated(
"Use constructor with AntiWindupStrategy instead.")]]
363 double p,
double i,
double d,
double u_max,
double u_min,
377 bool validate(std::string &
error_msg)
const
386 error_msg =
"Gains: u_min and u_max must not be NaN";
393 catch (
const std::exception & e)
395 error_msg = e.what();
403 "Use constructor with AntiWindupStrategy only. The default constructor might be deleted in "
411 <<
", u_max: " <<
u_max_ <<
", u_min: " <<
u_min_ << std::endl;
419 std::numeric_limits<double>::infinity();
421 -std::numeric_limits<double>::infinity();
422 double u_max_ = std::numeric_limits<double>::infinity();
423 double u_min_ = -std::numeric_limits<double>::infinity();
444 [[deprecated(
"Use constructor with AntiWindupStrategy only.")]]
446 double p = 0.0,
double i = 0.0,
double d = 0.0,
447 double i_max = std::numeric_limits<double>::infinity(),
448 double i_min = -std::numeric_limits<double>::infinity(),
bool antiwindup =
false);
465 double p,
double i,
double d,
double u_max,
double u_min,
494 [[deprecated(
"Use initialize with AntiWindupStrategy instead.")]]
496 double p,
double i,
double d,
double i_max,
double i_min,
bool antiwindup =
false);
513 [[deprecated(
"Use initialize() instead")]]
void initPid(
514 double p,
double i,
double d,
double i_max,
double i_min,
bool antiwindup =
false);
532 double p,
double i,
double d,
double u_max,
double u_min,
563 void get_gains(
double &
p,
double & i,
double &
d,
double & i_max,
double & i_min);
573 [[deprecated(
"Use get_gains() instead")]]
void getGains(
574 double &
p,
double & i,
double &
d,
double & i_max,
double & i_min);
590 [[deprecated(
"Use get_gains overload with AntiWindupStrategy argument.")]]
592 double &
p,
double & i,
double &
d,
double & i_max,
double & i_min,
bool &
antiwindup);
606 [[deprecated(
"Use get_gains() instead")]]
void getGains(
607 double &
p,
double & i,
double &
d,
double & i_max,
double & i_min,
bool &
antiwindup);
623 double &
p,
double & i,
double &
d,
double &
u_max,
double &
u_min,
666 [[deprecated(
"Use set_gains with AntiWindupStrategy instead.")]]
683 [[deprecated(
"Use set_gains() instead")]]
void setGains(
684 double p,
double i,
double d,
double i_max,
double i_min,
bool antiwindup =
false);
703 double p,
double i,
double d,
double u_max,
double u_min,
722 [[deprecated(
"Use set_gains() instead")]]
void setGains(
const Gains &
gains);
851 double error,
double error_dot,
const std::chrono::nanoseconds &
dt_ns);
861 [[deprecated(
"Use set_current_cmd() instead")]]
void setCurrentCmd(
double cmd);
871 [[deprecated(
"Use get_current_cmd() instead")]]
double getCurrentCmd();
893 double &
pe,
double &
ie,
double &
de);
907 gains_box_ =
source.gains_box_;
921 std::numeric_limits<double>::infinity(),
922 -std::numeric_limits<double>::infinity(),
928 double p_error_last_ = 0;