Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
is_battery_charging_condition.hpp
1 // Copyright (c) 2023 Alberto J. Tudela Roldán
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_BATTERY_CHARGING_CONDITION_HPP_
16 #define NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__IS_BATTERY_CHARGING_CONDITION_HPP_
17 
18 #include <string>
19 #include <memory>
20 #include <mutex>
21 #include <chrono>
22 
23 #include "nav2_ros_common/lifecycle_node.hpp"
24 #include "sensor_msgs/msg/battery_state.hpp"
25 #include "behaviortree_cpp/condition_node.h"
26 
27 namespace nav2_behavior_tree
28 {
29 
36 class IsBatteryChargingCondition : public BT::ConditionNode
37 {
38 public:
45  const std::string & condition_name,
46  const BT::NodeConfiguration & conf);
47 
48  IsBatteryChargingCondition() = delete;
49 
54  BT::NodeStatus tick() override;
55 
60  static BT::PortsList providedPorts()
61  {
62  return {
63  BT::InputPort<std::string>(
64  "battery_topic", std::string("/battery_status"), "Battery topic")
65  };
66  }
67 
68 private:
72  void initialize();
76  void createROSInterfaces();
77 
82  void batteryCallback(sensor_msgs::msg::BatteryState::SharedPtr msg);
83 
84  rclcpp::CallbackGroup::SharedPtr callback_group_;
85  rclcpp::executors::SingleThreadedExecutor callback_group_executor_;
86  nav2::Subscription<sensor_msgs::msg::BatteryState>::SharedPtr battery_sub_;
87  std::string battery_topic_;
88  bool is_battery_charging_;
89  std::chrono::milliseconds bt_loop_duration_;
90 };
91 
92 } // namespace nav2_behavior_tree
93 
94 #endif // NAV2_BEHAVIOR_TREE__PLUGINS__CONDITION__IS_BATTERY_CHARGING_CONDITION_HPP_
A BT::ConditionNode that listens to a battery topic and returns SUCCESS when battery is charging and ...
BT::NodeStatus tick() override
The main override required by a BT action.
static BT::PortsList providedPorts()
Creates list of BT ports.