|
| 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 <string> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +#include "controller_interface/helpers.hpp" |
| 22 | +#include "hardware_interface/loaned_command_interface.hpp" |
| 23 | + |
| 24 | +namespace semantic_components |
| 25 | +{ |
| 26 | +template <typename MessageInputType> |
| 27 | +class SemanticComponentCommandInterface |
| 28 | +{ |
| 29 | +public: |
| 30 | + SemanticComponentCommandInterface( |
| 31 | + const std::string & name, const std::vector<std::string> & interface_names) |
| 32 | + : name_(name), interface_names_(interface_names) |
| 33 | + { |
| 34 | + assert(interface_names.size() > 0); |
| 35 | + command_interfaces_.reserve(interface_names.size()); |
| 36 | + } |
| 37 | + |
| 38 | + virtual ~SemanticComponentCommandInterface() = default; |
| 39 | + |
| 40 | + /// Assign loaned command interfaces from the hardware. |
| 41 | + /** |
| 42 | + * Assign loaned command interfaces on the controller start. |
| 43 | + * |
| 44 | + * \param[in] command_interfaces vector of command interfaces provided by the controller. |
| 45 | + */ |
| 46 | + bool assign_loaned_command_interfaces( |
| 47 | + std::vector<hardware_interface::LoanedCommandInterface> & command_interfaces) |
| 48 | + { |
| 49 | + return controller_interface::get_ordered_interfaces( |
| 50 | + command_interfaces, interface_names_, "", command_interfaces_); |
| 51 | + } |
| 52 | + |
| 53 | + /// Release loaned command interfaces from the hardware. |
| 54 | + void release_interfaces() { command_interfaces_.clear(); } |
| 55 | + |
| 56 | + /// Definition of command interface names for the component. |
| 57 | + /** |
| 58 | + * The function should be used in "command_interface_configuration()" of a controller to provide |
| 59 | + * standardized command interface names semantic component. |
| 60 | + * |
| 61 | + * \default Default implementation defined command interfaces as "name/NR" where NR is number |
| 62 | + * from 0 to size of values; |
| 63 | + * \return list of strings with command interface names for the semantic component. |
| 64 | + */ |
| 65 | + const std::vector<std::string> & get_command_interface_names() const { return interface_names_; } |
| 66 | + |
| 67 | + /// Return all values. |
| 68 | + /** |
| 69 | + * \return true if it gets all the values, else false (i.e., invalid size or if the method |
| 70 | + * ``hardware_interface::LoanedCommandInterface::set_value`` fails). |
| 71 | + */ |
| 72 | + bool set_values(const std::vector<double> & values) |
| 73 | + { |
| 74 | + // check we have sufficient memory |
| 75 | + if (values.size() != command_interfaces_.size()) |
| 76 | + { |
| 77 | + return false; |
| 78 | + } |
| 79 | + // set values |
| 80 | + bool all_set = true; |
| 81 | + for (auto i = 0u; i < values.size(); ++i) |
| 82 | + { |
| 83 | + all_set &= command_interfaces_[i].get().set_value(values[i]); |
| 84 | + } |
| 85 | + return all_set; |
| 86 | + } |
| 87 | + |
| 88 | + /// Set values from MessageInputType |
| 89 | + /** |
| 90 | + * \return True if all values were set successfully, false otherwise. |
| 91 | + */ |
| 92 | + virtual bool set_values_from_message(const MessageInputType & /* message */) = 0; |
| 93 | + |
| 94 | +protected: |
| 95 | + std::string name_; |
| 96 | + std::vector<std::string> interface_names_; |
| 97 | + std::vector<std::reference_wrapper<hardware_interface::LoanedCommandInterface>> |
| 98 | + command_interfaces_; |
| 99 | +}; |
| 100 | + |
| 101 | +} // namespace semantic_components |
| 102 | + |
| 103 | +#endif // SEMANTIC_COMPONENTS__SEMANTIC_COMPONENT_COMMAND_INTERFACE_HPP_ |
0 commit comments