15#ifndef HARDWARE_INTERFACE__SYSTEM_INTERFACE_HPP_
16#define HARDWARE_INTERFACE__SYSTEM_INTERFACE_HPP_
21#include <unordered_map>
25#include "hardware_interface/component_parser.hpp"
26#include "hardware_interface/handle.hpp"
27#include "hardware_interface/hardware_info.hpp"
28#include "hardware_interface/types/hardware_interface_return_values.hpp"
29#include "hardware_interface/types/hardware_interface_type_values.hpp"
30#include "hardware_interface/types/lifecycle_state_names.hpp"
31#include "hardware_interface/types/trigger_type.hpp"
32#include "lifecycle_msgs/msg/state.hpp"
33#include "rclcpp/duration.hpp"
34#include "rclcpp/logger.hpp"
35#include "rclcpp/logging.hpp"
36#include "rclcpp/node_interfaces/node_clock_interface.hpp"
37#include "rclcpp/time.hpp"
38#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
39#include "rclcpp_lifecycle/state.hpp"
40#include "realtime_tools/async_function_handler.hpp"
80using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
82class SystemInterface :
public rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface
86 : lifecycle_state_(rclcpp_lifecycle::State(
87 lifecycle_msgs::msg::State::PRIMARY_STATE_UNKNOWN, lifecycle_state_names::UNKNOWN)),
88 system_logger_(rclcpp::get_logger(
"system_interface"))
113 const HardwareInfo & hardware_info, rclcpp::Logger logger,
114 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
116 clock_interface_ = clock_interface;
117 system_logger_ = logger.get_child(
"hardware_component.system." + hardware_info.
name);
118 info_ = hardware_info;
123 async_handler_ = std::make_unique<realtime_tools::AsyncFunctionHandler<return_type>>();
124 async_handler_->init(
125 [
this](
const rclcpp::Time & time,
const rclcpp::Duration & period)
127 if (next_trigger_ == TriggerType::READ)
129 const auto ret =
read(time, period);
130 next_trigger_ = TriggerType::WRITE;
135 const auto ret =
write(time, period);
136 next_trigger_ = TriggerType::READ;
141 async_handler_->start_thread();
154 info_ = hardware_info;
160 return CallbackReturn::SUCCESS;
176 "Replaced by vector<StateInterface::ConstSharedPtr> on_export_state_interfaces() method. "
177 "Exporting is handled "
178 "by the Framework.")]]
virtual std::vector<StateInterface>
193 virtual std::vector<hardware_interface::InterfaceDescription>
210 std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
213 std::vector<StateInterface::ConstSharedPtr> state_interfaces;
214 state_interfaces.reserve(
215 unlisted_interface_descriptions.size() + joint_state_interfaces_.size() +
216 sensor_state_interfaces_.size() + gpio_state_interfaces_.size());
220 for (
const auto & description : unlisted_interface_descriptions)
222 auto name = description.get_name();
223 unlisted_state_interfaces_.insert(std::make_pair(name, description));
224 auto state_interface = std::make_shared<StateInterface>(description);
225 system_states_.insert(std::make_pair(name, state_interface));
226 unlisted_states_.push_back(state_interface);
227 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
230 for (
const auto & [name, descr] : joint_state_interfaces_)
232 auto state_interface = std::make_shared<StateInterface>(descr);
233 system_states_.insert(std::make_pair(name, state_interface));
234 joint_states_.push_back(state_interface);
235 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
237 for (
const auto & [name, descr] : sensor_state_interfaces_)
239 auto state_interface = std::make_shared<StateInterface>(descr);
240 system_states_.insert(std::make_pair(name, state_interface));
241 sensor_states_.push_back(state_interface);
242 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
244 for (
const auto & [name, descr] : gpio_state_interfaces_)
246 auto state_interface = std::make_shared<StateInterface>(descr);
247 system_states_.insert(std::make_pair(name, state_interface));
248 gpio_states_.push_back(state_interface);
249 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
251 return state_interfaces;
267 "Replaced by vector<CommandInterface::SharedPtr> on_export_command_interfaces() method. "
270 "by the Framework.")]]
virtual std::vector<CommandInterface>
285 virtual std::vector<hardware_interface::InterfaceDescription>
302 std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
305 std::vector<CommandInterface::SharedPtr> command_interfaces;
306 command_interfaces.reserve(
307 unlisted_interface_descriptions.size() + joint_command_interfaces_.size() +
308 gpio_command_interfaces_.size());
312 for (
const auto & description : unlisted_interface_descriptions)
314 auto name = description.get_name();
315 unlisted_command_interfaces_.insert(std::make_pair(name, description));
316 auto command_interface = std::make_shared<CommandInterface>(description);
317 system_commands_.insert(std::make_pair(name, command_interface));
318 unlisted_commands_.push_back(command_interface);
319 command_interfaces.push_back(command_interface);
322 for (
const auto & [name, descr] : joint_command_interfaces_)
324 auto command_interface = std::make_shared<CommandInterface>(descr);
325 system_commands_.insert(std::make_pair(name, command_interface));
326 joint_commands_.push_back(command_interface);
327 command_interfaces.push_back(command_interface);
330 for (
const auto & [name, descr] : gpio_command_interfaces_)
332 auto command_interface = std::make_shared<CommandInterface>(descr);
333 system_commands_.insert(std::make_pair(name, command_interface));
334 gpio_commands_.push_back(command_interface);
335 command_interfaces.push_back(command_interface);
337 return command_interfaces;
354 const std::vector<std::string> & ,
355 const std::vector<std::string> & )
357 return return_type::OK;
373 const std::vector<std::string> & ,
374 const std::vector<std::string> & )
376 return return_type::OK;
390 return_type
trigger_read(
const rclcpp::Time & time,
const rclcpp::Duration & period)
392 return_type result = return_type::ERROR;
395 bool trigger_status =
true;
396 if (next_trigger_ == TriggerType::WRITE)
400 "Trigger read called while write async handler call is still pending for hardware "
401 "interface : '%s'. Skipping read cycle and will wait for a write cycle!",
403 return return_type::OK;
405 std::tie(trigger_status, result) = async_handler_->trigger_async_callback(time, period);
410 "Trigger read called while write async trigger is still in progress for hardware "
411 "interface : '%s'. Failed to trigger read cycle!",
413 return return_type::OK;
418 result =
read(time, period);
433 virtual return_type
read(
const rclcpp::Time & time,
const rclcpp::Duration & period) = 0;
445 return_type
trigger_write(
const rclcpp::Time & time,
const rclcpp::Duration & period)
447 return_type result = return_type::ERROR;
450 bool trigger_status =
true;
451 if (next_trigger_ == TriggerType::READ)
455 "Trigger write called while read async handler call is still pending for hardware "
456 "interface : '%s'. Skipping write cycle and will wait for a read cycle!",
458 return return_type::OK;
460 std::tie(trigger_status, result) = async_handler_->trigger_async_callback(time, period);
465 "Trigger write called while read async trigger is still in progress for hardware "
466 "interface : '%s'. Failed to trigger write cycle!",
468 return return_type::OK;
473 result =
write(time, period);
487 virtual return_type
write(
const rclcpp::Time & time,
const rclcpp::Duration & period) = 0;
513 lifecycle_state_ = new_state;
516 void set_state(
const std::string & interface_name,
const double & value)
518 system_states_.at(interface_name)->set_value(value);
521 double get_state(
const std::string & interface_name)
const
523 return system_states_.at(interface_name)->get_value();
526 void set_command(
const std::string & interface_name,
const double & value)
528 system_commands_.at(interface_name)->set_value(value);
531 double get_command(
const std::string & interface_name)
const
533 return system_commands_.at(interface_name)->get_value();
546 rclcpp::Clock::SharedPtr
get_clock()
const {
return clock_interface_->get_clock(); }
557 std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
558 std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
560 std::unordered_map<std::string, InterfaceDescription> sensor_state_interfaces_;
562 std::unordered_map<std::string, InterfaceDescription> gpio_state_interfaces_;
563 std::unordered_map<std::string, InterfaceDescription> gpio_command_interfaces_;
565 std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
566 std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
568 rclcpp_lifecycle::State lifecycle_state_;
569 std::unique_ptr<realtime_tools::AsyncFunctionHandler<return_type>> async_handler_;
572 std::vector<StateInterface::SharedPtr> joint_states_;
573 std::vector<CommandInterface::SharedPtr> joint_commands_;
575 std::vector<StateInterface::SharedPtr> sensor_states_;
577 std::vector<StateInterface::SharedPtr> gpio_states_;
578 std::vector<CommandInterface::SharedPtr> gpio_commands_;
580 std::vector<StateInterface::SharedPtr> unlisted_states_;
581 std::vector<CommandInterface::SharedPtr> unlisted_commands_;
584 rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface_;
585 rclcpp::Logger system_logger_;
587 std::unordered_map<std::string, StateInterface::SharedPtr> system_states_;
588 std::unordered_map<std::string, CommandInterface::SharedPtr> system_commands_;
589 std::atomic<TriggerType> next_trigger_ = TriggerType::READ;
Definition system_interface.hpp:83
virtual std::vector< CommandInterface::SharedPtr > on_export_command_interfaces()
Definition system_interface.hpp:299
return_type trigger_read(const rclcpp::Time &time, const rclcpp::Duration &period)
Triggers the read method synchronously or asynchronously depending on the HardwareInfo.
Definition system_interface.hpp:390
virtual return_type perform_command_mode_switch(const std::vector< std::string > &, const std::vector< std::string > &)
Definition system_interface.hpp:372
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_command_interface_descriptions()
Definition system_interface.hpp:286
virtual std::vector< CommandInterface > export_command_interfaces()
Exports all command interfaces for this hardware interface.
Definition system_interface.hpp:271
virtual std::vector< StateInterface > export_state_interfaces()
Exports all state interfaces for this hardware interface.
Definition system_interface.hpp:179
virtual std::string get_group_name() const
Get name of the actuator hardware group to which it belongs to.
Definition system_interface.hpp:499
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_state_interface_descriptions()
Definition system_interface.hpp:194
const HardwareInfo & get_hardware_info() const
Get the hardware info of the SystemInterface.
Definition system_interface.hpp:552
virtual std::string get_name() const
Get name of the actuator hardware.
Definition system_interface.hpp:493
virtual CallbackReturn on_init(const HardwareInfo &hardware_info)
Initialization of the hardware interface from data parsed from the robot's URDF.
Definition system_interface.hpp:152
rclcpp::Logger get_logger() const
Get the logger of the SystemInterface.
Definition system_interface.hpp:540
return_type trigger_write(const rclcpp::Time &time, const rclcpp::Duration &period)
Triggers the write method synchronously or asynchronously depending on the HardwareInfo.
Definition system_interface.hpp:445
void set_lifecycle_state(const rclcpp_lifecycle::State &new_state)
Set life-cycle state of the actuator hardware.
Definition system_interface.hpp:511
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 system_interface.hpp:353
rclcpp::Clock::SharedPtr get_clock() const
Get the clock of the SystemInterface.
Definition system_interface.hpp:546
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::node_interfaces::NodeClockInterface::SharedPtr clock_interface)
Definition system_interface.hpp:112
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get life-cycle state of the actuator hardware.
Definition system_interface.hpp:505
virtual return_type read(const rclcpp::Time &time, const rclcpp::Duration &period)=0
Read the current state values from the actuator.
virtual std::vector< StateInterface::ConstSharedPtr > on_export_state_interfaces()
Definition system_interface.hpp:207
SystemInterface(const SystemInterface &other)=delete
SystemInterface copy constructor is actively deleted.
virtual return_type write(const rclcpp::Time &time, const rclcpp::Duration &period)=0
Write the current command values to the actuator.
Definition actuator.hpp:33
std::vector< InterfaceDescription > parse_command_interface_descriptions(const std::vector< ComponentInfo > &component_info)
Definition component_parser.cpp:1036
std::vector< InterfaceDescription > parse_state_interface_descriptions(const std::vector< ComponentInfo > &component_info)
Definition component_parser.cpp:1003
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn CallbackReturn
Virtual Class to implement when integrating a 1 DoF actuator into ros2_control.
Definition actuator_interface.hpp:77
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:170
int thread_priority
Async thread priority.
Definition hardware_info.hpp:182
std::string group
Hardware group to which the hardware belongs.
Definition hardware_info.hpp:176
bool is_async
Component is async.
Definition hardware_info.hpp:180
std::vector< ComponentInfo > gpios
Definition hardware_info.hpp:205
std::vector< ComponentInfo > joints
Definition hardware_info.hpp:191
std::string name
Name of the hardware.
Definition hardware_info.hpp:172
std::vector< ComponentInfo > sensors
Definition hardware_info.hpp:200