ros2_control - rolling
Loading...
Searching...
No Matches
semantic_component_interface.hpp
1// Copyright (c) 2021, 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 SEMANTIC_COMPONENTS__SEMANTIC_COMPONENT_INTERFACE_HPP_
16#define SEMANTIC_COMPONENTS__SEMANTIC_COMPONENT_INTERFACE_HPP_
17
18#include <string>
19#include <vector>
20
21#include "controller_interface/helpers.hpp"
22#include "hardware_interface/loaned_state_interface.hpp"
23
24namespace semantic_components
25{
26template <typename MessageReturnType>
28{
29public:
31 const std::string & name, const std::vector<std::string> & interface_names)
32 : name_(name), interface_names_(interface_names)
33 {
34 state_interfaces_.reserve(interface_names.size());
35 }
36
37 explicit SemanticComponentInterface(const std::string & name, std::size_t size = 0) : name_(name)
38 {
39 interface_names_.reserve(size);
40 state_interfaces_.reserve(size);
41 }
42
43 virtual ~SemanticComponentInterface() = default;
44
46
52 std::vector<hardware_interface::LoanedStateInterface> & state_interfaces)
53 {
54 return controller_interface::get_ordered_interfaces(
55 state_interfaces, interface_names_, "", state_interfaces_);
56 }
57
59 void release_interfaces() { state_interfaces_.clear(); }
60
62
70 virtual std::vector<std::string> get_state_interface_names()
71 {
72 if (interface_names_.empty())
73 {
74 for (auto i = 0u; i < interface_names_.capacity(); ++i)
75 {
76 interface_names_.emplace_back(name_ + "/" + std::to_string(i + 1));
77 }
78 }
79 return interface_names_;
80 }
81
83
86 bool get_values(std::vector<double> & values) const
87 {
88 // check we have sufficient memory
89 if (values.capacity() != state_interfaces_.size())
90 {
91 return false;
92 }
93 // insert all the values
94 for (auto i = 0u; i < state_interfaces_.size(); ++i)
95 {
96 values.emplace_back(state_interfaces_[i].get().get_value());
97 }
98 return true;
99 }
100
102
105 bool get_values_as_message(MessageReturnType & /* message */) { return false; }
106
107protected:
108 std::string name_;
109 std::vector<std::string> interface_names_;
110 std::vector<std::reference_wrapper<hardware_interface::LoanedStateInterface>> state_interfaces_;
111};
112
113} // namespace semantic_components
114
115#endif // SEMANTIC_COMPONENTS__SEMANTIC_COMPONENT_INTERFACE_HPP_
Definition semantic_component_interface.hpp:28
bool get_values(std::vector< double > &values) const
Return all values.
Definition semantic_component_interface.hpp:86
bool assign_loaned_state_interfaces(std::vector< hardware_interface::LoanedStateInterface > &state_interfaces)
Assign loaned state interfaces from the hardware.
Definition semantic_component_interface.hpp:51
bool get_values_as_message(MessageReturnType &)
Return values as MessageReturnType.
Definition semantic_component_interface.hpp:105
virtual std::vector< std::string > get_state_interface_names()
Definition of state interface names for the component.
Definition semantic_component_interface.hpp:70
void release_interfaces()
Release loaned interfaces from the hardware.
Definition semantic_component_interface.hpp:59