ros2_control - rolling
Loading...
Searching...
No Matches
hardware_component_interface.hpp
1// Copyright 2025 ros2_control Development Team
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__HARDWARE_COMPONENT_INTERFACE_HPP_
16#define HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
17
18#include <fmt/format.h>
19
20#include <cstdint>
21#include <memory>
22#include <stdexcept>
23#include <string>
24#include <tuple>
25#include <unordered_map>
26#include <vector>
27
28#include "control_msgs/msg/hardware_status.hpp"
29#include "hardware_interface/component_parser.hpp"
30#include "hardware_interface/handle.hpp"
31#include "hardware_interface/hardware_info.hpp"
32#include "hardware_interface/introspection.hpp"
33#include "hardware_interface/types/hardware_component_interface_params.hpp"
34#include "hardware_interface/types/hardware_component_params.hpp"
35#include "hardware_interface/types/hardware_interface_return_values.hpp"
36#include "hardware_interface/types/hardware_interface_type_values.hpp"
37#include "hardware_interface/types/lifecycle_state_names.hpp"
38#include "hardware_interface/types/trigger_type.hpp"
39#include "lifecycle_msgs/msg/state.hpp"
40#include "rclcpp/clock.hpp"
41#include "rclcpp/duration.hpp"
42#include "rclcpp/logger.hpp"
43#include "rclcpp/logging.hpp"
44#include "rclcpp/node.hpp"
45#include "rclcpp/node_interfaces/node_clock_interface.hpp"
46#include "rclcpp/node_options.hpp"
47#include "rclcpp/time.hpp"
48#include "rclcpp/version.h"
49#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
50#include "rclcpp_lifecycle/state.hpp"
51#include "realtime_tools/async_function_handler.hpp"
52#include "realtime_tools/realtime_publisher.hpp"
53#include "realtime_tools/realtime_thread_safe_box.hpp"
54
55namespace hardware_interface
56{
57
58using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
59
68class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface
69{
70public:
72
74
79
81
83
86
96 CallbackReturn init(const hardware_interface::HardwareComponentParams & params);
97
99
108 virtual CallbackReturn init_hardware_status_message(
109 control_msgs::msg::HardwareStatus & msg_template);
110
112
120 virtual return_type update_hardware_status_message(control_msgs::msg::HardwareStatus & msg);
121
123
132 virtual CallbackReturn on_init(
134
136
140 virtual rclcpp::NodeOptions define_custom_node_options() const;
141
148 virtual std::vector<hardware_interface::InterfaceDescription>
150
158 virtual std::vector<StateInterface::ConstSharedPtr> on_export_state_interfaces();
159
166 virtual std::vector<hardware_interface::InterfaceDescription>
168
179 virtual std::vector<CommandInterface::SharedPtr> on_export_command_interfaces();
180
182
192 virtual return_type prepare_command_mode_switch(
193 const std::vector<std::string> & start_interfaces,
194 const std::vector<std::string> & stop_interfaces);
195
196 // Perform switching to the new command interface.
206 virtual return_type perform_command_mode_switch(
207 const std::vector<std::string> & start_interfaces,
208 const std::vector<std::string> & stop_interfaces);
209
211
222 const rclcpp::Time & time, const rclcpp::Duration & period);
223
225
234 virtual return_type read(const rclcpp::Time & time, const rclcpp::Duration & period) = 0;
235
237
247 const rclcpp::Time & time, const rclcpp::Duration & period);
248
250
258 virtual return_type write(const rclcpp::Time & time, const rclcpp::Duration & period);
259
261
264 const std::string & get_name() const;
265
267
270 const std::string & get_group_name() const;
271
273
279 const rclcpp_lifecycle::State & get_lifecycle_state() const;
280
282
288 void set_lifecycle_state(const rclcpp_lifecycle::State & new_state);
289
291 uint8_t get_lifecycle_id() const;
292
294
298 virtual bool has_state(const std::string & interface_name) const;
299
301
307 virtual const StateInterface::SharedPtr & get_state_interface_handle(
308 const std::string & interface_name) const;
309
311
321 template <typename T>
323 const StateInterface::SharedPtr & interface_handle, const T & value, bool wait_until_set)
324 {
325 if (!interface_handle)
326 {
327 throw std::runtime_error(
328 fmt::format(
329 "State interface handle is null in hardware component: {}, while calling set_state "
330 "method. This should not happen.",
331 info_.name));
332 }
333 return interface_handle->set_value(value, wait_until_set);
334 }
335
337
345 template <typename T>
346 void set_state(const std::string & interface_name, const T & value)
347 {
348 std::ignore = set_state(get_state_interface_handle(interface_name), value, true);
349 }
350
361 template <typename T>
363 const StateInterface::SharedPtr & interface_handle, T & state, bool wait_until_get) const
364 {
365 if (!interface_handle)
366 {
367 throw std::runtime_error(
368 fmt::format(
369 "State interface handle is null in hardware component: {}, while calling get_state "
370 "method. This should not happen.",
371 info_.name));
372 }
373 const bool success = interface_handle->get_value(state, wait_until_get);
374 if (!success && wait_until_get)
375 {
376 throw std::runtime_error(
377 fmt::format(
378 "Failed to get state value from interface: {} in hardware component: {}. This should "
379 "not happen.",
380 interface_handle->get_name(), info_.name));
381 }
382 return success;
383 }
384
386
394 template <typename T = double>
395 T get_state(const std::string & interface_name) const
396 {
397 T state;
398 get_state<T>(get_state_interface_handle(interface_name), state, true);
399 return state;
400 }
401
403
407 virtual bool has_command(const std::string & interface_name) const;
408
410
416 virtual const CommandInterface::SharedPtr & get_command_interface_handle(
417 const std::string & interface_name) const;
418
420
429 template <typename T>
431 const CommandInterface::SharedPtr & interface_handle, const T & value, bool wait_until_set)
432 {
433 if (!interface_handle)
434 {
435 throw std::runtime_error(
436 fmt::format(
437 "Command interface handle is null in hardware component: {}, while calling set_command "
438 "method. This should not happen.",
439 info_.name));
440 }
441 return interface_handle->set_value(value, wait_until_set);
442 }
443
445
453 template <typename T>
454 void set_command(const std::string & interface_name, const T & value)
455 {
456 std::ignore = set_command(get_command_interface_handle(interface_name), value, true);
457 }
458
469 template <typename T>
471 const CommandInterface::SharedPtr & interface_handle, T & command, bool wait_until_get) const
472 {
473 if (!interface_handle)
474 {
475 throw std::runtime_error(
476 fmt::format(
477 "Command interface handle is null in hardware component: {}, while calling get_command "
478 "method. This should not happen.",
479 info_.name));
480 }
481 const bool success = interface_handle->get_value(command, wait_until_get);
482 if (!success && wait_until_get)
483 {
484 throw std::runtime_error(
485 fmt::format(
486 "Failed to get command value from interface: {} in hardware component: {}. This should "
487 "not happen.",
488 interface_handle->get_name(), info_.name));
489 }
490 return success;
491 }
492
494
502 template <typename T = double>
503 T get_command(const std::string & interface_name) const
504 {
505 T command;
506 get_command<T>(get_command_interface_handle(interface_name), command, true);
507 return command;
508 }
509
511
514 virtual rclcpp::Logger get_logger() const;
515
517
520 virtual rclcpp::Clock::SharedPtr get_clock() const;
521
523
526 virtual rclcpp::Node::SharedPtr get_node() const;
527
529
532 const HardwareInfo & get_hardware_info() const;
533
535
540
542
545 void stop_async_handler();
546
548
552
554
557 void enable_introspection(bool enable);
558
559protected:
560 HardwareInfo info_;
561 // interface names to InterfaceDescription
562 std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
563 std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
564
565 std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;
566
567 std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
568 std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;
569
570 std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
571 std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
572
573 rclcpp_lifecycle::State lifecycle_state_;
574 std::unique_ptr<realtime_tools::AsyncFunctionHandler<return_type>> async_handler_;
575
576 // Exported Command- and StateInterfaces in order they are listed in the hardware description.
577 std::vector<StateInterface::SharedPtr> joint_states_;
578 std::vector<CommandInterface::SharedPtr> joint_commands_;
579
580 std::vector<StateInterface::SharedPtr> sensor_states_;
581
582 std::vector<StateInterface::SharedPtr> gpio_states_;
583 std::vector<CommandInterface::SharedPtr> gpio_commands_;
584
585 std::vector<StateInterface::SharedPtr> unlisted_states_;
586 std::vector<CommandInterface::SharedPtr> unlisted_commands_;
587
588private:
589 class HardwareComponentInterfaceImpl;
590 std::unique_ptr<HardwareComponentInterfaceImpl> impl_;
591
592protected:
593 pal_statistics::RegistrationsRAII stats_registrations_;
594};
595
596} // namespace hardware_interface
597#endif // HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
Virtual base class for all hardware components (Actuators, Sensors, and Systems).
Definition hardware_component_interface.hpp:69
const std::string & get_name() const
Get name of the hardware.
Definition hardware_component_interface.cpp:477
void prepare_for_activation()
Prepare for the activation of the hardware.
Definition hardware_component_interface.cpp:563
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_state_interface_descriptions()
Definition hardware_component_interface.cpp:293
T get_state(const std::string &interface_name) const
Get the value from a state interface.
Definition hardware_component_interface.hpp:395
bool get_state(const StateInterface::SharedPtr &interface_handle, T &state, bool wait_until_get) const
Definition hardware_component_interface.hpp:362
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get life-cycle state of the hardware.
Definition hardware_component_interface.cpp:481
virtual return_type read(const rclcpp::Time &time, const rclcpp::Duration &period)=0
Read the current state values from the hardware.
virtual return_type write(const rclcpp::Time &time, const rclcpp::Duration &period)
Write the current command values to the hardware.
Definition hardware_component_interface.cpp:471
bool set_command(const CommandInterface::SharedPtr &interface_handle, const T &value, bool wait_until_set)
Set the value of a command interface.
Definition hardware_component_interface.hpp:430
virtual rclcpp::NodeOptions define_custom_node_options() const
Define custom node options for the hardware component interface.
Definition hardware_component_interface.cpp:282
void set_state(const std::string &interface_name, const T &value)
Set the value of a state interface.
Definition hardware_component_interface.hpp:346
HardwareComponentCycleStatus trigger_read(const rclcpp::Time &time, const rclcpp::Duration &period)
Triggers the read method synchronously or asynchronously depending on the HardwareInfo.
Definition hardware_component_interface.cpp:408
virtual return_type update_hardware_status_message(control_msgs::msg::HardwareStatus &msg)
User-overridable method to fill the hardware status message with real-time data.
Definition hardware_component_interface.cpp:250
virtual std::vector< CommandInterface::SharedPtr > on_export_command_interfaces()
Definition hardware_component_interface.cpp:353
void set_lifecycle_state(const rclcpp_lifecycle::State &new_state)
Set life-cycle state of the hardware.
Definition hardware_component_interface.cpp:486
virtual bool has_command(const std::string &interface_name) const
Does the command interface exist?
Definition hardware_component_interface.cpp:516
void pause_async_operations()
Pause any asynchronous operations.
Definition hardware_component_interface.cpp:546
const std::string & get_group_name() const
Get name of the hardware group to which it belongs to.
Definition hardware_component_interface.cpp:479
virtual CallbackReturn init_hardware_status_message(control_msgs::msg::HardwareStatus &msg_template)
User-overridable method to configure the structure of the HardwareStatus message.
Definition hardware_component_interface.cpp:243
HardwareComponentCycleStatus trigger_write(const rclcpp::Time &time, const rclcpp::Duration &period)
Triggers the write method synchronously or asynchronously depending on the HardwareInfo.
Definition hardware_component_interface.cpp:445
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_command_interface_descriptions()
Definition hardware_component_interface.cpp:347
bool get_command(const CommandInterface::SharedPtr &interface_handle, T &command, bool wait_until_get) const
Definition hardware_component_interface.hpp:470
virtual const StateInterface::SharedPtr & get_state_interface_handle(const std::string &interface_name) const
Get the state interface handle.
Definition hardware_component_interface.cpp:502
HardwareComponentInterface(const HardwareComponentInterface &other)=delete
HardwareComponentInterface copy constructor is actively deleted.
uint8_t get_lifecycle_id() const
Get the lifecycle id of the hardware component interface.
Definition hardware_component_interface.cpp:492
bool set_state(const StateInterface::SharedPtr &interface_handle, const T &value, bool wait_until_set)
Set the value of a state interface.
Definition hardware_component_interface.hpp:322
void set_command(const std::string &interface_name, const T &value)
Set the value of a command interface.
Definition hardware_component_interface.hpp:454
virtual return_type prepare_command_mode_switch(const std::vector< std::string > &start_interfaces, const std::vector< std::string > &stop_interfaces)
Prepare for a new command interface switch.
Definition hardware_component_interface.cpp:394
virtual rclcpp::Node::SharedPtr get_node() const
Get the default node of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:539
virtual std::vector< StateInterface::ConstSharedPtr > on_export_state_interfaces()
Definition hardware_component_interface.cpp:299
CallbackReturn init(const hardware_interface::HardwareComponentParams &params)
Definition hardware_component_interface.cpp:76
void enable_introspection(bool enable)
Enable or disable introspection of the hardware.
Definition hardware_component_interface.cpp:571
virtual const CommandInterface::SharedPtr & get_command_interface_handle(const std::string &interface_name) const
Get the command interface handle.
Definition hardware_component_interface.cpp:521
virtual rclcpp::Logger get_logger() const
Get the logger of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:535
T get_command(const std::string &interface_name) const
Get the value from a command interface.
Definition hardware_component_interface.hpp:503
virtual rclcpp::Clock::SharedPtr get_clock() const
Get the clock.
Definition hardware_component_interface.cpp:537
virtual CallbackReturn on_init(const hardware_interface::HardwareComponentInterfaceParams &params)
Initialization of the hardware interface from data parsed from the robot's URDF.
Definition hardware_component_interface.cpp:257
const HardwareInfo & get_hardware_info() const
Get the hardware info of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:544
virtual return_type perform_command_mode_switch(const std::vector< std::string > &start_interfaces, const std::vector< std::string > &stop_interfaces)
Definition hardware_component_interface.cpp:401
virtual bool has_state(const std::string &interface_name) const
Does the state interface exist?
Definition hardware_component_interface.cpp:497
void stop_async_handler()
Stop and release the async handler thread.
Definition hardware_component_interface.cpp:554
Definition mujoco_system_interface.hpp:57
Definition hardware_interface_return_values.hpp:41
Parameters required for the initialization of a specific hardware component plugin....
Definition hardware_component_interface_params.hpp:32
Parameters required for the initialization of a specific hardware component plugin....
Definition hardware_component_params.hpp:33
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:375
std::string name
Name of the hardware.
Definition hardware_info.hpp:377