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())
287 double p,
double i,
double d,
double u_max,
double u_min,
300 bool validate(std::string &
error_msg)
const
309 error_msg =
"Gains: u_min and u_max must not be NaN";
316 catch (
const std::exception & e)
318 error_msg = e.what();
327 <<
", u_max: " <<
u_max_ <<
", u_min: " <<
u_min_ << std::endl;
335 std::numeric_limits<double>::infinity();
337 -std::numeric_limits<double>::infinity();
338 double u_max_ = std::numeric_limits<double>::infinity();
339 double u_min_ = -std::numeric_limits<double>::infinity();
358 double p = 0.0,
double i = 0.0,
double d = 0.0,
359 double u_max = std::numeric_limits<double>::infinity(),
360 double u_min = -std::numeric_limits<double>::infinity(),
390 double p,
double i,
double d,
double u_max,
double u_min,
425 double &
p,
double & i,
double &
d,
double &
u_max,
double &
u_min,
461 double p,
double i,
double d,
double u_max,
double u_min,
574 double error,
double error_dot,
const std::chrono::nanoseconds &
dt_ns);
606 gains_box_ =
source.gains_box_;
620 std::numeric_limits<double>::infinity(),
621 -std::numeric_limits<double>::infinity(),
627 double p_error_last_ = 0;