Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
validate_path_action.cpp
1 // Copyright (c) 2021 Joshua Wallace
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 <string>
16 #include <memory>
17 #include <limits>
18 
19 #include "nav2_behavior_tree/plugins/action/validate_path_action.hpp"
20 
21 namespace nav2_behavior_tree
22 {
23 
25  const std::string & service_node_name,
26  const BT::NodeConfiguration & conf)
27 : BtServiceNode<nav2_msgs::srv::IsPathValid>(service_node_name, conf, "is_path_valid")
28 {
29 }
30 
31 
33 {
34  getInput<unsigned int>("max_cost", max_cost_);
35  getInput<bool>("consider_unknown_as_obstacle", consider_unknown_as_obstacle_);
36  getInput<std::string>("layer_name", layer_name_);
37  getInput<std::string>("footprint", footprint_);
38  getInput<bool>("stop_at_first_collision", stop_at_first_collision_);
39  getInput<double>("max_lookahead_distance", max_lookahead_distance_);
40  getInput("path", path_);
41 
42  request_ = std::make_shared<nav2_msgs::srv::IsPathValid::Request>();
43  request_->path = path_;
44  request_->max_cost = max_cost_;
45  request_->consider_unknown_as_obstacle = consider_unknown_as_obstacle_;
46  request_->layer_name = layer_name_;
47  request_->footprint = footprint_;
48  request_->stop_at_first_collision = stop_at_first_collision_;
49  request_->max_lookahead_distance = max_lookahead_distance_;
50 }
51 
53  std::shared_ptr<nav2_msgs::srv::IsPathValid::Response> response)
54 {
55  // Check if validation was successful
56  if (!response->success) {
57  RCLCPP_ERROR(
58  node_->get_logger(),
59  "IsPathValid service failed to validate path");
60  return BT::NodeStatus::FAILURE;
61  }
62 
63  if (response->is_valid) {
64  // Ensure collision_poses output is cleared when the path is valid
65  std::vector<geometry_msgs::msg::PoseStamped> collision_poses;
66  setOutput("collision_poses", collision_poses);
67  return BT::NodeStatus::SUCCESS;
68  }
69 
70  // Extract collision poses based on invalid pose indices
71  std::vector<geometry_msgs::msg::PoseStamped> collision_poses;
72  if (!response->invalid_pose_indices.empty()) {
73  std::stringstream ss;
74  ss << "Path validation failed. Invalid pose indices: [";
75  for (size_t i = 0; i < response->invalid_pose_indices.size(); ++i) {
76  int32_t idx = response->invalid_pose_indices[i];
77  ss << idx;
78  if (i < response->invalid_pose_indices.size() - 1) {
79  ss << ", ";
80  }
81  // Add the collision pose if index is valid
82  if (idx >= 0 && static_cast<size_t>(idx) < path_.poses.size()) {
83  collision_poses.push_back(path_.poses[idx]);
84  }
85  }
86  ss << "]";
87  RCLCPP_WARN(node_->get_logger(), "%s", ss.str().c_str());
88  }
89 
90  // Set collision poses output
91  setOutput("collision_poses", collision_poses);
92 
93  return BT::NodeStatus::FAILURE;
94 }
95 
96 } // namespace nav2_behavior_tree
97 
98 #include "behaviortree_cpp/bt_factory.h"
99 BT_REGISTER_NODES(factory)
100 {
101  factory.registerNodeType<nav2_behavior_tree::ValidatePath>("ValidatePath");
102 }
Abstract class representing a service based BT node.
A nav2_behavior_tree::BtServiceNode class that validates a path by calling the IsPathValid service on...
BT::NodeStatus on_completion(std::shared_ptr< nav2_msgs::srv::IsPathValid::Response > response) override
Function to perform some user-defined operation after receiving a result from the service.
void on_tick() override
The main override required by a BT service.
ValidatePath(const std::string &service_node_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::ValidatePath.