@@ -703,66 +703,106 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif
703703 return hardware_states_.find (interface_name) != hardware_states_.end ();
704704 }
705705
706- // / Set the value of a state interface.
706+ // / Get the state interface handle
707707 /* *
708- * \tparam T The type of the value to be stored.
709708 * \param[in] interface_name The name of the state interface to access.
710- * \param[in] value The value to store .
711- * \throws std::runtime_error This method throws a runtime error if it cannot
712- * access the state interface .
709+ * \return Shared pointer to the state interface handle .
710+ * \throws std::runtime_error This method throws a runtime error if it cannot find the state
711+ * interface with the given name .
713712 */
714- template < typename T>
715- void set_state ( const std::string & interface_name, const T & value)
713+ const StateInterface::SharedPtr & get_state_interface_handle (
714+ const std::string & interface_name) const
716715 {
717716 auto it = hardware_states_.find (interface_name);
718717 if (it == hardware_states_.end ())
719718 {
720719 throw std::runtime_error (
721720 fmt::format (
722- FMT_COMPILE (
723- " State interface not found: {} in hardware component: {}. "
724- " This should not happen." ),
721+ " The requested state interface not found: '{}' in hardware component: '{}'." ,
725722 interface_name, info_.name ));
726723 }
727- auto & handle = it->second ;
728- std::unique_lock<std::shared_mutex> lock (handle->get_mutex ());
729- std::ignore = handle->set_value (lock, value);
724+ return it->second ;
730725 }
731726
732- // / Get the value from a state interface.
727+ // / Set the value of a state interface.
733728 /* *
734- * \tparam T The type of the value to be retrieved.
729+ * \tparam T The type of the value to be stored.
730+ * \param interface_handle The shared pointer to the state interface to access.
731+ * \param value The value to store.
732+ */
733+ template <typename T>
734+ void set_state (const StateInterface::SharedPtr & interface_handle, const T & value)
735+ {
736+ if (!interface_handle)
737+ {
738+ throw std::runtime_error (
739+ fmt::format (
740+ " State interface handle is null in hardware component: {}, while calling set_state "
741+ " method. This should not happen." ,
742+ info_.name ));
743+ }
744+ std::unique_lock<std::shared_mutex> lock (interface_handle->get_mutex ());
745+ std::ignore = interface_handle->set_value (lock, value);
746+ }
747+
748+ // / Set the value of a state interface.
749+ /* *
750+ * \tparam T The type of the value to be stored.
735751 * \param[in] interface_name The name of the state interface to access.
752+ * \param[in] value The value to store.
753+ * \throws std::runtime_error This method throws a runtime error if it cannot
754+ * access the state interface.
755+ */
756+ template <typename T>
757+ void set_state (const std::string & interface_name, const T & value)
758+ {
759+ set_state (get_state_interface_handle (interface_name), value);
760+ }
761+
762+ /* *
763+ * \tparam T The type of the value to be retrieved.
764+ * \param[in] interface_handle The shared pointer to the state interface to access.
736765 * \return The value obtained from the interface.
737766 * \throws std::runtime_error This method throws a runtime error if it cannot
738767 * access the state interface or its stored value.
739768 */
740- template <typename T = double >
741- T get_state (const std::string & interface_name ) const
769+ template <typename T>
770+ T get_state (const StateInterface::SharedPtr & interface_handle ) const
742771 {
743- auto it = hardware_states_.find (interface_name);
744- if (it == hardware_states_.end ())
772+ if (!interface_handle)
745773 {
746774 throw std::runtime_error (
747775 fmt::format (
748- FMT_COMPILE (
749- " State interface not found: {} in hardware component: {}. "
750- " This should not happen." ),
751- interface_name, info_.name ));
776+ " State interface handle is null in hardware component: {}, while calling get_state "
777+ " method. This should not happen." ,
778+ info_.name ));
752779 }
753- auto & handle = it->second ;
754- std::shared_lock<std::shared_mutex> lock (handle->get_mutex ());
755- const auto opt_value = handle->get_optional <T>(lock);
780+ std::shared_lock<std::shared_mutex> lock (interface_handle->get_mutex ());
781+ const auto opt_value = interface_handle->get_optional <T>(lock);
756782 if (!opt_value)
757783 {
758784 throw std::runtime_error (
759785 fmt::format (
760- FMT_COMPILE ( " Failed to get state value from interface: {}. This should not happen." ) ,
761- interface_name ));
786+ " Failed to get state value from interface: {}. This should not happen." ,
787+ interface_handle-> get_name () ));
762788 }
763789 return opt_value.value ();
764790 }
765791
792+ // / Get the value from a state interface.
793+ /* *
794+ * \tparam T The type of the value to be retrieved.
795+ * \param[in] interface_name The name of the state interface to access.
796+ * \return The value obtained from the interface.
797+ * \throws std::runtime_error This method throws a runtime error if it cannot
798+ * access the state interface or its stored value.
799+ */
800+ template <typename T = double >
801+ T get_state (const std::string & interface_name) const
802+ {
803+ return get_state<T>(get_state_interface_handle (interface_name));
804+ }
805+
766806 // / Does the command interface exist?
767807 /* *
768808 * \param[in] interface_name The name of the command interface.
@@ -773,6 +813,49 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif
773813 return hardware_commands_.find (interface_name) != hardware_commands_.end ();
774814 }
775815
816+ // / Get the command interface handle
817+ /* *
818+ * \param[in] interface_name The name of the command interface to access.
819+ * \return Shared pointer to the command interface handle.
820+ * \throws std::runtime_error This method throws a runtime error if it cannot find the command
821+ * interface with the given name.
822+ */
823+ const CommandInterface::SharedPtr & get_command_interface_handle (
824+ const std::string & interface_name) const
825+ {
826+ auto it = hardware_commands_.find (interface_name);
827+ if (it == hardware_commands_.end ())
828+ {
829+ throw std::runtime_error (
830+ fmt::format (
831+ " The requested command interface not found: '{}' in hardware component: '{}'." ,
832+ interface_name, info_.name ));
833+ }
834+ return it->second ;
835+ }
836+
837+ // / Set the value of a command interface.
838+ /* *
839+ * \tparam T The type of the value to be stored.
840+ * \param interface_handle The shared pointer to the command interface to access.
841+ * \param value The value to store.
842+ * \throws This method throws a runtime error if it cannot access the command interface.
843+ */
844+ template <typename T>
845+ void set_command (const CommandInterface::SharedPtr & interface_handle, const T & value)
846+ {
847+ if (!interface_handle)
848+ {
849+ throw std::runtime_error (
850+ fmt::format (
851+ " Command interface handle is null in hardware component: {}, while calling set_command "
852+ " method. This should not happen." ,
853+ info_.name ));
854+ }
855+ std::unique_lock<std::shared_mutex> lock (interface_handle->get_mutex ());
856+ std::ignore = interface_handle->set_value (lock, value);
857+ }
858+
776859 // / Set the value of a command interface.
777860 /* *
778861 * \tparam T The type of the value to be stored.
@@ -785,55 +868,53 @@ class HardwareComponentInterface : public rclcpp_lifecycle::node_interfaces::Lif
785868 template <typename T>
786869 void set_command (const std::string & interface_name, const T & value)
787870 {
788- auto it = hardware_commands_.find (interface_name);
789- if (it == hardware_commands_.end ())
790- {
791- throw std::runtime_error (
792- fmt::format (
793- FMT_COMPILE (
794- " Command interface not found: {} in hardware component: {}. "
795- " This should not happen." ),
796- interface_name, info_.name ));
797- }
798- auto & handle = it->second ;
799- std::unique_lock<std::shared_mutex> lock (handle->get_mutex ());
800- std::ignore = handle->set_value (lock, value);
871+ set_command (get_command_interface_handle (interface_name), value);
801872 }
802873
803- // / Get the value from a command interface.
804874 /* *
805875 * \tparam T The type of the value to be retrieved.
806- * \param[in] interface_name The name of the command interface to access.
876+ * \param[in] interface_handle The shared pointer to the command interface to access.
807877 * \return The value obtained from the interface.
808878 * \throws std::runtime_error This method throws a runtime error if it cannot
809879 * access the command interface or its stored value.
810880 */
811- template <typename T = double >
812- T get_command (const std::string & interface_name ) const
881+ template <typename T>
882+ T get_command (const CommandInterface::SharedPtr & interface_handle ) const
813883 {
814- auto it = hardware_commands_.find (interface_name);
815- if (it == hardware_commands_.end ())
884+ if (!interface_handle)
816885 {
817886 throw std::runtime_error (
818887 fmt::format (
819- FMT_COMPILE (
820- " Command interface not found: {} in hardware component: {}. "
821- " This should not happen." ),
822- interface_name, info_.name ));
888+ " Command interface handle is null in hardware component: {}, while calling get_command "
889+ " method. This should not happen." ,
890+ info_.name ));
823891 }
824- auto & handle = it->second ;
825- std::shared_lock<std::shared_mutex> lock (handle->get_mutex ());
826- const auto opt_value = handle->get_optional <T>(lock);
892+ std::shared_lock<std::shared_mutex> lock (interface_handle->get_mutex ());
893+ const auto opt_value = interface_handle->get_optional <T>(lock);
827894 if (!opt_value)
828895 {
829896 throw std::runtime_error (
830897 fmt::format (
831- FMT_COMPILE ( " Failed to get command value from interface: {}. This should not happen." ) ,
832- interface_name ));
898+ " Failed to get command value from interface: {}. This should not happen." ,
899+ interface_handle-> get_name () ));
833900 }
834901 return opt_value.value ();
835902 }
836903
904+ // / Get the value from a command interface.
905+ /* *
906+ * \tparam T The type of the value to be retrieved.
907+ * \param[in] interface_name The name of the command interface to access.
908+ * \return The value obtained from the interface.
909+ * \throws std::runtime_error This method throws a runtime error if it cannot
910+ * access the command interface or its stored value.
911+ */
912+ template <typename T = double >
913+ T get_command (const std::string & interface_name) const
914+ {
915+ return get_command<T>(get_command_interface_handle (interface_name));
916+ }
917+
837918 // / Get the logger of the HardwareComponentInterface.
838919 /* *
839920 * \return logger of the HardwareComponentInterface.
0 commit comments