|
| 1 | +// Copyright 2024 Tier IV, Inc. |
| 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 "control_evaluator/control_evaluator_node.hpp" |
| 16 | + |
| 17 | +#include <fstream> |
| 18 | +#include <iostream> |
| 19 | +#include <map> |
| 20 | +#include <memory> |
| 21 | +#include <string> |
| 22 | +#include <utility> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace control_diagnostics |
| 26 | +{ |
| 27 | +controlEvaluatorNode::controlEvaluatorNode(const rclcpp::NodeOptions & node_options) |
| 28 | +: Node("control_evaluator", node_options) |
| 29 | +{ |
| 30 | + using std::placeholders::_1; |
| 31 | + |
| 32 | + control_diag_sub_ = create_subscription<DiagnosticArray>( |
| 33 | + "~/input/diagnostics", 1, std::bind(&controlEvaluatorNode::onDiagnostics, this, _1)); |
| 34 | + |
| 35 | + // Publisher |
| 36 | + metrics_pub_ = create_publisher<DiagnosticArray>("~/metrics", 1); |
| 37 | + |
| 38 | + // Timer callback to publish evaluator diagnostics |
| 39 | + using namespace std::literals::chrono_literals; |
| 40 | + timer_ = |
| 41 | + rclcpp::create_timer(this, get_clock(), 100ms, std::bind(&controlEvaluatorNode::onTimer, this)); |
| 42 | +} |
| 43 | + |
| 44 | +DiagnosticStatus controlEvaluatorNode::generateDiagnosticStatus(const bool is_emergency_brake) const |
| 45 | +{ |
| 46 | + DiagnosticStatus status; |
| 47 | + status.level = status.OK; |
| 48 | + status.name = "autonomous_emergency_braking"; |
| 49 | + diagnostic_msgs::msg::KeyValue key_value; |
| 50 | + key_value.key = "decision"; |
| 51 | + key_value.value = (is_emergency_brake) ? "stop" : "none"; |
| 52 | + status.values.push_back(key_value); |
| 53 | + return status; |
| 54 | +} |
| 55 | + |
| 56 | +void controlEvaluatorNode::onTimer() |
| 57 | +{ |
| 58 | + if (!metrics_msg_.status.empty()) { |
| 59 | + metrics_pub_->publish(metrics_msg_); |
| 60 | + metrics_msg_.status.clear(); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +void controlEvaluatorNode::onDiagnostics(const DiagnosticArray::ConstSharedPtr diag_msg) |
| 65 | +{ |
| 66 | + const auto start = now(); |
| 67 | + const auto aeb_status = |
| 68 | + std::find_if(diag_msg->status.begin(), diag_msg->status.end(), [](const auto & status) { |
| 69 | + const bool aeb_found = status.name.find("autonomous_emergency_braking") != std::string::npos; |
| 70 | + return aeb_found; |
| 71 | + }); |
| 72 | + |
| 73 | + if (aeb_status == diag_msg->status.end()) return; |
| 74 | + |
| 75 | + const bool is_emergency_brake = (aeb_status->level == DiagnosticStatus::ERROR); |
| 76 | + metrics_msg_.header.stamp = now(); |
| 77 | + metrics_msg_.status.emplace_back(generateDiagnosticStatus(is_emergency_brake)); |
| 78 | + |
| 79 | + const auto runtime = (now() - start).seconds(); |
| 80 | + RCLCPP_DEBUG(get_logger(), "control evaluation calculation time: %2.2f ms", runtime * 1e3); |
| 81 | +} |
| 82 | + |
| 83 | +} // namespace control_diagnostics |
| 84 | + |
| 85 | +#include "rclcpp_components/register_node_macro.hpp" |
| 86 | +RCLCPP_COMPONENTS_REGISTER_NODE(control_diagnostics::controlEvaluatorNode) |
0 commit comments