Skip to content

Commit 1940266

Browse files
SteveMacenskiCarl Delsey
andauthored
[Continued from 1329] Provide behavior tree logging output on the console and a topic (#1486)
* Add console logging * Fix node names so every node is uniquely identified in log * Adding ros topic bt logger * update BT nav + recovery for merge issues * rename compute path -> compute path to pose * renaming to follow literature Co-authored-by: Carl Delsey <[email protected]>
1 parent a3e0344 commit 1940266

File tree

10 files changed

+137
-9
lines changed

10 files changed

+137
-9
lines changed

nav2_behavior_tree/src/behavior_tree_engine.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ BehaviorTreeEngine::run(
5151
return BtStatus::CANCELED;
5252
}
5353

54-
onLoop();
55-
5654
result = tree->root_node->executeTick();
5755

56+
onLoop();
57+
5858
loopRate.sleep();
5959
}
6060

nav2_bt_navigator/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(library_name ${executable_name}_core)
3232

3333
add_library(${library_name} SHARED
3434
src/bt_navigator.cpp
35+
src/ros_topic_logger.cpp
3536
)
3637

3738
set(dependencies

nav2_bt_navigator/behavior_trees/navigate_w_replanning_and_recovery.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
<!--
23
This Behavior Tree replans the global path periodically at 1 Hz and it also has
34
recovery actions.
@@ -9,17 +10,17 @@
910
<RateController hz="1.0">
1011
<RecoveryNode number_of_retries="1" name="ComputePathToPose">
1112
<ComputePathToPose goal="{goal}" path="{path}" planner_id="GridBased"/>
12-
<ClearEntireCostmap service_name="global_costmap/clear_entirely_global_costmap"/>
13+
<ClearEntireCostmap name="ClearGlobalCostmap-Context" service_name="global_costmap/clear_entirely_global_costmap"/>
1314
</RecoveryNode>
1415
</RateController>
1516
<RecoveryNode number_of_retries="1" name="FollowPath">
1617
<FollowPath path="{path}" controller_id="FollowPath"/>
17-
<ClearEntireCostmap service_name="local_costmap/clear_entirely_local_costmap"/>
18+
<ClearEntireCostmap name="ClearLocalCostmap-Context" service_name="local_costmap/clear_entirely_local_costmap"/>
1819
</RecoveryNode>
1920
</PipelineSequence>
2021
<SequenceStar name="RecoveryActions">
21-
<ClearEntireCostmap service_name="local_costmap/clear_entirely_local_costmap"/>
22-
<ClearEntireCostmap service_name="global_costmap/clear_entirely_global_costmap"/>
22+
<ClearEntireCostmap name="ClearLocalCostmap-Subtree" service_name="local_costmap/clear_entirely_local_costmap"/>
23+
<ClearEntireCostmap name="ClearGlobalCostmap-Subtree" service_name="global_costmap/clear_entirely_global_costmap"/>
2324
<Spin spin_dist="1.57"/>
2425
<Wait wait_duration="5"/>
2526
</SequenceStar>

nav2_bt_navigator/behavior_trees/navigate_w_replanning_and_round_robin_recovery.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
<RecoveryNode number_of_retries="4" name="ComputePathToPose">
1212
<ComputePathToPose goal="{goal}" path="{path}" planner_id="GridBased"/>
1313
<RoundRobin name="GlobalPlannerRecoveryActions">
14-
<ClearEntireCostmap service_name="global_costmap/clear_entirely_global_costmap"/>
14+
<ClearEntireCostmap name="ClearGlobalCostmap-Context" service_name="global_costmap/clear_entirely_global_costmap"/>
1515
<Wait wait_duration="2"/>
1616
</RoundRobin>
1717
</RecoveryNode>
1818
</RateController>
1919
<RecoveryNode number_of_retries="4" name="FollowPath">
2020
<FollowPath path="{path}" controller_id="FollowPath"/>
2121
<RoundRobin name="PlannerRecoveryActions">
22-
<ClearEntireCostmap service_name="local_costmap/clear_entirely_local_costmap"/>
22+
<ClearEntireCostmap name="ClearLocalCostmap-Context" service_name="local_costmap/clear_entirely_local_costmap"/>
2323
<Spin spin_dist="1.57"/>
2424
</RoundRobin>
2525
</RecoveryNode>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_BT_NAVIGATOR__ROS_TOPIC_LOGGER_HPP_
16+
#define NAV2_BT_NAVIGATOR__ROS_TOPIC_LOGGER_HPP_
17+
18+
#include <vector>
19+
#include "behaviortree_cpp_v3/loggers/abstract_logger.h"
20+
#include "rclcpp/rclcpp.hpp"
21+
#include "nav2_msgs/msg/behavior_tree_log.hpp"
22+
#include "nav2_msgs/msg/behavior_tree_status_change.h"
23+
24+
namespace nav2_bt_navigator
25+
{
26+
27+
class RosTopicLogger : public BT::StatusChangeLogger
28+
{
29+
public:
30+
RosTopicLogger(const rclcpp::Node::SharedPtr & ros_node, const BT::Tree & tree);
31+
32+
void callback(
33+
BT::Duration timestamp,
34+
const BT::TreeNode & node,
35+
BT::NodeStatus prev_status,
36+
BT::NodeStatus status) override;
37+
38+
void flush() override;
39+
40+
protected:
41+
rclcpp::Node::SharedPtr ros_node_;
42+
rclcpp::Publisher<nav2_msgs::msg::BehaviorTreeLog>::SharedPtr log_pub_;
43+
std::vector<nav2_msgs::msg::BehaviorTreeStatusChange> event_log_;
44+
};
45+
46+
} // namespace nav2_bt_navigator
47+
48+
#endif // NAV2_BT_NAVIGATOR__ROS_TOPIC_LOGGER_HPP_

nav2_bt_navigator/src/bt_navigator.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <vector>
2323

2424
#include "nav2_behavior_tree/bt_conversions.hpp"
25+
#include "nav2_bt_navigator/ros_topic_logger.hpp"
2526

2627
namespace nav2_bt_navigator
2728
{
@@ -206,12 +207,15 @@ BtNavigator::navigateToPose()
206207
return action_server_->is_cancel_requested();
207208
};
208209

209-
auto on_loop = [this]() {
210+
RosTopicLogger topic_logger(client_node_, *tree_);
211+
212+
auto on_loop = [&]() {
210213
if (action_server_->is_preempt_requested()) {
211214
RCLCPP_INFO(get_logger(), "Received goal preemption request");
212215
action_server_->accept_pending_goal();
213216
initializeGoalPose();
214217
}
218+
topic_logger.flush();
215219
};
216220

217221
// Execute the BT that was previously created in the configure step
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
#include <utility>
16+
#include "nav2_bt_navigator/ros_topic_logger.hpp"
17+
#include "tf2_ros/buffer_interface.h"
18+
19+
namespace nav2_bt_navigator
20+
{
21+
22+
RosTopicLogger::RosTopicLogger(
23+
const rclcpp::Node::SharedPtr & ros_node, const BT::Tree & tree)
24+
: StatusChangeLogger(tree.root_node), ros_node_(ros_node)
25+
{
26+
log_pub_ = ros_node_->create_publisher<nav2_msgs::msg::BehaviorTreeLog>(
27+
"behavior_tree_log",
28+
rclcpp::QoS(10));
29+
}
30+
31+
void RosTopicLogger::callback(
32+
BT::Duration timestamp,
33+
const BT::TreeNode & node,
34+
BT::NodeStatus prev_status,
35+
BT::NodeStatus status)
36+
{
37+
nav2_msgs::msg::BehaviorTreeStatusChange event;
38+
39+
// BT timestamps are a duration since the epoch. Need to convert to a time_point
40+
// before converting to a msg.
41+
event.timestamp =
42+
tf2_ros::toMsg(std::chrono::time_point<std::chrono::high_resolution_clock>(timestamp));
43+
event.node_name = node.name();
44+
event.previous_status = toStr(prev_status, false);
45+
event.current_status = toStr(status, false);
46+
event_log_.push_back(std::move(event));
47+
48+
RCLCPP_DEBUG(ros_node_->get_logger(), "[%.3f]: %25s %s -> %s",
49+
std::chrono::duration<double>(timestamp).count(),
50+
node.name().c_str(),
51+
toStr(prev_status, true).c_str(),
52+
toStr(status, true).c_str() );
53+
}
54+
55+
void RosTopicLogger::flush()
56+
{
57+
if (event_log_.size() > 0) {
58+
nav2_msgs::msg::BehaviorTreeLog log_msg;
59+
log_msg.timestamp = ros_node_->now();
60+
log_msg.event_log = event_log_;
61+
log_pub_->publish(log_msg);
62+
event_log_.clear();
63+
}
64+
}
65+
66+
} // namespace nav2_bt_navigator

nav2_msgs/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ rosidl_generate_interfaces(${PROJECT_NAME}
1616
"msg/Costmap.msg"
1717
"msg/CostmapMetaData.msg"
1818
"msg/VoxelGrid.msg"
19+
"msg/BehaviorTreeStatusChange.msg"
20+
"msg/BehaviorTreeLog.msg"
1921
"srv/GetCostmap.srv"
2022
"srv/ClearCostmapExceptRegion.srv"
2123
"srv/ClearCostmapAroundRobot.srv"

nav2_msgs/msg/BehaviorTreeLog.msg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
builtin_interfaces/Time timestamp # ROS time that this log message was sent.
2+
BehaviorTreeStatusChange[] event_log
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
builtin_interfaces/Time timestamp # internal behavior tree event timestamp. Typically this is wall clock time
2+
string node_name
3+
string previous_status # IDLE, RUNNING, SUCCESS or FAILURE
4+
string current_status # IDLE, RUNNING, SUCCESS or FAILURE

0 commit comments

Comments
 (0)