|
| 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) |
0 commit comments