From 838107ae3aab4c941f1894bfcc7e7ae5feada271 Mon Sep 17 00:00:00 2001 From: Christoph Froehlich Date: Sat, 7 Dec 2024 09:11:29 +0000 Subject: [PATCH] Add specializations for duration types --- include/control_toolbox/pid.hpp | 59 +++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/include/control_toolbox/pid.hpp b/include/control_toolbox/pid.hpp index b00321c..6cfcd0f 100644 --- a/include/control_toolbox/pid.hpp +++ b/include/control_toolbox/pid.hpp @@ -38,6 +38,7 @@ #include #include +#include "rclcpp/duration.hpp" #include "realtime_tools/realtime_buffer.h" #include "control_toolbox/visibility_control.hpp" @@ -275,6 +276,34 @@ class CONTROL_TOOLBOX_PUBLIC Pid */ [[nodiscard]] double computeCommand(double error, uint64_t dt_ns); + /*! + * \brief Set the PID error and compute the PID command with nonuniform time + * step size. The derivative error is computed from the change in the error + * and the timestep \c dt. + * + * \param error Error since last call (error = target - state) + * \param dt Change in time since last call + * + * \returns PID command + */ + [[nodiscard]] double computeCommand(double error, rcl_duration_value_t dt) { + return computeCommand(error, static_cast(dt)); + } + + /*! + * \brief Set the PID error and compute the PID command with nonuniform time + * step size. The derivative error is computed from the change in the error + * and the timestep \c dt. + * + * \param error Error since last call (error = target - state) + * \param dt Change in time since last call + * + * \returns PID command + */ + [[nodiscard]] double computeCommand(double error, std::chrono::nanoseconds dt_ns) { + return computeCommand(error, static_cast(dt_ns.count())); + } + /*! * \brief Set the PID error and compute the PID command with nonuniform * time step size. This also allows the user to pass in a precomputed @@ -301,6 +330,36 @@ class CONTROL_TOOLBOX_PUBLIC Pid */ [[nodiscard]] double computeCommand(double error, double error_dot, uint64_t dt_ns); + /*! + * \brief Set the PID error and compute the PID command with nonuniform + * time step size. This also allows the user to pass in a precomputed + * derivative error. + * + * \param error Error since last call (error = target - state) + * \param error_dot d(Error)/dt since last call + * \param dt Change in time since last call + * + * \returns PID command + */ + [[nodiscard]] double computeCommand(double error, double error_dot, rcl_duration_value_t dt) { + return computeCommand(error, error_dot, static_cast(dt)); + } + + /*! + * \brief Set the PID error and compute the PID command with nonuniform + * time step size. This also allows the user to pass in a precomputed + * derivative error. + * + * \param error Error since last call (error = target - state) + * \param error_dot d(Error)/dt since last call + * \param dt Change in time since last call + * + * \returns PID command + */ + [[nodiscard]] double computeCommand(double error, double error_dot, std::chrono::nanoseconds dt_ns) { + return computeCommand(error, error_dot, static_cast(dt_ns.count())); + } + /*! * \brief Set current command for this PID controller */