Skip to content

Commit f331ee3

Browse files
Add add_analyzer functionality (#329) (#361)
* Add add_analyzer functionality * Add copyright notice and license, remove unused includes, re-order includes correctly * Increase clarity of prefix_ by renaming it to analyzers_ns_ * Add add_analyzer functionality * Fix bug where base_path is not reset correctly * Make the parameter forwarding condition more generic, fix the default service namespace from diagnostics_agg to analyzers * Add an add_analyzer example to the diagnostic_aggregator * Update the relevant READMEs * Fix linter errors * Add test for add_analyzer at runtime, remove unnecessary ros info logger, remove unnecessary hardcoded namespace from yaml files * Remove the now redundant analyzers_ns_ * Change the copyright of add_analyzer, forgot to update it to Nobleo after copying the notice (cherry picked from commit 5e1415c) Co-authored-by: MartinCornelis2 <[email protected]>
1 parent f8710be commit f331ee3

19 files changed

+343
-29
lines changed

diagnostic_aggregator/CMakeLists.txt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ find_package(ament_cmake REQUIRED)
1414
find_package(diagnostic_msgs REQUIRED)
1515
find_package(pluginlib REQUIRED)
1616
find_package(rclcpp REQUIRED)
17+
find_package(rcl_interfaces REQUIRED)
1718
find_package(std_msgs REQUIRED)
1819

1920
add_library(${PROJECT_NAME} SHARED
@@ -67,6 +68,10 @@ add_executable(aggregator_node src/aggregator_node.cpp)
6768
target_link_libraries(aggregator_node
6869
${PROJECT_NAME})
6970

71+
# Add analyzer
72+
add_executable(add_analyzer src/add_analyzer.cpp)
73+
ament_target_dependencies(add_analyzer rclcpp rcl_interfaces)
74+
7075
# Testing macro
7176
if(BUILD_TESTING)
7277
find_package(ament_lint_auto REQUIRED)
@@ -77,6 +82,7 @@ if(BUILD_TESTING)
7782
find_package(launch_testing_ament_cmake REQUIRED)
7883

7984
file(TO_CMAKE_PATH "${CMAKE_INSTALL_PREFIX}/lib/${PROJECT_NAME}/aggregator_node" AGGREGATOR_NODE)
85+
file(TO_CMAKE_PATH "${CMAKE_INSTALL_PREFIX}/lib/${PROJECT_NAME}/add_analyzer" ADD_ANALYZER)
8086
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/test/test_listener.py" TEST_LISTENER)
8187
set(create_analyzers_tests
8288
"primitive_analyzers"
@@ -124,6 +130,27 @@ if(BUILD_TESTING)
124130
)
125131
endforeach()
126132

133+
set(add_analyzers_tests
134+
"all_analyzers")
135+
136+
foreach(test_name ${add_analyzers_tests})
137+
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/test/default.yaml" PARAMETER_FILE)
138+
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/test/${test_name}.yaml" ADD_PARAMETER_FILE)
139+
file(TO_CMAKE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/test/expected_output/add_${test_name}" EXPECTED_OUTPUT)
140+
141+
configure_file(
142+
"test/add_analyzers.launch.py.in"
143+
"test_add_${test_name}.launch.py"
144+
@ONLY
145+
)
146+
add_launch_test(
147+
"${CMAKE_CURRENT_BINARY_DIR}/test_add_${test_name}.launch.py"
148+
TARGET "test_add_${test_name}"
149+
TIMEOUT 30
150+
ENV
151+
)
152+
endforeach()
153+
127154
add_launch_test(
128155
test/test_critical_pub.py
129156
TIMEOUT 30
@@ -140,6 +167,11 @@ install(
140167
DESTINATION lib/${PROJECT_NAME}
141168
)
142169

170+
install(
171+
TARGETS add_analyzer
172+
DESTINATION lib/${PROJECT_NAME}
173+
)
174+
143175
install(
144176
TARGETS ${PROJECT_NAME} ${ANALYZERS}
145177
EXPORT ${PROJECT_NAME}Targets
@@ -157,6 +189,7 @@ ament_python_install_package(${PROJECT_NAME})
157189

158190
# Install Example
159191
set(ANALYZER_PARAMS_FILEPATH "${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}/example_analyzers.yaml")
192+
set(ADD_ANALYZER_PARAMS_FILEPATH "${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}/example_add_analyzers.yaml")
160193
configure_file(example/example.launch.py.in example.launch.py @ONLY)
161194
install( # launch descriptor
162195
FILES ${CMAKE_CURRENT_BINARY_DIR}/example.launch.py
@@ -167,7 +200,7 @@ install( # example publisher
167200
DESTINATION lib/${PROJECT_NAME}
168201
)
169202
install( # example aggregator configration
170-
FILES example/example_analyzers.yaml
203+
FILES example/example_analyzers.yaml example/example_add_analyzers.yaml
171204
DESTINATION share/${PROJECT_NAME}
172205
)
173206

diagnostic_aggregator/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,33 @@ You can launch the `aggregator_node` like this (see [example.launch.py.in](examp
135135
])
136136
```
137137

138+
You can add analyzers at runtime using the `add_analyzer` node like this (see [example.launch.py.in](example/example.launch.py.in)):
139+
```
140+
add_analyzer = launch_ros.actions.Node(
141+
package='diagnostic_aggregator',
142+
executable='add_analyzer',
143+
output='screen',
144+
parameters=[add_analyzer_params_filepath])
145+
return launch.LaunchDescription([
146+
add_analyzer,
147+
])
148+
```
149+
This node updates the parameters of the `aggregator_node` by calling the service `/analyzers/set_parameters_atomically`.
150+
The `aggregator_node` will detect when a `parameter-event` has introduced new parameters to it.
151+
When this happens the `aggregator_node` will reload all analyzers based on its new set of parameters.
152+
Adding analyzers this way can be done at runtime and can be made conditional.
153+
154+
In the example, `add_analyzer` will add an analyzer for diagnostics that are marked optional:
155+
``` yaml
156+
/**:
157+
ros__parameters:
158+
optional:
159+
type: diagnostic_aggregator/GenericAnalyzer
160+
path: Optional
161+
startswith: [ '/optional' ]
162+
```
163+
This will move the `/optional/runtime/analyzer` diagnostic from the "Other" to "Aggregation" where it will not go stale after 5 seconds and will be taken into account for the toplevel state.
164+
138165
# Basic analyzers
139166
The `diagnostic_aggregator` package provides a few basic analyzers that you can use to aggregate your diagnostics.
140167

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Aggregator Example
22

3-
This is a simple example to show the diagnostic_aggregator in action. It involves one python script producing dummy diagnostic data ([example_pub.py](./example_pub.py)), and one diagnostic aggregator configuration ([example.yaml](./example.yaml)) that provides analyzers aggregating it.
3+
This is a simple example to show the diagnostic_aggregator and add_analyzer in action. It involves one python script producing dummy diagnostic data ([example_pub.py](./example_pub.py)), one diagnostic aggregator configuration ([example_analyzers.yaml](./example_analyzers.yaml)) and one add_analyzer configuration ([example_add_analyzers.yaml](./example_add_analyzers.yaml)).
4+
5+
The aggregator will launch and load all the analyzers listed in ([example_analyzers.yaml](./example_analyzers.yaml)). Then the aggregator will be notified that there are additional analyzers that we also want to load in ([example_add_analyzers.yaml](./example_add_analyzers.yaml)). After this reload all analyzers will be active.
46

57
Run the example with `ros2 launch diagnostic_aggregator example.launch.py`

diagnostic_aggregator/example/example.launch.py.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import launch
44
import launch_ros.actions
55

66
analyzer_params_filepath = "@ANALYZER_PARAMS_FILEPATH@"
7+
add_analyzer_params_filepath = "@ADD_ANALYZER_PARAMS_FILEPATH@"
78

89

910
def generate_launch_description():
@@ -12,11 +13,18 @@ def generate_launch_description():
1213
executable='aggregator_node',
1314
output='screen',
1415
parameters=[analyzer_params_filepath])
16+
add_analyzer = launch_ros.actions.Node(
17+
package='diagnostic_aggregator',
18+
executable='add_analyzer',
19+
output='screen',
20+
parameters=[add_analyzer_params_filepath]
21+
)
1522
diag_publisher = launch_ros.actions.Node(
1623
package='diagnostic_aggregator',
1724
executable='example_pub.py')
1825
return launch.LaunchDescription([
1926
aggregator,
27+
add_analyzer,
2028
diag_publisher,
2129
launch.actions.RegisterEventHandler(
2230
event_handler=launch.event_handlers.OnProcessExit(
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**:
2+
ros__parameters:
3+
optional:
4+
type: diagnostic_aggregator/GenericAnalyzer
5+
path: Optional
6+
contains: [ '/optional' ]

diagnostic_aggregator/example/example_pub.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ def __init__(self):
8181
name='/sensors/front/cam', message='OK'),
8282
DiagnosticStatus(level=DiagnosticStatus.OK,
8383
name='/sensors/rear/cam', message='OK'),
84+
85+
# Optional
86+
DiagnosticStatus(level=DiagnosticStatus.OK,
87+
name='/optional/runtime/analyzer', message='OK'),
8488
]
8589

8690
def timer_callback(self):

diagnostic_aggregator/include/diagnostic_aggregator/aggregator.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ class Aggregator
133133
rclcpp::Service<diagnostic_msgs::srv::AddDiagnostics>::SharedPtr add_srv_;
134134
/// DiagnosticArray, /diagnostics
135135
rclcpp::Subscription<diagnostic_msgs::msg::DiagnosticArray>::SharedPtr diag_sub_;
136+
/// ParameterEvent, /parameter_events
137+
rclcpp::Subscription<rcl_interfaces::msg::ParameterEvent>::SharedPtr param_sub_;
136138
/// DiagnosticArray, /diagnostics_agg
137139
rclcpp::Publisher<diagnostic_msgs::msg::DiagnosticArray>::SharedPtr agg_pub_;
138140
/// DiagnosticStatus, /diagnostics_toplevel_state
@@ -165,6 +167,16 @@ class Aggregator
165167
/// Records all ROS warnings. No warnings are repeated.
166168
std::set<std::string> ros_warnings_;
167169

170+
/*
171+
*!\brief Checks for new parameters to trigger reinitialization of the AnalyzerGroup and OtherAnalyzer
172+
*/
173+
void parameterCallback(const rcl_interfaces::msg::ParameterEvent::SharedPtr param_msg);
174+
175+
/*
176+
*!\brief (re)initializes the AnalyzerGroup and OtherAnalyzer
177+
*/
178+
void initAnalyzers();
179+
168180
/*
169181
*!\brief Checks timestamp of message, and warns if timestamp is 0 (not set)
170182
*/

diagnostic_aggregator/package.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<license>BSD-3-Clause</license>
1313

1414
<url type="website">http://www.ros.org/wiki/diagnostic_aggregator</url>
15-
15+
1616
<author>Kevin Watts</author>
1717
<author email="[email protected]">Brice Rebsamen</author>
1818
<author email="[email protected]">Arne Nordmann</author>
@@ -22,6 +22,7 @@
2222

2323
<build_depend>diagnostic_msgs</build_depend>
2424
<build_depend>pluginlib</build_depend>
25+
<build_depend>rcl_interfaces</build_depend>
2526
<build_depend>rclcpp</build_depend>
2627
<build_depend>std_msgs</build_depend>
2728

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*********************************************************************
2+
* Software License Agreement (BSD License)
3+
*
4+
* Copyright (c) 2024, Nobleo Technology
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
*
11+
* * Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* * Redistributions in binary form must reproduce the above
14+
* copyright notice, this list of conditions and the following
15+
* disclaimer in the documentation and/or other materials provided
16+
* with the distribution.
17+
* * Neither the name of the copyright holder nor the names of its
18+
* contributors may be used to endorse or promote products derived
19+
* from this software without specific prior written permission.
20+
*
21+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32+
* POSSIBILITY OF SUCH DAMAGE.
33+
*********************************************************************/
34+
35+
/**< \author Martin Cornelis */
36+
37+
#include <chrono>
38+
39+
#include "rclcpp/rclcpp.hpp"
40+
#include "rcl_interfaces/srv/set_parameters_atomically.hpp"
41+
#include "rcl_interfaces/msg/parameter.hpp"
42+
43+
using namespace std::chrono_literals;
44+
45+
class AddAnalyzer : public rclcpp::Node
46+
{
47+
public:
48+
AddAnalyzer()
49+
: Node("add_analyzer_node", "", rclcpp::NodeOptions().allow_undeclared_parameters(
50+
true).automatically_declare_parameters_from_overrides(true))
51+
{
52+
client_ = this->create_client<rcl_interfaces::srv::SetParametersAtomically>(
53+
"/analyzers/set_parameters_atomically");
54+
}
55+
56+
void send_request()
57+
{
58+
while (!client_->wait_for_service(1s)) {
59+
if (!rclcpp::ok()) {
60+
RCLCPP_ERROR(this->get_logger(), "Interrupted while waiting for the service. Exiting.");
61+
return;
62+
}
63+
RCLCPP_INFO_ONCE(this->get_logger(), "service not available, waiting ...");
64+
}
65+
auto request = std::make_shared<rcl_interfaces::srv::SetParametersAtomically::Request>();
66+
std::map<std::string, rclcpp::Parameter> parameters;
67+
68+
if (!this->get_parameters("", parameters)) {
69+
RCLCPP_ERROR(this->get_logger(), "Failed to retrieve parameters");
70+
}
71+
for (const auto & [param_name, param] : parameters) {
72+
// Find the suffix
73+
size_t suffix_start = param_name.find_last_of('.');
74+
// Remove suffix if it exists
75+
if (suffix_start != std::string::npos) {
76+
std::string stripped_param_name = param_name.substr(0, suffix_start);
77+
// Check in map if the stripped param name with the added suffix "path" exists
78+
// This indicates the parameter is part of an analyzer description
79+
if (parameters.count(stripped_param_name + ".path") > 0) {
80+
auto parameter_msg = param.to_parameter_msg();
81+
request->parameters.push_back(parameter_msg);
82+
}
83+
}
84+
}
85+
86+
auto result = client_->async_send_request(request);
87+
// Wait for the result.
88+
if (rclcpp::spin_until_future_complete(this->get_node_base_interface(), result) ==
89+
rclcpp::FutureReturnCode::SUCCESS)
90+
{
91+
RCLCPP_INFO(this->get_logger(), "Parameters succesfully set");
92+
} else {
93+
RCLCPP_ERROR(this->get_logger(), "Failed to set parameters");
94+
}
95+
}
96+
97+
private:
98+
rclcpp::Client<rcl_interfaces::srv::SetParametersAtomically>::SharedPtr client_;
99+
};
100+
101+
int main(int argc, char ** argv)
102+
{
103+
rclcpp::init(argc, argv);
104+
105+
auto add_analyzer = std::make_shared<AddAnalyzer>();
106+
add_analyzer->send_request();
107+
rclcpp::shutdown();
108+
109+
return 0;
110+
}

0 commit comments

Comments
 (0)