|
| 1 | +// Copyright 2023 Autoware Foundation |
| 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 "gyro_odometer/diagnostics_module.hpp" |
| 16 | + |
| 17 | +#include <rclcpp/rclcpp.hpp> |
| 18 | + |
| 19 | +#include <diagnostic_msgs/msg/diagnostic_array.hpp> |
| 20 | + |
| 21 | +#include <algorithm> |
| 22 | +#include <string> |
| 23 | + |
| 24 | +namespace autoware::gyro_odometer |
| 25 | +{ |
| 26 | + |
| 27 | +DiagnosticsModule::DiagnosticsModule(rclcpp::Node * node, const std::string & diagnostic_name) |
| 28 | +: clock_(node->get_clock()) |
| 29 | +{ |
| 30 | + diagnostics_pub_ = |
| 31 | + node->create_publisher<diagnostic_msgs::msg::DiagnosticArray>("/diagnostics", 10); |
| 32 | + |
| 33 | + diagnostics_status_msg_.name = |
| 34 | + std::string(node->get_name()) + std::string(": ") + diagnostic_name; |
| 35 | + diagnostics_status_msg_.hardware_id = node->get_name(); |
| 36 | +} |
| 37 | + |
| 38 | +void DiagnosticsModule::clear() |
| 39 | +{ |
| 40 | + diagnostics_status_msg_.values.clear(); |
| 41 | + diagnostics_status_msg_.values.shrink_to_fit(); |
| 42 | + |
| 43 | + diagnostics_status_msg_.level = diagnostic_msgs::msg::DiagnosticStatus::OK; |
| 44 | + diagnostics_status_msg_.message = ""; |
| 45 | +} |
| 46 | + |
| 47 | +void DiagnosticsModule::addKeyValue(const diagnostic_msgs::msg::KeyValue & key_value_msg) |
| 48 | +{ |
| 49 | + auto it = std::find_if( |
| 50 | + std::begin(diagnostics_status_msg_.values), std::end(diagnostics_status_msg_.values), |
| 51 | + [key_value_msg](const auto & arg) { return arg.key == key_value_msg.key; }); |
| 52 | + |
| 53 | + if (it != std::cend(diagnostics_status_msg_.values)) { |
| 54 | + it->value = key_value_msg.value; |
| 55 | + } else { |
| 56 | + diagnostics_status_msg_.values.push_back(key_value_msg); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +template <> |
| 61 | +void DiagnosticsModule::addKeyValue(const std::string & key, const std::string & value) |
| 62 | +{ |
| 63 | + diagnostic_msgs::msg::KeyValue key_value; |
| 64 | + key_value.key = key; |
| 65 | + key_value.value = value; |
| 66 | + addKeyValue(key_value); |
| 67 | +} |
| 68 | + |
| 69 | +template <> |
| 70 | +void DiagnosticsModule::addKeyValue(const std::string & key, const bool & value) |
| 71 | +{ |
| 72 | + diagnostic_msgs::msg::KeyValue key_value; |
| 73 | + key_value.key = key; |
| 74 | + key_value.value = value ? "True" : "False"; |
| 75 | + addKeyValue(key_value); |
| 76 | +} |
| 77 | + |
| 78 | +void DiagnosticsModule::updateLevelAndMessage(const int8_t level, const std::string & message) |
| 79 | +{ |
| 80 | + if ((level > diagnostic_msgs::msg::DiagnosticStatus::OK)) { |
| 81 | + if (!diagnostics_status_msg_.message.empty()) { |
| 82 | + diagnostics_status_msg_.message += "; "; |
| 83 | + } |
| 84 | + diagnostics_status_msg_.message += message; |
| 85 | + } |
| 86 | + if (level > diagnostics_status_msg_.level) { |
| 87 | + diagnostics_status_msg_.level = level; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +void DiagnosticsModule::publish(const rclcpp::Time & publish_time_stamp) |
| 92 | +{ |
| 93 | + diagnostics_pub_->publish(createDiagnosticsArray(publish_time_stamp)); |
| 94 | +} |
| 95 | + |
| 96 | +diagnostic_msgs::msg::DiagnosticArray DiagnosticsModule::createDiagnosticsArray( |
| 97 | + const rclcpp::Time & publish_time_stamp) const |
| 98 | +{ |
| 99 | + diagnostic_msgs::msg::DiagnosticArray diagnostics_msg; |
| 100 | + diagnostics_msg.header.stamp = publish_time_stamp; |
| 101 | + diagnostics_msg.status.push_back(diagnostics_status_msg_); |
| 102 | + |
| 103 | + if (diagnostics_msg.status.at(0).level == diagnostic_msgs::msg::DiagnosticStatus::OK) { |
| 104 | + diagnostics_msg.status.at(0).message = "OK"; |
| 105 | + } |
| 106 | + |
| 107 | + return diagnostics_msg; |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace autoware::gyro_odometer |
0 commit comments