ros2_control - rolling
Loading...
Searching...
No Matches
pid_ros.hpp
1// Copyright (c) 2020, Open Source Robotics Foundation, Inc.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8//
9// * Redistributions in binary form must reproduce the above copyright
10// notice, this list of conditions and the following disclaimer in the
11// documentation and/or other materials provided with the distribution.
12//
13// * Neither the name of the Willow Garage nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28
29#ifndef CONTROL_TOOLBOX__PID_ROS_HPP_
30#define CONTROL_TOOLBOX__PID_ROS_HPP_
31
32#include <memory>
33#include <string>
34
35#include "control_msgs/msg/pid_state.hpp"
36
37#include "rclcpp/clock.hpp"
38#include "rclcpp/duration.hpp"
39#include "rclcpp/node.hpp"
40
41#include "realtime_tools/realtime_publisher.hpp"
42
43#include "control_toolbox/pid.hpp"
44
45namespace control_toolbox
46{
47
48class PidROS
49{
50public:
66 template <class NodeT>
67 explicit PidROS(
68 std::shared_ptr<NodeT> node_ptr, const std::string & param_prefix,
69 const std::string & topic_prefix)
70 : PidROS(
71 node_ptr->get_node_base_interface(), node_ptr->get_node_logging_interface(),
72 node_ptr->get_node_parameters_interface(), node_ptr->get_node_topics_interface(),
73 param_prefix, topic_prefix, !topic_prefix.empty())
74 {
75 }
76
91 template <class NodeT>
92 explicit PidROS(
93 std::shared_ptr<NodeT> node_ptr, const std::string & param_prefix,
94 const std::string & topic_prefix, bool activate_state_publisher)
95 : PidROS(
96 node_ptr->get_node_base_interface(), node_ptr->get_node_logging_interface(),
97 node_ptr->get_node_parameters_interface(), node_ptr->get_node_topics_interface(),
98 param_prefix, topic_prefix, activate_state_publisher)
99 {
100 }
101
113 PidROS(
114 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base,
115 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
116 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params,
117 rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface,
118 const std::string & param_prefix, const std::string & topic_prefix,
119 bool activate_state_publisher);
120
138 double p, double i, double d, double u_max, double u_min,
139 const AntiWindupStrategy & antiwindup_strat, bool save_i_term);
140
147
153 void reset();
154
160 void reset(bool save_i_term);
161
172 double compute_command(double error, const rclcpp::Duration & dt);
173
185 double compute_command(double error, double error_dot, const rclcpp::Duration & dt);
186
194
211 bool set_gains(
212 double p, double i, double d, double u_max, double u_min,
213 const AntiWindupStrategy & antiwindup_strat);
214
223 bool set_gains(const Pid::Gains & gains);
224
229 void set_current_cmd(double cmd);
230
235 double get_current_cmd();
236
243 void get_current_pid_errors(double & pe, double & ie, double & de);
244
248 void print_values();
249
254 inline rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr
256 {
257 return parameter_callback_;
258 }
259
260protected:
261 std::string topic_prefix_;
262 std::string param_prefix_;
263
264private:
265 void set_parameter_event_callback();
266
267 void publish_pid_state(double cmd, double error, rclcpp::Duration dt);
268
269 void declare_param(const std::string & param_name, rclcpp::ParameterValue param_value);
270
271 bool get_double_param(const std::string & param_name, double & value);
272
273 bool get_boolean_param(const std::string & param_name, bool & value);
274
275 bool get_string_param(const std::string & param_name, std::string & value);
276
277 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr parameter_callback_;
278
279 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
280 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
281 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params_;
282 rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface_;
283
284 std::shared_ptr<realtime_tools::RealtimePublisher<control_msgs::msg::PidState>> rt_state_pub_;
285 control_msgs::msg::PidState pid_state_msg_;
286
287 Pid pid_;
288};
289
290} // namespace control_toolbox
291
292#endif // CONTROL_TOOLBOX__PID_ROS_HPP_
Definition pid_ros.hpp:49
void print_values()
Print to console the current parameters.
Definition pid_ros.cpp:389
bool initialize_from_ros_parameters()
Initialize the PID controller based on already set parameters.
Definition pid_ros.cpp:155
PidROS(std::shared_ptr< NodeT > node_ptr, const std::string &param_prefix, const std::string &topic_prefix)
Constructor of PidROS class.
Definition pid_ros.hpp:67
void set_current_cmd(double cmd)
Set current command for this PID controller.
Definition pid_ros.cpp:376
PidROS(std::shared_ptr< NodeT > node_ptr, const std::string &param_prefix, const std::string &topic_prefix, bool activate_state_publisher)
Constructor of PidROS class.
Definition pid_ros.hpp:92
void get_current_pid_errors(double &pe, double &ie, double &de)
Return PID error terms for the controller.
Definition pid_ros.cpp:380
double get_current_cmd()
Return current command for this PID controller.
Definition pid_ros.cpp:378
double compute_command(double error, const rclcpp::Duration &dt)
Set the PID error and compute the PID command with nonuniform time step size. The derivative error is...
Definition pid_ros.cpp:292
bool set_gains(double p, double i, double d, double u_max, double u_min, const AntiWindupStrategy &antiwindup_strat)
Set PID gains for the controller.
Definition pid_ros.cpp:308
bool initialize_from_args(double p, double i, double d, double u_max, double u_min, const AntiWindupStrategy &antiwindup_strat, bool save_i_term)
Initialize the PID controller and set the parameters.
Definition pid_ros.cpp:228
Pid::Gains get_gains()
Get PID gains for the controller.
Definition pid_ros.cpp:306
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr get_parameters_callback_handle()
Return PID parameters callback handle.
Definition pid_ros.hpp:255
void reset()
Reset the state of this PID controller.
Definition pid_ros.cpp:283
Generic Proportional–Integral–Derivative (PID) controller.
Definition pid.hpp:264
Definition dither.hpp:42
Antiwindup strategy for PID controllers.
Definition pid.hpp:63
Store gains in a struct to allow easier realtime box usage.
Definition pid.hpp:270