ros2_control - rolling
controller_interface_base.hpp
1 // Copyright (c) 2022, Stogl Robotics Consulting UG (haftungsbeschränkt)
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CONTROLLER_INTERFACE__CONTROLLER_INTERFACE_BASE_HPP_
16 #define CONTROLLER_INTERFACE__CONTROLLER_INTERFACE_BASE_HPP_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "controller_interface/visibility_control.h"
23 
24 #include "hardware_interface/handle.hpp"
25 #include "hardware_interface/loaned_command_interface.hpp"
26 #include "hardware_interface/loaned_state_interface.hpp"
27 
28 #include "rclcpp/version.h"
29 #include "rclcpp_lifecycle/lifecycle_node.hpp"
30 
31 namespace controller_interface
32 {
33 using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
34 
35 enum class return_type : std::uint8_t
36 {
37  OK = 0,
38  ERROR = 1,
39 };
40 
42 
47 enum class interface_configuration_type : std::uint8_t
48 {
49  ALL = 0,
50  INDIVIDUAL = 1,
51  NONE = 2,
52 };
53 
56 {
57  interface_configuration_type type;
58  std::vector<std::string> names = {};
59 };
60 
66 class ControllerInterfaceBase : public rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface
67 {
68 public:
69  CONTROLLER_INTERFACE_PUBLIC
70  ControllerInterfaceBase() = default;
71 
72  CONTROLLER_INTERFACE_PUBLIC
73  virtual ~ControllerInterfaceBase() = default;
74 
76 
87  CONTROLLER_INTERFACE_PUBLIC
89 
91 
104  CONTROLLER_INTERFACE_PUBLIC
106 
108 
116  CONTROLLER_INTERFACE_PUBLIC
117  virtual void assign_interfaces(
118  std::vector<hardware_interface::LoanedCommandInterface> && command_interfaces,
119  std::vector<hardware_interface::LoanedStateInterface> && state_interfaces);
120 
122 
125  CONTROLLER_INTERFACE_PUBLIC
126  virtual void release_interfaces();
127 
128  CONTROLLER_INTERFACE_PUBLIC
129  return_type init(
130  const std::string & controller_name, const std::string & urdf, unsigned int cm_update_rate,
131  const std::string & node_namespace, const rclcpp::NodeOptions & node_options);
132 
134  /*
135  * Override default implementation for configure of LifecycleNode to get parameters.
136  */
137  CONTROLLER_INTERFACE_PUBLIC
138  const rclcpp_lifecycle::State & configure();
139 
141  CONTROLLER_INTERFACE_PUBLIC
142  virtual CallbackReturn on_init() = 0;
143 
153  CONTROLLER_INTERFACE_PUBLIC
154  virtual return_type update(const rclcpp::Time & time, const rclcpp::Duration & period) = 0;
155 
156  CONTROLLER_INTERFACE_PUBLIC
157  std::shared_ptr<rclcpp_lifecycle::LifecycleNode> get_node();
158 
159  CONTROLLER_INTERFACE_PUBLIC
160  std::shared_ptr<const rclcpp_lifecycle::LifecycleNode> get_node() const;
161 
162  CONTROLLER_INTERFACE_PUBLIC
163  const rclcpp_lifecycle::State & get_lifecycle_state() const;
164 
165  CONTROLLER_INTERFACE_PUBLIC
166  unsigned int get_update_rate() const;
167 
168  CONTROLLER_INTERFACE_PUBLIC
169  bool is_async() const;
170 
171  CONTROLLER_INTERFACE_PUBLIC
172  const std::string & get_robot_description() const;
173 
185  CONTROLLER_INTERFACE_PUBLIC
186  virtual rclcpp::NodeOptions define_custom_node_options() const
187  {
188 // \note The versions conditioning is added here to support the source-compatibility with Humble
189 #if RCLCPP_VERSION_MAJOR >= 21
190  return rclcpp::NodeOptions().enable_logger_service(true);
191 #else
192  return rclcpp::NodeOptions()
193  .allow_undeclared_parameters(true)
194  .automatically_declare_parameters_from_overrides(true);
195 #endif
196  }
197 
199 
205  template <typename ParameterT>
206  auto auto_declare(const std::string & name, const ParameterT & default_value)
207  {
208  if (!node_->has_parameter(name))
209  {
210  return node_->declare_parameter<ParameterT>(name, default_value);
211  }
212  else
213  {
214  return node_->get_parameter(name).get_value<ParameterT>();
215  }
216  }
217 
218  // Methods for chainable controller types with default values so we can put all controllers into
219  // one list in Controller Manager
220 
222 
227  CONTROLLER_INTERFACE_PUBLIC
228  virtual bool is_chainable() const = 0;
229 
236  CONTROLLER_INTERFACE_PUBLIC
237  virtual std::vector<hardware_interface::CommandInterface> export_reference_interfaces() = 0;
238 
245  CONTROLLER_INTERFACE_PUBLIC
246  virtual std::vector<hardware_interface::StateInterface> export_state_interfaces() = 0;
247 
256  CONTROLLER_INTERFACE_PUBLIC
257  virtual bool set_chained_mode(bool chained_mode) = 0;
258 
260 
267  CONTROLLER_INTERFACE_PUBLIC
268  virtual bool is_in_chained_mode() const = 0;
269 
270 protected:
271  std::vector<hardware_interface::LoanedCommandInterface> command_interfaces_;
272  std::vector<hardware_interface::LoanedStateInterface> state_interfaces_;
273 
274 private:
275  std::shared_ptr<rclcpp_lifecycle::LifecycleNode> node_;
276  unsigned int update_rate_ = 0;
277  bool is_async_ = false;
278  std::string urdf_ = "";
279 };
280 
281 using ControllerInterfaceBaseSharedPtr = std::shared_ptr<ControllerInterfaceBase>;
282 
283 } // namespace controller_interface
284 
285 #endif // CONTROLLER_INTERFACE__CONTROLLER_INTERFACE_BASE_HPP_
Definition: controller_interface_base.hpp:67
auto auto_declare(const std::string &name, const ParameterT &default_value)
Declare and initialize a parameter with a type.
Definition: controller_interface_base.hpp:206
virtual CONTROLLER_INTERFACE_PUBLIC std::vector< hardware_interface::CommandInterface > export_reference_interfaces()=0
virtual CONTROLLER_INTERFACE_PUBLIC InterfaceConfiguration state_interface_configuration() const =0
Get configuration for controller's required state interfaces.
virtual CONTROLLER_INTERFACE_PUBLIC CallbackReturn on_init()=0
Extending interface with initialization method which is individual for each controller.
virtual CONTROLLER_INTERFACE_PUBLIC bool set_chained_mode(bool chained_mode)=0
virtual CONTROLLER_INTERFACE_PUBLIC std::vector< hardware_interface::StateInterface > export_state_interfaces()=0
virtual CONTROLLER_INTERFACE_PUBLIC InterfaceConfiguration command_interface_configuration() const =0
Get configuration for controller's required command interfaces.
virtual CONTROLLER_INTERFACE_PUBLIC void release_interfaces()
Method that releases the Loaned interfaces from the controller.
Definition: controller_interface_base.cpp:103
virtual CONTROLLER_INTERFACE_PUBLIC bool is_in_chained_mode() const =0
Get information if a controller is currently in chained mode.
CONTROLLER_INTERFACE_PUBLIC const rclcpp_lifecycle::State & configure()
Custom configure method to read additional parameters for controller-nodes.
Definition: controller_interface_base.cpp:76
virtual CONTROLLER_INTERFACE_PUBLIC void assign_interfaces(std::vector< hardware_interface::LoanedCommandInterface > &&command_interfaces, std::vector< hardware_interface::LoanedStateInterface > &&state_interfaces)
Method that assigns the Loaned interfaces to the controller.
Definition: controller_interface_base.cpp:95
virtual CONTROLLER_INTERFACE_PUBLIC bool is_chainable() const =0
Get information if a controller is chainable.
virtual CONTROLLER_INTERFACE_PUBLIC return_type update(const rclcpp::Time &time, const rclcpp::Duration &period)=0
virtual CONTROLLER_INTERFACE_PUBLIC rclcpp::NodeOptions define_custom_node_options() const
Definition: controller_interface_base.hpp:186
Configuring what command/state interfaces to claim.
Definition: controller_interface_base.hpp:56