Skip to content

Commit 54df1fa

Browse files
authored
Be able to invoke a single recovery action at a time (#1388)
Added parallel recovery behaviors
1 parent 59f9637 commit 54df1fa

File tree

9 files changed

+133
-3
lines changed

9 files changed

+133
-3
lines changed

nav2_behavior_tree/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ list(APPEND plugin_libs nav2_random_crawl_action_bt_node)
9595
add_library(nav2_pipeline_sequence_bt_node SHARED plugins/control/pipeline_sequence.cpp)
9696
list(APPEND plugin_libs nav2_pipeline_sequence_bt_node)
9797

98+
add_library(nav2_round_robin_node_bt_node SHARED plugins/control/round_robin_node.cpp)
99+
list(APPEND plugin_libs nav2_round_robin_node_bt_node)
100+
98101
foreach(bt_plugin ${plugin_libs})
99102
ament_target_dependencies(${bt_plugin} ${dependencies})
100103
target_compile_definitions(${bt_plugin} PRIVATE BT_PLUGIN_EXPORT)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 <string>
16+
17+
#include "behaviortree_cpp_v3/control_node.h"
18+
#include "behaviortree_cpp_v3/bt_factory.h"
19+
20+
namespace nav2_behavior_tree
21+
{
22+
23+
class RoundRobinNode : public BT::ControlNode
24+
{
25+
public:
26+
explicit RoundRobinNode(const std::string & name)
27+
: BT::ControlNode::ControlNode(name, {})
28+
{
29+
setRegistrationID("RoundRobin");
30+
}
31+
32+
BT::NodeStatus tick() override
33+
{
34+
const unsigned num_children = children_nodes_.size();
35+
36+
setStatus(BT::NodeStatus::RUNNING);
37+
38+
TreeNode * child_node = children_nodes_[current_child_idx_];
39+
const BT::NodeStatus child_status = child_node->executeTick();
40+
41+
switch (child_status) {
42+
case BT::NodeStatus::SUCCESS:
43+
// Wrap around to the first child
44+
if (++current_child_idx_ == num_children) {
45+
// TODO(mjeronimo): halt this child (not all children)
46+
current_child_idx_ = 0;
47+
}
48+
49+
haltChildren(0);
50+
return BT::NodeStatus::SUCCESS;
51+
52+
case BT::NodeStatus::FAILURE:
53+
haltChildren(0);
54+
return BT::NodeStatus::FAILURE;
55+
56+
case BT::NodeStatus::RUNNING:
57+
break;
58+
59+
default:
60+
throw BT::LogicError("Invalid status return from BT node");
61+
break;
62+
}
63+
64+
return BT::NodeStatus::RUNNING;
65+
}
66+
67+
void halt() override
68+
{
69+
ControlNode::halt();
70+
current_child_idx_ = 0;
71+
}
72+
73+
private:
74+
unsigned int current_child_idx_{0};
75+
};
76+
77+
} // namespace nav2_behavior_tree
78+
79+
BT_REGISTER_NODES(factory)
80+
{
81+
factory.registerNodeType<nav2_behavior_tree::RoundRobinNode>("RoundRobin");
82+
}

nav2_bringup/bringup/params/nav2_params.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ bt_navigator:
6464
- nav2_rate_controller_bt_node
6565
- nav2_recovery_node_bt_node
6666
- nav2_pipeline_sequence_bt_node
67+
- nav2_round_robin_node_bt_node
6768

6869
bt_navigator_rclcpp_node:
6970
ros__parameters:

nav2_bt_navigator/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ children till the second one succeeds. Then it will tick the first three till th
5656
third succeeds and so on, till there are no more children. This will return RUNNING,
5757
till the last child succeeds, at which time it also returns SUCCESS. If any child
5858
returns FAILURE, all nodes are halted and this node returns FAILURE.
59+
* RoundRobin: This is a custom control node introduced to the Behavior Tree. When this node is ticked, it will tick the first child until it returns SUCCESS or FAILURE. If the child returns either SUCCESS or FAILURE, it will tick its next child. Once the node reaches the last child, it will restart ticking from the first child. The main difference between the RoundRobin node versus the Sequence node is that when a child returns FAILURE the RoundRobin node will tick the next child but in the Sequence node, it will return FAILURE.
60+
5961
* Recovery: This is a control flow type node with two children. It returns success if and only if the first child returns success. The second child will be executed only if the first child returns failure. The second child is responsible for recovery actions such as re-initializing system or other recovery behaviors. If the recovery behaviors are succeeded, then the first child will be executed again. The user can specify how many times the recovery actions should be taken before returning failure. The figure below depicts a simple recovery node.
6062

6163
<img src="./doc/recovery_node.png" title="" width="40%" align="middle">
@@ -88,12 +90,17 @@ With the recovery node, simple recoverable navigation with replanning can be imp
8890

8991
This tree is currently our default tree in the stack and the xml file is located here: [navigate_w_replanning_and_recovery.xml](behavior_trees/navigate_w_replanning_and_recovery.xml).
9092

91-
## Future Work
92-
Scope-based failure handling: Utilizing Behavior Trees with a recovery node allows one to handle failures at multiple scopes. With this capability, any action in a large system can be constructed with specific recovery actions suitable for that action. Thus, failures in these actions can be handled locally within the scope. With such design, a system can be recovered at multiple levels based on the nature of the failure. Higher level recovery actions could be recovery actions such as re-initializing the system, re-calibrating the robot, bringing the system to a good known state, etc. Currently, in the navigation stack, multi-scope recovery actions are not implemented. The figure below highlights a simple multi-scope recovery handling for the navigation task.
93+
## Multi-Scope Recoveries
94+
Scope-based failure handling: Utilizing Behavior Trees with a recovery node allows one to handle failures at multiple scopes. With this capability, any action in a large system can be constructed with specific recovery actions suitable for that action. Thus, failures in these actions can be handled locally within the scope. With such design, a system can be recovered at multiple levels based on the nature of the failure. Higher level recovery actions could be recovery actions such as re-initializing the system, re-calibrating the robot, bringing the system to a good known state, etc.
95+
96+
### Navigate with replanning and simple Multi-Scope Recoveries
97+
In the navigation stack, multi-scope recovery actions are implemented for each module. Currently, the recovery actions for the Global planner are: Clear Entire Global Costmap and Wait. The recovery actions for the Local planner are: Clear Entire Local Costmap and Spin; the recovery actions for the system level is just Wait. The figure below highlights a simple multi-scope recovery handling for the navigation task. With this tree, if the Global Planner fails, the ClearEntireCostmap which is the first recovery action for this module will be ticked, then the ComputePathToPose will be ticked again. If the ComputePathToPose fails again, the Wait which is the second recovery action for this module will be ticked. After trying the second recovery action, the ComputePathToPose will be ticked again. These actions can be repeated n times until ComputePathToPose succeeds. If the ComputePathToPose fails and the Global Planner cannot be recovered locally, the higher-level recovery actions will be ticked. In this simple example, our higher-level recovery action is just a longer wait. The same strategy is applied to the Local Planner. If the Local Planner fails, the tree will first tick the ClearEntireCostmap and then if it fails again the tree will tick the Spin.
9398

94-
<img src="./doc/proposed_recovery.png" title="" width="95%" align="middle">
99+
<img src="./doc/parallel_w_round_robin_recovery.png" title="" width="95%" align="middle">
95100
<br/>
96101

102+
This tree currently is not our default tree in the stack. The xml file is located here: [navigate_w_replanning_and_round_robin_recovery.xml](behavior_trees/navigate_w_replanning_and_round_robin_recovery.xml).
103+
97104
## Open Issues
98105

99106
* **Schema definition and XML document validation** - Currently, there is no dynamic validation of incoming XML. The Behavior-Tree.CPP library is using tinyxml2, which doesn't have a validator. Instead, we can create a schema for the Mission Planning-level XML and use build-time validation of the XML input to ensure that it is well-formed and valid.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
<!--
3+
This Behavior Tree replans the global path periodically at 1 Hz and it also has
4+
recovery actions.
5+
-->
6+
<root main_tree_to_execute="MainTree">
7+
<BehaviorTree ID="MainTree">
8+
<RecoveryNode number_of_retries="2" name="NavigateRecovery">
9+
<PipelineSequence name="NavigateWithReplanning">
10+
<RateController hz="1.0">
11+
<RecoveryNode number_of_retries="4" name="ComputePathToPose">
12+
<ComputePathToPose goal="{goal}" path="{path}" planner_id="GridBased"/>
13+
<RoundRobin name="GlobalPlannerRecoveryActions">
14+
<ClearEntireCostmap service_name="global_costmap/clear_entirely_global_costmap"/>
15+
<Wait wait_duration="2"/>
16+
</RoundRobin>
17+
</RecoveryNode>
18+
</RateController>
19+
<RecoveryNode number_of_retries="4" name="FollowPath">
20+
<FollowPath path="{path}" controller_id="FollowPath"/>
21+
<RoundRobin name="PlannerRecoveryActions">
22+
<ClearEntireCostmap service_name="local_costmap/clear_entirely_local_costmap"/>
23+
<Spin spin_dist="1.57"/>
24+
</RoundRobin>
25+
</RecoveryNode>
26+
</PipelineSequence>
27+
<Wait wait_duration="5"/>
28+
</RecoveryNode>
29+
</BehaviorTree>
30+
</root>
94.9 KB
Loading

nav2_controller/src/nav2_controller.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ void ControllerServer::setPlannerPath(const nav_msgs::msg::Path & path)
260260
{
261261
RCLCPP_DEBUG(get_logger(),
262262
"Providing path to the controller %s", current_controller_);
263+
if (path.poses.empty()) {
264+
throw nav2_core::PlannerException("Invalid path, Path is empty.");
265+
}
263266
controllers_[current_controller_]->setPlan(path);
264267

265268
auto end_pose = *(path.poses.end() - 1);

tools/bt2img.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"RateController",
4040
"RecoveryNode",
4141
"PipelineSequence",
42+
"RoundRobin"
4243
]
4344
action_nodes = [
4445
"AlwaysFailure",

tools/update_bt_diagrams.bash

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ navigation2/tools/bt2img.py \
99
navigation2/tools/bt2img.py \
1010
--behavior_tree navigation2/nav2_bt_navigator/behavior_trees/navigate_w_replanning_and_recovery.xml \
1111
--image_out navigation2/nav2_bt_navigator/doc/parallel_w_recovery
12+
navigation2/tools/bt2img.py \
13+
--behavior_tree navigation2/nav2_bt_navigator/behavior_trees/navigate_w_replanning_and_round_robin_recovery.xml \
14+
--image_out navigation2/nav2_bt_navigator/doc/parallel_w_round_robin_recovery

0 commit comments

Comments
 (0)