Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
semantic_scorer.cpp
1 // Copyright (c) 2025, Open Navigation LLC
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 <memory>
16 #include <string>
17 
18 #include "nav2_route/plugins/edge_cost_functions/semantic_scorer.hpp"
19 #include "nav2_ros_common/tf2_factories.hpp"
20 
21 namespace nav2_route
22 {
23 
25  const nav2::LifecycleNode::SharedPtr node,
26  const nav2::TransformBuffer::SharedPtr/* tf_buffer */,
27  std::shared_ptr<nav2_costmap_2d::CostmapSubscriber>/* costmap_subscriber */,
28  const std::string & name)
29 {
30  RCLCPP_INFO(node->get_logger(), "Configuring semantic scorer.");
31  name_ = name;
32 
33  // Find the semantic data
34  std::vector<std::string> classes = node->declare_or_get_parameter(
35  getName() + ".semantic_classes", std::vector<std::string>{});
36  for (auto & cl : classes) {
37  const double cost = node->declare_or_get_parameter<double>(
38  getName() + "." + cl);
39  semantic_info_[cl] = static_cast<float>(cost);
40  }
41 
42  // Find the key to look for semantic data for within the metadata. If set to empty string,
43  // will search instead for any key in the metadata.
44  key_ = node->declare_or_get_parameter(
45  getName() + ".semantic_key", std::string("class"));
46 
47  // Find the proportional weight to apply, if multiple cost functions
48  weight_ = static_cast<float>(
49  node->declare_or_get_parameter(getName() + ".weight", 1.0));
50 }
51 
52 void SemanticScorer::metadataKeyScorer(Metadata & mdata, float & score)
53 {
54  for (auto it = mdata.data.cbegin(); it != mdata.data.cend(); ++it) {
55  if (auto sem = semantic_info_.find(it->first); sem != semantic_info_.end()) {
56  score += sem->second;
57  }
58  }
59 }
60 
61 void SemanticScorer::metadataValueScorer(Metadata & mdata, float & score)
62 {
63  std::string cl;
64  cl = mdata.getValue<std::string>(key_, cl);
65  if (auto sem = semantic_info_.find(cl); sem != semantic_info_.end()) {
66  score += sem->second;
67  }
68 }
69 
71  const EdgePtr edge,
72  const RouteRequest & /* route_request */,
73  const EdgeType & /* edge_type */, float & cost)
74 {
75  float score = 0.0;
76  Metadata & node_mdata = edge->end->metadata;
77  Metadata & edge_mdata = edge->metadata;
78 
79  // If a particular key is known to have semantic info, use it, else search
80  // each metadata key field to see if it matches
81  if (key_.empty()) {
82  metadataKeyScorer(node_mdata, score);
83  metadataKeyScorer(edge_mdata, score);
84  } else {
85  metadataValueScorer(node_mdata, score);
86  metadataValueScorer(edge_mdata, score);
87  }
88 
89  cost = weight_ * score;
90  return true;
91 }
92 
94 {
95  return name_;
96 }
97 
98 } // namespace nav2_route
99 
100 #include "pluginlib/class_list_macros.hpp"
A plugin interface to score edges during graph search to modify the lowest cost path (e....
Scores an edge based on arbitrary graph semantic data such as set priority/danger levels or regional ...
void metadataValueScorer(Metadata &mdata, float &score)
Scores graph object based on metadata's semantic value at key.
std::string getName() override
Get name of the plugin for parameter scope mapping.
void configure(const nav2::LifecycleNode::SharedPtr node, const nav2::TransformBuffer::SharedPtr tf_buffer, std::shared_ptr< nav2_costmap_2d::CostmapSubscriber > costmap_subscriber, const std::string &name) override
Configure.
void metadataKeyScorer(Metadata &mdata, float &score)
Scores graph object based on metadata's key values.
bool score(const EdgePtr edge, const RouteRequest &route_request, const EdgeType &edge_type, float &cost) override
Main scoring plugin API.
An object representing edges between nodes.
Definition: types.hpp:134
An object to store arbitrary metadata regarding nodes from the graph file.
Definition: types.hpp:35
An object to store salient features of the route request including its start and goal node ids,...
Definition: types.hpp:224