Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
is_stopped_condition.cpp
1 // Copyright (c) 2024 Angsa Robotics
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 <chrono>
17 
18 #include "nav2_behavior_tree/plugins/condition/is_stopped_condition.hpp"
19 
20 namespace nav2_behavior_tree
21 {
22 
23 IsStoppedCondition::IsStoppedCondition(
24  const std::string & condition_name,
25  const BT::NodeConfiguration & conf)
26 : BT::ConditionNode(condition_name, conf),
27  velocity_threshold_(0.01),
28  duration_stopped_(1000ms),
29  stopped_stamp_(rclcpp::Time(0, 0, RCL_ROS_TIME))
30 {
31  node_ = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
32  odom_smoother_ = config().blackboard->get<std::shared_ptr<nav2_util::OdomSmoother>>(
33  "odom_smoother");
34 }
35 
37 {
38  RCLCPP_DEBUG(node_->get_logger(), "Shutting down IsStoppedCondition BT node");
39 }
40 
41 BT::NodeStatus IsStoppedCondition::tick()
42 {
43  getInput("velocity_threshold", velocity_threshold_);
44  getInput("duration_stopped", duration_stopped_);
45 
46  auto twist = odom_smoother_->getRawTwistStamped();
47 
48  // if there is no timestamp, set it to now
49  if (twist.header.stamp.sec == 0 && twist.header.stamp.nanosec == 0) {
50  twist.header.stamp = node_->get_clock()->now();
51  }
52 
53  if (abs(twist.twist.linear.x) < velocity_threshold_ &&
54  abs(twist.twist.linear.y) < velocity_threshold_ &&
55  abs(twist.twist.angular.z) < velocity_threshold_)
56  {
57  if (stopped_stamp_ == rclcpp::Time(0, 0, RCL_ROS_TIME)) {
58  stopped_stamp_ = rclcpp::Time(twist.header.stamp);
59  }
60 
61  if (node_->get_clock()->now() - stopped_stamp_ > rclcpp::Duration(duration_stopped_)) {
62  stopped_stamp_ = rclcpp::Time(0, 0, RCL_ROS_TIME);
63  return BT::NodeStatus::SUCCESS;
64  } else {
65  return BT::NodeStatus::RUNNING;
66  }
67 
68  } else {
69  stopped_stamp_ = rclcpp::Time(0, 0, RCL_ROS_TIME);
70  return BT::NodeStatus::FAILURE;
71  }
72 }
73 
74 } // namespace nav2_behavior_tree
75 
76 #include "behaviortree_cpp/bt_factory.h"
77 BT_REGISTER_NODES(factory)
78 {
79  factory.registerNodeType<nav2_behavior_tree::IsStoppedCondition>("IsStopped");
80 }
A BT::ConditionNode that tracks robot odometry and returns SUCCESS if robot is considered stopped for...
~IsStoppedCondition() override
A destructor for nav2_behavior_tree::IsStoppedCondition.
BT::NodeStatus tick() override
The main override required by a BT action.