|
| 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 "perception_evaluator/perception_evaluator_node.hpp" |
| 16 | + |
| 17 | +#include "perception_evaluator/utils/marker_utils.hpp" |
| 18 | +#include "tier4_autoware_utils/ros/marker_helper.hpp" |
| 19 | +#include "tier4_autoware_utils/ros/parameter.hpp" |
| 20 | +#include "tier4_autoware_utils/ros/update_param.hpp" |
| 21 | + |
| 22 | +#include <tier4_autoware_utils/ros/uuid_helper.hpp> |
| 23 | + |
| 24 | +#include "boost/lexical_cast.hpp" |
| 25 | + |
| 26 | +#include <glog/logging.h> |
| 27 | + |
| 28 | +#include <fstream> |
| 29 | +#include <iostream> |
| 30 | +#include <map> |
| 31 | +#include <memory> |
| 32 | +#include <string> |
| 33 | +#include <utility> |
| 34 | +#include <vector> |
| 35 | + |
| 36 | +namespace perception_diagnostics |
| 37 | +{ |
| 38 | +PerceptionEvaluatorNode::PerceptionEvaluatorNode(const rclcpp::NodeOptions & node_options) |
| 39 | +: Node("perception_evaluator", node_options), |
| 40 | + parameters_(std::make_shared<Parameters>()), |
| 41 | + metrics_calculator_(parameters_) |
| 42 | +{ |
| 43 | + using std::placeholders::_1; |
| 44 | + |
| 45 | + google::InitGoogleLogging("map_based_prediction_node"); |
| 46 | + google::InstallFailureSignalHandler(); |
| 47 | + |
| 48 | + objects_sub_ = create_subscription<PredictedObjects>( |
| 49 | + "~/input/objects", 1, std::bind(&PerceptionEvaluatorNode::onObjects, this, _1)); |
| 50 | + metrics_pub_ = create_publisher<DiagnosticArray>("~/metrics", 1); |
| 51 | + pub_marker_ = create_publisher<MarkerArray>("~/markers", 10); |
| 52 | + |
| 53 | + tf_buffer_ = std::make_unique<tf2_ros::Buffer>(this->get_clock()); |
| 54 | + transform_listener_ = std::make_shared<tf2_ros::TransformListener>(*tf_buffer_); |
| 55 | + |
| 56 | + // Parameters |
| 57 | + initParameter(); |
| 58 | + |
| 59 | + set_param_res_ = this->add_on_set_parameters_callback( |
| 60 | + std::bind(&PerceptionEvaluatorNode::onParameter, this, std::placeholders::_1)); |
| 61 | + |
| 62 | + // Timer |
| 63 | + initTimer(/*period_s=*/0.1); |
| 64 | +} |
| 65 | + |
| 66 | +PerceptionEvaluatorNode::~PerceptionEvaluatorNode() |
| 67 | +{ |
| 68 | +} |
| 69 | + |
| 70 | +void PerceptionEvaluatorNode::initTimer(double period_s) |
| 71 | +{ |
| 72 | + const auto period_ns = |
| 73 | + std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::duration<double>(period_s)); |
| 74 | + timer_ = rclcpp::create_timer( |
| 75 | + this, get_clock(), period_ns, std::bind(&PerceptionEvaluatorNode::onTimer, this)); |
| 76 | +} |
| 77 | + |
| 78 | +void PerceptionEvaluatorNode::onTimer() |
| 79 | +{ |
| 80 | + DiagnosticArray metrics_msg; |
| 81 | + for (const Metric & metric : parameters_->metrics) { |
| 82 | + const auto metric_stat_map = metrics_calculator_.calculate(Metric(metric)); |
| 83 | + if (!metric_stat_map.has_value()) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + for (const auto & [metric, stat] : metric_stat_map.value()) { |
| 88 | + if (stat.count() > 0) { |
| 89 | + metrics_msg.status.push_back(generateDiagnosticStatus(metric, stat)); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (!metrics_msg.status.empty()) { |
| 95 | + metrics_msg.header.stamp = now(); |
| 96 | + metrics_pub_->publish(metrics_msg); |
| 97 | + } |
| 98 | + publishDebugMarker(); |
| 99 | +} |
| 100 | + |
| 101 | +DiagnosticStatus PerceptionEvaluatorNode::generateDiagnosticStatus( |
| 102 | + const std::string metric, const Stat<double> & metric_stat) const |
| 103 | +{ |
| 104 | + DiagnosticStatus status; |
| 105 | + |
| 106 | + status.level = status.OK; |
| 107 | + status.name = metric; |
| 108 | + |
| 109 | + diagnostic_msgs::msg::KeyValue key_value; |
| 110 | + key_value.key = "min"; |
| 111 | + key_value.value = std::to_string(metric_stat.min()); |
| 112 | + status.values.push_back(key_value); |
| 113 | + key_value.key = "max"; |
| 114 | + key_value.value = std::to_string(metric_stat.max()); |
| 115 | + status.values.push_back(key_value); |
| 116 | + key_value.key = "mean"; |
| 117 | + key_value.value = std::to_string(metric_stat.mean()); |
| 118 | + status.values.push_back(key_value); |
| 119 | + |
| 120 | + return status; |
| 121 | +} |
| 122 | + |
| 123 | +void PerceptionEvaluatorNode::onObjects(const PredictedObjects::ConstSharedPtr objects_msg) |
| 124 | +{ |
| 125 | + metrics_calculator_.setPredictedObjects(*objects_msg); |
| 126 | +} |
| 127 | + |
| 128 | +void PerceptionEvaluatorNode::publishDebugMarker() |
| 129 | +{ |
| 130 | + using marker_utils::createColorFromString; |
| 131 | + using marker_utils::createDeviationLines; |
| 132 | + using marker_utils::createObjectPolygonMarkerArray; |
| 133 | + using marker_utils::createPointsMarkerArray; |
| 134 | + using marker_utils::createPosesMarkerArray; |
| 135 | + |
| 136 | + MarkerArray marker; |
| 137 | + |
| 138 | + const auto add = [&marker](MarkerArray added) { |
| 139 | + for (auto & marker : added.markers) { |
| 140 | + marker.lifetime = rclcpp::Duration::from_seconds(1.5); |
| 141 | + } |
| 142 | + tier4_autoware_utils::appendMarkerArray(added, &marker); |
| 143 | + }; |
| 144 | + |
| 145 | + const auto history_path_map = metrics_calculator_.getHistoryPathMap(); |
| 146 | + const auto & p = parameters_->debug_marker_parameters; |
| 147 | + |
| 148 | + for (const auto & [uuid, history_path] : history_path_map) { |
| 149 | + { |
| 150 | + const auto c = createColorFromString(uuid + "_raw"); |
| 151 | + if (p.show_history_path) { |
| 152 | + add(createPointsMarkerArray(history_path.first, "history_path_" + uuid, 0, c.r, c.g, c.b)); |
| 153 | + } |
| 154 | + if (p.show_history_path_arrows) { |
| 155 | + add(createPosesMarkerArray( |
| 156 | + history_path.first, "history_path_arrows_" + uuid, 0, c.r, c.g, c.b, 0.1, 0.05, 0.05)); |
| 157 | + } |
| 158 | + } |
| 159 | + { |
| 160 | + const auto c = createColorFromString(uuid); |
| 161 | + if (p.show_smoothed_history_path) { |
| 162 | + add(createPointsMarkerArray( |
| 163 | + history_path.second, "smoothed_history_path_" + uuid, 0, c.r, c.g, c.b)); |
| 164 | + } |
| 165 | + if (p.show_smoothed_history_path_arrows) { |
| 166 | + add(createPosesMarkerArray( |
| 167 | + history_path.second, "smoothed_history_path_arrows_" + uuid, c.r, c.g, c.b, 0, 0.1, 0.05, |
| 168 | + 0.05)); |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + const auto object_data_map = metrics_calculator_.getDebugObjectData(); |
| 173 | + for (const auto & [uuid, object_data] : object_data_map) { |
| 174 | + const auto c = createColorFromString(uuid); |
| 175 | + const auto predicted_path = object_data.getPredictedPath(); |
| 176 | + const auto history_path = object_data.getHistoryPath(); |
| 177 | + if (p.show_predicted_path) { |
| 178 | + add(createPosesMarkerArray(predicted_path, "predicted_path_" + uuid, 0, 0, 0, 1)); |
| 179 | + } |
| 180 | + if (p.show_predicted_path_gt) { |
| 181 | + add(createPosesMarkerArray(history_path, "predicted_path_gt_" + uuid, 0, 1, 0, 0)); |
| 182 | + } |
| 183 | + if (p.show_deviation_lines) { |
| 184 | + add( |
| 185 | + createDeviationLines(predicted_path, history_path, "deviation_lines_" + uuid, 0, 1, 1, 1)); |
| 186 | + } |
| 187 | + if (p.show_object_polygon) { |
| 188 | + add(createObjectPolygonMarkerArray( |
| 189 | + object_data.object, "object_polygon_" + uuid, 0, c.r, c.g, c.b)); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + pub_marker_->publish(marker); |
| 194 | +} |
| 195 | + |
| 196 | +rcl_interfaces::msg::SetParametersResult PerceptionEvaluatorNode::onParameter( |
| 197 | + const std::vector<rclcpp::Parameter> & parameters) |
| 198 | +{ |
| 199 | + using tier4_autoware_utils::updateParam; |
| 200 | + |
| 201 | + auto & p = parameters_; |
| 202 | + |
| 203 | + updateParam<size_t>(parameters, "smoothing_window_size", p->smoothing_window_size); |
| 204 | + |
| 205 | + // update metrics |
| 206 | + { |
| 207 | + std::vector<std::string> metrics_str; |
| 208 | + updateParam<std::vector<std::string>>(parameters, "selected_metrics", metrics_str); |
| 209 | + std::vector<Metric> metrics; |
| 210 | + for (const std::string & selected_metric : metrics_str) { |
| 211 | + const Metric metric = str_to_metric.at(selected_metric); |
| 212 | + metrics.push_back(metric); |
| 213 | + } |
| 214 | + p->metrics = metrics; |
| 215 | + } |
| 216 | + |
| 217 | + // update parameters for each object class |
| 218 | + { |
| 219 | + const auto get_object_param = [&](std::string && ns) -> std::optional<ObjectParameter> { |
| 220 | + ObjectParameter param{}; |
| 221 | + if (updateParam<bool>(parameters, ns + "check_deviation", param.check_deviation)) { |
| 222 | + return param; |
| 223 | + } |
| 224 | + return std::nullopt; |
| 225 | + }; |
| 226 | + |
| 227 | + const std::string ns = "target_object."; |
| 228 | + if (const auto new_param = get_object_param(ns + "car.")) { |
| 229 | + p->object_parameters.at(ObjectClassification::CAR) = *new_param; |
| 230 | + } |
| 231 | + if (const auto new_param = get_object_param(ns + "truck.")) { |
| 232 | + p->object_parameters.at(ObjectClassification::TRUCK) = *new_param; |
| 233 | + } |
| 234 | + if (const auto new_param = get_object_param(ns + "bus.")) { |
| 235 | + p->object_parameters.at(ObjectClassification::BUS) = *new_param; |
| 236 | + } |
| 237 | + if (const auto new_param = get_object_param(ns + "trailer.")) { |
| 238 | + p->object_parameters.at(ObjectClassification::TRAILER) = *new_param; |
| 239 | + } |
| 240 | + if (const auto new_param = get_object_param(ns + "bicycle.")) { |
| 241 | + p->object_parameters.at(ObjectClassification::BICYCLE) = *new_param; |
| 242 | + } |
| 243 | + if (const auto new_param = get_object_param(ns + "motorcycle.")) { |
| 244 | + p->object_parameters.at(ObjectClassification::MOTORCYCLE) = *new_param; |
| 245 | + } |
| 246 | + if (const auto new_param = get_object_param(ns + "pedestrian.")) { |
| 247 | + p->object_parameters.at(ObjectClassification::PEDESTRIAN) = *new_param; |
| 248 | + } |
| 249 | + if (const auto new_param = get_object_param(ns + "unknown.")) { |
| 250 | + p->object_parameters.at(ObjectClassification::UNKNOWN) = *new_param; |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + // update debug marker parameters |
| 255 | + { |
| 256 | + const std::string ns = "debug_marker."; |
| 257 | + updateParam<bool>( |
| 258 | + parameters, ns + "history_path", p->debug_marker_parameters.show_history_path); |
| 259 | + updateParam<bool>( |
| 260 | + parameters, ns + "history_path_arrows", p->debug_marker_parameters.show_history_path_arrows); |
| 261 | + updateParam<bool>( |
| 262 | + parameters, ns + "smoothed_history_path", |
| 263 | + p->debug_marker_parameters.show_smoothed_history_path); |
| 264 | + updateParam<bool>( |
| 265 | + parameters, ns + "smoothed_history_path_arrows", |
| 266 | + p->debug_marker_parameters.show_smoothed_history_path_arrows); |
| 267 | + updateParam<bool>( |
| 268 | + parameters, ns + "predicted_path", p->debug_marker_parameters.show_predicted_path); |
| 269 | + updateParam<bool>( |
| 270 | + parameters, ns + "predicted_path_gt", p->debug_marker_parameters.show_predicted_path_gt); |
| 271 | + updateParam<bool>( |
| 272 | + parameters, ns + "deviation_lines", p->debug_marker_parameters.show_deviation_lines); |
| 273 | + updateParam<bool>( |
| 274 | + parameters, ns + "object_polygon", p->debug_marker_parameters.show_object_polygon); |
| 275 | + } |
| 276 | + |
| 277 | + rcl_interfaces::msg::SetParametersResult result; |
| 278 | + result.successful = true; |
| 279 | + result.reason = "success"; |
| 280 | + |
| 281 | + return result; |
| 282 | +} |
| 283 | + |
| 284 | +void PerceptionEvaluatorNode::initParameter() |
| 285 | +{ |
| 286 | + using tier4_autoware_utils::getOrDeclareParameter; |
| 287 | + using tier4_autoware_utils::updateParam; |
| 288 | + |
| 289 | + auto & p = parameters_; |
| 290 | + |
| 291 | + p->smoothing_window_size = getOrDeclareParameter<int>(*this, "smoothing_window_size"); |
| 292 | + p->prediction_time_horizons = |
| 293 | + getOrDeclareParameter<std::vector<double>>(*this, "prediction_time_horizons"); |
| 294 | + |
| 295 | + // set metrics |
| 296 | + const auto selected_metrics = |
| 297 | + getOrDeclareParameter<std::vector<std::string>>(*this, "selected_metrics"); |
| 298 | + for (const std::string & selected_metric : selected_metrics) { |
| 299 | + const Metric metric = str_to_metric.at(selected_metric); |
| 300 | + parameters_->metrics.push_back(metric); |
| 301 | + } |
| 302 | + |
| 303 | + // set parameters for each object class |
| 304 | + { |
| 305 | + const auto get_object_param = [&](std::string && ns) -> ObjectParameter { |
| 306 | + ObjectParameter param{}; |
| 307 | + param.check_deviation = getOrDeclareParameter<bool>(*this, ns + "check_deviation"); |
| 308 | + return param; |
| 309 | + }; |
| 310 | + |
| 311 | + const std::string ns = "target_object."; |
| 312 | + p->object_parameters.emplace(ObjectClassification::CAR, get_object_param(ns + "car.")); |
| 313 | + p->object_parameters.emplace(ObjectClassification::TRUCK, get_object_param(ns + "truck.")); |
| 314 | + p->object_parameters.emplace(ObjectClassification::BUS, get_object_param(ns + "bus.")); |
| 315 | + p->object_parameters.emplace(ObjectClassification::TRAILER, get_object_param(ns + "trailer.")); |
| 316 | + p->object_parameters.emplace(ObjectClassification::BICYCLE, get_object_param(ns + "bicycle.")); |
| 317 | + p->object_parameters.emplace( |
| 318 | + ObjectClassification::MOTORCYCLE, get_object_param(ns + "motorcycle.")); |
| 319 | + p->object_parameters.emplace( |
| 320 | + ObjectClassification::PEDESTRIAN, get_object_param(ns + "pedestrian.")); |
| 321 | + p->object_parameters.emplace(ObjectClassification::UNKNOWN, get_object_param(ns + "unknown.")); |
| 322 | + } |
| 323 | + |
| 324 | + // set debug marker parameters |
| 325 | + { |
| 326 | + const std::string ns = "debug_marker."; |
| 327 | + p->debug_marker_parameters.show_history_path = |
| 328 | + getOrDeclareParameter<bool>(*this, ns + "history_path"); |
| 329 | + p->debug_marker_parameters.show_history_path_arrows = |
| 330 | + getOrDeclareParameter<bool>(*this, ns + "history_path_arrows"); |
| 331 | + p->debug_marker_parameters.show_smoothed_history_path = |
| 332 | + getOrDeclareParameter<bool>(*this, ns + "smoothed_history_path"); |
| 333 | + p->debug_marker_parameters.show_smoothed_history_path_arrows = |
| 334 | + getOrDeclareParameter<bool>(*this, ns + "smoothed_history_path_arrows"); |
| 335 | + p->debug_marker_parameters.show_predicted_path = |
| 336 | + getOrDeclareParameter<bool>(*this, ns + "predicted_path"); |
| 337 | + p->debug_marker_parameters.show_predicted_path_gt = |
| 338 | + getOrDeclareParameter<bool>(*this, ns + "predicted_path_gt"); |
| 339 | + p->debug_marker_parameters.show_deviation_lines = |
| 340 | + getOrDeclareParameter<bool>(*this, ns + "deviation_lines"); |
| 341 | + p->debug_marker_parameters.show_object_polygon = |
| 342 | + getOrDeclareParameter<bool>(*this, ns + "object_polygon"); |
| 343 | + } |
| 344 | +} |
| 345 | +} // namespace perception_diagnostics |
| 346 | + |
| 347 | +#include "rclcpp_components/register_node_macro.hpp" |
| 348 | +RCLCPP_COMPONENTS_REGISTER_NODE(perception_diagnostics::PerceptionEvaluatorNode) |
0 commit comments