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"
74 CONDITIONAL_INTEGRATION
79 i_max(std::numeric_limits<double>::infinity()),
80 i_min(-std::numeric_limits<double>::infinity()),
86 void set_type(
const std::string & s)
88 if (s ==
"back_calculation")
90 type = BACK_CALCULATION;
92 else if (s ==
"conditional_integration")
94 type = CONDITIONAL_INTEGRATION;
103 throw std::invalid_argument(
104 "AntiWindupStrategy: Unknown antiwindup strategy : '" + s +
105 "'. Valid strategies are: 'back_calculation', 'conditional_integration', "
110 void validate()
const
112 if (type == UNDEFINED)
114 throw std::invalid_argument(
"AntiWindupStrategy is UNDEFINED. Please set a valid type");
117 type == BACK_CALCULATION &&
120 throw std::invalid_argument(
121 "AntiWindupStrategy 'back_calculation' requires a valid positive tracking time constant "
122 "(tracking_time_constant)");
126 throw std::invalid_argument(
127 fmt::format(
"PID requires i_min to be smaller or equal to 0 (i_min: {})",
i_min));
131 throw std::invalid_argument(
132 fmt::format(
"PID requires i_max to be greater or equal to 0 (i_max: {})",
i_max));
135 type != NONE && type != UNDEFINED && type != BACK_CALCULATION &&
136 type != CONDITIONAL_INTEGRATION)
138 throw std::invalid_argument(
"AntiWindupStrategy has an invalid type");
144 std::cout <<
"antiwindup_strat: " << to_string() <<
"\ti_max: " <<
i_max <<
", i_min: " <<
i_min
149 operator std::string()
const {
return to_string(); }
151 constexpr bool operator==(Value other)
const {
return type == other; }
152 constexpr bool operator!=(Value other)
const {
return type != other; }
154 std::string to_string()
const
158 case BACK_CALCULATION:
159 return "back_calculation";
160 case CONDITIONAL_INTEGRATION:
161 return "conditional_integration";
170 Value type = UNDEFINED;
171 double i_max = std::numeric_limits<double>::infinity();
172 double i_min = -std::numeric_limits<double>::infinity();
179 std::numeric_limits<double>::epsilon();
183inline bool is_zero(T value, T tolerance = std::numeric_limits<T>::epsilon())
185 return std::abs(value) <= tolerance;
287 double p,
double i,
double d,
double u_max,
double u_min,
298 bool validate(std::string & error_msg)
const
302 error_msg = fmt::format(
"Gains: u_min ({}) must be less than u_max ({})",
u_min_,
u_max_);
307 error_msg =
"Gains: u_min and u_max must not be NaN";
314 catch (
const std::exception & e)
316 error_msg = e.what();
325 <<
", u_max: " <<
u_max_ <<
", u_min: " <<
u_min_ << std::endl;
332 double u_max_ = std::numeric_limits<double>::infinity();
333 double u_min_ = -std::numeric_limits<double>::infinity();
352 double p = 0.0,
double i = 0.0,
double d = 0.0,
353 double u_max = std::numeric_limits<double>::infinity(),
354 double u_min = -std::numeric_limits<double>::infinity(),
384 double p,
double i,
double d,
double u_max,
double u_min,
398 void reset(
bool save_i_term);
419 double & p,
double & i,
double & d,
double & u_max,
double & u_min,
455 double p,
double i,
double d,
double u_max,
double u_min,
478 [[nodiscard]]
double compute_command(
double error,
const double & dt_s);
490 [[nodiscard]]
double compute_command(
double error,
const rcl_duration_value_t & dt_ns);
502 [[nodiscard]]
double compute_command(
double error,
const rclcpp::Duration & dt);
514 [[nodiscard]]
double compute_command(
double error,
const std::chrono::nanoseconds & dt_ns);
527 [[nodiscard]]
double compute_command(
double error,
double error_dot,
const double & dt_s);
541 double error,
double error_dot,
const rcl_duration_value_t & dt_ns);
554 [[nodiscard]]
double compute_command(
double error,
double error_dot,
const rclcpp::Duration & dt);
568 double error,
double error_dot,
const std::chrono::nanoseconds & dt_ns);
600 gains_box_ = source.gains_box_;
614 std::numeric_limits<double>::infinity(),
615 -std::numeric_limits<double>::infinity(),
621 double p_error_last_ = 0;