ros2_control - rolling
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
107 double p, double i, double d, double i_max, double i_min, bool antiwindup);
108
122 [[deprecated("Use initialize_from_args() instead")]] void initPid(
123 double p, double i, double d, double i_max, double i_min, bool antiwindup);
124
138 double p, double i, double d, double i_max, double i_min, bool antiwindup, bool save_iterm);
139
152 [[deprecated("Use initialize_from_args() instead")]] void initPid(double p, double i, double d,
153 double i_max, double i_min, bool antiwindup, bool save_iterm);
154
160
165 [[deprecated("Use initialize_from_ros_parameters() instead")]] bool initPid();
166
172 void reset();
173
179 void reset(bool save_iterm);
180
191 double compute_command(double error, const rclcpp::Duration & dt);
192
203 [[deprecated("Use compute_command() instead")]] double computeCommand(
204 double error, rclcpp::Duration dt);
205
217 double compute_command(double error, double error_dot, const rclcpp::Duration & dt);
218
230 [[deprecated("Use compute_command() instead")]] double computeCommand(
231 double error, double error_dot, rclcpp::Duration dt);
232
238
243 [[deprecated("Use get_gains() instead")]] Pid::Gains getGains();
244
259 void set_gains(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);
260
275 [[deprecated("Use set_gains() instead")]] void setGains(
276 double p, double i, double d, double i_max, double i_min, bool antiwindup = false);
277
284 void set_gains(const Pid::Gains & gains);
285
292 [[deprecated("Use set_gains() instead")]] void setGains(const Pid::Gains & gains);
293
298 void set_current_cmd(double cmd);
299
304 [[deprecated("Use set_current_cmd() instead")]] void setCurrentCmd(double cmd);
305
310 double get_current_cmd();
311
316 [[deprecated("Use get_current_cmd() instead")]] double getCurrentCmd();
317
322 std::shared_ptr<rclcpp::Publisher<control_msgs::msg::PidState>> get_pid_state_publisher();
323
328 [[deprecated("Use get_pid_state_publisher() instead")]]
329 std::shared_ptr<rclcpp::Publisher<control_msgs::msg::PidState>> getPidStatePublisher();
330
337 void get_current_pid_errors(double & pe, double & ie, double & de);
338
345 [[deprecated("Use get_current_pid_errors() instead")]] void getCurrentPIDErrors(
346 double & pe, double & ie, double & de);
347
351 void print_values();
352
356 [[deprecated("Use print_values() instead")]] void printValues();
357
362 inline rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr
364 {
365 return parameter_callback_;
366 }
367
372 [[deprecated("Use get_parameters_callback_handle() instead")]]
373 inline rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr
378
379protected:
380 std::string topic_prefix_;
381 std::string param_prefix_;
382
383private:
384 // DEPRECATION START
385 // this was added to avoid ABI breaks
386 [[deprecated]] void setParameterEventCallback();
387
388 [[deprecated]] void publishPIDState(double cmd, double error, rclcpp::Duration dt);
389
390 [[deprecated]] void declareParam(const std::string & param_name,
391 rclcpp::ParameterValue param_value);
392
393 [[deprecated]] bool getDoubleParam(const std::string & param_name, double & value);
394
395 [[deprecated]] bool getBooleanParam(const std::string & param_name, bool & value);
396
403 [[deprecated]] void initialize(std::string topic_prefix);
404 // DEPRECATED END
405
406 void set_parameter_event_callback();
407
408 void publish_pid_state(double cmd, double error, rclcpp::Duration dt);
409
410 void declare_param(const std::string & param_name, rclcpp::ParameterValue param_value);
411
412 bool get_double_param(const std::string & param_name, double & value);
413
414 bool get_boolean_param(const std::string & param_name, bool & value);
415
423 void set_prefixes(const std::string &topic_prefix);
424
425 rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr parameter_callback_;
426
427 rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
428 rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
429 rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params_;
430 rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface_;
431
432 std::shared_ptr<realtime_tools::RealtimePublisher<control_msgs::msg::PidState>> rt_state_pub_;
433 std::shared_ptr<rclcpp::Publisher<control_msgs::msg::PidState>> state_pub_;
434
435 Pid pid_;
436};
437
438} // namespace control_toolbox
439
440#endif // CONTROL_TOOLBOX__PID_ROS_HPP_
Definition pid_ros.hpp:54
void print_values()
Print to console the current parameters.
Definition pid_ros.cpp:313
bool initialize_from_ros_parameters()
Initialize the PID controller based on already set parameters.
Definition pid_ros.cpp:167
void setCurrentCmd(double cmd)
Set current command for this PID controller.
Definition pid_ros.cpp:436
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr getParametersCallbackHandle()
Return PID parameters callback handle.
Definition pid_ros.hpp:374
void getCurrentPIDErrors(double &pe, double &ie, double &de)
Return PID error terms for the controller.
Definition pid_ros.cpp:448
void set_current_cmd(double cmd)
Set current command for this PID controller.
Definition pid_ros.cpp:300
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:412
double getCurrentCmd()
Return current command for this PID controller.
Definition pid_ros.cpp:440
void get_current_pid_errors(double &pe, double &ie, double &de)
Return PID error terms for the controller.
Definition pid_ros.cpp:304
double get_current_cmd()
Return current command for this PID controller.
Definition pid_ros.cpp:302
std::shared_ptr< rclcpp::Publisher< control_msgs::msg::PidState > > getPidStatePublisher()
Return PID state publisher.
Definition pid_ros.cpp:444
bool initPid()
Initialize the PID controller based on already set parameters.
Definition pid_ros.cpp:408
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
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:240
void printValues()
Print to console the current parameters.
Definition pid_ros.cpp:453
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:427
Pid::Gains getGains()
Get PID gains for the controller.
Definition pid_ros.cpp:426
Pid::Gains get_gains()
Get PID gains for the controller.
Definition pid_ros.cpp:254
void set_gains(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:256
rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr get_parameters_callback_handle()
Return PID parameters callback handle.
Definition pid_ros.hpp:363
std::shared_ptr< rclcpp::Publisher< control_msgs::msg::PidState > > get_pid_state_publisher()
Return PID state publisher.
Definition pid_ros.cpp:235
void reset()
Reset the state of this PID controller.
Definition pid_ros.cpp:224
void initialize_from_args(double p, double i, double d, double i_max, double i_min, bool antiwindup)
Initialize the PID controller and set the parameters.
Definition pid_ros.cpp:198
A basic pid class.
Definition pid.hpp:110
Definition dither.hpp:46
Store gains in a struct to allow easier realtime buffer usage.
Definition pid.hpp:116