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 "hardware_interface/component_parser.hpp"
28#include "hardware_interface/handle.hpp"
29#include "hardware_interface/hardware_info.hpp"
30#include "hardware_interface/introspection.hpp"
31#include "hardware_interface/types/hardware_component_interface_params.hpp"
32#include "hardware_interface/types/hardware_component_params.hpp"
33#include "hardware_interface/types/hardware_interface_return_values.hpp"
34#include "hardware_interface/types/hardware_interface_type_values.hpp"
35#include "hardware_interface/types/lifecycle_state_names.hpp"
36#include "hardware_interface/types/trigger_type.hpp"
37#include "lifecycle_msgs/msg/state.hpp"
38#include "rclcpp/duration.hpp"
39#include "rclcpp/logger.hpp"
40#include "rclcpp/logging.hpp"
41#include "rclcpp/node_interfaces/node_clock_interface.hpp"
42#include "rclcpp/time.hpp"
43#include "rclcpp/version.h"
44#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
45#include "rclcpp_lifecycle/state.hpp"
46#include "realtime_tools/async_function_handler.hpp"
51static inline rclcpp::NodeOptions get_hardware_component_node_options()
53 rclcpp::NodeOptions node_options;
55#if RCLCPP_VERSION_MAJOR >= 21
56 node_options.enable_logger_service(
true);
61using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
76 rclcpp_lifecycle::State(
77 lifecycle_msgs::msg::State::PRIMARY_STATE_UNKNOWN, lifecycle_state_names::UNKNOWN)),
78 logger_(rclcpp::get_logger(
"hardware_component_interface"))
103 "Replaced by CallbackReturn init(const hardware_interface::HardwareComponentParams & "
104 "params). Initialization is handled by the Framework.")]]
106 const HardwareInfo & hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
110 params.
clock = clock;
129 clock_ = params.
clock;
130 auto logger_copy = params.
logger;
131 logger_ = logger_copy.get_child(
138 async_handler_ = std::make_unique<realtime_tools::AsyncFunctionHandler<return_type>>();
139 async_handler_->init(
140 [
this](
const rclcpp::Time & time,
const rclcpp::Duration & period)
142 const auto read_start_time = std::chrono::steady_clock::now();
143 const auto ret_read =
read(time, period);
144 const auto read_end_time = std::chrono::steady_clock::now();
145 read_return_info_.store(ret_read, std::memory_order_release);
146 read_execution_time_.store(
147 std::chrono::duration_cast<std::chrono::nanoseconds>(read_end_time - read_start_time),
148 std::memory_order_release);
149 if (ret_read != return_type::OK)
153 if (info_.
type !=
"sensor")
155 const auto write_start_time = std::chrono::steady_clock::now();
156 const auto ret_write =
write(time, period);
157 const auto write_end_time = std::chrono::steady_clock::now();
158 write_return_info_.store(ret_write, std::memory_order_release);
159 write_execution_time_.store(
160 std::chrono::duration_cast<std::chrono::nanoseconds>(
161 write_end_time - write_start_time),
162 std::memory_order_release);
165 return return_type::OK;
168 async_handler_->start_thread();
171 if (
auto locked_executor = params.
executor.lock())
175 node_name.begin(), node_name.end(), node_name.begin(),
176 [](
unsigned char c) { return std::tolower(c); });
177 std::replace(node_name.begin(), node_name.end(),
'/',
'_');
178 hardware_component_node_ =
179 std::make_shared<rclcpp::Node>(node_name, get_hardware_component_node_options());
180 locked_executor->add_node(hardware_component_node_->get_node_base_interface());
186 "Executor is not available during hardware component initialization for '%s'. Skipping "
194 return on_init(interface_params);
203 [[deprecated(
"Use on_init(const HardwareComponentInterfaceParams & params) instead.")]]
206 info_ = hardware_info;
207 if (info_.
type ==
"actuator")
212 else if (info_.
type ==
"sensor")
217 else if (info_.
type ==
"system")
225 return CallbackReturn::SUCCESS;
242#pragma GCC diagnostic push
243#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
245#pragma GCC diagnostic pop
261 "Replaced by vector<StateInterface::ConstSharedPtr> on_export_state_interfaces() method. "
262 "Exporting is handled by the Framework.")]]
277 virtual std::vector<hardware_interface::InterfaceDescription>
294 std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
297 std::vector<StateInterface::ConstSharedPtr> state_interfaces;
298 state_interfaces.reserve(
299 unlisted_interface_descriptions.size() + joint_state_interfaces_.size() +
300 sensor_state_interfaces_.size() + gpio_state_interfaces_.size());
304 for (
const auto & description : unlisted_interface_descriptions)
306 auto name = description.get_name();
307 unlisted_state_interfaces_.insert(std::make_pair(name, description));
308 auto state_interface = std::make_shared<StateInterface>(description);
309 hardware_states_.insert(std::make_pair(name, state_interface));
310 unlisted_states_.push_back(state_interface);
311 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
314 for (
const auto & [name, descr] : joint_state_interfaces_)
316 auto state_interface = std::make_shared<StateInterface>(descr);
317 hardware_states_.insert(std::make_pair(name, state_interface));
318 joint_states_.push_back(state_interface);
319 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
321 for (
const auto & [name, descr] : sensor_state_interfaces_)
323 auto state_interface = std::make_shared<StateInterface>(descr);
324 hardware_states_.insert(std::make_pair(name, state_interface));
325 sensor_states_.push_back(state_interface);
326 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
328 for (
const auto & [name, descr] : gpio_state_interfaces_)
330 auto state_interface = std::make_shared<StateInterface>(descr);
331 hardware_states_.insert(std::make_pair(name, state_interface));
332 gpio_states_.push_back(state_interface);
333 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
335 return state_interfaces;
351 "Replaced by vector<CommandInterface::SharedPtr> on_export_command_interfaces() method. "
352 "Exporting is handled by the Framework.")]]
367 virtual std::vector<hardware_interface::InterfaceDescription>
387 std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
390 std::vector<CommandInterface::SharedPtr> command_interfaces;
391 command_interfaces.reserve(
392 unlisted_interface_descriptions.size() + joint_command_interfaces_.size() +
393 gpio_command_interfaces_.size());
397 for (
const auto & description : unlisted_interface_descriptions)
399 auto name = description.get_name();
400 unlisted_command_interfaces_.insert(std::make_pair(name, description));
401 auto command_interface = std::make_shared<CommandInterface>(description);
402 hardware_commands_.insert(std::make_pair(name, command_interface));
403 unlisted_commands_.push_back(command_interface);
404 command_interfaces.push_back(command_interface);
407 for (
const auto & [name, descr] : joint_command_interfaces_)
409 auto command_interface = std::make_shared<CommandInterface>(descr);
410 hardware_commands_.insert(std::make_pair(name, command_interface));
411 joint_commands_.push_back(command_interface);
412 command_interfaces.push_back(command_interface);
415 for (
const auto & [name, descr] : gpio_command_interfaces_)
417 auto command_interface = std::make_shared<CommandInterface>(descr);
418 hardware_commands_.insert(std::make_pair(name, command_interface));
419 gpio_commands_.push_back(command_interface);
420 command_interfaces.push_back(command_interface);
422 return command_interfaces;
437 const std::vector<std::string> & ,
438 const std::vector<std::string> & )
440 return return_type::OK;
454 const std::vector<std::string> & ,
455 const std::vector<std::string> & )
457 return return_type::OK;
472 const rclcpp::Time & time,
const rclcpp::Duration & period)
475 status.result = return_type::ERROR;
478 status.result = read_return_info_.load(std::memory_order_acquire);
479 const auto read_exec_time = read_execution_time_.load(std::memory_order_acquire);
480 if (read_exec_time.count() > 0)
482 status.execution_time = read_exec_time;
484 const auto result = async_handler_->trigger_async_callback(time, period);
485 status.successful = result.first;
486 if (!status.successful)
490 "Trigger read/write called while the previous async trigger is still in progress for "
491 "hardware interface : '%s'. Failed to trigger read/write cycle!",
493 status.result = return_type::OK;
499 const auto start_time = std::chrono::steady_clock::now();
500 status.successful =
true;
501 status.result =
read(time, period);
502 status.execution_time = std::chrono::duration_cast<std::chrono::nanoseconds>(
503 std::chrono::steady_clock::now() - start_time);
518 virtual return_type
read(
const rclcpp::Time & time,
const rclcpp::Duration & period) = 0;
531 const rclcpp::Time & time,
const rclcpp::Duration & period)
534 status.result = return_type::ERROR;
537 status.successful =
true;
538 const auto write_exec_time = write_execution_time_.load(std::memory_order_acquire);
539 if (write_exec_time.count() > 0)
541 status.execution_time = write_exec_time;
543 status.result = write_return_info_.load(std::memory_order_acquire);
547 const auto start_time = std::chrono::steady_clock::now();
548 status.successful =
true;
549 status.result =
write(time, period);
550 status.execution_time = std::chrono::duration_cast<std::chrono::nanoseconds>(
551 std::chrono::steady_clock::now() - start_time);
565 virtual return_type
write(
const rclcpp::Time & ,
const rclcpp::Duration & )
567 return return_type::OK;
594 lifecycle_state_ = new_state;
605 template <
typename T>
606 void set_state(
const std::string & interface_name,
const T & value)
608 auto it = hardware_states_.find(interface_name);
609 if (it == hardware_states_.end())
611 throw std::runtime_error(
614 "State interface not found: {} in hardware component: {}. "
615 "This should not happen."),
616 interface_name, info_.
name));
618 auto & handle = it->second;
619 std::unique_lock<std::shared_mutex> lock(handle->get_mutex());
620 std::ignore = handle->set_value(lock, value);
631 template <
typename T =
double>
634 auto it = hardware_states_.find(interface_name);
635 if (it == hardware_states_.end())
637 throw std::runtime_error(
640 "State interface not found: {} in hardware component: {}. "
641 "This should not happen."),
642 interface_name, info_.
name));
644 auto & handle = it->second;
645 std::shared_lock<std::shared_mutex> lock(handle->get_mutex());
646 const auto opt_value = handle->get_optional<T>(lock);
649 throw std::runtime_error(
651 FMT_COMPILE(
"Failed to get state value from interface: {}. This should not happen."),
654 return opt_value.value();
666 template <
typename T>
667 void set_command(
const std::string & interface_name,
const T & value)
669 auto it = hardware_commands_.find(interface_name);
670 if (it == hardware_commands_.end())
672 throw std::runtime_error(
675 "Command interface not found: {} in hardware component: {}. "
676 "This should not happen."),
677 interface_name, info_.
name));
679 auto & handle = it->second;
680 std::unique_lock<std::shared_mutex> lock(handle->get_mutex());
681 std::ignore = handle->set_value(lock, value);
692 template <
typename T =
double>
695 auto it = hardware_commands_.find(interface_name);
696 if (it == hardware_commands_.end())
698 throw std::runtime_error(
701 "Command interface not found: {} in hardware component: {}. "
702 "This should not happen."),
703 interface_name, info_.
name));
705 auto & handle = it->second;
706 std::shared_lock<std::shared_mutex> lock(handle->get_mutex());
707 const auto opt_value = handle->get_optional<T>(lock);
710 throw std::runtime_error(
712 FMT_COMPILE(
"Failed to get command value from interface: {}. This should not happen."),
715 return opt_value.value();
728 rclcpp::Clock::SharedPtr
get_clock()
const {
return clock_; }
734 rclcpp::Node::SharedPtr
get_node()
const {
return hardware_component_node_; }
748 read_return_info_.store(return_type::OK, std::memory_order_release);
749 read_execution_time_.store(std::chrono::nanoseconds::zero(), std::memory_order_release);
750 write_return_info_.store(return_type::OK, std::memory_order_release);
751 write_execution_time_.store(std::chrono::nanoseconds::zero(), std::memory_order_release);
762 stats_registrations_.enableAll();
766 stats_registrations_.disableAll();
773 std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
774 std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
776 std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;
778 std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
779 std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;
781 std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
782 std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
784 rclcpp_lifecycle::State lifecycle_state_;
785 std::unique_ptr<realtime_tools::AsyncFunctionHandler<return_type>> async_handler_;
788 std::vector<StateInterface::SharedPtr> joint_states_;
789 std::vector<CommandInterface::SharedPtr> joint_commands_;
791 std::vector<StateInterface::SharedPtr> sensor_states_;
793 std::vector<StateInterface::SharedPtr> gpio_states_;
794 std::vector<CommandInterface::SharedPtr> gpio_commands_;
796 std::vector<StateInterface::SharedPtr> unlisted_states_;
797 std::vector<CommandInterface::SharedPtr> unlisted_commands_;
800 rclcpp::Clock::SharedPtr clock_;
801 rclcpp::Logger logger_;
802 rclcpp::Node::SharedPtr hardware_component_node_ =
nullptr;
804 std::unordered_map<std::string, StateInterface::SharedPtr> hardware_states_;
805 std::unordered_map<std::string, CommandInterface::SharedPtr> hardware_commands_;
806 std::atomic<return_type> read_return_info_ = return_type::OK;
807 std::atomic<std::chrono::nanoseconds> read_execution_time_ = std::chrono::nanoseconds::zero();
808 std::atomic<return_type> write_return_info_ = return_type::OK;
809 std::atomic<std::chrono::nanoseconds> write_execution_time_ = std::chrono::nanoseconds::zero();
812 pal_statistics::RegistrationsRAII stats_registrations_;
Virtual base class for all hardware components (Actuators, Sensors, and Systems).
Definition hardware_component_interface.hpp:72
const std::string & get_name() const
Get name of the hardware.
Definition hardware_component_interface.hpp:574
void prepare_for_activation()
Prepare for the activation of the hardware.
Definition hardware_component_interface.hpp:746
T get_state(const std::string &interface_name) const
Get the value from a state interface.
Definition hardware_component_interface.hpp:632
virtual std::vector< StateInterface > export_state_interfaces()
Exports all state interfaces for this hardware interface.
Definition hardware_component_interface.hpp:263
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get life-cycle state of the hardware.
Definition hardware_component_interface.hpp:586
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 &, const rclcpp::Duration &)
Write the current command values to the hardware.
Definition hardware_component_interface.hpp:565
virtual std::vector< StateInterface::ConstSharedPtr > on_export_state_interfaces()
Definition hardware_component_interface.hpp:291
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_command_interface_descriptions()
Definition hardware_component_interface.hpp:368
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_state_interface_descriptions()
Definition hardware_component_interface.hpp:278
void set_state(const std::string &interface_name, const T &value)
Set the value of a state interface.
Definition hardware_component_interface.hpp:606
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.hpp:471
void set_lifecycle_state(const rclcpp_lifecycle::State &new_state)
Set life-cycle state of the hardware.
Definition hardware_component_interface.hpp:592
virtual return_type perform_command_mode_switch(const std::vector< std::string > &, const std::vector< std::string > &)
Definition hardware_component_interface.hpp:453
const std::string & get_group_name() const
Get name of the hardware group to which it belongs to.
Definition hardware_component_interface.hpp:580
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.hpp:530
HardwareComponentInterface(const HardwareComponentInterface &other)=delete
HardwareComponentInterface copy constructor is actively deleted.
void set_command(const std::string &interface_name, const T &value)
Set the value of a command interface.
Definition hardware_component_interface.hpp:667
rclcpp::Node::SharedPtr get_node() const
Get the default node of the HardwareComponentInterface.
Definition hardware_component_interface.hpp:734
virtual return_type prepare_command_mode_switch(const std::vector< std::string > &, const std::vector< std::string > &)
Prepare for a new command interface switch.
Definition hardware_component_interface.hpp:436
CallbackReturn init(const hardware_interface::HardwareComponentParams ¶ms)
Definition hardware_component_interface.hpp:127
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
Definition hardware_component_interface.hpp:105
void enable_introspection(bool enable)
Enable or disable introspection of the hardware.
Definition hardware_component_interface.hpp:758
rclcpp::Logger get_logger() const
Get the logger of the HardwareComponentInterface.
Definition hardware_component_interface.hpp:722
T get_command(const std::string &interface_name) const
Get the value from a command interface.
Definition hardware_component_interface.hpp:693
virtual std::vector< CommandInterface > export_command_interfaces()
Exports all command interfaces for this hardware interface.
Definition hardware_component_interface.hpp:353
virtual std::vector< CommandInterface::SharedPtr > on_export_command_interfaces()
Definition hardware_component_interface.hpp:384
rclcpp::Clock::SharedPtr get_clock() const
Get the clock.
Definition hardware_component_interface.hpp:728
const HardwareInfo & get_hardware_info() const
Get the hardware info of the HardwareComponentInterface.
Definition hardware_component_interface.hpp:740
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.hpp:238
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.hpp:204
Definition actuator.hpp:22
std::vector< InterfaceDescription > parse_command_interface_descriptions(const std::vector< ComponentInfo > &component_info)
Definition component_parser.cpp:1067
std::vector< InterfaceDescription > parse_state_interface_descriptions(const std::vector< ComponentInfo > &component_info)
Definition component_parser.cpp:1034
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
rclcpp::Executor::WeakPtr executor
Weak pointer to the rclcpp::Executor instance. Hardware components can use this (after locking) to ad...
Definition hardware_component_interface_params.hpp:46
hardware_interface::HardwareInfo hardware_info
Reference to the HardwareInfo struct for this specific component, parsed from the URDF....
Definition hardware_component_interface_params.hpp:39
Parameters required for the initialization of a specific hardware component plugin....
Definition hardware_component_params.hpp:32
rclcpp::Executor::WeakPtr executor
Weak pointer to the rclcpp::Executor instance. Hardware components can use this (after locking) to ad...
Definition hardware_component_params.hpp:57
rclcpp::Logger logger
A logger instance taken from resource manager.
Definition hardware_component_params.hpp:44
rclcpp::Clock::SharedPtr clock
Shared pointer to the rclcpp::Clock to be used by this hardware component. Typically,...
Definition hardware_component_params.hpp:50
hardware_interface::HardwareInfo hardware_info
Reference to the HardwareInfo struct for this specific component, parsed from the URDF....
Definition hardware_component_params.hpp:39
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:252
std::string type
Type of the hardware: actuator, sensor or system.
Definition hardware_info.hpp:256
int thread_priority
Async thread priority.
Definition hardware_info.hpp:264
std::string group
Hardware group to which the hardware belongs.
Definition hardware_info.hpp:258
bool is_async
Component is async.
Definition hardware_info.hpp:262
std::vector< ComponentInfo > gpios
Definition hardware_info.hpp:287
std::vector< ComponentInfo > joints
Definition hardware_info.hpp:273
std::string name
Name of the hardware.
Definition hardware_info.hpp:254
std::vector< ComponentInfo > sensors
Definition hardware_info.hpp:282