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");
142 void print()
const { std::cout << string() << std::endl; }
144 std::string string()
const
147 "antiwindup_strat: {}\ti_max: {}\ti_min: {}\ttracking_time_constant: {}\terror_deadband: {}",
151 operator std::string()
const {
return to_string(); }
153 constexpr bool operator==(Value
other)
const {
return type ==
other; }
154 constexpr bool operator!=(Value
other)
const {
return type !=
other; }
156 std::string to_string()
const
160 case BACK_CALCULATION:
161 return "back_calculation";
162 case CONDITIONAL_INTEGRATION:
163 return "conditional_integration";
172 Value type = UNDEFINED;
173 double i_max = std::numeric_limits<double>::infinity();
174 double i_min = -std::numeric_limits<double>::infinity();
181 std::numeric_limits<double>::epsilon();
185inline bool is_zero(T value, T
tolerance = std::numeric_limits<T>::epsilon())
289 double p,
double i,
double d,
double u_max,
double u_min,
302 bool validate(std::string &
error_msg)
const
311 error_msg =
"Gains: u_min and u_max must not be NaN";
318 catch (
const std::exception & e)
320 error_msg = e.what();
326 void print()
const { std::cout << string() << std::endl; }
328 std::string string()
const
331 "Gains(p: {}, i: {}, d: {}, u_max: {}, u_min: {}, antiwindup_strat: '{}')",
p_gain_,
339 std::numeric_limits<double>::infinity();
341 -std::numeric_limits<double>::infinity();
342 double u_max_ = std::numeric_limits<double>::infinity();
343 double u_min_ = -std::numeric_limits<double>::infinity();
362 double p = 0.0,
double i = 0.0,
double d = 0.0,
363 double u_max = std::numeric_limits<double>::infinity(),
364 double u_min = -std::numeric_limits<double>::infinity(),
394 double p,
double i,
double d,
double u_max,
double u_min,
429 double &
p,
double & i,
double &
d,
double &
u_max,
double &
u_min,
465 double p,
double i,
double d,
double u_max,
double u_min,
578 double error,
double error_dot,
const std::chrono::nanoseconds &
dt_ns);
610 gains_box_ =
source.gains_box_;
624 std::numeric_limits<double>::infinity(),
625 -std::numeric_limits<double>::infinity(),
631 double p_error_last_ = 0;