Skip to content

Commit 8dd4133

Browse files
feat: add timeout_diagnostics (#84)
* add: timeout_diagnostics (see below) * This changes are copied from `autoware_control_command_gate` - https://github.com/autowarefoundation/autoware_universe/tree/01ff80101dfc78a0288f063e975e6e16ac042bf7/control/autoware_control_command_gate * We are going to use `timeout_diagnostics` via `autoware_utils` not `autoware_universe` in near future for adaptation to new ADAPI (>= 1.9.0) Signed-off-by: Junya Sasaki <[email protected]> * style(pre-commit): autofix --------- Signed-off-by: Junya Sasaki <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3b0094f commit 8dd4133

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

autoware_utils_diagnostics/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ autoware_package()
66

77
ament_auto_add_library(${PROJECT_NAME} SHARED
88
"src/diagnostics_interface.cpp"
9+
"src/timeout_diagnostics.cpp"
910
)
1011

1112
if(BUILD_TESTING)
1213
ament_add_ros_isolated_gtest(test_${PROJECT_NAME}
1314
"test/main.cpp"
1415
"test/cases/diagnostics_interface.cpp"
16+
"test/cases/timeout_diagnostics.cpp"
1517
)
1618
target_link_libraries(test_${PROJECT_NAME} ${PROJECT_NAME})
1719
endif()
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2025 The Autoware Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef AUTOWARE_UTILS_DIAGNOSTICS__TIMEOUT_DIAGNOSTICS_HPP_
16+
#define AUTOWARE_UTILS_DIAGNOSTICS__TIMEOUT_DIAGNOSTICS_HPP_
17+
18+
#include <diagnostic_updater/diagnostic_status_wrapper.hpp>
19+
#include <diagnostic_updater/diagnostic_updater.hpp>
20+
21+
#include <string>
22+
23+
namespace autoware_utils_diagnostics
24+
{
25+
26+
class TimeoutDiag : public diagnostic_updater::DiagnosticTask
27+
{
28+
public:
29+
struct Params
30+
{
31+
double warn_duration_;
32+
double error_duration_;
33+
};
34+
35+
public:
36+
using DiagnosticStatus = diagnostic_msgs::msg::DiagnosticStatus;
37+
38+
TimeoutDiag(const Params & params, const rclcpp::Clock & clock, const std::string & name);
39+
void update();
40+
bool is_error() const { return level_ >= DiagnosticStatus::ERROR; }
41+
42+
private:
43+
void run(diagnostic_updater::DiagnosticStatusWrapper & stat) override;
44+
45+
const Params params_;
46+
rclcpp::Clock clock_;
47+
std::optional<rclcpp::Time> last_stamp_;
48+
uint8_t level_;
49+
};
50+
51+
} // namespace autoware_utils_diagnostics
52+
53+
#endif // AUTOWARE_UTILS_DIAGNOSTICS__TIMEOUT_DIAGNOSTICS_HPP_

autoware_utils_diagnostics/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<buildtool_depend>autoware_cmake</buildtool_depend>
1616

1717
<depend>diagnostic_msgs</depend>
18+
<depend>diagnostic_updater</depend>
1819
<depend>rclcpp</depend>
1920

2021
<test_depend>ament_cmake_ros</test_depend>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2025 The Autoware Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "autoware_utils_diagnostics/timeout_diagnostics.hpp"
16+
17+
#include <string>
18+
19+
namespace autoware_utils_diagnostics
20+
{
21+
22+
TimeoutDiag::TimeoutDiag(
23+
const Params & params, const rclcpp::Clock & clock, const std::string & name)
24+
: DiagnosticTask(name), params_(params), clock_(clock)
25+
{
26+
level_ = DiagnosticStatus::STALE;
27+
}
28+
29+
void TimeoutDiag::update()
30+
{
31+
last_stamp_ = clock_.now();
32+
}
33+
34+
void TimeoutDiag::run(diagnostic_updater::DiagnosticStatusWrapper & stat)
35+
{
36+
if (!last_stamp_) {
37+
stat.summary(DiagnosticStatus::ERROR, "no data");
38+
return;
39+
}
40+
41+
const auto duration = (clock_.now() - *last_stamp_).seconds();
42+
43+
if (duration < params_.warn_duration_) {
44+
level_ = DiagnosticStatus::OK;
45+
} else if (duration < params_.error_duration_) {
46+
level_ = DiagnosticStatus::WARN;
47+
} else {
48+
level_ = DiagnosticStatus::ERROR;
49+
}
50+
51+
stat.summary(level_, "");
52+
stat.add("duration", duration);
53+
}
54+
55+
} // namespace autoware_utils_diagnostics
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2025 The Autoware Contributors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "autoware_utils_diagnostics/timeout_diagnostics.hpp"
16+
17+
#include <rclcpp/rclcpp.hpp>
18+
19+
#include <gtest/gtest.h>
20+
21+
#include <memory>
22+
23+
TEST(TimeoutDiagTest, Instantiation)
24+
{
25+
const auto node = std::make_shared<rclcpp::Node>("test_node");
26+
autoware_utils_diagnostics::TimeoutDiag::Params params{1.0, 2.0};
27+
autoware_utils_diagnostics::TimeoutDiag timeout_diag(params, *node->get_clock(), "test_diag");
28+
}

0 commit comments

Comments
 (0)