Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions diagnostic_aggregator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(param.as_int());
}
return param.as_double();
}

/*!
*\brief Level of StatusItem. OK, Warn, Error, Stale
*/
Expand Down
2 changes: 1 addition & 1 deletion diagnostic_aggregator/src/aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion diagnostic_aggregator/src/generic_analyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
143 changes: 143 additions & 0 deletions diagnostic_aggregator/test/test_integer_parameters.py
Original file line number Diff line number Diff line change
@@ -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()