Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
time_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/time_scorer.hpp"
19 
20 namespace nav2_route
21 {
22 
24  const rclcpp_lifecycle::LifecycleNode::SharedPtr node,
25  const std::shared_ptr<tf2_ros::Buffer>/* tf_buffer */,
26  std::shared_ptr<nav2_costmap_2d::CostmapSubscriber>/* costmap_subscriber */,
27  const std::string & name)
28 {
29  RCLCPP_INFO(node->get_logger(), "Configuring time scorer.");
30  name_ = name;
31 
32  // Find the tag at high the speed limit information is stored
33  nav2_util::declare_parameter_if_not_declared(
34  node, getName() + ".speed_tag", rclcpp::ParameterValue("abs_speed_limit"));
35  speed_tag_ = node->get_parameter(getName() + ".speed_tag").as_string();
36 
37  nav2_util::declare_parameter_if_not_declared(
38  node, getName() + ".time_tag", rclcpp::ParameterValue("abs_time_taken"));
39  prev_time_tag_ = node->get_parameter(getName() + ".time_tag").as_string();
40 
41  // Find the proportional weight to apply, if multiple cost functions
42  nav2_util::declare_parameter_if_not_declared(
43  node, getName() + ".weight", rclcpp::ParameterValue(1.0));
44  weight_ = static_cast<float>(node->get_parameter(getName() + ".weight").as_double());
45 
46  nav2_util::declare_parameter_if_not_declared(
47  node, getName() + ".max_vel", rclcpp::ParameterValue(0.5));
48  max_vel_ = static_cast<float>(node->get_parameter(getName() + ".max_vel").as_double());
49 }
50 
52  const EdgePtr edge,
53  const RouteRequest & /* route_request */,
54  const EdgeType & /* edge_type */, float & cost)
55 {
56  // We have a previous actual time to utilize for a refined estimate
57  float time = 0.0;
58  time = edge->metadata.getValue<float>(prev_time_tag_, time);
59  if (time > 0.0f) {
60  cost = weight_ * time;
61  return true;
62  }
63 
64  // Else: Get the speed limit, if set for an edge, else use max velocity
65  float velocity = 0.0f;
66  velocity = edge->metadata.getValue<float>(speed_tag_, velocity);
67  if (velocity <= 0.0f) {
68  velocity = max_vel_;
69  }
70 
71  cost = weight_ * hypotf(
72  edge->end->coords.x - edge->start->coords.x,
73  edge->end->coords.y - edge->start->coords.y) / velocity;
74  return true;
75 }
76 
77 std::string TimeScorer::getName()
78 {
79  return name_;
80 }
81 
82 } // namespace nav2_route
83 
84 #include "pluginlib/class_list_macros.hpp"
A plugin interface to score edges during graph search to modify the lowest cost path (e....
Scores edges by the time to traverse an edge. It uses previous times to navigate the edge primarily,...
Definition: time_scorer.hpp:35
void configure(const rclcpp_lifecycle::LifecycleNode::SharedPtr node, const std::shared_ptr< tf2_ros::Buffer > tf_buffer, std::shared_ptr< nav2_costmap_2d::CostmapSubscriber > costmap_subscriber, const std::string &name) override
Configure.
Definition: time_scorer.cpp:23
bool score(const EdgePtr edge, const RouteRequest &route_request, const EdgeType &edge_type, float &cost) override
Main scoring plugin API.
Definition: time_scorer.cpp:51
std::string getName() override
Get name of the plugin for parameter scope mapping.
Definition: time_scorer.cpp:77
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