ros2_control - humble
resource_manager.hpp
1 // Copyright 2020 Open Source Robotics Foundation, Inc.
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 HARDWARE_INTERFACE__RESOURCE_MANAGER_HPP_
16 #define HARDWARE_INTERFACE__RESOURCE_MANAGER_HPP_
17 
18 #include <memory>
19 #include <mutex>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "hardware_interface/hardware_component_info.hpp"
25 #include "hardware_interface/hardware_info.hpp"
26 #include "hardware_interface/loaned_command_interface.hpp"
27 #include "hardware_interface/loaned_state_interface.hpp"
28 #include "hardware_interface/types/hardware_interface_return_values.hpp"
29 #include "rclcpp/duration.hpp"
30 #include "rclcpp/time.hpp"
31 
32 namespace hardware_interface
33 {
34 class ActuatorInterface;
35 class SensorInterface;
36 class SystemInterface;
37 class ResourceStorage;
38 
40 {
41  bool ok;
42  std::vector<std::string> failed_hardware_names;
43 };
44 
45 class HARDWARE_INTERFACE_PUBLIC ResourceManager
46 {
47 public:
50 
52 
66  explicit ResourceManager(
67  const std::string & urdf, bool validate_interfaces = true, bool activate_all = false);
68 
69  ResourceManager(const ResourceManager &) = delete;
70 
71  ~ResourceManager();
72 
74 
85  void load_urdf(
86  const std::string & urdf, bool validate_interfaces = true,
87  bool load_and_initialize_components = true);
88 
97  bool is_urdf_already_loaded() const;
98 
100 
108  LoanedStateInterface claim_state_interface(const std::string & key);
109 
111 
115  std::vector<std::string> state_interface_keys() const;
116 
118 
122  std::vector<std::string> available_state_interfaces() const;
123 
125 
128  bool state_interface_is_available(const std::string & name) const;
129 
131 
140  void import_controller_reference_interfaces(
141  const std::string & controller_name, std::vector<CommandInterface> & interfaces);
142 
144 
150  std::vector<std::string> get_controller_reference_interface_names(
151  const std::string & controller_name);
152 
154 
161  void make_controller_reference_interfaces_available(const std::string & controller_name);
162 
164 
171  void make_controller_reference_interfaces_unavailable(const std::string & controller_name);
172 
174 
180  void remove_controller_reference_interfaces(const std::string & controller_name);
181 
183 
190  void cache_controller_to_hardware(
191  const std::string & controller_name, const std::vector<std::string> & interfaces);
192 
194 
201  std::vector<std::string> get_cached_controllers_to_hardware(const std::string & hardware_name);
202 
204 
212  bool command_interface_is_claimed(const std::string & key) const;
213 
215 
223  LoanedCommandInterface claim_command_interface(const std::string & key);
224 
226 
230  std::vector<std::string> command_interface_keys() const;
231 
233 
237  std::vector<std::string> available_command_interfaces() const;
238 
240 
244  bool command_interface_is_available(const std::string & interface) const;
245 
247 
250  size_t actuator_components_size() const;
251 
253 
256  size_t sensor_components_size() const;
257 
259 
262  size_t system_components_size() const;
263 
265 
277  void import_component(
278  std::unique_ptr<ActuatorInterface> actuator, const HardwareInfo & hardware_info);
279 
281 
293  void import_component(
294  std::unique_ptr<SensorInterface> sensor, const HardwareInfo & hardware_info);
295 
297 
309  void import_component(
310  std::unique_ptr<SystemInterface> system, const HardwareInfo & hardware_info);
311 
313 
316  std::unordered_map<std::string, HardwareComponentInfo> get_components_status();
317 
319 
335  bool prepare_command_mode_switch(
336  const std::vector<std::string> & start_interfaces,
337  const std::vector<std::string> & stop_interfaces);
338 
340 
352  bool perform_command_mode_switch(
353  const std::vector<std::string> & start_interfaces,
354  const std::vector<std::string> & stop_interfaces);
355 
357 
369  return_type set_component_state(
370  const std::string & component_name, rclcpp_lifecycle::State & target_state);
371 
373 
379  HardwareReadWriteStatus read(const rclcpp::Time & time, const rclcpp::Duration & period);
380 
382 
388  HardwareReadWriteStatus write(const rclcpp::Time & time, const rclcpp::Duration & period);
389 
391 
396  [[deprecated(
397  "The method 'activate_all_components' is deprecated. "
398  "Use the new 'hardware_components_initial_state' parameter structure to setup the "
399  "components")]] void
400  activate_all_components();
401 
403 
407  bool command_interface_exists(const std::string & key) const;
408 
410 
413  bool state_interface_exists(const std::string & key) const;
414 
415 private:
416  void validate_storage(const std::vector<hardware_interface::HardwareInfo> & hardware_info) const;
417 
418  void release_command_interface(const std::string & key);
419 
420  std::unordered_map<std::string, bool> claimed_command_interface_map_;
421 
422  mutable std::recursive_mutex resource_interfaces_lock_;
423  mutable std::recursive_mutex claimed_command_interfaces_lock_;
424  mutable std::recursive_mutex resources_lock_;
425 
426  std::unique_ptr<ResourceStorage> resource_storage_;
427 
428  // Structure to store read and write status so it is not initialized in the real-time loop
429  HardwareReadWriteStatus read_write_status;
430 
431  bool is_urdf_loaded__ = false;
432 };
433 
434 } // namespace hardware_interface
435 #endif // HARDWARE_INTERFACE__RESOURCE_MANAGER_HPP_
Definition: loaned_command_interface.hpp:27
Definition: loaned_state_interface.hpp:27
Definition: resource_manager.hpp:46
Definition: actuator.hpp:31
This structure stores information about hardware defined in a robot's URDF.
Definition: hardware_info.hpp:106
Definition: resource_manager.hpp:40