15#ifndef HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
16#define HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
18#include <fmt/format.h>
25#include <unordered_map>
28#include "hardware_interface/component_parser.hpp"
29#include "hardware_interface/handle.hpp"
30#include "hardware_interface/hardware_info.hpp"
31#include "hardware_interface/introspection.hpp"
32#include "hardware_interface/types/hardware_component_interface_params.hpp"
33#include "hardware_interface/types/hardware_component_params.hpp"
34#include "hardware_interface/types/hardware_interface_return_values.hpp"
35#include "hardware_interface/types/hardware_interface_type_values.hpp"
36#include "hardware_interface/types/lifecycle_state_names.hpp"
37#include "hardware_interface/types/trigger_type.hpp"
38#include "lifecycle_msgs/msg/state.hpp"
39#include "rclcpp/clock.hpp"
40#include "rclcpp/duration.hpp"
41#include "rclcpp/logger.hpp"
42#include "rclcpp/logging.hpp"
43#include "rclcpp/node.hpp"
44#include "rclcpp/node_interfaces/node_clock_interface.hpp"
45#include "rclcpp/node_options.hpp"
46#include "rclcpp/time.hpp"
47#include "rclcpp/version.h"
48#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
49#include "rclcpp_lifecycle/state.hpp"
50#include "realtime_tools/async_function_handler.hpp"
55using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
90 [[deprecated(
"Use init(HardwareInfo, rclcpp::Logger, rclcpp::Clock::SharedPtr) instead.")]]
92 const HardwareInfo & hardware_info, rclcpp::Logger logger,
93 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
95#pragma GCC diagnostic push
96#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
97 return this->
init(hardware_info, logger, clock_interface->get_clock());
98#pragma GCC diagnostic pop
111 "Replaced by CallbackReturn init(const hardware_interface::HardwareComponentParams & "
112 "params). Initialization is handled by the Framework.")]]
114 const HardwareInfo & hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
118 params.
clock = clock;
143 [[deprecated(
"Use on_init(const HardwareComponentInterfaceParams & params) instead.")]]
156 virtual CallbackReturn
on_init(
179 "Replaced by vector<StateInterface::ConstSharedPtr> on_export_state_interfaces() method. "
180 "Exporting is handled by the Framework.")]]
virtual std::vector<StateInterface>
189 virtual std::vector<hardware_interface::InterfaceDescription>
214 "Replaced by vector<CommandInterface::SharedPtr> on_export_command_interfaces() method. "
215 "Exporting is handled by the Framework.")]]
virtual std::vector<CommandInterface>
224 virtual std::vector<hardware_interface::InterfaceDescription>
251 const std::vector<std::string> & start_interfaces,
252 const std::vector<std::string> & stop_interfaces);
265 const std::vector<std::string> & start_interfaces,
266 const std::vector<std::string> & stop_interfaces);
280 const rclcpp::Time & time,
const rclcpp::Duration & period);
292 virtual return_type
read(
const rclcpp::Time & time,
const rclcpp::Duration & period) = 0;
305 const rclcpp::Time & time,
const rclcpp::Duration & period);
316 virtual return_type
write(
const rclcpp::Time & time,
const rclcpp::Duration & period);
322 const std::string &
get_name()
const;
356 virtual bool has_state(
const std::string & interface_name)
const;
366 const std::string & interface_name)
const;
379 template <
typename T>
381 const StateInterface::SharedPtr & interface_handle,
const T & value,
bool wait_until_set)
383 if (!interface_handle)
385 throw std::runtime_error(
387 "State interface handle is null in hardware component: {}, while calling set_state "
388 "method. This should not happen.",
391 return interface_handle->set_value(value, wait_until_set);
403 template <
typename T>
404 void set_state(
const std::string & interface_name,
const T & value)
419 template <
typename T>
421 const StateInterface::SharedPtr & interface_handle, T & state,
bool wait_until_get)
const
423 if (!interface_handle)
425 throw std::runtime_error(
427 "State interface handle is null in hardware component: {}, while calling get_state "
428 "method. This should not happen.",
431 const bool success = interface_handle->get_value(state, wait_until_get);
432 if (!success && wait_until_get)
434 throw std::runtime_error(
436 "Failed to get state value from interface: {} in hardware component: {}. This should "
438 interface_handle->get_name(), info_.
name));
452 template <
typename T =
double>
465 virtual bool has_command(
const std::string & interface_name)
const;
475 const std::string & interface_name)
const;
487 template <
typename T>
489 const CommandInterface::SharedPtr & interface_handle,
const T & value,
bool wait_until_set)
491 if (!interface_handle)
493 throw std::runtime_error(
495 "Command interface handle is null in hardware component: {}, while calling set_command "
496 "method. This should not happen.",
499 return interface_handle->set_value(value, wait_until_set);
511 template <
typename T>
512 void set_command(
const std::string & interface_name,
const T & value)
527 template <
typename T>
529 const CommandInterface::SharedPtr & interface_handle, T & command,
bool wait_until_get)
const
531 if (!interface_handle)
533 throw std::runtime_error(
535 "Command interface handle is null in hardware component: {}, while calling get_command "
536 "method. This should not happen.",
539 const bool success = interface_handle->get_value(command, wait_until_get);
540 if (!success && wait_until_get)
542 throw std::runtime_error(
544 "Failed to get command value from interface: {} in hardware component: {}. This should "
546 interface_handle->get_name(), info_.
name));
560 template <
typename T =
double>
578 virtual rclcpp::Clock::SharedPtr
get_clock()
const;
584 virtual rclcpp::Node::SharedPtr
get_node()
const;
620 std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
621 std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
623 std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;
625 std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
626 std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;
628 std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
629 std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
631 rclcpp_lifecycle::State lifecycle_state_;
632 std::unique_ptr<realtime_tools::AsyncFunctionHandler<return_type>> async_handler_;
635 std::vector<StateInterface::SharedPtr> joint_states_;
636 std::vector<CommandInterface::SharedPtr> joint_commands_;
638 std::vector<StateInterface::SharedPtr> sensor_states_;
640 std::vector<StateInterface::SharedPtr> gpio_states_;
641 std::vector<CommandInterface::SharedPtr> gpio_commands_;
643 std::vector<StateInterface::SharedPtr> unlisted_states_;
644 std::vector<CommandInterface::SharedPtr> unlisted_commands_;
647 class HardwareComponentInterfaceImpl;
648 std::unique_ptr<HardwareComponentInterfaceImpl> impl_;
651 pal_statistics::RegistrationsRAII stats_registrations_;
Virtual base class for all hardware components (Actuators, Sensors, and Systems).
Definition hardware_component_interface.hpp:66
const std::string & get_name() const
Get name of the hardware.
Definition hardware_component_interface.cpp:400
void prepare_for_activation()
Prepare for the activation of the hardware.
Definition hardware_component_interface.cpp:486
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_state_interface_descriptions()
Definition hardware_component_interface.cpp:208
T get_state(const std::string &interface_name) const
Get the value from a state interface.
Definition hardware_component_interface.hpp:453
bool get_state(const StateInterface::SharedPtr &interface_handle, T &state, bool wait_until_get) const
Definition hardware_component_interface.hpp:420
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get life-cycle state of the hardware.
Definition hardware_component_interface.cpp:404
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:394
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:488
virtual rclcpp::NodeOptions define_custom_node_options() const
Define custom node options for the hardware component interface.
Definition hardware_component_interface.cpp:189
void set_state(const std::string &interface_name, const T &value)
Set the value of a state interface.
Definition hardware_component_interface.hpp:404
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:331
virtual std::vector< CommandInterface::SharedPtr > on_export_command_interfaces()
Definition hardware_component_interface.cpp:276
virtual CallbackReturn on_init(const HardwareInfo &hardware_info)
Initialization of the hardware interface from data parsed from the robot's URDF.
Definition hardware_component_interface.cpp:154
void set_lifecycle_state(const rclcpp_lifecycle::State &new_state)
Set life-cycle state of the hardware.
Definition hardware_component_interface.cpp:409
virtual bool has_command(const std::string &interface_name) const
Does the command interface exist?
Definition hardware_component_interface.cpp:439
void pause_async_operations()
Pause any asynchronous operations.
Definition hardware_component_interface.cpp:469
const std::string & get_group_name() const
Get name of the hardware group to which it belongs to.
Definition hardware_component_interface.cpp:402
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:368
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_command_interface_descriptions()
Definition hardware_component_interface.cpp:270
virtual std::vector< CommandInterface > export_command_interfaces()
Exports all command interfaces for this hardware interface.
Definition hardware_component_interface.cpp:261
bool get_command(const CommandInterface::SharedPtr &interface_handle, T &command, bool wait_until_get) const
Definition hardware_component_interface.hpp:528
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
Definition hardware_component_interface.hpp:91
virtual const StateInterface::SharedPtr & get_state_interface_handle(const std::string &interface_name) const
Get the state interface handle.
Definition hardware_component_interface.cpp:425
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:415
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:380
void set_command(const std::string &interface_name, const T &value)
Set the value of a command interface.
Definition hardware_component_interface.hpp:512
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:317
virtual rclcpp::Node::SharedPtr get_node() const
Get the default node of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:462
virtual std::vector< StateInterface::ConstSharedPtr > on_export_state_interfaces()
Definition hardware_component_interface.cpp:214
virtual std::vector< StateInterface > export_state_interfaces()
Exports all state interfaces for this hardware interface.
Definition hardware_component_interface.cpp:199
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
Definition hardware_component_interface.hpp:113
void enable_introspection(bool enable)
Enable or disable introspection of the hardware.
Definition hardware_component_interface.cpp:494
virtual const CommandInterface::SharedPtr & get_command_interface_handle(const std::string &interface_name) const
Get the command interface handle.
Definition hardware_component_interface.cpp:444
virtual rclcpp::Logger get_logger() const
Get the logger of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:458
T get_command(const std::string &interface_name) const
Get the value from a command interface.
Definition hardware_component_interface.hpp:561
virtual rclcpp::Clock::SharedPtr get_clock() const
Get the clock.
Definition hardware_component_interface.cpp:460
const HardwareInfo & get_hardware_info() const
Get the hardware info of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:467
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:324
virtual bool has_state(const std::string &interface_name) const
Does the state interface exist?
Definition hardware_component_interface.cpp:420
void stop_async_handler()
Stop and release the async handler thread.
Definition hardware_component_interface.cpp:477
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
rclcpp::Logger logger
A logger instance taken from resource manager.
Definition hardware_component_params.hpp:45
rclcpp::Clock::SharedPtr clock
Shared pointer to the rclcpp::Clock to be used by this hardware component. Typically,...
Definition hardware_component_params.hpp:51
hardware_interface::HardwareInfo hardware_info
Reference to the HardwareInfo struct for this specific component, parsed from the URDF....
Definition hardware_component_params.hpp:40
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