Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
dynamic_edges_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/dynamic_edges_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 adjust edges scorer.");
31  name_ = name;
32  logger_ = node->get_logger();
33  service_ =
34  node->create_service<nav2_msgs::srv::DynamicEdges>(
35  std::string(node->get_name()) + "/" + getName() + "/adjust_edges",
36  std::bind(
38  std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
39 
40  dynamic_penalties_.clear();
41  closed_edges_.clear();
42 }
43 
45  const std::shared_ptr<rmw_request_id_t>,
46  const std::shared_ptr<nav2_msgs::srv::DynamicEdges::Request> request,
47  std::shared_ptr<nav2_msgs::srv::DynamicEdges::Response> response)
48 {
49  RCLCPP_INFO(logger_, "Edge closure and cost adjustment in progress!");
50 
51  // Add new closed edges
52  for (unsigned int edge : request->closed_edges) {
53  closed_edges_.insert(edge);
54  }
55 
56  // Removed now opened edges, if stored
57  for (unsigned int edge : request->opened_edges) {
58  if (closed_edges_.find(edge) != closed_edges_.end()) {
59  closed_edges_.erase(edge);
60  }
61  }
62 
63  // Add dynamic costs from application system for edges
64  for (auto & edge : request->adjust_edges) {
65  dynamic_penalties_[edge.edgeid] = edge.cost;
66  }
67 
68  response->success = true;
69 }
70 
72  const EdgePtr edge,
73  const RouteRequest & /* route_request */,
74  const EdgeType & /* edge_type */, float & cost)
75 {
76  // Find if this edge is in the closed set of edges
77  if (closed_edges_.find(edge->edgeid) != closed_edges_.end()) {
78  return false;
79  }
80 
81  const auto & dyn_pen = dynamic_penalties_.find(edge->edgeid);
82  if (dyn_pen != dynamic_penalties_.end()) {
83  cost = dyn_pen->second;
84  }
85 
86  return true;
87 }
88 
90 {
91  return name_;
92 }
93 
94 } // namespace nav2_route
95 
96 #include "pluginlib/class_list_macros.hpp"
Rejects edges that are in the closed set of edges for navigation to prevent routes from containing pa...
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 closedEdgesCb(const std::shared_ptr< rmw_request_id_t > request_header, const std::shared_ptr< nav2_msgs::srv::DynamicEdges::Request > request, std::shared_ptr< nav2_msgs::srv::DynamicEdges::Response > response)
Service callback to process edge changes.
bool score(const EdgePtr edge, const RouteRequest &route_request, const EdgeType &edge_type, float &cost) override
Main scoring plugin API.
std::string getName() override
Get name of the plugin for parameter scope mapping.
A plugin interface to score edges during graph search to modify the lowest cost path (e....
An object representing edges between nodes.
Definition: types.hpp:134
An object to store salient features of the route request including its start and goal node ids,...
Definition: types.hpp:224