Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
navigator.cpp
1 // Copyright (c) 2024 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 <chrono>
16 #include "opennav_docking/navigator.hpp"
17 
18 namespace opennav_docking
19 {
20 
21 using namespace std::chrono_literals; // NOLINT
22 
23 Navigator::Navigator(const nav2::LifecycleNode::WeakPtr & parent)
24 : node_(parent)
25 {
26  auto node = node_.lock();
27  navigator_bt_xml_ = node->declare_or_get_parameter("navigator_bt_xml", std::string(""));
28 }
29 
31 {
32  // Need separate callback group and executor to call Nav action within docking action
33  auto node = node_.lock();
34  callback_group_ = node->create_callback_group(
35  rclcpp::CallbackGroupType::MutuallyExclusive,
36  false);
37  executor_.add_callback_group(callback_group_, node->get_node_base_interface());
38  nav_to_pose_client_ = node->create_action_client<Nav2Pose>(
39  "navigate_to_pose", callback_group_);
40 }
41 
43 {
44  nav_to_pose_client_.reset();
45 }
46 
48  const geometry_msgs::msg::PoseStamped & pose,
49  rclcpp::Duration remaining_staging_duration,
50  std::function<bool()> isPreempted,
51  bool recursed)
52 {
53  auto node = node_.lock();
54 
55  Nav2Pose::Goal goal;
56  goal.pose = pose;
57  goal.behavior_tree = navigator_bt_xml_;
58  const auto start_time = node->now();
59 
60  // Wait for server to be active
61  nav_to_pose_client_->wait_for_action_server(1s);
62  auto future_goal_handle = nav_to_pose_client_->async_send_goal(goal);
63  if (executor_.spin_until_future_complete(
64  future_goal_handle, 2s) == rclcpp::FutureReturnCode::SUCCESS)
65  {
66  auto future_result = nav_to_pose_client_->async_get_result(future_goal_handle.get());
67 
68  while (rclcpp::ok()) {
69  if (isPreempted()) {
70  auto cancel_future = nav_to_pose_client_->async_cancel_goal(future_goal_handle.get());
71  executor_.spin_until_future_complete(cancel_future, 1s);
72  throw opennav_docking_core::FailedToStage("Navigation request to staging pose preempted.");
73  }
74 
75  if (node->now() - start_time > remaining_staging_duration) {
76  auto cancel_future = nav_to_pose_client_->async_cancel_goal(future_goal_handle.get());
77  executor_.spin_until_future_complete(cancel_future, 1s);
78  throw opennav_docking_core::FailedToStage("Navigation request to staging pose timed out.");
79  }
80 
81  if (executor_.spin_until_future_complete(
82  future_result, 10ms) == rclcpp::FutureReturnCode::SUCCESS)
83  {
84  auto result = future_result.get();
85  if (result.code == rclcpp_action::ResultCode::SUCCEEDED &&
86  result.result->error_code == 0)
87  {
88  return; // Success!
89  } else {
90  RCLCPP_WARN(node->get_logger(), "Navigation request to staging pose failed.");
91  break;
92  }
93  }
94  }
95  }
96 
97  // Attempt to retry once using single iteration recursion
98  if (!recursed) {
99  auto elapsed_time = node->now() - start_time;
100  remaining_staging_duration = remaining_staging_duration - elapsed_time;
101  goToPose(pose, remaining_staging_duration, isPreempted, true);
102  return;
103  }
104 
105  throw opennav_docking_core::FailedToStage("Navigation request to staging pose failed.");
106 }
107 
108 } // namespace opennav_docking
void activate()
An activation method.
Definition: navigator.cpp:30
Navigator(const nav2::LifecycleNode::WeakPtr &parent)
A constructor for opennav_docking::Navigator.
Definition: navigator.cpp:23
void goToPose(const geometry_msgs::msg::PoseStamped &pose, rclcpp::Duration remaining_staging_duration, std::function< bool()> isPreempted, bool recursed=false)
An method to go to a particular pose May throw exception if fails to navigate or communicate Blocks u...
Definition: navigator.cpp:47
void deactivate()
An deactivation method.
Definition: navigator.cpp:42
Failed to navigate to the staging pose.