From 055f759fbb0c442216ee534025a24e560351890d Mon Sep 17 00:00:00 2001 From: Alejandro Bordallo Date: Wed, 10 May 2023 23:19:00 +0100 Subject: [PATCH] Implement parse_bool and refactor a few (#1014) (cherry picked from commit c9709f3ce2ebf31ab5ea2c25829c422f7465f0d7) # Conflicts: # hardware_interface/src/component_parser.cpp # hardware_interface/src/mock_components/generic_system.cpp --- .../hardware_interface/component_parser.hpp | 3 +++ hardware_interface/src/component_parser.cpp | 22 ++++++++++++++++ .../src/mock_components/generic_system.cpp | 25 +++++++++++++++++-- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/hardware_interface/include/hardware_interface/component_parser.hpp b/hardware_interface/include/hardware_interface/component_parser.hpp index 1d0f07d94b..9f81a2a863 100644 --- a/hardware_interface/include/hardware_interface/component_parser.hpp +++ b/hardware_interface/include/hardware_interface/component_parser.hpp @@ -33,5 +33,8 @@ namespace hardware_interface HARDWARE_INTERFACE_PUBLIC std::vector parse_control_resources_from_urdf(const std::string & urdf); +HARDWARE_INTERFACE_PUBLIC +bool parse_bool(const std::string & bool_string); + } // namespace hardware_interface #endif // HARDWARE_INTERFACE__COMPONENT_PARSER_HPP_ diff --git a/hardware_interface/src/component_parser.cpp b/hardware_interface/src/component_parser.cpp index b8d20e763d..501595b40b 100644 --- a/hardware_interface/src/component_parser.cpp +++ b/hardware_interface/src/component_parser.cpp @@ -206,6 +206,23 @@ std::string parse_data_type_attribute(const tinyxml2::XMLElement * elem) return data_type; } +<<<<<<< HEAD +======= +/// Parse is_async attribute +/** + * Parses an XMLElement and returns the value of the is_async attribute. + * Defaults to "false" if not specified. + * + * \param[in] elem XMLElement that has the data_type attribute. + * \return boolean specifying the if the value read was true or false. + */ +bool parse_is_async_attribute(const tinyxml2::XMLElement * elem) +{ + const tinyxml2::XMLAttribute * attr = elem->FindAttribute(kIsAsyncAttribute); + return attr ? parse_bool(attr->Value()) : false; +} + +>>>>>>> c9709f3 (Implement parse_bool and refactor a few (#1014)) /// Search XML snippet from URDF for parameters. /** * \param[in] params_it pointer to the iterator where parameters info should be found @@ -590,4 +607,9 @@ std::vector parse_control_resources_from_urdf(const std::string & return hardware_info; } +bool parse_bool(const std::string & bool_string) +{ + return bool_string == "true" || bool_string == "True"; +} + } // namespace hardware_interface diff --git a/hardware_interface/src/mock_components/generic_system.cpp b/hardware_interface/src/mock_components/generic_system.cpp index bf9a9644df..b4cf17625e 100644 --- a/hardware_interface/src/mock_components/generic_system.cpp +++ b/hardware_interface/src/mock_components/generic_system.cpp @@ -24,6 +24,7 @@ #include #include +#include "hardware_interface/component_parser.hpp" #include "hardware_interface/types/hardware_interface_type_values.hpp" #include "rcutils/logging_macros.h" @@ -61,20 +62,40 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i auto it = info_.hardware_parameters.find("fake_sensor_commands"); if (it != info_.hardware_parameters.end()) { +<<<<<<< HEAD // TODO(anyone): change this to parse_bool() (see ros2_control#339) use_fake_sensor_command_interfaces_ = it->second == "true" || it->second == "True"; } else { use_fake_sensor_command_interfaces_ = false; +======= + use_mock_sensor_command_interfaces_ = hardware_interface::parse_bool(it->second); + } + else + { + // check if fake_sensor_commands was set instead and issue warning. + it = info_.hardware_parameters.find("fake_sensor_commands"); + if (it != info_.hardware_parameters.end()) + { + use_mock_sensor_command_interfaces_ = hardware_interface::parse_bool(it->second); + RCUTILS_LOG_WARN_NAMED( + "fake_generic_system", + "Parameter 'fake_sensor_commands' has been deprecated from usage. Use" + "'mock_sensor_commands' instead."); + } + else + { + use_mock_sensor_command_interfaces_ = false; + } +>>>>>>> c9709f3 (Implement parse_bool and refactor a few (#1014)) } // check if to create fake command interface for gpio it = info_.hardware_parameters.find("fake_gpio_commands"); if (it != info_.hardware_parameters.end()) { - // TODO(anyone): change this to parse_bool() (see ros2_control#339) - use_fake_gpio_command_interfaces_ = it->second == "true" || it->second == "True"; + use_fake_gpio_command_interfaces_ = hardware_interface::parse_bool(it->second); } else {