Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
is_stuck_condition.hpp
1 // Copyright (c) 2018 Intel Corporation
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 #ifndef NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__IS_STUCK_CONDITION_HPP_
16 #define NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__IS_STUCK_CONDITION_HPP_
17 
18 #include <string>
19 #include <atomic>
20 #include <deque>
21 
22 #include "nav2_ros_common/lifecycle_node.hpp"
23 #include "behaviortree_cpp/condition_node.h"
24 #include "nav_msgs/msg/odometry.hpp"
25 
26 namespace nav2_behavior_tree
27 {
28 
38 class IsStuckCondition : public BT::ConditionNode
39 {
40 public:
47  const std::string & condition_name,
48  const BT::NodeConfiguration & conf);
49 
50  IsStuckCondition() = delete;
51 
55  ~IsStuckCondition() override;
56 
61  void onOdomReceived(const nav_msgs::msg::Odometry::ConstSharedPtr & msg);
62 
67  BT::NodeStatus tick() override;
68 
72  void logStuck(const std::string & msg) const;
73 
77  void updateStates();
78 
83  bool isStuck();
84 
89  static BT::PortsList providedPorts() {return {};}
90 
91 private:
92  // The node that will be used for any ROS operations
93  nav2::LifecycleNode::SharedPtr node_;
94  rclcpp::CallbackGroup::SharedPtr callback_group_;
95  rclcpp::executors::SingleThreadedExecutor callback_group_executor_;
96  std::thread callback_group_executor_thread;
97 
98  std::atomic<bool> is_stuck_;
99 
100  // Listen to odometry
101  nav2::Subscription<nav_msgs::msg::Odometry>::SharedPtr odom_sub_;
102  // Store history of odometry measurements
103  std::deque<nav_msgs::msg::Odometry> odom_history_;
104  std::deque<nav_msgs::msg::Odometry>::size_type odom_history_size_;
105 
106  // Calculated states
107  double current_accel_;
108 
109  // Robot specific parameters
110  double brake_accel_limit_;
111 };
112 
113 } // namespace nav2_behavior_tree
114 
115 #endif // NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__IS_STUCK_CONDITION_HPP_
A BT::ConditionNode that tracks robot odometry and returns SUCCESS if robot is stuck somewhere and FA...
void updateStates()
Function to approximate acceleration from the odom history.
static BT::PortsList providedPorts()
Creates list of BT ports.
void onOdomReceived(const nav_msgs::msg::Odometry::ConstSharedPtr &msg)
Callback function for odom topic.
bool isStuck()
Detect if robot bumped into something by checking for abnormal deceleration.
~IsStuckCondition() override
A destructor for nav2_behavior_tree::IsStuckCondition.
BT::NodeStatus tick() override
The main override required by a BT action.
void logStuck(const std::string &msg) const
Function to log status when robot is stuck/free.