Program Listing for File controller_interface_base.hpp

Return to documentation for file (include/controller_interface/controller_interface_base.hpp)

// Copyright (c) 2022, Stogl Robotics Consulting UG (haftungsbeschränkt)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef CONTROLLER_INTERFACE__CONTROLLER_INTERFACE_BASE_HPP_
#define CONTROLLER_INTERFACE__CONTROLLER_INTERFACE_BASE_HPP_

#include <memory>
#include <string>
#include <vector>

#include "controller_interface/visibility_control.h"

#include "hardware_interface/handle.hpp"
#include "hardware_interface/loaned_command_interface.hpp"
#include "hardware_interface/loaned_state_interface.hpp"

#include "rclcpp/rclcpp.hpp"
#include "rclcpp/version.h"
#include "rclcpp_lifecycle/lifecycle_node.hpp"

namespace controller_interface
{
using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;

enum class return_type : std::uint8_t
{
  OK = 0,
  ERROR = 1,
};


enum class interface_configuration_type : std::uint8_t
{
  ALL = 0,
  INDIVIDUAL = 1,
  NONE = 2,
};

struct InterfaceConfiguration
{
  interface_configuration_type type;
  std::vector<std::string> names = {};
};

class ControllerInterfaceBase : public rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface
{
public:
  CONTROLLER_INTERFACE_PUBLIC
  ControllerInterfaceBase() = default;

  CONTROLLER_INTERFACE_PUBLIC
  virtual ~ControllerInterfaceBase() = default;


  CONTROLLER_INTERFACE_PUBLIC
  virtual InterfaceConfiguration command_interface_configuration() const = 0;


  CONTROLLER_INTERFACE_PUBLIC
  virtual InterfaceConfiguration state_interface_configuration() const = 0;

  CONTROLLER_INTERFACE_PUBLIC
  void assign_interfaces(
    std::vector<hardware_interface::LoanedCommandInterface> && command_interfaces,
    std::vector<hardware_interface::LoanedStateInterface> && state_interfaces);

  CONTROLLER_INTERFACE_PUBLIC
  void release_interfaces();

  CONTROLLER_INTERFACE_PUBLIC
  return_type init(
    const std::string & controller_name, const std::string & urdf, unsigned int cm_update_rate,
    const std::string & node_namespace, const rclcpp::NodeOptions & node_options);

  /*
   * Override default implementation for configure of LifecycleNode to get parameters.
   */
  CONTROLLER_INTERFACE_PUBLIC
  const rclcpp_lifecycle::State & configure();

  CONTROLLER_INTERFACE_PUBLIC
  virtual CallbackReturn on_init() = 0;

  CONTROLLER_INTERFACE_PUBLIC
  virtual return_type update(const rclcpp::Time & time, const rclcpp::Duration & period) = 0;

  CONTROLLER_INTERFACE_PUBLIC
  std::shared_ptr<rclcpp_lifecycle::LifecycleNode> get_node();

  CONTROLLER_INTERFACE_PUBLIC
  std::shared_ptr<const rclcpp_lifecycle::LifecycleNode> get_node() const;

  CONTROLLER_INTERFACE_PUBLIC
  const rclcpp_lifecycle::State & get_state() const;

  CONTROLLER_INTERFACE_PUBLIC
  unsigned int get_update_rate() const;

  CONTROLLER_INTERFACE_PUBLIC
  bool is_async() const;

  CONTROLLER_INTERFACE_PUBLIC
  const std::string & get_robot_description() const;

  CONTROLLER_INTERFACE_PUBLIC
  virtual rclcpp::NodeOptions define_custom_node_options() const
  {
// \note The versions conditioning is added here to support the source-compatibility with Humble
#if RCLCPP_VERSION_MAJOR >= 21
    return rclcpp::NodeOptions().enable_logger_service(true);
#else
    return rclcpp::NodeOptions()
      .allow_undeclared_parameters(true)
      .automatically_declare_parameters_from_overrides(true);
#endif
  }


  template <typename ParameterT>
  auto auto_declare(const std::string & name, const ParameterT & default_value)
  {
    if (!node_->has_parameter(name))
    {
      return node_->declare_parameter<ParameterT>(name, default_value);
    }
    else
    {
      return node_->get_parameter(name).get_value<ParameterT>();
    }
  }

  // Methods for chainable controller types with default values so we can put all controllers into
  // one list in Controller Manager


  CONTROLLER_INTERFACE_PUBLIC
  virtual bool is_chainable() const = 0;

  CONTROLLER_INTERFACE_PUBLIC
  virtual std::vector<hardware_interface::CommandInterface> export_reference_interfaces() = 0;

  CONTROLLER_INTERFACE_PUBLIC
  virtual bool set_chained_mode(bool chained_mode) = 0;


  CONTROLLER_INTERFACE_PUBLIC
  virtual bool is_in_chained_mode() const = 0;

protected:
  std::vector<hardware_interface::LoanedCommandInterface> command_interfaces_;
  std::vector<hardware_interface::LoanedStateInterface> state_interfaces_;
  unsigned int update_rate_ = 0;
  bool is_async_ = false;
  std::string urdf_ = "";

private:
  std::shared_ptr<rclcpp_lifecycle::LifecycleNode> node_;
};

using ControllerInterfaceBaseSharedPtr = std::shared_ptr<ControllerInterfaceBase>;

}  // namespace controller_interface

#endif  // CONTROLLER_INTERFACE__CONTROLLER_INTERFACE_BASE_HPP_