Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
is_battery_low_condition.hpp
1 // Copyright (c) 2020 Sarthak Mittal
2 // Copyright (c) 2019 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__IS_BATTERY_LOW_CONDITION_HPP_
17 #define NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__IS_BATTERY_LOW_CONDITION_HPP_
18 
19 #include <string>
20 #include <memory>
21 #include <mutex>
22 #include <chrono>
23 
24 #include "nav2_ros_common/lifecycle_node.hpp"
25 #include "sensor_msgs/msg/battery_state.hpp"
26 #include "behaviortree_cpp/condition_node.h"
27 
28 namespace nav2_behavior_tree
29 {
30 
41 class IsBatteryLowCondition : public BT::ConditionNode
42 {
43 public:
50  const std::string & condition_name,
51  const BT::NodeConfiguration & conf);
52 
53  IsBatteryLowCondition() = delete;
54 
59  BT::NodeStatus tick() override;
60 
64  void initialize();
65 
69  void createROSInterfaces();
70 
75  static BT::PortsList providedPorts()
76  {
77  return {
78  BT::InputPort<double>("min_battery", "Minimum battery percentage/voltage"),
79  BT::InputPort<std::string>(
80  "battery_topic", "/battery_status", "Battery topic"),
81  BT::InputPort<bool>(
82  "is_voltage", false, "If true voltage will be used to check for low battery"),
83  };
84  }
85 
86 private:
91  void batteryCallback(const sensor_msgs::msg::BatteryState::ConstSharedPtr & msg);
92 
93  nav2::LifecycleNode::SharedPtr node_;
94  rclcpp::CallbackGroup::SharedPtr callback_group_;
95  rclcpp::executors::SingleThreadedExecutor callback_group_executor_;
96  nav2::Subscription<sensor_msgs::msg::BatteryState>::SharedPtr battery_sub_;
97  std::string battery_topic_;
98  double min_battery_;
99  bool is_voltage_;
100  bool is_battery_low_;
101  std::chrono::milliseconds bt_loop_duration_;
102 };
103 
104 } // namespace nav2_behavior_tree
105 
106 #endif // NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__IS_BATTERY_LOW_CONDITION_HPP_
A BT::ConditionNode that listens to a battery topic and returns SUCCESS when battery is low and FAILU...
void createROSInterfaces()
Function to create ROS interfaces.
static BT::PortsList providedPorts()
Creates list of BT ports.
BT::NodeStatus tick() override
The main override required by a BT action.
void initialize()
Function to read parameters and initialize class variables.