15#ifndef HARDWARE_INTERFACE__ACTUATOR_INTERFACE_HPP_
16#define HARDWARE_INTERFACE__ACTUATOR_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_interface_return_values.hpp"
32#include "hardware_interface/types/lifecycle_state_names.hpp"
33#include "hardware_interface/types/trigger_type.hpp"
34#include "lifecycle_msgs/msg/state.hpp"
35#include "rclcpp/duration.hpp"
36#include "rclcpp/logger.hpp"
37#include "rclcpp/node_interfaces/node_clock_interface.hpp"
38#include "rclcpp/time.hpp"
39#include "rclcpp_lifecycle/node_interfaces/lifecycle_node_interface.hpp"
40#include "rclcpp_lifecycle/state.hpp"
41#include "realtime_tools/async_function_handler.hpp"
46using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
98 rclcpp_lifecycle::State(
99 lifecycle_msgs::msg::State::PRIMARY_STATE_UNKNOWN, lifecycle_state_names::UNKNOWN)),
100 actuator_logger_(rclcpp::get_logger(
"actuator_interface"))
125 const HardwareInfo & hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
127 actuator_clock_ = clock;
128 actuator_logger_ = logger.get_child(
"hardware_component.actuator." + hardware_info.
name);
129 info_ = hardware_info;
134 async_handler_ = std::make_unique<realtime_tools::AsyncFunctionHandler<return_type>>();
135 async_handler_->init(
136 [
this](
const rclcpp::Time & time,
const rclcpp::Duration & period)
138 const auto read_start_time = std::chrono::steady_clock::now();
139 const auto ret_read =
read(time, period);
140 const auto read_end_time = std::chrono::steady_clock::now();
141 read_return_info_.store(ret_read, std::memory_order_release);
142 read_execution_time_.store(
143 std::chrono::duration_cast<std::chrono::nanoseconds>(read_end_time - read_start_time),
144 std::memory_order_release);
145 if (ret_read != return_type::OK)
149 const auto write_start_time = std::chrono::steady_clock::now();
150 const auto ret_write =
write(time, period);
151 const auto write_end_time = std::chrono::steady_clock::now();
152 write_return_info_.store(ret_write, std::memory_order_release);
153 write_execution_time_.store(
154 std::chrono::duration_cast<std::chrono::nanoseconds>(write_end_time - write_start_time),
155 std::memory_order_release);
159 async_handler_->start_thread();
172 info_ = hardware_info;
175 return CallbackReturn::SUCCESS;
191 "Replaced by vector<StateInterface::ConstSharedPtr> on_export_state_interfaces() method. "
194 "by the Framework.")]]
virtual std::vector<StateInterface>
209 virtual std::vector<hardware_interface::InterfaceDescription>
226 std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
229 std::vector<StateInterface::ConstSharedPtr> state_interfaces;
230 state_interfaces.reserve(
231 unlisted_interface_descriptions.size() + joint_state_interfaces_.size());
235 for (
const auto & description : unlisted_interface_descriptions)
237 auto name = description.get_name();
238 unlisted_state_interfaces_.insert(std::make_pair(name, description));
239 auto state_interface = std::make_shared<StateInterface>(description);
240 actuator_states_.insert(std::make_pair(name, state_interface));
241 unlisted_states_.push_back(state_interface);
242 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
245 for (
const auto & [name, descr] : joint_state_interfaces_)
247 auto state_interface = std::make_shared<StateInterface>(descr);
248 actuator_states_.insert(std::make_pair(name, state_interface));
249 joint_states_.push_back(state_interface);
250 state_interfaces.push_back(std::const_pointer_cast<const StateInterface>(state_interface));
252 return state_interfaces;
268 "Replaced by vector<CommandInterface::SharedPtr> on_export_command_interfaces() method. "
271 "by the Framework.")]]
virtual std::vector<CommandInterface>
286 virtual std::vector<hardware_interface::InterfaceDescription>
303 std::vector<hardware_interface::InterfaceDescription> unlisted_interface_descriptions =
306 std::vector<CommandInterface::SharedPtr> command_interfaces;
307 command_interfaces.reserve(
308 unlisted_interface_descriptions.size() + joint_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 actuator_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 actuator_commands_.insert(std::make_pair(name, command_interface));
326 joint_commands_.push_back(command_interface);
327 command_interfaces.push_back(command_interface);
330 return command_interfaces;
344 const std::vector<std::string> & ,
345 const std::vector<std::string> & )
347 return return_type::OK;
361 const std::vector<std::string> & ,
362 const std::vector<std::string> & )
364 return return_type::OK;
379 const rclcpp::Time & time,
const rclcpp::Duration & period)
382 status.result = return_type::ERROR;
385 status.result = read_return_info_.load(std::memory_order_acquire);
386 const auto read_exec_time = read_execution_time_.load(std::memory_order_acquire);
387 if (read_exec_time.count() > 0)
389 status.execution_time = read_exec_time;
391 const auto result = async_handler_->trigger_async_callback(time, period);
392 status.successful = result.first;
393 if (!status.successful)
397 "Trigger read/write called while the previous async trigger is still in progress for "
398 "hardware interface : '%s'. Failed to trigger read/write cycle!",
400 status.result = return_type::OK;
406 const auto start_time = std::chrono::steady_clock::now();
407 status.successful =
true;
408 status.result =
read(time, period);
409 status.execution_time = std::chrono::duration_cast<std::chrono::nanoseconds>(
410 std::chrono::steady_clock::now() - start_time);
425 virtual return_type
read(
const rclcpp::Time & time,
const rclcpp::Duration & period) = 0;
438 const rclcpp::Time & time,
const rclcpp::Duration & period)
441 status.result = return_type::ERROR;
444 status.successful =
true;
445 const auto write_exec_time = write_execution_time_.load(std::memory_order_acquire);
446 if (write_exec_time.count() > 0)
448 status.execution_time = write_exec_time;
450 status.result = write_return_info_.load(std::memory_order_acquire);
454 const auto start_time = std::chrono::steady_clock::now();
455 status.successful =
true;
456 status.result =
write(time, period);
457 status.execution_time = std::chrono::duration_cast<std::chrono::nanoseconds>(
458 std::chrono::steady_clock::now() - start_time);
472 virtual return_type
write(
const rclcpp::Time & time,
const rclcpp::Duration & period) = 0;
498 lifecycle_state_ = new_state;
501 template <
typename T>
502 void set_state(
const std::string & interface_name,
const T & value)
504 auto it = actuator_states_.find(interface_name);
505 if (it == actuator_states_.end())
507 throw std::runtime_error(
510 "State interface not found: {} in actuator hardware component: {}. "
511 "This should not happen."),
512 interface_name, info_.
name));
514 auto & handle = it->second;
515 std::unique_lock<std::shared_mutex> lock(handle->get_mutex());
516 std::ignore = handle->set_value(lock, value);
519 template <
typename T =
double>
520 T get_state(
const std::string & interface_name)
const
522 auto it = actuator_states_.find(interface_name);
523 if (it == actuator_states_.end())
525 throw std::runtime_error(
528 "State interface not found: {} in actuator hardware component: {}. "
529 "This should not happen."),
530 interface_name, info_.
name));
532 auto & handle = it->second;
533 std::shared_lock<std::shared_mutex> lock(handle->get_mutex());
534 const auto opt_value = handle->get_optional<T>(lock);
537 throw std::runtime_error(
539 FMT_COMPILE(
"Failed to get state value from interface: {}. This should not happen."),
542 return opt_value.value();
545 void set_command(
const std::string & interface_name,
const double & value)
547 auto it = actuator_commands_.find(interface_name);
548 if (it == actuator_commands_.end())
550 throw std::runtime_error(
553 "Command interface not found: {} in actuator hardware component: {}. "
554 "This should not happen."),
555 interface_name, info_.
name));
557 auto & handle = it->second;
558 std::unique_lock<std::shared_mutex> lock(handle->get_mutex());
559 std::ignore = handle->set_value(lock, value);
562 template <
typename T =
double>
563 T get_command(
const std::string & interface_name)
const
565 auto it = actuator_commands_.find(interface_name);
566 if (it == actuator_commands_.end())
568 throw std::runtime_error(
571 "Command interface not found: {} in actuator hardware component: {}. "
572 "This should not happen."),
573 interface_name, info_.
name));
575 auto & handle = it->second;
576 std::shared_lock<std::shared_mutex> lock(handle->get_mutex());
577 const auto opt_value = handle->get_optional<
double>(lock);
580 throw std::runtime_error(
582 FMT_COMPILE(
"Failed to get command value from interface: {}. This should not happen."),
585 return opt_value.value();
592 rclcpp::Logger
get_logger()
const {
return actuator_logger_; }
598 rclcpp::Clock::SharedPtr
get_clock()
const {
return actuator_clock_; }
612 read_return_info_.store(return_type::OK, std::memory_order_release);
613 read_execution_time_.store(std::chrono::nanoseconds::zero(), std::memory_order_release);
614 write_return_info_.store(return_type::OK, std::memory_order_release);
615 write_execution_time_.store(std::chrono::nanoseconds::zero(), std::memory_order_release);
626 stats_registrations_.enableAll();
630 stats_registrations_.disableAll();
637 std::unordered_map<std::string, InterfaceDescription> joint_state_interfaces_;
638 std::unordered_map<std::string, InterfaceDescription> joint_command_interfaces_;
640 std::unordered_map<std::string, InterfaceDescription> unlisted_state_interfaces_;
641 std::unordered_map<std::string, InterfaceDescription> unlisted_command_interfaces_;
644 std::vector<StateInterface::SharedPtr> joint_states_;
645 std::vector<CommandInterface::SharedPtr> joint_commands_;
647 std::vector<StateInterface::SharedPtr> unlisted_states_;
648 std::vector<CommandInterface::SharedPtr> unlisted_commands_;
650 rclcpp_lifecycle::State lifecycle_state_;
651 std::unique_ptr<realtime_tools::AsyncFunctionHandler<return_type>> async_handler_;
654 rclcpp::Clock::SharedPtr actuator_clock_;
655 rclcpp::Logger actuator_logger_;
657 std::unordered_map<std::string, StateInterface::SharedPtr> actuator_states_;
658 std::unordered_map<std::string, CommandInterface::SharedPtr> actuator_commands_;
659 std::atomic<return_type> read_return_info_ = return_type::OK;
660 std::atomic<std::chrono::nanoseconds> read_execution_time_ = std::chrono::nanoseconds::zero();
661 std::atomic<return_type> write_return_info_ = return_type::OK;
662 std::atomic<std::chrono::nanoseconds> write_execution_time_ = std::chrono::nanoseconds::zero();
665 pal_statistics::RegistrationsRAII stats_registrations_;
Virtual Class to implement when integrating a 1 DoF actuator into ros2_control.
Definition actuator_interface.hpp:94
virtual return_type read(const rclcpp::Time &time, const rclcpp::Duration &period)=0
Read the current state values from the actuator.
virtual std::vector< CommandInterface > export_command_interfaces()
Exports all command interfaces for this hardware interface.
Definition actuator_interface.hpp:272
virtual CallbackReturn on_init(const HardwareInfo &hardware_info)
Initialization of the hardware interface from data parsed from the robot's URDF.
Definition actuator_interface.hpp:170
virtual std::vector< StateInterface::ConstSharedPtr > on_export_state_interfaces()
Definition actuator_interface.hpp:223
const std::string & get_group_name() const
Get name of the actuator hardware group to which it belongs to.
Definition actuator_interface.hpp:484
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_command_interface_descriptions()
Definition actuator_interface.hpp:287
HardwareComponentCycleStatus trigger_read(const rclcpp::Time &time, const rclcpp::Duration &period)
Triggers the read method synchronously or asynchronously depending on the HardwareInfo.
Definition actuator_interface.hpp:378
virtual return_type write(const rclcpp::Time &time, const rclcpp::Duration &period)=0
Write the current command values to the actuator.
CallbackReturn init(const HardwareInfo &hardware_info, rclcpp::Logger logger, rclcpp::Clock::SharedPtr clock)
Definition actuator_interface.hpp:124
virtual std::vector< hardware_interface::InterfaceDescription > export_unlisted_state_interface_descriptions()
Definition actuator_interface.hpp:210
rclcpp::Logger get_logger() const
Get the logger of the ActuatorInterface.
Definition actuator_interface.hpp:592
virtual std::vector< StateInterface > export_state_interfaces()
Exports all state interfaces for this hardware interface.
Definition actuator_interface.hpp:195
void set_lifecycle_state(const rclcpp_lifecycle::State &new_state)
Set life-cycle state of the actuator hardware.
Definition actuator_interface.hpp:496
virtual std::vector< CommandInterface::SharedPtr > on_export_command_interfaces()
Definition actuator_interface.hpp:300
const rclcpp_lifecycle::State & get_lifecycle_state() const
Get life-cycle state of the actuator hardware.
Definition actuator_interface.hpp:490
const HardwareInfo & get_hardware_info() const
Get the hardware info of the ActuatorInterface.
Definition actuator_interface.hpp:604
void enable_introspection(bool enable)
Enable or disable introspection of the hardware.
Definition actuator_interface.hpp:622
const std::string & get_name() const
Get name of the actuator hardware.
Definition actuator_interface.hpp:478
HardwareComponentCycleStatus trigger_write(const rclcpp::Time &time, const rclcpp::Duration &period)
Triggers the write method synchronously or asynchronously depending on the HardwareInfo.
Definition actuator_interface.hpp:437
void prepare_for_activation()
Prepare for the activation of the hardware.
Definition actuator_interface.hpp:610
ActuatorInterface(const ActuatorInterface &other)=delete
ActuatorInterface copy constructor is actively deleted.
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 actuator_interface.hpp:343
virtual return_type perform_command_mode_switch(const std::vector< std::string > &, const std::vector< std::string > &)
Definition actuator_interface.hpp:360
rclcpp::Clock::SharedPtr get_clock() const
Get the clock of the ActuatorInterface.
Definition actuator_interface.hpp:598
Definition actuator.hpp:34
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
This structure stores information about hardware defined in a robot's URDF.
Definition hardware_info.hpp:234
int thread_priority
Async thread priority.
Definition hardware_info.hpp:246
std::string group
Hardware group to which the hardware belongs.
Definition hardware_info.hpp:240
bool is_async
Component is async.
Definition hardware_info.hpp:244
std::vector< ComponentInfo > joints
Definition hardware_info.hpp:255
std::string name
Name of the hardware.
Definition hardware_info.hpp:236