Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
distance_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/distance_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 distance scorer.");
31  name_ = name;
32 
33  // Find the tag at high the speed limit information is stored
34  speed_tag_ = node->declare_or_get_parameter(
35  getName() + ".speed_tag", std::string("speed_limit"));
36 
37  // Find the proportional weight to apply, if multiple cost functions
38  weight_ = static_cast<float>(
39  node->declare_or_get_parameter(getName() + ".weight", 1.0));
40 }
41 
43  const EdgePtr edge,
44  const RouteRequest & /* route_request */,
45  const EdgeType & /* edge_type */, float & cost)
46 {
47  // Get the speed limit, if set for an edge
48  float speed_val = 1.0f;
49  speed_val = edge->metadata.getValue<float>(speed_tag_, speed_val);
50  cost = weight_ * hypotf(
51  edge->end->coords.x - edge->start->coords.x,
52  edge->end->coords.y - edge->start->coords.y) / speed_val;
53  return true;
54 }
55 
57 {
58  return name_;
59 }
60 
61 } // namespace nav2_route
62 
63 #include "pluginlib/class_list_macros.hpp"
Scores edges by the distance traversed, weighted by speed limit metadata to optimize for time to goal...
std::string getName() override
Get name of the plugin for parameter scope mapping.
bool score(const EdgePtr edge, const RouteRequest &route_request, const EdgeType &edge_type, float &cost) override
Main scoring plugin API.
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.
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