@@ -59,7 +59,8 @@ using diagnostic_msgs::msg::DiagnosticStatus;
59
59
Aggregator::Aggregator ()
60
60
: n_(std::make_shared<rclcpp::Node>(
61
61
" analyzers" , " " ,
62
- rclcpp::NodeOptions ().automatically_declare_parameters_from_overrides(true ))),
62
+ rclcpp::NodeOptions ().allow_undeclared_parameters(true ).
63
+ automatically_declare_parameters_from_overrides(true ))),
63
64
logger_(rclcpp::get_logger(" Aggregator" )),
64
65
pub_rate_(1.0 ),
65
66
history_depth_(1000 ),
@@ -69,6 +70,35 @@ Aggregator::Aggregator()
69
70
last_top_level_state_(DiagnosticStatus::STALE)
70
71
{
71
72
RCLCPP_DEBUG (logger_, " constructor" );
73
+ initAnalyzers ();
74
+
75
+ diag_sub_ = n_->create_subscription <DiagnosticArray>(
76
+ " /diagnostics" , rclcpp::SystemDefaultsQoS ().keep_last (history_depth_),
77
+ std::bind (&Aggregator::diagCallback, this , _1));
78
+ agg_pub_ = n_->create_publisher <DiagnosticArray>(" /diagnostics_agg" , 1 );
79
+ toplevel_state_pub_ =
80
+ n_->create_publisher <DiagnosticStatus>(" /diagnostics_toplevel_state" , 1 );
81
+
82
+ int publish_rate_ms = 1000 / pub_rate_;
83
+ publish_timer_ = n_->create_wall_timer (
84
+ std::chrono::milliseconds (publish_rate_ms),
85
+ std::bind (&Aggregator::publishData, this ));
86
+
87
+ param_sub_ = n_->create_subscription <rcl_interfaces::msg::ParameterEvent>(
88
+ " /parameter_events" , 1 , std::bind (&Aggregator::parameterCallback, this , _1));
89
+ }
90
+
91
+ void Aggregator::parameterCallback (const rcl_interfaces::msg::ParameterEvent::SharedPtr msg)
92
+ {
93
+ if (msg->node == " /" + std::string (n_->get_name ())) {
94
+ if (msg->new_parameters .size () != 0 ) {
95
+ initAnalyzers ();
96
+ }
97
+ }
98
+ }
99
+
100
+ void Aggregator::initAnalyzers ()
101
+ {
72
102
bool other_as_errors = false ;
73
103
74
104
std::map<std::string, rclcpp::Parameter> parameters;
@@ -101,26 +131,17 @@ Aggregator::Aggregator()
101
131
RCLCPP_DEBUG (
102
132
logger_, " Aggregator critical publisher configured to: %s" , (critical_ ? " true" : " false" ));
103
133
104
- analyzer_group_ = std::make_unique<AnalyzerGroup>();
105
- if (!analyzer_group_->init (base_path_, " " , n_)) {
106
- RCLCPP_ERROR (logger_, " Analyzer group for diagnostic aggregator failed to initialize!" );
107
- }
108
-
109
- // Last analyzer handles remaining data
110
- other_analyzer_ = std::make_unique<OtherAnalyzer>(other_as_errors);
111
- other_analyzer_->init (base_path_); // This always returns true
112
-
113
- diag_sub_ = n_->create_subscription <DiagnosticArray>(
114
- " /diagnostics" , rclcpp::SystemDefaultsQoS ().keep_last (history_depth_),
115
- std::bind (&Aggregator::diagCallback, this , _1));
116
- agg_pub_ = n_->create_publisher <DiagnosticArray>(" /diagnostics_agg" , 1 );
117
- toplevel_state_pub_ =
118
- n_->create_publisher <DiagnosticStatus>(" /diagnostics_toplevel_state" , 1 );
134
+ { // lock the mutex while analyzer_group_ and other_analyzer_ are being updated
135
+ std::lock_guard<std::mutex> lock (mutex_);
136
+ analyzer_group_ = std::make_unique<AnalyzerGroup>();
137
+ if (!analyzer_group_->init (base_path_, " " , n_)) {
138
+ RCLCPP_ERROR (logger_, " Analyzer group for diagnostic aggregator failed to initialize!" );
139
+ }
119
140
120
- int publish_rate_ms = 1000 / pub_rate_;
121
- publish_timer_ = n_-> create_wall_timer (
122
- std::chrono::milliseconds (publish_rate_ms),
123
- std::bind (&Aggregator::publishData, this ));
141
+ // Last analyzer handles remaining data
142
+ other_analyzer_ = std::make_unique<OtherAnalyzer>(other_as_errors);
143
+ other_analyzer_-> init (base_path_); // This always returns true
144
+ }
124
145
}
125
146
126
147
void Aggregator::checkTimestamp (const DiagnosticArray::SharedPtr diag_msg)
0 commit comments