ros2_control - humble
Loading...
Searching...
No Matches
pid_ros.hpp
1// Copyright (c) 2020, Open Source Robotics Foundation, Inc.
2// All rights reserved.
3//
4// Software License Agreement (BSD License 2.0)
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions
8// are met:
9//
10// * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following
14// disclaimer in the documentation and/or other materials provided
15// with the distribution.
16// * Neither the name of the Willow Garage nor the names of its
17// contributors may be used to endorse or promote products derived
18// from this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31// POSSIBILITY OF SUCH DAMAGE.
32
33#ifndef CONTROL_TOOLBOX__PID_ROS_HPP_
34#define CONTROL_TOOLBOX__PID_ROS_HPP_
35
36#include <memory>
37#include <string>
38
39#include "control_msgs/msg/pid_state.hpp"
40
41#include "rclcpp/clock.hpp"
42#include "rclcpp/duration.hpp"
43#include "rclcpp/node.hpp"
44
45#include "realtime_tools/realtime_buffer.hpp"
46#include "realtime_tools/realtime_publisher.hpp"
47
48#include "control_toolbox/pid.hpp"
49
50namespace control_toolbox
51{
52
53class PidROS
54{
55public:
70 template<class NodeT>
71 explicit PidROS(
72 std::shared_ptr<NodeT> node_ptr,
73 std::string prefix = std::string(""),
74 bool prefix_is_for_params = false
75 )
76 : PidROS(
77 node_ptr->get_node_base_interface(),
78 node_ptr->get_node_logging_interface(),
79 node_ptr->get_node_parameters_interface(),
80 node_ptr->get_node_topics_interface(),
81 prefix, prefix_is_for_params)
82 {
83 }
84
85 PidROS(
86 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base,
87 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
88 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params,
89 rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface,
90 std::string prefix = std::string(""), bool prefix_is_for_params = false);
91
103 void initPid(double p, double i, double d, double i_max, double i_min, bool antiwindup);
104
109 bool initPid();
110
114 void reset();
115
126 double computeCommand(double error, rclcpp::Duration dt);
127
139 double computeCommand(double error, double error_dot, rclcpp::Duration dt);
140
146
158 void setGains(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);
159
166 void setGains(const Pid::Gains & gains);
167
172 void setCurrentCmd(double cmd);
173
178 double getCurrentCmd();
179
184 std::shared_ptr<rclcpp::Publisher<control_msgs::msg::PidState>> getPidStatePublisher();
185
192 void getCurrentPIDErrors(double & pe, double & ie, double & de);
193
197 void printValues();
198
203 inline rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr
205 {
206 return parameter_callback_;
207 }
208
209protected:
210 std::string topic_prefix_;
211 std::string param_prefix_;
212
213private:
214 void setParameterEventCallback();
215
216 void publishPIDState(double cmd, double error, rclcpp::Duration dt);
217
218 void declareParam(const std::string & param_name, rclcpp::ParameterValue param_value);
219
220 bool getDoubleParam(const std::string & param_name, double & value);
221
222 bool getBooleanParam(const std::string & param_name, bool & value);
223
230 void initialize(std::string topic_prefix);
231
232 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr parameter_callback_;
233
234 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
235 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
236 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params_;
237 rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface_;
238
239 std::shared_ptr<realtime_tools::RealtimePublisher<control_msgs::msg::PidState>> rt_state_pub_;
240 std::shared_ptr<rclcpp::Publisher<control_msgs::msg::PidState>> state_pub_;
241
242 Pid pid_;
243};
244
245} // namespace control_toolbox
246
247#endif // CONTROL_TOOLBOX__PID_ROS_HPP_
Definition pid_ros.hpp:54
void setCurrentCmd(double cmd)
Set current command for this PID controller.
Definition pid_ros.cpp:284
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr getParametersCallbackHandle()
Return PID parameters callback handle.
Definition pid_ros.hpp:204
void getCurrentPIDErrors(double &pe, double &ie, double &de)
Return PID error terms for the controller.
Definition pid_ros.cpp:288
double computeCommand(double error, 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:222
double getCurrentCmd()
Return current command for this PID controller.
Definition pid_ros.cpp:286
std::shared_ptr< rclcpp::Publisher< control_msgs::msg::PidState > > getPidStatePublisher()
Return PID state publisher.
Definition pid_ros.cpp:217
bool initPid()
Initialize the PID controller based on already set parameters.
Definition pid_ros.cpp:167
PidROS(std::shared_ptr< NodeT > node_ptr, std::string prefix=std::string(""), bool prefix_is_for_params=false)
Constructor of PidROS class.
Definition pid_ros.hpp:71
void printValues()
Print to console the current parameters.
Definition pid_ros.cpp:297
void setGains(double p, double i, double d, double i_max, double i_min, bool antiwindup=false)
Set PID gains for the controller.
Definition pid_ros.cpp:240
Pid::Gains getGains()
Get PID gains for the controller.
Definition pid_ros.cpp:238
void reset()
Reset the state of this PID controller.
Definition pid_ros.cpp:215
A basic pid class.
Definition pid.hpp:104
Definition dither.hpp:46
Store gains in a struct to allow easier realtime buffer usage.
Definition pid.hpp:110