From cf9aab663b5cf46845276054be4f953f10a44082 Mon Sep 17 00:00:00 2001 From: Alejandro Bordallo Date: Tue, 9 May 2023 12:29:11 +0100 Subject: [PATCH 1/4] Issue_339: Implement parse_bool and refactor a few --- .../include/hardware_interface/component_parser.hpp | 2 ++ hardware_interface/src/component_parser.cpp | 5 +++++ .../src/mock_components/generic_system.cpp | 9 ++++----- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/hardware_interface/include/hardware_interface/component_parser.hpp b/hardware_interface/include/hardware_interface/component_parser.hpp index 1d0f07d94b..a371bebd47 100644 --- a/hardware_interface/include/hardware_interface/component_parser.hpp +++ b/hardware_interface/include/hardware_interface/component_parser.hpp @@ -33,5 +33,7 @@ namespace hardware_interface HARDWARE_INTERFACE_PUBLIC std::vector parse_control_resources_from_urdf(const std::string & urdf); +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 14f2dee21e..b570e51e62 100644 --- a/hardware_interface/src/component_parser.cpp +++ b/hardware_interface/src/component_parser.cpp @@ -612,4 +612,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 d1405dac8c..7b7343abea 100644 --- a/hardware_interface/src/mock_components/generic_system.cpp +++ b/hardware_interface/src/mock_components/generic_system.cpp @@ -25,6 +25,7 @@ #include #include +#include "hardware_interface/component_parser.h" #include "hardware_interface/types/hardware_interface_type_values.hpp" #include "rcutils/logging_macros.h" @@ -74,8 +75,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i auto it = info_.hardware_parameters.find("mock_sensor_commands"); if (it != info_.hardware_parameters.end()) { - // TODO(anyone): change this to parse_bool() (see ros2_control#339) - use_mock_sensor_command_interfaces_ = it->second == "true" || it->second == "True"; + use_mock_sensor_command_interfaces_ = parse_bool(it->second); } else { @@ -83,7 +83,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i it = info_.hardware_parameters.find("fake_sensor_commands"); if (it != info_.hardware_parameters.end()) { - use_mock_sensor_command_interfaces_ = it->second == "true" || it->second == "True"; + use_mock_sensor_command_interfaces_ = parse_bool(it->second); RCUTILS_LOG_WARN_NAMED( "fake_generic_system", "Parameter 'fake_sensor_commands' has been deprecated from usage. Use" @@ -99,8 +99,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i 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 = parse_bool(it->second); } else { From e0a439a32eb0b1d4d1f5ebb74a3cf199752f323d Mon Sep 17 00:00:00 2001 From: Bence Magyar Date: Tue, 9 May 2023 18:14:38 +0100 Subject: [PATCH 2/4] Fix compilation --- .../include/hardware_interface/component_parser.hpp | 1 + hardware_interface/src/mock_components/generic_system.cpp | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/hardware_interface/include/hardware_interface/component_parser.hpp b/hardware_interface/include/hardware_interface/component_parser.hpp index a371bebd47..9f81a2a863 100644 --- a/hardware_interface/include/hardware_interface/component_parser.hpp +++ b/hardware_interface/include/hardware_interface/component_parser.hpp @@ -33,6 +33,7 @@ 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 diff --git a/hardware_interface/src/mock_components/generic_system.cpp b/hardware_interface/src/mock_components/generic_system.cpp index 7b7343abea..7278e2fb48 100644 --- a/hardware_interface/src/mock_components/generic_system.cpp +++ b/hardware_interface/src/mock_components/generic_system.cpp @@ -75,7 +75,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i auto it = info_.hardware_parameters.find("mock_sensor_commands"); if (it != info_.hardware_parameters.end()) { - use_mock_sensor_command_interfaces_ = parse_bool(it->second); + use_mock_sensor_command_interfaces_ = hardware_interface::parse_bool(it->second); } else { @@ -83,7 +83,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i it = info_.hardware_parameters.find("fake_sensor_commands"); if (it != info_.hardware_parameters.end()) { - use_mock_sensor_command_interfaces_ = parse_bool(it->second); + 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" @@ -99,7 +99,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i it = info_.hardware_parameters.find("fake_gpio_commands"); if (it != info_.hardware_parameters.end()) { - use_fake_gpio_command_interfaces = parse_bool(it->second); + use_fake_gpio_command_interfaces = hardware_interface::parse_bool(it->second); } else { From ae12b9027f6f8ebd513849c08ed1e4b555d7c407 Mon Sep 17 00:00:00 2001 From: Alejandro Bordallo Date: Wed, 10 May 2023 15:22:51 +0100 Subject: [PATCH 3/4] Refactor yet another method and fix include --- hardware_interface/src/component_parser.cpp | 2 +- hardware_interface/src/mock_components/generic_system.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/hardware_interface/src/component_parser.cpp b/hardware_interface/src/component_parser.cpp index b570e51e62..0ffdfde440 100644 --- a/hardware_interface/src/component_parser.cpp +++ b/hardware_interface/src/component_parser.cpp @@ -224,7 +224,7 @@ std::string parse_data_type_attribute(const tinyxml2::XMLElement * elem) bool parse_is_async_attribute(const tinyxml2::XMLElement * elem) { const tinyxml2::XMLAttribute * attr = elem->FindAttribute(kIsAsyncAttribute); - return attr ? strcasecmp(attr->Value(), "true") == 0 : false; + return parse_bool(attr->Value()); } /// Search XML snippet from URDF for parameters. diff --git a/hardware_interface/src/mock_components/generic_system.cpp b/hardware_interface/src/mock_components/generic_system.cpp index 7278e2fb48..04f0884607 100644 --- a/hardware_interface/src/mock_components/generic_system.cpp +++ b/hardware_interface/src/mock_components/generic_system.cpp @@ -25,7 +25,7 @@ #include #include -#include "hardware_interface/component_parser.h" +#include "hardware_interface/component_parser.hpp" #include "hardware_interface/types/hardware_interface_type_values.hpp" #include "rcutils/logging_macros.h" @@ -99,7 +99,7 @@ CallbackReturn GenericSystem::on_init(const hardware_interface::HardwareInfo & i it = info_.hardware_parameters.find("fake_gpio_commands"); if (it != info_.hardware_parameters.end()) { - use_fake_gpio_command_interfaces = hardware_interface::parse_bool(it->second); + use_fake_gpio_command_interfaces_ = hardware_interface::parse_bool(it->second); } else { From af3a82baa2a3aa101f6e5615732519c4c463706f Mon Sep 17 00:00:00 2001 From: Bence Magyar Date: Wed, 10 May 2023 15:30:28 +0100 Subject: [PATCH 4/4] don't crash on no tag --- hardware_interface/src/component_parser.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hardware_interface/src/component_parser.cpp b/hardware_interface/src/component_parser.cpp index 0ffdfde440..46c6b5f0e0 100644 --- a/hardware_interface/src/component_parser.cpp +++ b/hardware_interface/src/component_parser.cpp @@ -224,7 +224,7 @@ std::string parse_data_type_attribute(const tinyxml2::XMLElement * elem) bool parse_is_async_attribute(const tinyxml2::XMLElement * elem) { const tinyxml2::XMLAttribute * attr = elem->FindAttribute(kIsAsyncAttribute); - return parse_bool(attr->Value()); + return attr ? parse_bool(attr->Value()) : false; } /// Search XML snippet from URDF for parameters.