ros2_control - rolling
Loading...
Searching...
No Matches
semantic_component_command_interface.hpp
1// Copyright (c) 2024, Sherpa Mobile Robotics
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_COMMAND_INTERFACE_HPP_
16#define SEMANTIC_COMPONENTS__SEMANTIC_COMPONENT_COMMAND_INTERFACE_HPP_
17
18#include <cassert>
19#include <functional>
20#include <string>
21#include <vector>
22
23#include "controller_interface/helpers.hpp"
24#include "hardware_interface/loaned_command_interface.hpp"
25
26namespace semantic_components
27{
28template <typename MessageInputType>
30{
31public:
33 const std::string & name, const std::vector<std::string> & interface_names)
34 : name_(name), interface_names_(interface_names)
35 {
36 assert(interface_names.size() > 0);
37 command_interfaces_.reserve(interface_names.size());
38 }
39
40 virtual ~SemanticComponentCommandInterface() = default;
41
50 std::vector<hardware_interface::LoanedCommandInterface> & command_interfaces)
51 {
52 return controller_interface::get_ordered_interfaces(
53 command_interfaces, interface_names_, "", command_interfaces_);
54 }
55
59 void release_interfaces() { command_interfaces_.clear(); }
60
71 const std::vector<std::string> & get_command_interface_names() const { return interface_names_; }
72
79 bool set_values(const std::vector<double> & values)
80 {
81 // check we have sufficient memory
82 if (values.size() != command_interfaces_.size())
83 {
84 return false;
85 }
86 // set values
87 bool all_set = true;
88 for (auto i = 0u; i < values.size(); ++i)
89 {
90 all_set &= command_interfaces_[i].get().set_value(values[i]);
91 }
92 return all_set;
93 }
94
100 virtual bool set_values_from_message(const MessageInputType & /* message */) = 0;
101
102protected:
103 std::string name_;
104 std::vector<std::string> interface_names_;
105 std::vector<std::reference_wrapper<hardware_interface::LoanedCommandInterface>>
106 command_interfaces_;
107};
108
109} // namespace semantic_components
110
111#endif // SEMANTIC_COMPONENTS__SEMANTIC_COMPONENT_COMMAND_INTERFACE_HPP_
Definition semantic_component_command_interface.hpp:30
const std::vector< std::string > & get_command_interface_names() const
Definition of command interface names for the component.
Definition semantic_component_command_interface.hpp:71
bool set_values(const std::vector< double > &values)
Return all values.
Definition semantic_component_command_interface.hpp:79
virtual bool set_values_from_message(const MessageInputType &)=0
Set values from MessageInputType.
void release_interfaces()
Release loaned command interfaces from the hardware.
Definition semantic_component_command_interface.hpp:59
bool assign_loaned_command_interfaces(std::vector< hardware_interface::LoanedCommandInterface > &command_interfaces)
Assign loaned command interfaces from the hardware.
Definition semantic_component_command_interface.hpp:49