Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
check_pose_occupancy_action.cpp
1 // Copyright (c) 2025 Maurice Alexander Purnawan
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/check_pose_occupancy_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::GetCosts>(service_node_name, conf,
28  "/global_costmap/get_cost_global_costmap")
29 {
30 }
31 
32 
34 {
35  getInput<double>("cost_threshold", cost_threshold_);
36  getInput<bool>("use_footprint", use_footprint_);
37  getInput<bool>("consider_unknown_as_obstacle", consider_unknown_as_obstacle_);
38  geometry_msgs::msg::PoseStamped pose;
39  getInput("pose", pose);
40 
41  request_ = std::make_shared<nav2_msgs::srv::GetCosts::Request>();
42  request_->use_footprint = use_footprint_;
43  request_->poses.push_back(pose);
44 }
45 
47  std::shared_ptr<nav2_msgs::srv::GetCosts::Response> response)
48 {
49  if (!response->success) {
50  RCLCPP_ERROR(
51  node_->get_logger(),
52  "GetCosts service call failed");
53  return BT::NodeStatus::FAILURE;
54  }
55 
56  if ((response->costs[0] == 255 && !consider_unknown_as_obstacle_) ||
57  response->costs[0] < cost_threshold_)
58  {
59  return BT::NodeStatus::FAILURE;
60  } else {
61  return BT::NodeStatus::SUCCESS;
62  }
63 }
64 
65 } // namespace nav2_behavior_tree
66 
67 #include "behaviortree_cpp/bt_factory.h"
68 BT_REGISTER_NODES(factory)
69 {
70  factory.registerNodeType<nav2_behavior_tree::CheckPoseOccupancy>("CheckPoseOccupancy");
71 }
Abstract class representing a service based BT node.
A nav2_behavior_tree::BtServiceNode class that checks if a pose is occupied by calling the GetCosts s...
BT::NodeStatus on_completion(std::shared_ptr< nav2_msgs::srv::GetCosts::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.
CheckPoseOccupancy(const std::string &service_node_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::CheckPoseOccupancy.