ros2_control - rolling
Loading...
Searching...
No Matches
helpers.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 CONTROLLER_INTERFACE__HELPERS_HPP_
16#define CONTROLLER_INTERFACE__HELPERS_HPP_
17
18#include <algorithm>
19#include <functional>
20#include <memory>
21#include <stdexcept>
22#include <string>
23#include <vector>
24
25// Add hardware interface helpers here, so all inherited controllers can use them
26#include "hardware_interface/helpers.hpp"
27
28namespace controller_interface
29{
31
48template <typename T>
49bool get_ordered_interfaces(
50 std::vector<T> & unordered_interfaces, const std::vector<std::string> & ordered_names,
51 const std::string & interface_type, std::vector<std::reference_wrapper<T>> & ordered_interfaces)
52{
53 if (ordered_interfaces.capacity() < ordered_names.size())
54 {
55 throw std::range_error(
56 "Capacity of ordered_interfaces (" + std::to_string(ordered_interfaces.capacity()) +
57 ") has to be equal or higher as size of ordered_names (" +
58 std::to_string(ordered_names.size()) +
59 ") for realtime reasons. Please reserve sufficient space in the on_configure method to avoid "
60 "allocating memory in real-time loop.");
61 }
62 for (const auto & name : ordered_names)
63 {
64 for (auto & interface : unordered_interfaces)
65 {
66 if (!interface_type.empty())
67 {
68 // check case where:
69 // (<joint> == <joint> AND <interface> == <interface>) OR <joint>/<interface> == 'full name'
70 if (
71 ((name == interface.get_prefix_name()) &&
72 (interface_type == interface.get_interface_name())) ||
73 ((name + "/" + interface_type) == interface.get_name()))
74 {
75 ordered_interfaces.push_back(std::ref(interface));
76 }
77 }
78 else
79 {
80 if (name == interface.get_name())
81 {
82 ordered_interfaces.push_back(std::ref(interface));
83 }
84 }
85 }
86 }
87
88 return ordered_names.size() == ordered_interfaces.size();
89}
90
91inline bool interface_list_contains_interface_type(
92 const std::vector<std::string> & interface_type_list, const std::string & interface_type)
93{
94 return std::find(interface_type_list.begin(), interface_type_list.end(), interface_type) !=
95 interface_type_list.end();
96}
97
98} // namespace controller_interface
99
100#endif // CONTROLLER_INTERFACE__HELPERS_HPP_