Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
check_stop_status_action.cpp
1 // Copyright (c) 2025 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 <string>
16 #include <memory>
17 #include <limits>
18 
19 #include "nav2_util/geometry_utils.hpp"
20 
21 #include "nav2_behavior_tree/plugins/action/check_stop_status_action.hpp"
22 
23 namespace nav2_behavior_tree
24 {
25 
27  const std::string & name,
28  const BT::NodeConfiguration & conf)
29 : BT::ActionNodeBase(name, conf),
30  velocity_threshold_(0.01),
31  duration_stopped_(1000ms),
32  stopped_stamp_(rclcpp::Time(0, 0, RCL_ROS_TIME))
33 {
34  node_ = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
35  odom_smoother_ = config().blackboard->get<std::shared_ptr<nav2_util::OdomSmoother>>(
36  "odom_smoother");
37 }
38 
39 inline BT::NodeStatus CheckStopStatus::tick()
40 {
41  getInput("velocity_threshold", velocity_threshold_);
42  getInput("duration_stopped", duration_stopped_);
43 
44  auto twist = odom_smoother_->getRawTwistStamped();
45 
46  // if there is no timestamp, set it to now
47  if (twist.header.stamp.sec == 0 && twist.header.stamp.nanosec == 0) {
48  twist.header.stamp = node_->get_clock()->now();
49  }
50 
51  if (abs(twist.twist.linear.x) < velocity_threshold_ &&
52  abs(twist.twist.linear.y) < velocity_threshold_ &&
53  abs(twist.twist.angular.z) < velocity_threshold_)
54  {
55  if (stopped_stamp_ == rclcpp::Time(0, 0, RCL_ROS_TIME)) {
56  stopped_stamp_ = rclcpp::Time(twist.header.stamp);
57  }
58 
59  if (node_->get_clock()->now() - stopped_stamp_ > rclcpp::Duration(duration_stopped_)) {
60  stopped_stamp_ = rclcpp::Time(0, 0, RCL_ROS_TIME);
61  return BT::NodeStatus::SUCCESS;
62  } else {
63  return BT::NodeStatus::RUNNING;
64  }
65 
66  } else {
67  stopped_stamp_ = rclcpp::Time(0, 0, RCL_ROS_TIME);
68  return BT::NodeStatus::FAILURE;
69  }
70 }
71 
72 } // namespace nav2_behavior_tree
73 
74 #include "behaviortree_cpp/bt_factory.h"
75 BT_REGISTER_NODES(factory)
76 {
77  factory.registerNodeType<nav2_behavior_tree::CheckStopStatus>(
78  "CheckStopStatus");
79 }
A nav2_behavior_tree::ActionNodeBase class that checks if the robot has been stopped for a specified ...
CheckStopStatus(const std::string &xml_tag_name, const BT::NodeConfiguration &conf)
A constructor for nav2_behavior_tree::CheckStopStatus.