|
| 1 | +// Copyright 2024 TIER IV, Inc. All rights reserved. |
| 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 | + |
| 16 | +#ifndef METRICS_VISUALIZE_PANEL_HPP_ |
| 17 | +#define METRICS_VISUALIZE_PANEL_HPP_ |
| 18 | + |
| 19 | +#ifndef Q_MOC_RUN |
| 20 | +#include <QChartView> |
| 21 | +#include <QColor> |
| 22 | +#include <QGridLayout> |
| 23 | +#include <QHeaderView> |
| 24 | +#include <QLabel> |
| 25 | +#include <QLineSeries> |
| 26 | +#include <QPainter> |
| 27 | +#include <QTableWidget> |
| 28 | +#include <QVBoxLayout> |
| 29 | +#endif |
| 30 | + |
| 31 | +#include <rclcpp/rclcpp.hpp> |
| 32 | +#include <rviz_common/panel.hpp> |
| 33 | + |
| 34 | +#include <diagnostic_msgs/msg/diagnostic_array.hpp> |
| 35 | + |
| 36 | +#include <limits> |
| 37 | +#include <string> |
| 38 | +#include <unordered_map> |
| 39 | + |
| 40 | +namespace rviz_plugins |
| 41 | +{ |
| 42 | + |
| 43 | +using diagnostic_msgs::msg::DiagnosticArray; |
| 44 | +using diagnostic_msgs::msg::DiagnosticStatus; |
| 45 | +using diagnostic_msgs::msg::KeyValue; |
| 46 | +using QtCharts::QChart; |
| 47 | +using QtCharts::QChartView; |
| 48 | +using QtCharts::QLineSeries; |
| 49 | + |
| 50 | +struct Metric |
| 51 | +{ |
| 52 | +public: |
| 53 | + explicit Metric(const DiagnosticStatus & status) : chart(new QChartView), table(new QTableWidget) |
| 54 | + { |
| 55 | + init(status); |
| 56 | + } |
| 57 | + |
| 58 | + void init(const DiagnosticStatus & status) |
| 59 | + { |
| 60 | + QStringList header{}; |
| 61 | + |
| 62 | + { |
| 63 | + auto label = new QLabel; |
| 64 | + label->setAlignment(Qt::AlignCenter); |
| 65 | + label->setText("metric_name"); |
| 66 | + labels.emplace("metric_name", label); |
| 67 | + |
| 68 | + header.push_back(QString::fromStdString(status.name)); |
| 69 | + } |
| 70 | + |
| 71 | + for (const auto & [key, value] : status.values) { |
| 72 | + auto label = new QLabel; |
| 73 | + label->setAlignment(Qt::AlignCenter); |
| 74 | + labels.emplace(key, label); |
| 75 | + |
| 76 | + auto plot = new QLineSeries; |
| 77 | + plot->setName(QString::fromStdString(key)); |
| 78 | + plots.emplace(key, plot); |
| 79 | + chart->chart()->addSeries(plot); |
| 80 | + chart->chart()->createDefaultAxes(); |
| 81 | + |
| 82 | + header.push_back(QString::fromStdString(key)); |
| 83 | + } |
| 84 | + |
| 85 | + { |
| 86 | + chart->chart()->setTitle(QString::fromStdString(status.name)); |
| 87 | + chart->chart()->legend()->setVisible(true); |
| 88 | + chart->chart()->legend()->detachFromChart(); |
| 89 | + chart->setRenderHint(QPainter::Antialiasing); |
| 90 | + } |
| 91 | + |
| 92 | + { |
| 93 | + table->setColumnCount(status.values.size() + 1); |
| 94 | + table->setHorizontalHeaderLabels(header); |
| 95 | + table->verticalHeader()->hide(); |
| 96 | + table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); |
| 97 | + table->setRowCount(1); |
| 98 | + table->setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + void updateData(const double time, const DiagnosticStatus & status) |
| 103 | + { |
| 104 | + for (const auto & [key, value] : status.values) { |
| 105 | + const double data = std::stod(value); |
| 106 | + labels.at(key)->setText(QString::fromStdString(toString(data))); |
| 107 | + plots.at(key)->append(time, data); |
| 108 | + updateMinMax(data); |
| 109 | + } |
| 110 | + |
| 111 | + { |
| 112 | + const auto area = chart->chart()->plotArea(); |
| 113 | + const auto rect = chart->chart()->legend()->rect(); |
| 114 | + chart->chart()->legend()->setGeometry( |
| 115 | + QRectF(area.x(), area.y(), area.width(), rect.height())); |
| 116 | + chart->chart()->axes(Qt::Horizontal).front()->setRange(time - 100.0, time); |
| 117 | + } |
| 118 | + |
| 119 | + { |
| 120 | + table->setCellWidget(0, 0, labels.at("metric_name")); |
| 121 | + } |
| 122 | + |
| 123 | + for (size_t i = 0; i < status.values.size(); ++i) { |
| 124 | + table->setCellWidget(0, i + 1, labels.at(status.values.at(i).key)); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + void updateMinMax(double data) |
| 129 | + { |
| 130 | + if (data < y_range_min) { |
| 131 | + y_range_min = data > 0.0 ? 0.9 * data : 1.1 * data; |
| 132 | + chart->chart()->axes(Qt::Vertical).front()->setMin(y_range_min); |
| 133 | + } |
| 134 | + |
| 135 | + if (data > y_range_max) { |
| 136 | + y_range_max = data > 0.0 ? 1.1 * data : 0.9 * data; |
| 137 | + chart->chart()->axes(Qt::Vertical).front()->setMax(y_range_max); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + void updateTable() { table->update(); } |
| 142 | + |
| 143 | + void updateGraph() { chart->update(); } |
| 144 | + |
| 145 | + QChartView * getChartView() const { return chart; } |
| 146 | + |
| 147 | + QTableWidget * getTable() const { return table; } |
| 148 | + |
| 149 | +private: |
| 150 | + static std::optional<std::string> getValue(const DiagnosticStatus & status, std::string && key) |
| 151 | + { |
| 152 | + const auto itr = std::find_if( |
| 153 | + status.values.begin(), status.values.end(), |
| 154 | + [&](const auto & value) { return value.key == key; }); |
| 155 | + |
| 156 | + if (itr == status.values.end()) { |
| 157 | + return std::nullopt; |
| 158 | + } |
| 159 | + |
| 160 | + return itr->value; |
| 161 | + } |
| 162 | + |
| 163 | + static std::string toString(const double & value) |
| 164 | + { |
| 165 | + std::stringstream ss; |
| 166 | + ss << std::scientific << std::setprecision(2) << value; |
| 167 | + return ss.str(); |
| 168 | + } |
| 169 | + |
| 170 | + QChartView * chart; |
| 171 | + QTableWidget * table; |
| 172 | + |
| 173 | + std::unordered_map<std::string, QLabel *> labels; |
| 174 | + std::unordered_map<std::string, QLineSeries *> plots; |
| 175 | + |
| 176 | + double y_range_min{std::numeric_limits<double>::max()}; |
| 177 | + double y_range_max{std::numeric_limits<double>::lowest()}; |
| 178 | +}; |
| 179 | + |
| 180 | +class MetricsVisualizePanel : public rviz_common::Panel |
| 181 | +{ |
| 182 | + Q_OBJECT |
| 183 | + |
| 184 | +public: |
| 185 | + explicit MetricsVisualizePanel(QWidget * parent = nullptr); |
| 186 | + void onInitialize() override; |
| 187 | + |
| 188 | +private Q_SLOTS: |
| 189 | + |
| 190 | +private: |
| 191 | + rclcpp::Node::SharedPtr raw_node_; |
| 192 | + rclcpp::TimerBase::SharedPtr timer_; |
| 193 | + rclcpp::Subscription<DiagnosticArray>::SharedPtr sub_; |
| 194 | + |
| 195 | + void onTimer(); |
| 196 | + void onMetrics(const DiagnosticArray::ConstSharedPtr msg); |
| 197 | + |
| 198 | + QGridLayout * grid_; |
| 199 | + |
| 200 | + std::mutex mutex_; |
| 201 | + std::unordered_map<std::string, Metric> metrics_; |
| 202 | +}; |
| 203 | +} // namespace rviz_plugins |
| 204 | + |
| 205 | +#endif // METRICS_VISUALIZE_PANEL_HPP_ |
0 commit comments