Nav2 Navigation Stack - kilted  kilted
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 
31  // Spin multiple times due to rclcpp regression in Jazzy requiring a 'warm up' spin
32  callback_group_executor_.spin_some(std::chrono::nanoseconds(1));
33 }
34 
35 void IsBatteryChargingCondition::initialize()
36 {
37  createROSInterfaces();
38 }
39 
40 void IsBatteryChargingCondition::createROSInterfaces()
41 {
42  std::string battery_topic_new;
43  getInput("battery_topic", battery_topic_new);
44 
45  // Only create a new subscriber if the topic has changed or subscriber is empty
46  if (battery_topic_new != battery_topic_ || !battery_sub_) {
47  battery_topic_ = battery_topic_new;
48  auto node = config().blackboard->get<rclcpp::Node::SharedPtr>("node");
49  callback_group_ = node->create_callback_group(
50  rclcpp::CallbackGroupType::MutuallyExclusive,
51  false);
52  callback_group_executor_.add_callback_group(callback_group_, node->get_node_base_interface());
53 
54  rclcpp::SubscriptionOptions sub_option;
55  sub_option.callback_group = callback_group_;
56  battery_sub_ = node->create_subscription<sensor_msgs::msg::BatteryState>(
57  battery_topic_,
58  rclcpp::SystemDefaultsQoS(),
59  std::bind(&IsBatteryChargingCondition::batteryCallback, this, std::placeholders::_1),
60  sub_option);
61  }
62 }
63 
65 {
66  if (!BT::isStatusActive(status())) {
67  initialize();
68  }
69 
70  callback_group_executor_.spin_some();
71  if (is_battery_charging_) {
72  return BT::NodeStatus::SUCCESS;
73  }
74  return BT::NodeStatus::FAILURE;
75 }
76 
77 void IsBatteryChargingCondition::batteryCallback(sensor_msgs::msg::BatteryState::SharedPtr msg)
78 {
79  is_battery_charging_ =
80  (msg->power_supply_status == sensor_msgs::msg::BatteryState::POWER_SUPPLY_STATUS_CHARGING);
81 }
82 
83 } // namespace nav2_behavior_tree
84 
85 #include "behaviortree_cpp/bt_factory.h"
86 BT_REGISTER_NODES(factory)
87 {
88  factory.registerNodeType<nav2_behavior_tree::IsBatteryChargingCondition>("IsBatteryCharging");
89 }
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.