Program Listing for File pid_ros.hpp

Return to documentation for file (/tmp/ws/src/control_toolbox/include/control_toolbox/pid_ros.hpp)

// Copyright (c) 2020, Open Source Robotics Foundation, Inc.
// All rights reserved.
//
// Software License Agreement (BSD License 2.0)
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//  * Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  * Redistributions in binary form must reproduce the above
//    copyright notice, this list of conditions and the following
//    disclaimer in the documentation and/or other materials provided
//    with the distribution.
//  * Neither the name of the Willow Garage nor the names of its
//    contributors may be used to endorse or promote products derived
//    from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef CONTROL_TOOLBOX__PID_ROS_HPP_
#define CONTROL_TOOLBOX__PID_ROS_HPP_

#include <memory>
#include <string>

#include "control_msgs/msg/pid_state.hpp"

#include "rclcpp/clock.hpp"
#include "rclcpp/duration.hpp"
#include "rclcpp/node.hpp"

#include "realtime_tools/realtime_buffer.h"
#include "realtime_tools/realtime_publisher.h"

#include "control_toolbox/pid.hpp"
#include "control_toolbox/visibility_control.hpp"

namespace control_toolbox
{

class CONTROL_TOOLBOX_PUBLIC PidROS
{
public:
  template<class NodeT>
  explicit PidROS(
    std::shared_ptr<NodeT> node_ptr,
    std::string prefix = std::string(""),
    bool prefix_is_for_params = false
  )
  : PidROS(
      node_ptr->get_node_base_interface(),
      node_ptr->get_node_logging_interface(),
      node_ptr->get_node_parameters_interface(),
      node_ptr->get_node_topics_interface(),
           prefix, prefix_is_for_params)
  {
  }

  PidROS(
    rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base,
    rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging,
    rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params,
    rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface,
    std::string prefix = std::string(""), bool prefix_is_for_params = false);

  void initPid(double p, double i, double d, double i_max, double i_min, bool antiwindup);

  bool initPid();

  void reset();

  double computeCommand(double error, rclcpp::Duration dt);

  double computeCommand(double error, double error_dot, rclcpp::Duration dt);

  Pid::Gains getGains();

  void setGains(double p, double i, double d, double i_max, double i_min, bool antiwindup = false);

  void setGains(const Pid::Gains & gains);

  void setCurrentCmd(double cmd);

  double getCurrentCmd();

  std::shared_ptr<rclcpp::Publisher<control_msgs::msg::PidState>> getPidStatePublisher();

  void getCurrentPIDErrors(double & pe, double & ie, double & de);

  void printValues();

  inline rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr
  getParametersCallbackHandle()
  {
    return parameter_callback_;
  }

protected:
  std::string topic_prefix_;
  std::string param_prefix_;

private:
  void setParameterEventCallback();

  void publishPIDState(double cmd, double error, rclcpp::Duration dt);

  void declareParam(const std::string & param_name, rclcpp::ParameterValue param_value);

  bool getDoubleParam(const std::string & param_name, double & value);

  bool getBooleanParam(const std::string & param_name, bool & value);

  void initialize(std::string topic_prefix);

  rclcpp::node_interfaces::OnSetParametersCallbackHandle::SharedPtr parameter_callback_;

  rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_;
  rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_;
  rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_params_;
  rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr topics_interface_;

  std::shared_ptr<realtime_tools::RealtimePublisher<control_msgs::msg::PidState>> rt_state_pub_;
  std::shared_ptr<rclcpp::Publisher<control_msgs::msg::PidState>> state_pub_;

  Pid pid_;
};

}  // namespace control_toolbox

#endif  // CONTROL_TOOLBOX__PID_ROS_HPP_