Nav2 Navigation Stack - lyrical  lyrical
ROS 2 Navigation Stack
ros_topic_logger.hpp
1 // Copyright (c) 2019 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__ROS_TOPIC_LOGGER_HPP_
16 #define NAV2_BEHAVIOR_TREE__ROS_TOPIC_LOGGER_HPP_
17 
18 #include <vector>
19 #include <memory>
20 #include <utility>
21 #include <string>
22 
23 #include "behaviortree_cpp/loggers/abstract_logger.h"
24 #include "rclcpp/rclcpp.hpp"
25 #include "nav2_msgs/msg/behavior_tree_log.hpp"
26 #include "nav2_msgs/msg/behavior_tree_status_change.hpp"
27 #include "tf2/time.hpp"
28 #include "tf2_ros/buffer_interface.hpp"
29 #include "nav2_ros_common/interface_factories.hpp"
30 
31 namespace nav2_behavior_tree
32 {
33 
37 class RosTopicLogger : public BT::StatusChangeLogger
38 {
39 public:
47  const nav2::LifecycleNode::WeakPtr & ros_node,
48  const BT::Tree & tree,
49  bool log_idle = true)
50  : StatusChangeLogger(tree.rootNode())
51  {
52  auto node = ros_node.lock();
53  clock_ = node->get_clock();
54  logger_ = node->get_logger().get_child("ros_topic_logger");
55  log_pub_ = nav2::interfaces::create_publisher<nav2_msgs::msg::BehaviorTreeLog>(
56  node, "behavior_tree_log");
57  log_pub_->on_activate();
58  enableTransitionToIdle(log_idle);
59  }
60 
68  void callback(
69  BT::Duration timestamp,
70  const BT::TreeNode & node,
71  BT::NodeStatus prev_status,
72  BT::NodeStatus status) override
73  {
74  nav2_msgs::msg::BehaviorTreeStatusChange event;
75 
76  // BT timestamps are a duration since the epoch. Need to convert to a time_point
77  // before converting to a msg.
78  event.timestamp = tf2_ros::toMsg(tf2::TimePoint(timestamp));
79  event.node_name = node.name();
80  event.uid = node.UID();
81  event.previous_status = toStr(prev_status, false);
82  event.current_status = toStr(status, false);
83  event_log_.push_back(std::move(event));
84 
85  auto prev_pad = std::string(kStatusWidth - toStr(prev_status, false).size(), ' ');
86  auto curr_pad = std::string(kStatusWidth - toStr(status, false).size(), ' ');
87  RCLCPP_DEBUG(
88  logger_, "[%.3f]: %s%s -> %s%s %s",
89  std::chrono::duration<double>(timestamp).count(),
90  toStr(prev_status, true).c_str(), prev_pad.c_str(),
91  toStr(status, true).c_str(), curr_pad.c_str(),
92  node.name().c_str() );
93  }
94 
98  void flush() override
99  {
100  if (!event_log_.empty()) {
101  auto log_msg = std::make_unique<nav2_msgs::msg::BehaviorTreeLog>();
102  log_msg->timestamp = clock_->now();
103  log_msg->event_log = event_log_;
104  log_pub_->publish(std::move(log_msg));
105  event_log_.clear();
106  }
107  }
108 
109 protected:
110  // Longest BT status string is 7 chars (RUNNING, SUCCESS, FAILURE); pad shorter ones (IDLE)
111  static constexpr size_t kStatusWidth = 7;
112  rclcpp::Clock::SharedPtr clock_;
113  rclcpp::Logger logger_{rclcpp::get_logger("bt_navigator")};
114  nav2::Publisher<nav2_msgs::msg::BehaviorTreeLog>::SharedPtr log_pub_;
115  std::vector<nav2_msgs::msg::BehaviorTreeStatusChange> event_log_;
116 };
117 
118 } // namespace nav2_behavior_tree
119 
120 #endif // NAV2_BEHAVIOR_TREE__ROS_TOPIC_LOGGER_HPP_
A class to publish BT logs on BT status change.
RosTopicLogger(const nav2::LifecycleNode::WeakPtr &ros_node, const BT::Tree &tree, bool log_idle=true)
A constructor for nav2_behavior_tree::RosTopicLogger.
void flush() override
Clear log buffer if any.
void callback(BT::Duration timestamp, const BT::TreeNode &node, BT::NodeStatus prev_status, BT::NodeStatus status) override
Callback function which is called each time BT changes status.