Skip to content

Commit 3f28e93

Browse files
Fix runtime variant access bug in HardwareComponentInterface::get_command helper method (#2491)
1 parent e433836 commit 3f28e93

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

hardware_interface/include/hardware_interface/hardware_component_interface.hpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,14 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif
582582
lifecycle_state_ = new_state;
583583
}
584584

585+
/// Set the value of a state interface.
586+
/**
587+
* \tparam T The type of the value to be stored.
588+
* \param[in] interface_name The name of the state interface to access.
589+
* \param[in] value The value to store.
590+
* \throws std::runtime_error This method throws a runtime error if it cannot
591+
* access the state interface.
592+
*/
585593
template <typename T>
586594
void set_state(const std::string & interface_name, const T & value)
587595
{
@@ -600,6 +608,14 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif
600608
std::ignore = handle->set_value(lock, value);
601609
}
602610

611+
/// Get the value from a state interface.
612+
/**
613+
* \tparam T The type of the value to be retrieved.
614+
* \param[in] interface_name The name of the state interface to access.
615+
* \return The value obtained from the interface.
616+
* \throws std::runtime_error This method throws a runtime error if it cannot
617+
* access the state interface or its stored value.
618+
*/
603619
template <typename T = double>
604620
T get_state(const std::string & interface_name) const
605621
{
@@ -626,6 +642,15 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif
626642
return opt_value.value();
627643
}
628644

645+
/// Set the value of a command interface.
646+
/**
647+
* \tparam T The type of the value to be stored.
648+
* \param interface_name The name of the command
649+
* interface to access.
650+
* \param value The value to store.
651+
* \throws This method throws a runtime error if it
652+
* cannot access the command interface.
653+
*/
629654
template <typename T>
630655
void set_command(const std::string & interface_name, const T & value)
631656
{
@@ -644,6 +669,14 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif
644669
std::ignore = handle->set_value(lock, value);
645670
}
646671

672+
/// Get the value from a command interface.
673+
/**
674+
* \tparam T The type of the value to be retrieved.
675+
* \param[in] interface_name The name of the command interface to access.
676+
* \return The value obtained from the interface.
677+
* \throws std::runtime_error This method throws a runtime error if it cannot
678+
* access the command interface or its stored value.
679+
*/
647680
template <typename T = double>
648681
T get_command(const std::string & interface_name) const
649682
{
@@ -659,7 +692,7 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif
659692
}
660693
auto & handle = it->second;
661694
std::shared_lock<std::shared_mutex> lock(handle->get_mutex());
662-
const auto opt_value = handle->get_optional<double>(lock);
695+
const auto opt_value = handle->get_optional<T>(lock);
663696
if (!opt_value)
664697
{
665698
throw std::runtime_error(

0 commit comments

Comments
 (0)