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 <functional>
19#include <string>
20#include <vector>
21
22#include "controller_interface/helpers.hpp"
23#include "hardware_interface/loaned_state_interface.hpp"
24
25namespace semantic_components
26{
27template <typename MessageReturnType>
29{
30public:
32 const std::string & name, const std::vector<std::string> & interface_names)
33 : name_(name), interface_names_(interface_names)
34 {
35 state_interfaces_.reserve(interface_names.size());
36 }
37
38 explicit SemanticComponentInterface(const std::string & name, std::size_t size = 0) : name_(name)
39 {
40 interface_names_.reserve(size);
41 state_interfaces_.reserve(size);
42 }
43
44 virtual ~SemanticComponentInterface() = default;
45
54 std::vector<hardware_interface::LoanedStateInterface> & state_interfaces)
55 {
56 return controller_interface::get_ordered_interfaces(
57 state_interfaces, interface_names_, "", state_interfaces_);
58 }
59
63 void release_interfaces() { state_interfaces_.clear(); }
64
75 virtual std::vector<std::string> get_state_interface_names()
76 {
77 if (interface_names_.empty())
78 {
79 for (auto i = 0u; i < interface_names_.capacity(); ++i)
80 {
81 interface_names_.emplace_back(name_ + "/" + std::to_string(i + 1));
82 }
83 }
84 return interface_names_;
85 }
86
92 bool get_values(std::vector<double> & values) const
93 {
94 // check we have sufficient memory
95 if (values.capacity() != state_interfaces_.size())
96 {
97 return false;
98 }
99 // insert all the values
100 for (auto i = 0u; i < state_interfaces_.size(); ++i)
101 {
102 values.emplace_back(state_interfaces_[i].get().get_optional().value());
103 }
104 return true;
105 }
106
112 bool get_values_as_message(MessageReturnType & /* message */) { return false; }
113
114 // delete copy constructor, because
115 // copy will change capacity of member variables
117
118protected:
119 std::string name_;
120 std::vector<std::string> interface_names_;
121 std::vector<std::reference_wrapper<hardware_interface::LoanedStateInterface>> state_interfaces_;
122};
123
124} // namespace semantic_components
125
126#endif // SEMANTIC_COMPONENTS__SEMANTIC_COMPONENT_INTERFACE_HPP_
Definition semantic_component_interface.hpp:29
bool get_values(std::vector< double > &values) const
Return all values.
Definition semantic_component_interface.hpp:92
bool assign_loaned_state_interfaces(std::vector< hardware_interface::LoanedStateInterface > &state_interfaces)
Assign loaned state interfaces from the hardware.
Definition semantic_component_interface.hpp:53
bool get_values_as_message(MessageReturnType &)
Return values as MessageReturnType.
Definition semantic_component_interface.hpp:112
virtual std::vector< std::string > get_state_interface_names()
Definition of state interface names for the component.
Definition semantic_component_interface.hpp:75
void release_interfaces()
Release loaned interfaces from the hardware.
Definition semantic_component_interface.hpp:63