29#ifndef CONTROL_TOOLBOX__PID_HPP_
30#define CONTROL_TOOLBOX__PID_HPP_
38#include "fmt/format.h"
39#include "rclcpp/duration.hpp"
40#include "realtime_tools/realtime_thread_safe_box.hpp"
70 CONDITIONAL_INTEGRATION
75 i_max(std::numeric_limits<double>::infinity()),
76 i_min(-std::numeric_limits<double>::infinity()),
82 void set_type(
const std::string & s)
84 if (s ==
"back_calculation")
86 type = BACK_CALCULATION;
88 else if (s ==
"conditional_integration")
90 type = CONDITIONAL_INTEGRATION;
99 throw std::invalid_argument(
100 "AntiWindupStrategy: Unknown antiwindup strategy : '" + s +
101 "'. Valid strategies are: 'back_calculation', 'conditional_integration', "
106 void validate()
const
108 if (type == UNDEFINED)
110 throw std::invalid_argument(
"AntiWindupStrategy is UNDEFINED. Please set a valid type");
113 type == BACK_CALCULATION &&
116 throw std::invalid_argument(
117 "AntiWindupStrategy 'back_calculation' requires a valid positive tracking time constant "
118 "(tracking_time_constant)");
122 throw std::invalid_argument(
123 fmt::format(
"PID requires i_min to be smaller or equal to 0 (i_min: {})",
i_min));
127 throw std::invalid_argument(
128 fmt::format(
"PID requires i_max to be greater or equal to 0 (i_max: {})",
i_max));
131 type != NONE && type != UNDEFINED && type != BACK_CALCULATION &&
132 type != CONDITIONAL_INTEGRATION)
134 throw std::invalid_argument(
"AntiWindupStrategy has an invalid type");
140 std::cout <<
"antiwindup_strat: " << to_string() <<
"\ti_max: " <<
i_max <<
", i_min: " <<
i_min
145 operator std::string()
const {
return to_string(); }
147 constexpr bool operator==(Value other)
const {
return type == other; }
148 constexpr bool operator!=(Value other)
const {
return type != other; }
150 std::string to_string()
const
154 case BACK_CALCULATION:
155 return "back_calculation";
156 case CONDITIONAL_INTEGRATION:
157 return "conditional_integration";
166 Value type = UNDEFINED;
167 double i_max = std::numeric_limits<double>::infinity();
168 double i_min = -std::numeric_limits<double>::infinity();
175 std::numeric_limits<double>::epsilon();
179inline bool is_zero(T value, T tolerance = std::numeric_limits<T>::epsilon())
181 return std::abs(value) <= tolerance;
283 double p,
double i,
double d,
double u_max,
double u_min,
294 bool validate(std::string & error_msg)
const
298 error_msg = fmt::format(
"Gains: u_min ({}) must be less than u_max ({})",
u_min_,
u_max_);
303 error_msg =
"Gains: u_min and u_max must not be NaN";
310 catch (
const std::exception & e)
312 error_msg = e.what();
321 <<
", u_max: " <<
u_max_ <<
", u_min: " <<
u_min_ << std::endl;
328 double u_max_ = std::numeric_limits<double>::infinity();
329 double u_min_ = -std::numeric_limits<double>::infinity();
348 double p = 0.0,
double i = 0.0,
double d = 0.0,
349 double u_max = std::numeric_limits<double>::infinity(),
350 double u_min = -std::numeric_limits<double>::infinity(),
380 double p,
double i,
double d,
double u_max,
double u_min,
394 void reset(
bool save_i_term);
415 double & p,
double & i,
double & d,
double & u_max,
double & u_min,
451 double p,
double i,
double d,
double u_max,
double u_min,
474 [[nodiscard]]
double compute_command(
double error,
const double & dt_s);
486 [[nodiscard]]
double compute_command(
double error,
const rcl_duration_value_t & dt_ns);
498 [[nodiscard]]
double compute_command(
double error,
const rclcpp::Duration & dt);
510 [[nodiscard]]
double compute_command(
double error,
const std::chrono::nanoseconds & dt_ns);
523 [[nodiscard]]
double compute_command(
double error,
double error_dot,
const double & dt_s);
537 double error,
double error_dot,
const rcl_duration_value_t & dt_ns);
550 [[nodiscard]]
double compute_command(
double error,
double error_dot,
const rclcpp::Duration & dt);
564 double error,
double error_dot,
const std::chrono::nanoseconds & dt_ns);
596 gains_box_ = source.gains_box_;
610 std::numeric_limits<double>::infinity(),
611 -std::numeric_limits<double>::infinity(),
617 double p_error_last_ = 0;