ros2_control - galactic
Loading...
Searching...
No Matches
controller_interface.hpp
1// Copyright 2017 Open Source Robotics Foundation, Inc.
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_HPP_
16#define CONTROLLER_INTERFACE__CONTROLLER_INTERFACE_HPP_
17
18#include <memory>
19#include <string>
20#include <vector>
21
22#include "controller_interface/visibility_control.h"
23
24#include "hardware_interface/loaned_command_interface.hpp"
25#include "hardware_interface/loaned_state_interface.hpp"
26#include "lifecycle_msgs/msg/state.hpp"
27
28#include "rclcpp/rclcpp.hpp"
29#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
30
31namespace controller_interface
32{
33// TODO(karsten1987): Remove clang pragma within Galactic
34#ifdef __clang__
35#pragma clang diagnostic push
36#pragma clang diagnostic ignored "-Wc++17-extensions"
37#endif
38enum class return_type : std::uint8_t
39{
40 OK = 0,
41 ERROR = 1,
42 SUCCESS [[deprecated("Use controller_interface::return_type::OK instead.")]] = OK
43};
44#ifdef __clang__
45#pragma clang diagnostic pop
46#endif
47
49
54enum class interface_configuration_type : std::uint8_t
55{
56 ALL = 0,
57 INDIVIDUAL = 1,
58 NONE = 2,
59};
60
63{
64 interface_configuration_type type;
65 std::vector<std::string> names = {};
66};
67
68class ControllerInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface
69{
70public:
71 CONTROLLER_INTERFACE_PUBLIC
72 ControllerInterface() = default;
73
74 CONTROLLER_INTERFACE_PUBLIC
75 virtual ~ControllerInterface() = default;
76
77 CONTROLLER_INTERFACE_PUBLIC
78 virtual InterfaceConfiguration command_interface_configuration() const = 0;
79
80 CONTROLLER_INTERFACE_PUBLIC
81 virtual InterfaceConfiguration state_interface_configuration() const = 0;
82
83 CONTROLLER_INTERFACE_PUBLIC
84 void assign_interfaces(
85 std::vector<hardware_interface::LoanedCommandInterface> && command_interfaces,
86 std::vector<hardware_interface::LoanedStateInterface> && state_interfaces);
87
88 CONTROLLER_INTERFACE_PUBLIC
89 void release_interfaces();
90
91 CONTROLLER_INTERFACE_PUBLIC
92 virtual LifecycleNodeInterface::CallbackReturn on_init() = 0;
93
94 CONTROLLER_INTERFACE_PUBLIC
95 virtual return_type init(const std::string & controller_name);
96
97 CONTROLLER_INTERFACE_PUBLIC
98 virtual return_type update(const rclcpp::Time & time, const rclcpp::Duration & period) = 0;
99
100 CONTROLLER_INTERFACE_PUBLIC
101 std::shared_ptr<rclcpp::Node> get_node();
102
104
110 template <typename ParameterT>
111 auto auto_declare(const std::string & name, const ParameterT & default_value)
112 {
113 if (!node_->has_parameter(name))
114 {
115 return node_->declare_parameter<ParameterT>(name, default_value);
116 }
117 else
118 {
119 return node_->get_parameter(name).get_value<ParameterT>();
120 }
121 }
122
133 CONTROLLER_INTERFACE_PUBLIC
134 const rclcpp_lifecycle::State & configure();
135
136 CONTROLLER_INTERFACE_PUBLIC
137 const rclcpp_lifecycle::State & cleanup();
138
139 CONTROLLER_INTERFACE_PUBLIC
140 const rclcpp_lifecycle::State & deactivate();
141
142 CONTROLLER_INTERFACE_PUBLIC
143 const rclcpp_lifecycle::State & activate();
144
145 CONTROLLER_INTERFACE_PUBLIC
146 const rclcpp_lifecycle::State & shutdown();
147
148 CONTROLLER_INTERFACE_PUBLIC
149 const rclcpp_lifecycle::State & get_state() const;
150
151 CONTROLLER_INTERFACE_PUBLIC
152 unsigned int get_update_rate() const;
153
154protected:
155 std::vector<hardware_interface::LoanedCommandInterface> command_interfaces_;
156 std::vector<hardware_interface::LoanedStateInterface> state_interfaces_;
157 std::shared_ptr<rclcpp::Node> node_;
158 rclcpp_lifecycle::State lifecycle_state_;
159 unsigned int update_rate_ = 0;
160};
161
162using ControllerInterfaceSharedPtr = std::shared_ptr<ControllerInterface>;
163
164} // namespace controller_interface
165
166#endif // CONTROLLER_INTERFACE__CONTROLLER_INTERFACE_HPP_
Definition controller_interface.hpp:69
CONTROLLER_INTERFACE_PUBLIC const rclcpp_lifecycle::State & configure()
Definition controller_interface.cpp:50
auto auto_declare(const std::string &name, const ParameterT &default_value)
Declare and initialize a parameter with a type.
Definition controller_interface.hpp:111
Configuring what command/state interfaces to claim.
Definition controller_interface.hpp:63