15#ifndef HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
16#define HARDWARE_INTERFACE__HARDWARE_COMPONENT_INTERFACE_HPP_
18#include <fmt/compile.h>
23#include <unordered_map>
27#include "control_msgs/msg/hardware_status.hpp"
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/duration.hpp"
40#include "rclcpp/logger.hpp"
41#include "rclcpp/logging.hpp"
42#include "rclcpp/node_interfaces/node_clock_interface.hpp"
43#include "rclcpp/time.hpp"
44#include "rclcpp/version.h"
45#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
46#include "rclcpp_lifecycle/state.hpp"
47#include "realtime_tools/async_function_handler.hpp"
48#include "realtime_tools/realtime_publisher.hpp"
49#include "realtime_tools/realtime_thread_safe_box.hpp"
54using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
105 control_msgs::msg::HardwareStatus & msg_template);
128 virtual CallbackReturn
on_init(
144 virtual std::vector<hardware_interface::InterfaceDescription>
162 virtual std::vector<hardware_interface::InterfaceDescription>
189 const std::vector<std::string> & start_interfaces,
190 const std::vector<std::string> & stop_interfaces);
203 const std::vector<std::string> & start_interfaces,
204 const std::vector<std::string> & stop_interfaces);
218 const rclcpp::Time & time,
const rclcpp::Duration & period);
230 virtual return_type
read(
const rclcpp::Time & time,
const rclcpp::Duration & period) = 0;
243 const rclcpp::Time & time,
const rclcpp::Duration & period);
254 virtual return_type
write(
const rclcpp::Time & time,
const rclcpp::Duration & period);
260 const std::string &
get_name()
const;
294 virtual bool has_state(
const std::string & interface_name)
const;
304 const std::string & interface_name)
const;
317 template <
typename T>
319 const StateInterface::SharedPtr & interface_handle,
const T & value,
bool wait_until_set)
321 if (!interface_handle)
323 throw std::runtime_error(
325 "State interface handle is null in hardware component: {}, while calling set_state "
326 "method. This should not happen.",
329 return interface_handle->set_value(value, wait_until_set);
341 template <
typename T>
342 void set_state(
const std::string & interface_name,
const T & value)
357 template <
typename T>
359 const StateInterface::SharedPtr & interface_handle, T & state,
bool wait_until_get)
const
361 if (!interface_handle)
363 throw std::runtime_error(
365 "State interface handle is null in hardware component: {}, while calling get_state "
366 "method. This should not happen.",
369 const bool success = interface_handle->get_value(state, wait_until_get);
370 if (!success && wait_until_get)
372 throw std::runtime_error(
374 "Failed to get state value from interface: {} in hardware component: {}. This should "
376 interface_handle->get_name(), info_.
name));
390 template <
typename T =
double>
403 virtual bool has_command(
const std::string & interface_name)
const;
413 const std::string & interface_name)
const;
425 template <
typename T>
427 const CommandInterface::SharedPtr & interface_handle,
const T & value,
bool wait_until_set)
429 if (!interface_handle)
431 throw std::runtime_error(
433 "Command interface handle is null in hardware component: {}, while calling set_command "
434 "method. This should not happen.",
437 return interface_handle->set_value(value, wait_until_set);
449 template <
typename T>
450 void set_command(
const std::string & interface_name,
const T & value)
465 template <
typename T>
467 const CommandInterface::SharedPtr & interface_handle, T & command,
bool wait_until_get)
const
469 if (!interface_handle)
471 throw std::runtime_error(
473 "Command interface handle is null in hardware component: {}, while calling get_command "
474 "method. This should not happen.",
477 const bool success = interface_handle->get_value(command, wait_until_get);
478 if (!success && wait_until_get)
480 throw std::runtime_error(
482 "Failed to get command value from interface: {} in hardware component: {}. This should "
484 interface_handle->get_name(), info_.
name));
498 template <
typename T =
double>
516 virtual rclcpp::Clock::SharedPtr
get_clock()
const;
522 virtual rclcpp::Node::SharedPtr
get_node()
const;
558 std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
559 std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
561 std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;
563 std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
564 std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;
566 std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
567 std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
569 rclcpp_lifecycle::State lifecycle_state_;
570 std::unique_ptr<realtime_tools::AsyncFunctionHandler<return_type>> async_handler_;
573 std::vector<StateInterface::SharedPtr> joint_states_;
574 std::vector<CommandInterface::SharedPtr> joint_commands_;
576 std::vector<StateInterface::SharedPtr> sensor_states_;
578 std::vector<StateInterface::SharedPtr> gpio_states_;
579 std::vector<CommandInterface::SharedPtr> gpio_commands_;
581 std::vector<StateInterface::SharedPtr> unlisted_states_;
582 std::vector<CommandInterface::SharedPtr> unlisted_commands_;
585 class HardwareComponentInterfaceImpl;
586 std::unique_ptr<HardwareComponentInterfaceImpl> impl_;
589 pal_statistics::RegistrationsRAII stats_registrations_;
Virtual base class for all hardware components (Actuators, Sensors, and Systems).
Definition hardware_component_interface.hpp:65
const std::string & get_name() const
Get name of the hardware.
Definition hardware_component_interface.cpp:468
void prepare_for_activation()
Prepare for the activation of the hardware.
Definition hardware_component_interface.cpp:554
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_state_interface_descriptions()
Definition hardware_component_interface.cpp:284
T get_state(const std::string &interface_name) const
Get the value from a state interface.
Definition hardware_component_interface.hpp:391
bool get_state(const StateInterface::SharedPtr &interface_handle, T &state, bool wait_until_get) const
Definition hardware_component_interface.hpp:358
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get life-cycle state of the hardware.
Definition hardware_component_interface.cpp:472
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:462
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:426
virtual rclcpp::NodeOptions define_custom_node_options() const
Define custom node options for the hardware component interface.
Definition hardware_component_interface.cpp:273
void set_state(const std::string &interface_name, const T &value)
Set the value of a state interface.
Definition hardware_component_interface.hpp:342
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:399
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:241
virtual std::vector< CommandInterface::SharedPtr > on_export_command_interfaces()
Definition hardware_component_interface.cpp:344
void set_lifecycle_state(const rclcpp_lifecycle::State &new_state)
Set life-cycle state of the hardware.
Definition hardware_component_interface.cpp:477
virtual bool has_command(const std::string &interface_name) const
Does the command interface exist?
Definition hardware_component_interface.cpp:507
void pause_async_operations()
Pause any asynchronous operations.
Definition hardware_component_interface.cpp:537
const std::string & get_group_name() const
Get name of the hardware group to which it belongs to.
Definition hardware_component_interface.cpp:470
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:234
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:436
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_command_interface_descriptions()
Definition hardware_component_interface.cpp:338
bool get_command(const CommandInterface::SharedPtr &interface_handle, T &command, bool wait_until_get) const
Definition hardware_component_interface.hpp:466
virtual const StateInterface::SharedPtr & get_state_interface_handle(const std::string &interface_name) const
Get the state interface handle.
Definition hardware_component_interface.cpp:493
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:483
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:318
void set_command(const std::string &interface_name, const T &value)
Set the value of a command interface.
Definition hardware_component_interface.hpp:450
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:385
virtual rclcpp::Node::SharedPtr get_node() const
Get the default node of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:530
virtual std::vector< StateInterface::ConstSharedPtr > on_export_state_interfaces()
Definition hardware_component_interface.cpp:290
CallbackReturn init(const hardware_interface::HardwareComponentParams ¶ms)
Definition hardware_component_interface.cpp:67
void enable_introspection(bool enable)
Enable or disable introspection of the hardware.
Definition hardware_component_interface.cpp:562
virtual const CommandInterface::SharedPtr & get_command_interface_handle(const std::string &interface_name) const
Get the command interface handle.
Definition hardware_component_interface.cpp:512
virtual rclcpp::Logger get_logger() const
Get the logger of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:526
T get_command(const std::string &interface_name) const
Get the value from a command interface.
Definition hardware_component_interface.hpp:499
virtual rclcpp::Clock::SharedPtr get_clock() const
Get the clock.
Definition hardware_component_interface.cpp:528
virtual CallbackReturn on_init(const hardware_interface::HardwareComponentInterfaceParams ¶ms)
Initialization of the hardware interface from data parsed from the robot's URDF.
Definition hardware_component_interface.cpp:248
const HardwareInfo & get_hardware_info() const
Get the hardware info of the HardwareComponentInterface.
Definition hardware_component_interface.cpp:535
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:392
virtual bool has_state(const std::string &interface_name) const
Does the state interface exist?
Definition hardware_component_interface.cpp:488
void stop_async_handler()
Stop and release the async handler thread.
Definition hardware_component_interface.cpp:545
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:373
std::string name
Name of the hardware.
Definition hardware_info.hpp:375