Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
wait_at_waypoint.cpp
1 // Copyright (c) 2020 Fetullah Atas
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 "nav2_waypoint_follower/plugins/wait_at_waypoint.hpp"
16 
17 #include <string>
18 #include <exception>
19 
20 #include "pluginlib/class_list_macros.hpp"
21 
22 #include "nav2_ros_common/node_utils.hpp"
23 
24 namespace nav2_waypoint_follower
25 {
27 : waypoint_pause_duration_(0),
28  is_enabled_(true)
29 {
30 }
31 
33 {
34 }
35 
37  const nav2::LifecycleNode::WeakPtr & parent,
38  const std::string & plugin_name)
39 {
40  auto node = parent.lock();
41  if (!node) {
42  throw std::runtime_error{"Failed to lock node in wait at waypoint plugin!"};
43  }
44  logger_ = node->get_logger();
45  clock_ = node->get_clock();
46 
47  waypoint_pause_duration_ = node->declare_or_get_parameter(
48  plugin_name + ".waypoint_pause_duration", 0);
49  is_enabled_ = node->declare_or_get_parameter(
50  plugin_name + ".enabled", true);
51 
52  if (waypoint_pause_duration_ == 0) {
53  is_enabled_ = false;
54  RCLCPP_INFO(
55  logger_,
56  "Waypoint pause duration is set to zero, disabling task executor plugin.");
57  } else if (!is_enabled_) {
58  RCLCPP_INFO(
59  logger_, "Waypoint task executor plugin is disabled.");
60  }
61 }
62 
64  const geometry_msgs::msg::PoseStamped & /*curr_pose*/, const int & curr_waypoint_index)
65 {
66  if (!is_enabled_) {
67  return true;
68  }
69  RCLCPP_INFO(
70  logger_, "Arrived at %i'th waypoint, sleeping for %i milliseconds",
71  curr_waypoint_index,
72  waypoint_pause_duration_);
73  clock_->sleep_for(std::chrono::milliseconds(waypoint_pause_duration_));
74  return true;
75 }
76 } // namespace nav2_waypoint_follower
77 PLUGINLIB_EXPORT_CLASS(
Base class for creating a plugin in order to perform a specific task at waypoint arrivals.
Simple plugin based on WaypointTaskExecutor, let's robot to sleep for a specified amount of time at w...
WaitAtWaypoint()
Construct a new Wait At Waypoint Arrival object.
void initialize(const nav2::LifecycleNode::WeakPtr &parent, const std::string &plugin_name)
declares and loads parameters used (waypoint_pause_duration_)
~WaitAtWaypoint()
Destroy the Wait At Waypoint Arrival object.
bool processAtWaypoint(const geometry_msgs::msg::PoseStamped &curr_pose, const int &curr_waypoint_index)
Override this to define the body of your task that you would like to execute once the robot arrived t...