ros2_control - rolling
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 
24 namespace semantic_components
25 {
26 template <typename MessageReturnType>
28 {
29 public:
30  explicit SemanticComponentInterface(const std::string & name, size_t size = 0)
31  {
32  name_ = name;
33  interface_names_.reserve(size);
34  state_interfaces_.reserve(size);
35  }
36 
37  ~SemanticComponentInterface() = default;
38 
40 
46  std::vector<hardware_interface::LoanedStateInterface> & state_interfaces)
47  {
48  return controller_interface::get_ordered_interfaces(
49  state_interfaces, interface_names_, "", state_interfaces_);
50  }
51 
53  void release_interfaces() { state_interfaces_.clear(); }
54 
56 
64  virtual std::vector<std::string> get_state_interface_names()
65  {
66  if (interface_names_.empty())
67  {
68  for (auto i = 0u; i < interface_names_.capacity(); ++i)
69  {
70  interface_names_.emplace_back(name_ + "/" + std::to_string(i + 1));
71  }
72  }
73  return interface_names_;
74  }
75 
77 
80  bool get_values(std::vector<double> & values) const
81  {
82  // check we have sufficient memory
83  if (values.capacity() != state_interfaces_.size())
84  {
85  return false;
86  }
87  // insert all the values
88  for (size_t i = 0; i < state_interfaces_.size(); ++i)
89  {
90  values.emplace_back(state_interfaces_[i].get().get_value());
91  }
92  return true;
93  }
94 
96 
99  bool get_values_as_message(MessageReturnType & /* message */) { return false; }
100 
101 protected:
102  std::string name_;
103  std::vector<std::string> interface_names_;
104  std::vector<std::reference_wrapper<hardware_interface::LoanedStateInterface>> state_interfaces_;
105 };
106 
107 } // namespace semantic_components
108 
109 #endif // SEMANTIC_COMPONENTS__SEMANTIC_COMPONENT_INTERFACE_HPP_
Definition: semantic_component_interface.hpp:28
virtual std::vector< std::string > get_state_interface_names()
Definition of state interface names for the component.
Definition: semantic_component_interface.hpp:64
bool get_values(std::vector< double > &values) const
Return all values.
Definition: semantic_component_interface.hpp:80
bool assign_loaned_state_interfaces(std::vector< hardware_interface::LoanedStateInterface > &state_interfaces)
Assign loaned state interfaces from the hardware.
Definition: semantic_component_interface.hpp:45
bool get_values_as_message(MessageReturnType &)
Return values as MessageReturnType.
Definition: semantic_component_interface.hpp:99
void release_interfaces()
Release loaned interfaces from the hardware.
Definition: semantic_component_interface.hpp:53