Skip to content

Commit 87c50d2

Browse files
tropapparMicha Sende
andauthored
add(behaviortree_ros2): logger using RCLCPP_DEBUG (#100)
Co-authored-by: Micha Sende <sende@lakeside-labs.com>
1 parent 623ba15 commit 87c50d2

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

behaviortree_ros2/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ generate_parameter_library(
2727
add_library(${PROJECT_NAME}
2828
src/bt_ros2.cpp
2929
src/bt_utils.cpp
30-
src/tree_execution_server.cpp )
30+
src/tree_execution_server.cpp
31+
src/bt_ros_logger.cpp )
3132

3233
target_include_directories(${PROJECT_NAME} PUBLIC
3334
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef BT_ROS_LOGGER_H
2+
#define BT_ROS_LOGGER_H
3+
4+
#include <cstring>
5+
#include <rclcpp/executors.hpp>
6+
#include <rclcpp/allocator/allocator_common.hpp>
7+
#include <rclcpp/logging.hpp>
8+
#include "behaviortree_cpp/loggers/abstract_logger.h"
9+
10+
namespace BT
11+
{
12+
/**
13+
* @brief RosLogger is a very simple logger that
14+
* displays all the transitions on the console.
15+
*/
16+
17+
class RosLogger : public StatusChangeLogger
18+
{
19+
public:
20+
RosLogger(const BT::Tree& tree, std::shared_ptr<rclcpp::Node> node);
21+
~RosLogger() override;
22+
23+
virtual void flush() override;
24+
25+
private:
26+
virtual void callback(Duration timestamp, const TreeNode& node, NodeStatus prev_status,
27+
NodeStatus status) override;
28+
std::weak_ptr<rclcpp::Node> node_;
29+
};
30+
} // namespace BT
31+
32+
#endif // BT_ROS_LOGGER_H
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "behaviortree_ros2/bt_ros_logger.hpp"
2+
3+
namespace BT
4+
{
5+
6+
RosLogger::RosLogger(const BT::Tree& tree, std::shared_ptr<rclcpp::Node> node) : StatusChangeLogger(tree.rootNode()), node_(node)
7+
{}
8+
RosLogger::~RosLogger()
9+
{}
10+
11+
void RosLogger::callback(Duration timestamp, const TreeNode& node,
12+
NodeStatus prev_status, NodeStatus status)
13+
{
14+
using namespace std::chrono;
15+
16+
// get ros node pointer
17+
auto ros_node = node_.lock();
18+
19+
if (ros_node) {
20+
21+
constexpr const char* whitespaces = " ";
22+
constexpr const size_t ws_count = 25;
23+
24+
double since_epoch = duration<double>(timestamp).count();
25+
26+
RCLCPP_DEBUG(
27+
ros_node->get_logger(), "[%.3f]: %s%s %s -> %s",
28+
since_epoch, node.name().c_str(),
29+
&whitespaces[std::min(ws_count, node.name().size())],
30+
toStr(prev_status, true).c_str(), toStr(status, true).c_str());
31+
}
32+
}
33+
34+
void RosLogger::flush()
35+
{
36+
}
37+
38+
} // namespace BT

0 commit comments

Comments
 (0)