ros2_control - rolling
Loading...
Searching...
No Matches
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 <chrono>
19#include <cstdint>
20#include <memory>
21#include <optional>
22#include <string>
23#include <unordered_map>
24#include <vector>
25
26#include "controller_interface/controller_interface_params.hpp"
27#include "hardware_interface/handle.hpp"
28// re-exported for derived controllers (REGISTER_ROS2_CONTROL_INTROSPECTION)
29#include "hardware_interface/introspection.hpp"
30#include "hardware_interface/loaned_command_interface.hpp"
31#include "hardware_interface/loaned_state_interface.hpp"
32#include "joint_limits/joint_limits.hpp"
33
34#include "pal_statistics/pal_statistics_utils.hpp"
35#include "rclcpp/duration.hpp"
36#include "rclcpp/node_options.hpp"
37#include "rclcpp/time.hpp"
38#include "rclcpp/version.h"
39#include "rclcpp_lifecycle/lifecycle_node.hpp"
40#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
41#include "rclcpp_lifecycle/state.hpp"
42
43namespace controller_interface
44{
45using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
46
47enum class return_type : std::uint8_t
48{
49 OK = 0,
50 ERROR = 1,
51};
52
65enum class interface_configuration_type : std::uint8_t
66{
67 ALL = 0,
68 INDIVIDUAL = 1,
69 NONE = 2,
70 INDIVIDUAL_BEST_EFFORT = 3,
71 REGEX = 10
72};
73
78{
79 interface_configuration_type type = interface_configuration_type::NONE;
80 std::vector<std::string> names = {};
81};
82
84{
85 void reset()
86 {
87 total_triggers = 0;
88 failed_triggers = 0;
89 }
90
91 unsigned int total_triggers;
92 unsigned int failed_triggers;
93};
94
106{
107 bool successful = true;
108 return_type result = return_type::OK;
109 std::optional<std::chrono::nanoseconds> execution_time = std::nullopt;
110 std::optional<rclcpp::Duration> period = std::nullopt;
111};
112
118class ControllerInterfaceBase : public rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface
119{
120public:
122
123 virtual ~ControllerInterfaceBase();
124
139
156
167 virtual void assign_interfaces(
168 std::vector<hardware_interface::LoanedCommandInterface> && command_interfaces,
169 std::vector<hardware_interface::LoanedStateInterface> && state_interfaces);
170
176 virtual void release_interfaces();
177
178 [[deprecated(
179 "Use the init(const controller_interface::ControllerInterfaceParams & params) method instead. "
180 "This method will be removed in the future ROS 2 releases.")]]
181 return_type init(
182 const std::string & controller_name, const std::string & urdf, unsigned int cm_update_rate,
183 const std::string & node_namespace, const rclcpp::NodeOptions & node_options);
184
185 return_type init(const controller_interface::ControllerInterfaceParams & params);
186
192 const rclcpp_lifecycle::State & configure();
193
197 virtual CallbackReturn on_init() = 0;
198
208 virtual return_type update(const rclcpp::Time & time, const rclcpp::Duration & period) = 0;
209
221 ControllerUpdateStatus trigger_update(const rclcpp::Time & time, const rclcpp::Duration & period);
222
223 std::shared_ptr<rclcpp_lifecycle::LifecycleNode> get_node();
224
225 std::shared_ptr<const rclcpp_lifecycle::LifecycleNode> get_node() const;
226
235 const rclcpp_lifecycle::State & get_lifecycle_state() const;
236
244 uint8_t get_lifecycle_id() const;
245
246 unsigned int get_update_rate() const;
247
248 bool is_async() const;
249
250 const std::string & get_robot_description() const;
251
255 const std::unordered_map<std::string, joint_limits::JointLimits> & get_hard_joint_limits() const;
256
260 const std::unordered_map<std::string, joint_limits::SoftJointLimits> & get_soft_joint_limits()
261 const;
262
274 virtual rclcpp::NodeOptions define_custom_node_options() const
275 {
276 rclcpp::NodeOptions node_options;
277// @note The versions conditioning is added here to support the source-compatibility with Humble
278#if RCLCPP_VERSION_MAJOR >= 21
279 node_options.enable_logger_service(true);
280#else
281 node_options.allow_undeclared_parameters(true);
282 node_options.automatically_declare_parameters_from_overrides(true);
283#endif
284 return node_options;
285 }
286
294 template <typename ParameterT>
295 auto auto_declare(const std::string & name, const ParameterT & default_value)
296 {
297 if (!get_node()->has_parameter(name))
298 {
299 return get_node()->declare_parameter<ParameterT>(name, default_value);
300 }
301 else
302 {
303 return get_node()->get_parameter(name).get_value<ParameterT>();
304 }
305 }
306
307 // Methods for chainable controller types with default values so we can put all controllers into
308 // one list in Controller Manager
309
317 virtual bool is_chainable() const = 0;
318
325 virtual std::vector<hardware_interface::CommandInterface::SharedPtr>
327
334 virtual std::vector<hardware_interface::StateInterface::ConstSharedPtr>
336
345 virtual bool set_chained_mode(bool chained_mode) = 0;
346
356 virtual bool is_in_chained_mode() const = 0;
357
370
384
385 std::string get_name() const;
386
392 void enable_introspection(bool enable);
393
394protected:
409 std::vector<hardware_interface::LoanedCommandInterface> command_interfaces_;
424 std::vector<hardware_interface::LoanedStateInterface> state_interfaces_;
425
426private:
431 void stop_async_handler_thread();
432
434 std::unique_ptr<ControllerInterfaceBaseImpl> impl_;
435
436protected:
437 pal_statistics::RegistrationsRAII stats_registrations_;
438};
439
440using ControllerInterfaceBaseSharedPtr = std::shared_ptr<ControllerInterfaceBase>;
441
442} // namespace controller_interface
443
444#endif // CONTROLLER_INTERFACE__CONTROLLER_INTERFACE_BASE_HPP_
Base interface class for an controller. The interface may not be used to implement a controller....
Definition controller_interface_base.hpp:119
virtual InterfaceConfiguration command_interface_configuration() const =0
Get configuration for controller's required command interfaces.
auto auto_declare(const std::string &name, const ParameterT &default_value)
Declare and initialize a parameter with a type.
Definition controller_interface_base.hpp:295
std::vector< hardware_interface::LoanedCommandInterface > command_interfaces_
Loaned command interfaces.
Definition controller_interface_base.hpp:409
virtual CallbackReturn on_init()=0
Extending interface with initialization method which is individual for each controller.
const std::unordered_map< std::string, joint_limits::JointLimits > & get_hard_joint_limits() const
Get the unordered map of joint limits that are defined in the robot description.
Definition controller_interface_base.cpp:420
const std::unordered_map< std::string, joint_limits::SoftJointLimits > & get_soft_joint_limits() const
Get the unordered map of soft joint limits that are defined in the robot description.
Definition controller_interface_base.cpp:426
virtual std::vector< hardware_interface::StateInterface::ConstSharedPtr > export_state_interfaces()=0
Export interfaces for a chainable controller that can be used as state interface by other controllers...
void enable_introspection(bool enable)
Enable or disable introspection of the controller.
Definition controller_interface_base.cpp:455
void wait_for_trigger_update_to_finish()
Method to wait for any running async update cycle to finish after finishing the current cycle....
Definition controller_interface_base.cpp:431
uint8_t get_lifecycle_id() const
Get the lifecycle id of the controller node that is cached internally to avoid calls to get_lifecycle...
Definition controller_interface_base.cpp:323
virtual std::vector< hardware_interface::CommandInterface::SharedPtr > export_reference_interfaces()=0
Export interfaces for a chainable controller that can be used as command interface of other controlle...
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get the current lifecycle state of the controller node.
Definition controller_interface_base.cpp:314
virtual void release_interfaces()
Method that releases the Loaned interfaces from the controller.
Definition controller_interface_base.cpp:308
virtual bool is_in_chained_mode() const =0
Get information if a controller is currently in chained mode.
virtual InterfaceConfiguration state_interface_configuration() const =0
Get configuration for controller's required state interfaces.
const rclcpp_lifecycle::State & configure()
Custom configure method to read additional parameters for controller-nodes.
Definition controller_interface_base.cpp:200
ControllerUpdateStatus trigger_update(const rclcpp::Time &time, const rclcpp::Duration &period)
Trigger update method. This method is used by the controller_manager to trigger the update method of ...
Definition controller_interface_base.cpp:341
virtual 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:300
virtual rclcpp::NodeOptions define_custom_node_options() const
Method used by the controller_manager for base NodeOptions to instantiate the Lifecycle node of the c...
Definition controller_interface_base.hpp:274
std::vector< hardware_interface::LoanedStateInterface > state_interfaces_
Loaned state interfaces.
Definition controller_interface_base.hpp:424
virtual bool is_chainable() const =0
Get information if a controller is chainable.
void prepare_for_deactivation()
Method to prepare the controller for deactivation. This method is called by the controller manager be...
Definition controller_interface_base.cpp:439
virtual bool set_chained_mode(bool chained_mode)=0
Set chained mode of a chainable controller. This method triggers internal processes to switch a chain...
virtual return_type update(const rclcpp::Time &time, const rclcpp::Duration &period)=0
Control step update. Command interfaces are updated based on on reference inputs and current states.
Definition controller_interface_params.hpp:44
Definition controller_interface_base.hpp:84
Definition controller_interface_base.hpp:106
Configuring what command/state interfaces to claim.
Definition controller_interface_base.hpp:78