From 4c476287798129415f8b5d489b9ab48c7ffcbf67 Mon Sep 17 00:00:00 2001 From: Tomoya Fujita Date: Sun, 8 Nov 2026 21:48:15 +0900 Subject: [PATCH] diagnostic_aggregator: accept integer-typed values for floating point parameters "pub_rate: 2" or "timeout: 5" in a parameter file are typed INTEGER by rclcpp, and rclcpp::Parameter::as_double() throws ParameterTypeException on them, which terminates the aggregator during initialization. Users had to know to write "5.0" even though these are conceptually floating point values. Add getDoubleParam(), which converts integer-typed parameters to double and otherwise falls back to as_double(), and use it for pub_rate and the GenericAnalyzer timeout. Add a launch test that starts the aggregator with integer-valued pub_rate and timeout and checks that it produces output; it fails with ParameterTypeException without the fix. Co-Authored-By: Claude Fable 5.1 Signed-off-by: Tomoya Fujita --- diagnostic_aggregator/CMakeLists.txt | 5 + .../diagnostic_aggregator/status_item.hpp | 15 ++ diagnostic_aggregator/src/aggregator.cpp | 2 +- .../src/generic_analyzer.cpp | 2 +- .../test/test_integer_parameters.py | 143 ++++++++++++++++++ 5 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 diagnostic_aggregator/test/test_integer_parameters.py diff --git a/diagnostic_aggregator/CMakeLists.txt b/diagnostic_aggregator/CMakeLists.txt index 2af6b8673..67088d6ba 100644 --- a/diagnostic_aggregator/CMakeLists.txt +++ b/diagnostic_aggregator/CMakeLists.txt @@ -169,6 +169,11 @@ if(BUILD_TESTING) "${CMAKE_CURRENT_SOURCE_DIR}/test/test_publish_values.py" TIMEOUT 60 ) + + ament_add_pytest_test(test_integer_parameters + "${CMAKE_CURRENT_SOURCE_DIR}/test/test_integer_parameters.py" + TIMEOUT 60 + ) endif() install( diff --git a/diagnostic_aggregator/include/diagnostic_aggregator/status_item.hpp b/diagnostic_aggregator/include/diagnostic_aggregator/status_item.hpp index e11dad5d6..e61fcd06a 100644 --- a/diagnostic_aggregator/include/diagnostic_aggregator/status_item.hpp +++ b/diagnostic_aggregator/include/diagnostic_aggregator/status_item.hpp @@ -69,6 +69,21 @@ inline std::string getOutputName(const std::string & item_name) return output_name; } +/*! + *\brief Returns a floating point parameter, accepting integer-typed values too. + * + * A YAML value such as "timeout: 5" is typed as an integer parameter by rclcpp, and + * rclcpp::Parameter::as_double() throws for it. Analyzer timeouts and rates are + * conceptually floating point, so accept both representations. + */ +inline double getDoubleParam(const rclcpp::Parameter & param) +{ + if (param.get_type() == rclcpp::ParameterType::PARAMETER_INTEGER) { + return static_cast(param.as_int()); + } + return param.as_double(); +} + /*! *\brief Level of StatusItem. OK, Warn, Error, Stale */ diff --git a/diagnostic_aggregator/src/aggregator.cpp b/diagnostic_aggregator/src/aggregator.cpp index cdb05f923..8975d470e 100644 --- a/diagnostic_aggregator/src/aggregator.cpp +++ b/diagnostic_aggregator/src/aggregator.cpp @@ -121,7 +121,7 @@ void Aggregator::initAnalyzers() for (const auto & param : parameters) { if (param.first.compare("pub_rate") == 0) { - pub_rate_ = param.second.as_double(); + pub_rate_ = getDoubleParam(param.second); } else if (param.first.compare("path") == 0) { // Leading slash when path is not empty if (!param.second.as_string().empty()) { diff --git a/diagnostic_aggregator/src/generic_analyzer.cpp b/diagnostic_aggregator/src/generic_analyzer.cpp index 3965b226d..b259602ae 100644 --- a/diagnostic_aggregator/src/generic_analyzer.cpp +++ b/diagnostic_aggregator/src/generic_analyzer.cpp @@ -136,7 +136,7 @@ bool GenericAnalyzer::init( RCLCPP_DEBUG( rclcpp::get_logger("GenericAnalyzer"), "GenericAnalyzer '%s' found timeout: %s", nice_name_.c_str(), pvalue.value_to_string().c_str()); - timeout = pvalue.as_double(); + timeout = getDoubleParam(pvalue); } else if (pname.compare("num_items") == 0) { RCLCPP_DEBUG( rclcpp::get_logger("GenericAnalyzer"), "GenericAnalyzer '%s' found num_items: %s", diff --git a/diagnostic_aggregator/test/test_integer_parameters.py b/diagnostic_aggregator/test/test_integer_parameters.py new file mode 100644 index 000000000..7c5204df5 --- /dev/null +++ b/diagnostic_aggregator/test/test_integer_parameters.py @@ -0,0 +1,143 @@ +# Software License Agreement (BSD License) +# +# Copyright (c) 2026, Tomoya Fujita +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above +# copyright notice, this list of conditions and the following +# disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of the copyright holder nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. + +""" +Test that floating point parameters also accept integer-typed YAML values. + +"pub_rate: 2" and "timeout: 2" are typed as integer parameters by rclcpp. +Before the fix, rclcpp::Parameter::as_double() threw on them and the +aggregator died during initialization. Now they must be accepted and the +aggregator must produce output. +""" + +import tempfile + +from diagnostic_msgs.msg import DiagnosticArray, DiagnosticStatus +import launch +import launch_pytest +import launch_ros +import pytest +import rclpy +from rclpy.executors import SingleThreadedExecutor +from rclpy.node import Node +from rclpy.task import Future + + +class DiagnosticsTestNode(Node): + """Publishes a raw diagnostic and waits for the aggregated header.""" + + def __init__(self): + super().__init__(node_name='integer_parameters_test_node') + self.future = Future() + self.subscriber = self.create_subscription( + msg_type=DiagnosticArray, + topic='/diagnostics_agg', + callback=self.diagnostics_aggregated_callback, + qos_profile=10, + ) + self.publisher = self.create_publisher( + msg_type=DiagnosticArray, topic='/diagnostics', qos_profile=10 + ) + self.timer = self.create_timer(0.1, self.timer_callback) + + def timer_callback(self): + msg = DiagnosticArray() + msg.header.stamp = self.get_clock().now().to_msg() + msg.status.append( + DiagnosticStatus(name='foo', level=DiagnosticStatus.OK, message='Foo') + ) + self.publisher.publish(msg) + + def diagnostics_aggregated_callback(self, msg): + for status in msg.status: + if status.name == '/robot/agg' and not self.future.done(): + self.future.set_result(status.level) + + +@pytest.fixture(scope='function') +def yaml_file(): + """Generate a configuration whose floating point parameters are written as integers.""" + with tempfile.NamedTemporaryFile(delete=False) as fp: + fp.write( + bytes( + """ +diagnostic_aggregator: + ros__parameters: + path: 'robot' + pub_rate: 2 + analyzers: + agg: + type: 'diagnostic_aggregator/GenericAnalyzer' + path: 'agg' + contains: ['foo'] + timeout: 2 +""", + 'utf-8', + ) + ) + return fp.name + + +@pytest.fixture(scope='function') +def diagnostic_aggregator_node(): + return launch_ros.actions.Node( + name='diagnostic_aggregator', + package='diagnostic_aggregator', + executable='aggregator_node', + ) + + +@launch_pytest.fixture(scope='function') +def launch_description(yaml_file, diagnostic_aggregator_node): + return launch.LaunchDescription( + [ + launch_ros.actions.SetParametersFromFile(yaml_file), + diagnostic_aggregator_node, + launch_pytest.actions.ReadyToTest(), + ] + ) + + +@pytest.mark.launch(fixture=launch_description) +def test_integer_parameters(launch_context): + rclpy.init() + node = DiagnosticsTestNode() + executor = SingleThreadedExecutor() + executor.add_node(node) + try: + executor.spin_until_future_complete(future=node.future, timeout_sec=15.0) + assert node.future.done(), ( + 'No aggregated output; the aggregator probably failed to parse integer parameters' + ) + assert node.future.result() == DiagnosticStatus.OK + finally: + rclpy.try_shutdown()