Skip to content

Commit 7dba541

Browse files
author
Davide Faconti
committed
simple logged added
1 parent 368ee7c commit 7dba541

File tree

10 files changed

+231
-0
lines changed

10 files changed

+231
-0
lines changed

CMakeLists.txt

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
cmake_minimum_required(VERSION 2.8.12) # version on Ubuntu Trusty
2+
project(behaviortree_ros)
3+
4+
if(NOT CMAKE_VERSION VERSION_LESS 3.1)
5+
set(CMAKE_CXX_STANDARD 11)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
else()
8+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
9+
endif()
10+
11+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
12+
13+
######################################################
14+
15+
set(ROS_DEPENDENCIES roscpp std_msgs message_generation behaviortree_cpp)
16+
17+
find_package(catkin REQUIRED COMPONENTS ${ROS_DEPENDENCIES} )
18+
find_package(GTest)
19+
20+
add_message_files(
21+
FILES
22+
BehaviorTree.msg
23+
NodeParameter.msg
24+
NodeStatus.msg
25+
StatusChange.msg
26+
StatusChangeLog.msg
27+
TreeNode.msg
28+
)
29+
30+
generate_messages(
31+
DEPENDENCIES
32+
std_msgs
33+
)
34+
35+
catkin_package(
36+
INCLUDE_DIRS include
37+
LIBRARIES
38+
CATKIN_DEPENDS ${ROS_DEPENDENCIES}
39+
)
40+
41+
include_directories( include ${catkin_INCLUDE_DIRS})
42+
43+
44+
######################################################
45+
# LIBRARIES
46+
47+
add_library(behaviortree_ros
48+
src/loggers/rosout_logger.cpp )
49+
50+
target_link_libraries(behaviortree_ros ${catkin_LIBRARIES})
51+
52+
######################################################
53+
# TESTS
54+
55+
56+
######################################################
57+
# INSTALL
58+
59+
60+
######################################################
61+
# EXAMPLES and TOOLS
62+
63+
64+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef BT_ROSOUT_LOGGER_H
2+
#define BT_ROSOUT_LOGGER_H
3+
4+
#include <behaviortree_cpp/loggers/abstract_logger.h>
5+
#include <ros/console.h>
6+
7+
namespace BT
8+
{
9+
10+
class RosoutLogger : public StatusChangeLogger
11+
{
12+
static std::atomic<bool> ref_count;
13+
14+
public:
15+
RosoutLogger(TreeNode* root_node,
16+
ros::console::Level verbosity_level = ros::console::Level::Info);
17+
18+
ros::console::Level getLevel() const;
19+
20+
// Accepts only Info and Debug
21+
void setLevel(ros::console::Level level);
22+
23+
~RosoutLogger() override;
24+
25+
virtual void callback(Duration timestamp,
26+
const TreeNode& node,
27+
NodeStatus prev_status,
28+
NodeStatus status) override;
29+
30+
virtual void flush() override;
31+
32+
private:
33+
ros::console::Level _level;
34+
};
35+
36+
} // end namespace
37+
38+
#endif //BT_ROSOUT_LOGGER_H

msg/BehaviorTree.msg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
uint16 root_uid
2+
TreeNode[] nodes

msg/NodeParameter.msg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
string key
2+
string value

msg/NodeStatus.msg

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
int8 IDLE = 0
2+
int8 RUNNING = 1
3+
int8 SUCCESS = 2
4+
int8 FAILURE = 3
5+
6+
7+
int8 value

msg/StatusChange.msg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
uint16 uid
2+
NodeStatus prev_status
3+
NodeStatus status
4+
time timestamp

msg/StatusChangeLog.msg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
BehaviorTree behavior_tree
3+
StatusChange[] state_changes

msg/TreeNode.msg

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
uint16 uid
2+
uint16[] children_uid
3+
int8 type
4+
NodeStatus status
5+
string instance_name
6+
string registration_name
7+
NodeParameter params
8+
9+
10+
int8 UNDEFINED = 0
11+
int8 ACTION = 1
12+
int8 CONDITION = 2
13+
int8 CONTROL = 3
14+
int8 DECORATOR = 4
15+
int8 SUBTREE = 5

package.xml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<package>
2+
<name>behaviortree_ros</name>
3+
<version>0.0.1</version>
4+
<description>
5+
This package provides usefull utilities on top of BehaviorTree.CPP.
6+
</description>
7+
8+
<maintainer email="[email protected]">Davide Faconti</maintainer>
9+
10+
<license>MIT</license>
11+
12+
<author>Davide Faconti</author>
13+
14+
<build_depend>roscpp</build_depend>
15+
<build_depend>std_msgs</build_depend>
16+
<build_depend>behaviortree_cpp</build_depend>
17+
18+
<run_depend>roscpp</run_depend>
19+
<run_depend>std_msgs</run_depend>
20+
<run_depend>behaviortree_cpp</run_depend>
21+
22+
<build_depend>message_generation</build_depend>
23+
<run_depend>message_generation</run_depend>
24+
25+
<buildtool_depend>catkin</buildtool_depend>
26+
27+
</package>

src/loggers/rosout_logger.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "behaviortree_ros/loggers/rosout_logger.h"
2+
3+
namespace BT
4+
{
5+
std::atomic<bool> RosoutLogger::ref_count(false);
6+
7+
RosoutLogger::RosoutLogger(TreeNode* root_node, ros::console::Level verbosity_level) :
8+
StatusChangeLogger(root_node),
9+
_level(verbosity_level)
10+
{
11+
bool expected = false;
12+
if (!ref_count.compare_exchange_strong(expected, true))
13+
{
14+
throw std::logic_error("Only a single instance of RosoutLogger shall be created");
15+
}
16+
}
17+
18+
ros::console::Level RosoutLogger::getLevel() const
19+
{
20+
return _level;
21+
}
22+
23+
void RosoutLogger::setLevel(ros::console::Level level)
24+
{
25+
if( level != ros::console::Level::Debug &&
26+
level != ros::console::Level::Info )
27+
{
28+
throw std::invalid_argument("RosoutLogger::setLevel acepts only Debug or Info");
29+
}
30+
_level = level;
31+
}
32+
33+
RosoutLogger::~RosoutLogger()
34+
{
35+
ref_count.store(false);
36+
}
37+
38+
void RosoutLogger::callback(Duration timestamp, const TreeNode& node, NodeStatus prev_status,
39+
NodeStatus status)
40+
{
41+
constexpr const char* whitespaces = " ";
42+
const size_t ws_count = strlen(whitespaces)-1;
43+
44+
const auto& node_name = node.name();
45+
46+
switch( _level )
47+
{
48+
case ros::console::Level::Debug :
49+
ROS_DEBUG("[%s%s]: %s -> %s", node_name.c_str(),
50+
&whitespaces[std::min(ws_count, node_name.size())],
51+
toStr(prev_status, true),
52+
toStr(status, true));
53+
break;
54+
55+
case ros::console::Level::Info :
56+
ROS_INFO("[%s%s]: %s -> %s", node_name.c_str(),
57+
&whitespaces[std::min(ws_count, node_name.size())],
58+
toStr(prev_status, true),
59+
toStr(status, true));
60+
break;
61+
}
62+
}
63+
64+
void RosoutLogger::flush()
65+
{
66+
67+
}
68+
69+
} // end namespace

0 commit comments

Comments
 (0)