Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
get_pose_from_path_action.cpp
1 // Copyright (c) 2024 Marc Morcos
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 "nav_msgs/msg/path.hpp"
20 #include "nav2_util/geometry_utils.hpp"
21 
22 #include "nav2_behavior_tree/plugins/action/get_pose_from_path_action.hpp"
23 
24 namespace nav2_behavior_tree
25 {
26 
27 GetPoseFromPath::GetPoseFromPath(
28  const std::string & name,
29  const BT::NodeConfiguration & conf)
30 : BT::ActionNodeBase(name, conf)
31 {
32 }
33 
34 inline BT::NodeStatus GetPoseFromPath::tick()
35 {
36  setStatus(BT::NodeStatus::RUNNING);
37 
38  nav_msgs::msg::Path input_path;
39  getInput("path", input_path);
40 
41  int pose_index;
42  getInput("index", pose_index);
43 
44  if (input_path.poses.empty()) {
45  return BT::NodeStatus::FAILURE;
46  }
47 
48  // Account for negative indices
49  if (pose_index < 0) {
50  pose_index = input_path.poses.size() + pose_index;
51  }
52 
53  // out of bounds index
54  if (pose_index < 0 || static_cast<unsigned>(pose_index) >= input_path.poses.size()) {
55  return BT::NodeStatus::FAILURE;
56  }
57 
58  // extract pose
59  geometry_msgs::msg::PoseStamped output_pose;
60  output_pose = input_path.poses[pose_index];
61 
62  // populate pose frame from path if necessary
63  if (output_pose.header.frame_id.empty()) {
64  output_pose.header.frame_id = input_path.header.frame_id;
65  }
66 
67 
68  setOutput("pose", output_pose);
69 
70  return BT::NodeStatus::SUCCESS;
71 }
72 
73 } // namespace nav2_behavior_tree
74 
75 #include "behaviortree_cpp_v3/bt_factory.h"
76 BT_REGISTER_NODES(factory)
77 {
78  factory.registerNodeType<nav2_behavior_tree::GetPoseFromPath>("GetPoseFromPath");
79 }