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
53 std::vector<hardware_interface::LoanedStateInterface> & state_interfaces)
54 {
55 return controller_interface::get_ordered_interfaces(
56 state_interfaces, interface_names_, "", state_interfaces_);
57 }
58
62 void release_interfaces() { state_interfaces_.clear(); }
63
74 virtual std::vector<std::string> get_state_interface_names()
75 {
76 if (interface_names_.empty())
77 {
78 for (auto i = 0u; i < interface_names_.capacity(); ++i)
79 {
80 interface_names_.emplace_back(name_ + "/" + std::to_string(i + 1));
81 }
82 }
83 return interface_names_;
84 }
85
91 bool get_values(std::vector<double> & values) const
92 {
93 // check we have sufficient memory
94 if (values.capacity() != state_interfaces_.size())
95 {
96 return false;
97 }
98 // insert all the values
99 for (auto i = 0u; i < state_interfaces_.size(); ++i)
100 {
101 values.emplace_back(state_interfaces_[i].get().get_optional().value());
102 }
103 return true;
104 }
105
111 bool get_values_as_message(MessageReturnType & /* message */) { return false; }
112
113 // delete copy constructor, because
114 // copy will change capacity of member variables
116
117protected:
118 std::string name_;
119 std::vector<std::string> interface_names_;
120 std::vector<std::reference_wrapper<hardware_interface::LoanedStateInterface>> state_interfaces_;
121};
122
123} // namespace semantic_components
124
125#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:91
bool assign_loaned_state_interfaces(std::vector< hardware_interface::LoanedStateInterface > &state_interfaces)
Assign loaned state interfaces from the hardware.
Definition semantic_component_interface.hpp:52
bool get_values_as_message(MessageReturnType &)
Return values as MessageReturnType.
Definition semantic_component_interface.hpp:111
virtual std::vector< std::string > get_state_interface_names()
Definition of state interface names for the component.
Definition semantic_component_interface.hpp:74
void release_interfaces()
Release loaned interfaces from the hardware.
Definition semantic_component_interface.hpp:62