Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
start_pose_orientation_scorer.cpp
1 // Copyright (c) 2025, Polymath Robotics 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 <memory>
16 #include <string>
17 
18 #include "nav2_route/plugins/edge_cost_functions/start_pose_orientation_scorer.hpp"
19 
20 namespace nav2_route
21 {
22 
24  const nav2::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 start pose orientation scorer.");
30  name_ = name;
31  logger_ = node->get_logger();
32 
33  nav2::declare_parameter_if_not_declared(
34  node,
35  getName() + ".orientation_tolerance", rclcpp::ParameterValue(M_PI / 2.0));
36 
37  nav2::declare_parameter_if_not_declared(
38  node, getName() + ".orientation_weight", rclcpp::ParameterValue(1.0));
39 
40  nav2::declare_parameter_if_not_declared(
41  node, getName() + ".use_orientation_threshold", rclcpp::ParameterValue(false));
42 
43  orientation_tolerance_ = node->get_parameter(getName() + ".orientation_tolerance").as_double();
44  orientation_weight_ =
45  static_cast<float>(node->get_parameter(getName() + ".orientation_weight").as_double());
46  use_orientation_threshold_ =
47  node->get_parameter(getName() + ".use_orientation_threshold").as_bool();
48 
49  tf_buffer_ = tf_buffer;
50 }
51 
53  const EdgePtr edge,
54  const RouteRequest & route_request,
55  const EdgeType & edge_type, float & cost)
56 {
57  if (!route_request.use_poses) {
59  "Cannot use start pose orientation scorer without start pose specified!");
60  }
61 
62  if (edge_type == EdgeType::START) {
63  double edge_orientation = std::atan2(
64  edge->end->coords.y - edge->start->coords.y,
65  edge->end->coords.x - edge->start->coords.x);
66  double start_orientation = tf2::getYaw(route_request.start_pose.pose.orientation);
67  double d_yaw = std::abs(angles::shortest_angular_distance(edge_orientation, start_orientation));
68 
69  if (use_orientation_threshold_) {
70  if (d_yaw > orientation_tolerance_) {
71  return false;
72  }
73  }
74 
75  cost = orientation_weight_ * static_cast<float>(d_yaw);
76  }
77  return true;
78 }
79 
81 {
82  return name_;
83 }
84 
85 } // namespace nav2_route
86 
87 #include "pluginlib/class_list_macros.hpp"
A plugin interface to score edges during graph search to modify the lowest cost path (e....
Scores initial edge by comparing the orientation of the robot's current pose and the orientation of t...
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.
void configure(const nav2::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.
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