Nav2 Navigation Stack - rolling  main
ROS 2 Navigation Stack
is_battery_charging_condition.cpp
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 #include <string>
16 
17 #include "nav2_behavior_tree/plugins/condition/is_battery_charging_condition.hpp"
18 
19 namespace nav2_behavior_tree
20 {
21 
22 IsBatteryChargingCondition::IsBatteryChargingCondition(
23  const std::string & condition_name,
24  const BT::NodeConfiguration & conf)
25 : BT::ConditionNode(condition_name, conf),
26  battery_topic_("/battery_status"),
27  is_battery_charging_(false)
28 {
29  initialize();
30  bt_loop_duration_ =
31  config().blackboard->template get<std::chrono::milliseconds>("bt_loop_duration");
32 }
33 
34 void IsBatteryChargingCondition::initialize()
35 {
36  createROSInterfaces();
37 }
38 
39 void IsBatteryChargingCondition::createROSInterfaces()
40 {
41  std::string battery_topic_new;
42  getInput("battery_topic", battery_topic_new);
43 
44  // Only create a new subscriber if the topic has changed or subscriber is empty
45  if (battery_topic_new != battery_topic_ || !battery_sub_) {
46  battery_topic_ = battery_topic_new;
47  auto node = config().blackboard->get<nav2::LifecycleNode::SharedPtr>("node");
48  callback_group_ = node->create_callback_group(
49  rclcpp::CallbackGroupType::MutuallyExclusive,
50  false);
51  callback_group_executor_.add_callback_group(callback_group_, node->get_node_base_interface());
52 
53  battery_sub_ = node->create_subscription<sensor_msgs::msg::BatteryState>(
54  battery_topic_,
55  std::bind(&IsBatteryChargingCondition::batteryCallback, this, std::placeholders::_1),
57  callback_group_);
58  }
59 }
60 
62 {
63  if (!BT::isStatusActive(status())) {
64  initialize();
65  }
66 
67  callback_group_executor_.spin_all(bt_loop_duration_);
68  if (is_battery_charging_) {
69  return BT::NodeStatus::SUCCESS;
70  }
71  return BT::NodeStatus::FAILURE;
72 }
73 
74 void IsBatteryChargingCondition::batteryCallback(sensor_msgs::msg::BatteryState::SharedPtr msg)
75 {
76  is_battery_charging_ =
77  (msg->power_supply_status == sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_CHARGING);
78 }
79 
80 } // namespace nav2_behavior_tree
81 
82 #include "behaviortree_cpp/bt_factory.h"
83 BT_REGISTER_NODES(factory)
84 {
85  factory.registerNodeType<nav2_behavior_tree::IsBatteryChargingCondition>("IsBatteryCharging");
86 }
A QoS profile for standard reliable topics with a history of 10 messages.
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.