Skip to content

Commit d438a6e

Browse files
committed
Add pause and seq with bb memory BT nodes
1 parent 7f561b0 commit d438a6e

File tree

6 files changed

+555
-0
lines changed

6 files changed

+555
-0
lines changed

nav2_behavior_tree/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ list(APPEND plugin_libs nav2_pipeline_sequence_bt_node)
212212
add_library(nav2_round_robin_node_bt_node SHARED plugins/control/round_robin_node.cpp)
213213
list(APPEND plugin_libs nav2_round_robin_node_bt_node)
214214

215+
add_library(nav2_pause_bt_node SHARED plugins/control/pause.cpp)
216+
list(APPEND plugin_libs nav2_pause_bt_node)
217+
218+
add_library(nav2_sequence_with_blackboard_memory_bt_node SHARED plugins/control/sequence_with_blackboard_memory.cpp)
219+
list(APPEND plugin_libs nav2_sequence_with_blackboard_memory_bt_node)
220+
215221
add_library(nav2_single_trigger_bt_node SHARED plugins/decorator/single_trigger_node.cpp)
216222
list(APPEND plugin_libs nav2_single_trigger_bt_node)
217223

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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__PLUGINS__CONTROL__PAUSE_HPP_
16+
#define NAV2_BEHAVIOR_TREE__PLUGINS__CONTROL__PAUSE_HPP_
17+
18+
// Other includes
19+
#include <string>
20+
#include <memory>
21+
#include <mutex>
22+
23+
// ROS includes
24+
#include "rclcpp/rclcpp.hpp"
25+
#include "behaviortree_cpp/control_node.h"
26+
27+
// Interface definitions
28+
#include "std_srvs/srv/trigger.hpp"
29+
30+
31+
namespace nav2_behavior_tree
32+
{
33+
34+
using Trigger = std_srvs::srv::Trigger;
35+
36+
enum state_t {UNPAUSED, PAUSED, PAUSE_REQUESTED, ON_PAUSE, RESUME_REQUESTED, ON_RESUME};
37+
38+
/* @brief Controlled through service calls to pause and resume the execution of the tree
39+
* It has one mandatory child for the UNPAUSED, and three optional for the PAUSED state,
40+
* the ON_PAUSE event and the ON_RESUME event.
41+
* It has two input ports:
42+
* - pause_service_name: name of the service to pause
43+
* - resume_service_name: name of the service to resume
44+
*/
45+
class Pause : public BT::ControlNode
46+
{
47+
public:
48+
//! @brief Constructor
49+
Pause(
50+
const std::string & xml_tag_name,
51+
const BT::NodeConfiguration & conf);
52+
53+
//! @brief Destructor
54+
~Pause();
55+
56+
//! @brief Reset state and go to Idle
57+
void halt() override;
58+
59+
//! @brief Return a NodeStatus according to the children's status
60+
BT::NodeStatus tick() override;
61+
62+
//! @brief Declare ports
63+
static BT::PortsList providedPorts()
64+
{
65+
return {
66+
BT::InputPort<std::string>(
67+
"pause_service_name",
68+
"Name of the service to pause"),
69+
BT::InputPort<std::string>(
70+
"resume_service_name",
71+
"Name of the service to resume"),
72+
};
73+
}
74+
75+
private:
76+
//! @brief Service callback to pause
77+
void pause_service_callback(
78+
const std::shared_ptr<Trigger::Request> request,
79+
std::shared_ptr<Trigger::Response> response);
80+
81+
//! @brief Service callback to resume
82+
void resume_service_callback(
83+
const std::shared_ptr<Trigger::Request> request,
84+
std::shared_ptr<Trigger::Response> response);
85+
86+
rclcpp::Node::SharedPtr node_;
87+
rclcpp::CallbackGroup::SharedPtr cb_group_;
88+
rclcpp::executors::SingleThreadedExecutor::SharedPtr executor_;
89+
std::unique_ptr<std::thread> spinner_thread_;
90+
rclcpp::Service<Trigger>::SharedPtr pause_srv_;
91+
rclcpp::Service<Trigger>::SharedPtr resume_srv_;
92+
state_t state_;
93+
std::mutex state_mutex_;
94+
};
95+
96+
} // namespace nav2_behavior_tree
97+
98+
#endif // NAV2_BEHAVIOR_TREE__PLUGINS__CONTROL__PAUSE_HPP_
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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__PLUGINS__CONTROL__SEQUENCE_WITH_BLACKBOARD_MEMORY_HPP_
16+
#define NAV2_BEHAVIOR_TREE__PLUGINS__CONTROL__SEQUENCE_WITH_BLACKBOARD_MEMORY_HPP_
17+
18+
#include <string>
19+
#include "behaviortree_cpp/control_node.h"
20+
#include "behaviortree_cpp/bt_factory.h"
21+
22+
namespace nav2_behavior_tree
23+
{
24+
/**
25+
* @brief The SequenceWithBlackboardMemoryNode is similar to the SequenceNode, but it
26+
* stores the index of the last running child in the blackboard (key: "current_child_idx"),
27+
* and it does not reset the index when it got halted.
28+
* It used to tick children in an ordered sequence. If any child returns RUNNING, previous
29+
* children will NOT be ticked again.
30+
*
31+
* - If all the children return SUCCESS, this node returns SUCCESS.
32+
*
33+
* - If a child returns RUNNING, this node returns RUNNING.
34+
* Loop is NOT restarted, the same running child will be ticked again.
35+
*
36+
* - If a child returns FAILURE, stop the loop and return FAILURE.
37+
* Restart the loop only if (reset_on_failure == true)
38+
*
39+
*/
40+
class SequenceWithBlackboardMemoryNode : public BT::ControlNode
41+
{
42+
public:
43+
SequenceWithBlackboardMemoryNode(const std::string & name, const BT::NodeConfiguration & conf);
44+
45+
~SequenceWithBlackboardMemoryNode() override = default;
46+
47+
void halt() override;
48+
49+
//! @brief Declare ports
50+
static BT::PortsList providedPorts()
51+
{
52+
return {
53+
BT::BidirectionalPort<int>("current_child_idx", "The index of the current child"),
54+
};
55+
}
56+
57+
private:
58+
BT::NodeStatus tick() override;
59+
};
60+
61+
} // namespace nav2_behavior_tree
62+
63+
#endif // NAV2_BEHAVIOR_TREE__PLUGINS__CONTROL__SEQUENCE_WITH_BLACKBOARD_MEMORY_HPP_

nav2_behavior_tree/nav2_tree_nodes.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,15 @@
390390

391391
<Control ID="RoundRobin"/>
392392

393+
<Control ID="Pause">
394+
<input_port name="pause_service_name">Service to call to pause</input_port>
395+
<input_port name="resume_service_name">Service to call to resume</input_port>
396+
</Control>
397+
398+
<Control ID="SequenceWithBlackboardMemory">
399+
<bidirectional_port name="current_child_idx">Index of the child to execute</bidirectional_port>
400+
</Control>
401+
393402
<!-- ############################### DECORATOR NODES ############################## -->
394403
<Decorator ID="RateController">
395404
<input_port name="hz">Rate</input_port>

0 commit comments

Comments
 (0)