Skip to content

Commit f2a3fd3

Browse files
authored
feat(default_ad_api): add diagnostics api (autowarefoundation#7052)
Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp>
1 parent 965fdf4 commit f2a3fd3

File tree

7 files changed

+142
-2
lines changed

7 files changed

+142
-2
lines changed

system/default_ad_api/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ find_package(autoware_cmake REQUIRED)
55
autoware_package()
66

77
ament_auto_add_library(${PROJECT_NAME} SHARED
8+
src/diagnostics.cpp
89
src/fail_safe.cpp
910
src/heartbeat.cpp
1011
src/interface.cpp
@@ -23,6 +24,7 @@ ament_auto_add_library(${PROJECT_NAME} SHARED
2324

2425
rclcpp_components_register_nodes(${PROJECT_NAME}
2526
"default_ad_api::AutowareStateNode"
27+
"default_ad_api::DiagnosticsNode"
2628
"default_ad_api::FailSafeNode"
2729
"default_ad_api::HeartbeatNode"
2830
"default_ad_api::InterfaceNode"

system/default_ad_api/launch/default_ad_api.launch.py

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def get_default_config():
4242
def generate_launch_description():
4343
components = [
4444
create_api_node("autoware_state", "AutowareStateNode"),
45+
create_api_node("diagnostics", "DiagnosticsNode"),
4546
create_api_node("fail_safe", "FailSafeNode"),
4647
create_api_node("heartbeat", "HeartbeatNode"),
4748
create_api_node("interface", "InterfaceNode"),

system/default_ad_api/package.xml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<depend>autoware_planning_msgs</depend>
2222
<depend>component_interface_specs</depend>
2323
<depend>component_interface_utils</depend>
24+
<depend>diagnostic_graph_utils</depend>
2425
<depend>geographic_msgs</depend>
2526
<depend>geography_utils</depend>
2627
<depend>motion_utils</depend>
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2024 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 "diagnostics.hpp"
16+
17+
#include <memory>
18+
#include <unordered_map>
19+
20+
namespace default_ad_api
21+
{
22+
23+
DiagnosticsNode::DiagnosticsNode(const rclcpp::NodeOptions & options) : Node("diagnostics", options)
24+
{
25+
using std::placeholders::_1;
26+
27+
pub_struct_ = create_publisher<autoware_adapi_v1_msgs::msg::DiagGraphStruct>(
28+
"/api/system/diagnostics/struct", rclcpp::QoS(1).transient_local());
29+
pub_status_ = create_publisher<autoware_adapi_v1_msgs::msg::DiagGraphStatus>(
30+
"/api/system/diagnostics/status", rclcpp::QoS(1));
31+
32+
sub_graph_.register_create_callback(std::bind(&DiagnosticsNode::on_create, this, _1));
33+
sub_graph_.register_update_callback(std::bind(&DiagnosticsNode::on_update, this, _1));
34+
sub_graph_.subscribe(*this, 10);
35+
}
36+
void DiagnosticsNode::on_create(DiagGraph::ConstSharedPtr graph)
37+
{
38+
const auto & units = graph->units();
39+
const auto & links = graph->links();
40+
41+
std::unordered_map<DiagUnit *, size_t> unit_indices_;
42+
for (size_t i = 0; i < units.size(); ++i) {
43+
unit_indices_[units[i]] = i;
44+
}
45+
46+
autoware_adapi_v1_msgs::msg::DiagGraphStruct msg;
47+
msg.stamp = graph->created_stamp();
48+
msg.id = graph->id();
49+
msg.nodes.reserve(units.size());
50+
msg.links.reserve(links.size());
51+
for (const auto & unit : units) {
52+
msg.nodes.emplace_back();
53+
msg.nodes.back().path = unit->path();
54+
}
55+
for (const auto & link : links) {
56+
msg.links.emplace_back();
57+
msg.links.back().parent = unit_indices_.at(link->parent());
58+
msg.links.back().child = unit_indices_.at(link->child());
59+
}
60+
pub_struct_->publish(msg);
61+
}
62+
63+
void DiagnosticsNode::on_update(DiagGraph::ConstSharedPtr graph)
64+
{
65+
const auto & units = graph->units();
66+
67+
autoware_adapi_v1_msgs::msg::DiagGraphStatus msg;
68+
msg.stamp = graph->updated_stamp();
69+
msg.id = graph->id();
70+
msg.nodes.reserve(units.size());
71+
for (const auto & unit : units) {
72+
msg.nodes.emplace_back();
73+
msg.nodes.back().level = unit->level();
74+
}
75+
pub_status_->publish(msg);
76+
}
77+
78+
} // namespace default_ad_api
79+
80+
#include <rclcpp_components/register_node_macro.hpp>
81+
RCLCPP_COMPONENTS_REGISTER_NODE(default_ad_api::DiagnosticsNode)
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2024 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 DIAGNOSTICS_HPP_
16+
#define DIAGNOSTICS_HPP_
17+
18+
#include "diagnostic_graph_utils/subscription.hpp"
19+
20+
#include <rclcpp/rclcpp.hpp>
21+
22+
#include <autoware_adapi_v1_msgs/msg/diag_graph_status.hpp>
23+
#include <autoware_adapi_v1_msgs/msg/diag_graph_struct.hpp>
24+
25+
namespace default_ad_api
26+
{
27+
28+
class DiagnosticsNode : public rclcpp::Node
29+
{
30+
public:
31+
explicit DiagnosticsNode(const rclcpp::NodeOptions & options);
32+
33+
private:
34+
using DiagGraph = diagnostic_graph_utils::DiagGraph;
35+
using DiagUnit = diagnostic_graph_utils::DiagUnit;
36+
using DiagLink = diagnostic_graph_utils::DiagLink;
37+
void on_create(DiagGraph::ConstSharedPtr graph);
38+
void on_update(DiagGraph::ConstSharedPtr graph);
39+
rclcpp::Publisher<autoware_adapi_v1_msgs::msg::DiagGraphStruct>::SharedPtr pub_struct_;
40+
rclcpp::Publisher<autoware_adapi_v1_msgs::msg::DiagGraphStatus>::SharedPtr pub_status_;
41+
diagnostic_graph_utils::DiagGraphSubscription sub_graph_;
42+
};
43+
44+
} // namespace default_ad_api
45+
46+
#endif // DIAGNOSTICS_HPP_

system/diagnostic_graph_utils/include/diagnostic_graph_utils/graph.hpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,20 @@ class DiagLink
5555
public:
5656
using DiagLinkStruct = tier4_system_msgs::msg::DiagLinkStruct;
5757
using DiagLinkStatus = tier4_system_msgs::msg::DiagLinkStatus;
58-
explicit DiagLink(const DiagLinkStruct & msg) : struct_(msg) {}
58+
DiagLink(const DiagLinkStruct & msg, DiagUnit * parent, DiagUnit * child) : struct_(msg)
59+
{
60+
parent_ = parent;
61+
child_ = child;
62+
}
5963
void update(const DiagLinkStatus & msg) { status_ = msg; }
64+
DiagUnit * parent() const { return parent_; }
65+
DiagUnit * child() const { return child_; }
6066

6167
private:
6268
DiagLinkStruct struct_;
6369
DiagLinkStatus status_;
70+
DiagUnit * parent_;
71+
DiagUnit * child_;
6472
};
6573

6674
class DiagNode : public DiagUnit
@@ -114,6 +122,7 @@ class DiagGraph
114122
bool update(const DiagGraphStatus & msg);
115123
rclcpp::Time created_stamp() const { return created_stamp_; }
116124
rclcpp::Time updated_stamp() const { return updated_stamp_; }
125+
std::string id() const { return id_; }
117126
std::vector<DiagUnit *> units() const;
118127
std::vector<DiagNode *> nodes() const;
119128
std::vector<DiagLeaf *> diags() const;

system/diagnostic_graph_utils/src/lib/graph.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void DiagGraph::create(const DiagGraphStruct & msg)
5454
for (const auto & data : msg.links) {
5555
DiagNode * parent = nodes_.at(data.parent).get();
5656
DiagUnit * child = get_child(data.is_leaf, data.child);
57-
const auto link = links_.emplace_back(std::make_unique<DiagLink>(data)).get();
57+
const auto link = links_.emplace_back(std::make_unique<DiagLink>(data, parent, child)).get();
5858
parent->add_child({link, child});
5959
}
6060
}

0 commit comments

Comments
 (0)