Skip to content

Commit 938f688

Browse files
feat: publish top level msg when error is received
1 parent 24fb79a commit 938f688

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

diagnostic_aggregator/include/diagnostic_aggregator/aggregator.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ class Aggregator
152152

153153
std::string base_path_; /**< \brief Prepended to all status names of aggregator. */
154154

155+
bool critical_; /**< \brief If true, aggregator will publish an error immediately after receiving. */
156+
std::uint8_t last_top_level_state_; /**< \brief Store the last top level value to publish the critical error only once */
157+
155158
/// Records all ROS warnings. No warnings are repeated.
156159
std::set<std::string> ros_warnings_;
157160

diagnostic_aggregator/src/aggregator.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ Aggregator::Aggregator()
6464
pub_rate_(1.0),
6565
history_depth_(1000),
6666
clock_(n_->get_clock()),
67-
base_path_("")
67+
base_path_(""),
68+
critical_(false),
69+
last_top_level_state_(DiagnosticStatus::STALE)
6870
{
6971
RCLCPP_DEBUG(logger_, "constructor");
7072
bool other_as_errors = false;
@@ -88,12 +90,16 @@ Aggregator::Aggregator()
8890
other_as_errors = param.second.as_bool();
8991
} else if (param.first.compare("history_depth") == 0) {
9092
history_depth_ = param.second.as_int();
93+
} else if (param.first.compare("critical") == 0) {
94+
critical_ = param.second.as_bool();
9195
}
9296
}
9397
RCLCPP_DEBUG(logger_, "Aggregator publication rate configured to: %f", pub_rate_);
9498
RCLCPP_DEBUG(logger_, "Aggregator base path configured to: %s", base_path_.c_str());
9599
RCLCPP_DEBUG(
96100
logger_, "Aggregator other_as_errors configured to: %s", (other_as_errors ? "true" : "false"));
101+
RCLCPP_DEBUG(
102+
logger_, "Aggregator critical publisher configured to: %s", (critical_ ? "true" : "false"));
97103

98104
analyzer_group_ = std::make_unique<AnalyzerGroup>();
99105
if (!analyzer_group_->init(base_path_, "", n_)) {
@@ -149,6 +155,23 @@ void Aggregator::diagCallback(const DiagnosticArray::SharedPtr diag_msg)
149155
std::lock_guard<std::mutex> lock(mutex_);
150156
for (auto j = 0u; j < diag_msg->status.size(); ++j) {
151157
analyzed = false;
158+
159+
const bool top_level_state_transition_to_error = (last_top_level_state_ != DiagnosticStatus::ERROR)
160+
&& (diag_msg->status[j].level == DiagnosticStatus::ERROR);
161+
162+
if (critical_ && top_level_state_transition_to_error) {
163+
RCLCPP_DEBUG(
164+
logger_, "Received error message: %s, publishing error immediately",
165+
diag_msg->status[j].name.c_str());
166+
DiagnosticStatus diag_toplevel_state;
167+
diag_toplevel_state.name = "toplevel_state_critical";
168+
diag_toplevel_state.level = diag_msg->status[j].level;
169+
toplevel_state_pub_->publish(diag_toplevel_state);
170+
171+
// store the last published state
172+
last_top_level_state_ = diag_toplevel_state.level;
173+
}
174+
152175
auto item = std::make_shared<StatusItem>(&diag_msg->status[j]);
153176

154177
if (analyzer_group_->match(item->getName())) {
@@ -217,6 +240,8 @@ void Aggregator::publishData()
217240
// have stale items but not all are stale
218241
diag_toplevel_state.level = DiagnosticStatus::ERROR;
219242
}
243+
last_top_level_state_ = diag_toplevel_state.level;
244+
220245
toplevel_state_pub_->publish(diag_toplevel_state);
221246
}
222247

0 commit comments

Comments
 (0)