Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
navigate_through_poses.cpp
1 // Copyright (c) 2021 Samsung Research
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 <vector>
16 #include <string>
17 #include <set>
18 #include <memory>
19 #include <limits>
20 #include "nav2_bt_navigator/navigators/navigate_through_poses.hpp"
21 
22 namespace nav2_bt_navigator
23 {
24 
25 bool
27  nav2::LifecycleNode::WeakPtr parent_node,
28  std::shared_ptr<nav2_util::OdomSmoother> odom_smoother)
29 {
30  start_time_ = rclcpp::Time(0);
31  auto node = parent_node.lock();
32 
33  goals_blackboard_id_ =
34  node->declare_or_get_parameter(getName() + ".goals_blackboard_id", std::string("goals"));
35  path_blackboard_id_ =
36  node->declare_or_get_parameter(getName() + ".path_blackboard_id", std::string("path"));
37  waypoint_statuses_blackboard_id_ =
38  node->declare_or_get_parameter(getName() + ".waypoint_statuses_blackboard_id",
39  std::string("waypoint_statuses"));
40 
41  // Odometry smoother object for getting current speed
42  odom_smoother_ = odom_smoother;
43 
44  bool enable_groot_monitoring =
45  node->declare_or_get_parameter(getName() + ".enable_groot_monitoring", false);
46  int groot_server_port =
47  node->declare_or_get_parameter(getName() + ".groot_server_port", 1669);
48 
49  bt_action_server_->setGrootMonitoring(
50  enable_groot_monitoring,
51  groot_server_port);
52 
53  return true;
54 }
55 
56 std::string
58  nav2::LifecycleNode::WeakPtr parent_node)
59 {
60  auto node = parent_node.lock();
61  std::string pkg_share_dir =
62  ament_index_cpp::get_package_share_directory("nav2_bt_navigator");
63 
64  auto default_bt_xml_filename = node->declare_or_get_parameter(
65  "default_nav_through_poses_bt_xml",
66  pkg_share_dir +
67  "/behavior_trees/navigate_through_poses_w_replanning_and_recovery.xml");
68 
69  return default_bt_xml_filename;
70 }
71 
72 bool
73 NavigateThroughPosesNavigator::goalReceived(ActionT::Goal::ConstSharedPtr goal)
74 {
75  if (!bt_action_server_->loadBehaviorTree(goal->behavior_tree)) {
76  bt_action_server_->setInternalError(ActionT::Result::FAILED_TO_LOAD_BEHAVIOR_TREE,
77  "Error loading BT: " + goal->behavior_tree + ". Navigation canceled.");
78  return false;
79  }
80 
81  return initializeGoalPoses(goal);
82 }
83 
84 void
86  typename ActionT::Result::SharedPtr result,
87  const nav2_behavior_tree::BtStatus final_bt_status)
88 {
89  if (result->error_code == 0) {
90  if (bt_action_server_->populateInternalError(result)) {
91  RCLCPP_WARN(logger_,
92  "NavigateThroughPosesNavigator::goalCompleted, internal error %d:'%s'.",
93  result->error_code,
94  result->error_msg.c_str());
95  }
96  } else {
97  RCLCPP_WARN(logger_, "NavigateThroughPosesNavigator::goalCompleted error %d:'%s'.",
98  result->error_code,
99  result->error_msg.c_str());
100  }
101 
102  // populate waypoint statuses in result
103  auto blackboard = bt_action_server_->getBlackboard();
104  auto waypoint_statuses =
105  blackboard->get<std::vector<nav2_msgs::msg::WaypointStatus>>(waypoint_statuses_blackboard_id_);
106 
107  // populate remaining waypoint statuses based on final_bt_status
108  auto integrate_waypoint_status = final_bt_status == nav2_behavior_tree::BtStatus::SUCCEEDED ?
109  nav2_msgs::msg::WaypointStatus::COMPLETED : nav2_msgs::msg::WaypointStatus::FAILED;
110  for (auto & waypoint_status : waypoint_statuses) {
111  if (waypoint_status.waypoint_status == nav2_msgs::msg::WaypointStatus::PENDING) {
112  waypoint_status.waypoint_status = integrate_waypoint_status;
113  }
114  }
115 
116  result->waypoint_statuses = std::move(waypoint_statuses);
117 }
118 
119 void
121 {
122  using namespace nav2_util::geometry_utils; // NOLINT
123 
124  // action server feedback (pose, duration of task,
125  // number of recoveries, and distance remaining to goal, etc)
126  auto feedback_msg = std::make_shared<ActionT::Feedback>();
127 
128  auto blackboard = bt_action_server_->getBlackboard();
129 
130  nav_msgs::msg::Goals goal_poses;
131  [[maybe_unused]] auto res = blackboard->get(goals_blackboard_id_, goal_poses);
132 
133  feedback_msg->waypoint_statuses =
134  blackboard->get<std::vector<nav2_msgs::msg::WaypointStatus>>(waypoint_statuses_blackboard_id_);
135 
136  if (goal_poses.goals.size() == 0) {
137  bt_action_server_->publishFeedback(feedback_msg);
138  return;
139  }
140 
141  geometry_msgs::msg::PoseStamped current_pose;
142  if (!nav2_util::getCurrentPose(
143  current_pose, *feedback_utils_.tf,
144  feedback_utils_.global_frame, feedback_utils_.robot_frame,
145  feedback_utils_.transform_tolerance))
146  {
147  RCLCPP_ERROR(logger_, "Robot pose is not available.");
148  return;
149  }
150 
151  // Get current path points
152  nav_msgs::msg::Path current_path;
153  res = blackboard->get(path_blackboard_id_, current_path);
154  if (res && current_path.poses.size() > 0u) {
155  // Find the closest pose to current pose on global path
156  auto find_closest_pose_idx =
157  [&current_pose, &current_path]() {
158  size_t closest_pose_idx = 0;
159  double curr_min_dist = std::numeric_limits<double>::max();
160  for (size_t curr_idx = 0; curr_idx < current_path.poses.size(); ++curr_idx) {
161  double curr_dist = nav2_util::geometry_utils::euclidean_distance(
162  current_pose, current_path.poses[curr_idx]);
163  if (curr_dist < curr_min_dist) {
164  curr_min_dist = curr_dist;
165  closest_pose_idx = curr_idx;
166  }
167  }
168  return closest_pose_idx;
169  };
170 
171  // Calculate distance on the path
172  double distance_remaining =
173  nav2_util::geometry_utils::calculate_path_length(current_path, find_closest_pose_idx());
174 
175  // Default value for time remaining
176  rclcpp::Duration estimated_time_remaining = rclcpp::Duration::from_seconds(0.0);
177 
178  // Get current speed
179  geometry_msgs::msg::Twist current_odom = odom_smoother_->getTwist();
180  double current_linear_speed = std::hypot(current_odom.linear.x, current_odom.linear.y);
181 
182  // Calculate estimated time taken to goal if speed is higher than 1cm/s
183  // and at least 10cm to go
184  if ((std::abs(current_linear_speed) > 0.01) && (distance_remaining > 0.1)) {
185  estimated_time_remaining =
186  rclcpp::Duration::from_seconds(distance_remaining / std::abs(current_linear_speed));
187  }
188 
189  feedback_msg->distance_remaining = distance_remaining;
190  feedback_msg->estimated_time_remaining = estimated_time_remaining;
191  }
192 
193  int recovery_count = 0;
194  res = blackboard->get("number_recoveries", recovery_count);
195  feedback_msg->number_of_recoveries = recovery_count;
196  feedback_msg->current_pose = current_pose;
197  feedback_msg->navigation_time = clock_->now() - start_time_;
198  feedback_msg->number_of_poses_remaining = goal_poses.goals.size();
199 
200  bt_action_server_->publishFeedback(feedback_msg);
201 }
202 
203 void
204 NavigateThroughPosesNavigator::onPreempt(ActionT::Goal::ConstSharedPtr goal)
205 {
206  RCLCPP_INFO(logger_, "Received goal preemption request");
207 
208  if (goal->behavior_tree == bt_action_server_->getCurrentBTFilenameOrID() ||
209  (goal->behavior_tree.empty() &&
210  bt_action_server_->getCurrentBTFilenameOrID() == bt_action_server_->getDefaultBTFilenameOrID()))
211  {
212  // if pending goal requests the same BT as the current goal, accept the pending goal
213  // if pending goal has an empty behavior_tree field, it requests the default BT file
214  // accept the pending goal if the current goal is running the default BT file
215  if (!initializeGoalPoses(bt_action_server_->acceptPendingGoal())) {
216  RCLCPP_WARN(
217  logger_,
218  "Preemption request was rejected since the goal poses could not be "
219  "transformed. For now, continuing to track the last goal until completion.");
220  bt_action_server_->terminatePendingGoal();
221  }
222  } else {
223  RCLCPP_WARN(
224  logger_,
225  "Preemption request was rejected since the requested BT XML file is not the same "
226  "as the one that the current goal is executing. Preemption with a new BT is invalid "
227  "since it would require cancellation of the previous goal instead of true preemption."
228  "\nCancel the current goal and send a new action request if you want to use a "
229  "different BT XML file. For now, continuing to track the last goal until completion.");
230  bt_action_server_->terminatePendingGoal();
231  }
232 }
233 
234 bool
235 NavigateThroughPosesNavigator::initializeGoalPoses(ActionT::Goal::ConstSharedPtr goal)
236 {
237  geometry_msgs::msg::PoseStamped current_pose;
238  if (!nav2_util::getCurrentPose(
239  current_pose, *feedback_utils_.tf,
240  feedback_utils_.global_frame, feedback_utils_.robot_frame,
241  feedback_utils_.transform_tolerance))
242  {
243  bt_action_server_->setInternalError(ActionT::Result::TF_ERROR,
244  "Initial robot pose is not available.");
245  return false;
246  }
247 
248  nav_msgs::msg::Goals goals_array = goal->poses;
249  int i = 0;
250  for (auto & goal_pose : goals_array.goals) {
251  if (!nav2_util::transformPoseInTargetFrame(
252  goal_pose, goal_pose, *feedback_utils_.tf, feedback_utils_.global_frame,
253  feedback_utils_.transform_tolerance))
254  {
255  bt_action_server_->setInternalError(ActionT::Result::TF_ERROR,
256  "Failed to transform a goal pose (" + std::to_string(i) + ") provided with frame_id '" +
257  goal_pose.header.frame_id +
258  "' to the global frame '" +
259  feedback_utils_.global_frame +
260  "'.");
261  return false;
262  }
263  i++;
264  }
265 
266  if (goals_array.goals.size() > 0) {
267  RCLCPP_INFO(
268  logger_, "Begin navigating from current location through %zu poses to (%.2f, %.2f)",
269  goals_array.goals.size(), goals_array.goals.back().pose.position.x,
270  goals_array.goals.back().pose.position.y);
271  }
272 
273  // Reset state for new action feedback
274  start_time_ = clock_->now();
275  auto blackboard = bt_action_server_->getBlackboard();
276  blackboard->set("number_recoveries", 0); // NOLINT
277 
278  // Update the goal pose on the blackboard
279  blackboard->set<nav_msgs::msg::Goals>(goals_blackboard_id_,
280  std::move(goals_array));
281 
282  // Reset the waypoint states vector in the blackboard
283  std::vector<nav2_msgs::msg::WaypointStatus> waypoint_statuses(goals_array.goals.size());
284  for (size_t waypoint_index = 0 ; waypoint_index < goals_array.goals.size() ; ++waypoint_index) {
285  waypoint_statuses[waypoint_index].waypoint_index = waypoint_index;
286  waypoint_statuses[waypoint_index].waypoint_pose = goals_array.goals[waypoint_index];
287  }
288  blackboard->set<decltype(waypoint_statuses)>(waypoint_statuses_blackboard_id_,
289  std::move(waypoint_statuses));
290 
291  return true;
292 }
293 
294 } // namespace nav2_bt_navigator
295 
296 #include "pluginlib/class_list_macros.hpp"
297 PLUGINLIB_EXPORT_CLASS(
A navigator for navigating to a a bunch of intermediary poses.
bool configure(nav2::LifecycleNode::WeakPtr node, std::shared_ptr< nav2_util::OdomSmoother > odom_smoother) override
A configure state transition to configure navigator's state.
void onPreempt(ActionT::Goal::ConstSharedPtr goal) override
A callback that is called when a preempt is requested.
std::string getName() override
Get action name for this navigator.
bool goalReceived(ActionT::Goal::ConstSharedPtr goal) override
A callback to be called when a new goal is received by the BT action server Can be used to check if g...
void goalCompleted(typename ActionT::Result::SharedPtr result, const nav2_behavior_tree::BtStatus final_bt_status) override
A callback that is called when a the action is completed, can fill in action result message or indica...
bool initializeGoalPoses(ActionT::Goal::ConstSharedPtr goal)
Goal pose initialization on the blackboard.
std::string getDefaultBTFilepath(nav2::LifecycleNode::WeakPtr node) override
Get navigator's default BT.
void onLoop() override
A callback that defines execution that happens on one iteration through the BT Can be used to publish...
Navigator interface to allow navigators to be stored in a vector and accessed via pluginlib due to te...