Skip to content

Commit 528aff6

Browse files
Fix the BT
1 parent 20fc528 commit 528aff6

File tree

11 files changed

+135
-128
lines changed

11 files changed

+135
-128
lines changed

src/task_behavior_tree/CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ endif()
99
find_package(behaviortree_cpp_v3 REQUIRED)
1010
find_package(geometry_msgs REQUIRED)
1111
find_package(rclcpp REQUIRED)
12+
find_package(rclcpp_action REQUIRED)
1213
find_package(rclcpp_lifecycle REQUIRED)
1314
find_package(task_msgs REQUIRED)
1415

@@ -32,6 +33,7 @@ set(dependencies
3233
behaviortree_cpp_v3
3334
geometry_msgs
3435
rclcpp
36+
rclcpp_action
3537
rclcpp_lifecycle
3638
task_msgs
3739
)
@@ -46,24 +48,31 @@ ament_target_dependencies(${library_name}
4648
${dependencies}
4749
)
4850

49-
add_library(task_initialize_navigation_action_bt_node SHARED plugins/src/action/initialize_navigation.cpp)
51+
add_library(task_initialize_navigation_action_bt_node SHARED plugins/action/initialize_navigation.cpp)
5052
list(APPEND plugin_libs task_initialize_navigation_action_bt_node)
5153

5254
foreach(bt_plugin ${plugin_libs})
5355
ament_target_dependencies(${bt_plugin} ${dependencies})
5456
target_compile_definitions(${bt_plugin} PRIVATE BT_PLUGIN_EXPORT)
5557
endforeach()
5658

57-
install(TARGETS ${plugin_libs}
59+
install(TARGETS ${library_name}
60+
${plugin_libs}
5861
ARCHIVE DESTINATION lib
5962
LIBRARY DESTINATION lib
6063
RUNTIME DESTINATION bin
6164
)
6265

66+
install(TARGETS ${executable_name}
67+
RUNTIME DESTINATION lib/${PROJECT_NAME}
68+
)
69+
6370
install(DIRECTORY include/
6471
DESTINATION include/
6572
)
6673

74+
install(DIRECTORY behavior_trees DESTINATION share/${PROJECT_NAME})
75+
6776
if(BUILD_TESTING)
6877
find_package(ament_lint_auto REQUIRED)
6978
# the following line skips the linter which checks for copyrights
@@ -77,6 +86,7 @@ ament_export_include_directories(
7786
)
7887

7988
ament_export_libraries(
89+
${library_name}
8090
${plugin_libs}
8191
)
8292

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!--
2+
This Behavior Tree initialize navigation at the moment, but will navigate and find an object
3+
-->
4+
<root main_tree_to_execute="MainTree">
5+
<BehaviorTree ID="MainTree">
6+
<Sequence name="root_sequence">
7+
<InitializeNavigation timeout_s="{timeout_s}"/>
8+
</Sequence>
9+
</BehaviorTree>
10+
</root>

src/task_behavior_tree/include/action/initialize_navigation.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class InitializeNavigation : public nav2_behavior_tree::BtActionNode<task_msgs::
4040
{
4141
return providedBasicPorts(
4242
{
43-
BT::InputPort<geometry_msgs::msg::PoseStamped>("pose", "Initial pose"),
44-
BT::InputPort<double>("timeout_s", "Timeout in seconds"),
43+
BT::InputPort<double>("timeout_s", 10.0, "Timeout in seconds"),
4544
});
4645
}
4746
};

src/task_behavior_tree/include/bt_navigate_and_find.hpp

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@
77
#include "behaviortree_cpp_v3/xml_parsing.h"
88
#include "behaviortree_cpp_v3/loggers/bt_zmq_publisher.h"
99
#include "rclcpp/rclcpp.hpp"
10-
#include "rclcpp_lifecycle/lifecycle_node.hpp"
1110

1211
namespace bt_navigate_and_find {
13-
14-
using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;
15-
1612
/**
1713
* @class bt_navigate_and_find::BtNavigateAndFind
1814
* @brief A bt class which navigates and tries to find and specific object
1915
*/
20-
class BtNavigateAndFind : public rclcpp_lifecycle::LifecycleNode {
16+
class BtNavigateAndFind : public rclcpp::Node {
2117
public:
2218
/**
2319
* @brief Constructor
@@ -30,38 +26,14 @@ class BtNavigateAndFind : public rclcpp_lifecycle::LifecycleNode {
3026

3127
protected:
3228
/**
33-
* @brief Configures member variables
34-
*
35-
* Builds behavior tree from xml file.
36-
* @param state Reference to LifeCycle node state
37-
* @return SUCCESS or FAILURE
29+
* @brief Configures the behavior tree
30+
* @return bool with the status
3831
*/
39-
CallbackReturn on_configure(const rclcpp_lifecycle::State &state) override;
32+
bool ConfigureBT();
4033
/**
41-
* @brief Activates action server
42-
* @param state Reference to LifeCycle node state
43-
* @return SUCCESS or FAILURE
34+
* @brief Run the behavior tree
4435
*/
45-
CallbackReturn on_activate(const rclcpp_lifecycle::State &state) override;
46-
/**
47-
* @brief Deactivates action server
48-
* @param state Reference to LifeCycle node state
49-
* @return SUCCESS or FAILURE
50-
*/
51-
CallbackReturn on_deactivate(const rclcpp_lifecycle::State &state) override;
52-
/**
53-
* @brief Resets member variables
54-
* @param state Reference to LifeCycle node state
55-
* @return SUCCESS or FAILURE
56-
*/
57-
CallbackReturn on_cleanup(const rclcpp_lifecycle::State &state) override;
58-
/**
59-
* @brief Called when in shutdown state
60-
* @param state Reference to LifeCycle node state
61-
* @return SUCCESS or FAILURE
62-
*/
63-
CallbackReturn on_shutdown(const rclcpp_lifecycle::State &state) override;
64-
36+
void RunBT();
6537
/**
6638
* @brief Load the desired behavior tree
6739
* @param bt_xml_filename The file containing the new BT
@@ -75,6 +47,8 @@ class BtNavigateAndFind : public rclcpp_lifecycle::LifecycleNode {
7547
BT::BehaviorTreeFactory factory_;
7648
// The tree of the BT
7749
BT::Tree tree_;
50+
// A regular, non-spinning ROS node that we can use for calls to the action client
51+
rclcpp::Node::SharedPtr client_node_;
7852
// The XML file that cointains the Behavior Tree to create
7953
std::string default_bt_xml_filename_;
8054
// Libraries to pull plugins (BT Nodes) from

src/task_behavior_tree/package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
<build_depend>behaviortree_cpp_v3</build_depend>
1313
<build_depend>geometry_msgs</build_depend>
1414
<build_depend>rclcpp</build_depend>
15+
<build_depend>rclcpp_action</build_depend>
1516
<build_depend>rclcpp_lifecycle</build_depend>
1617
<build_depend>task_msgs</build_depend>
1718

1819
<exec_depend>behaviortree_cpp_v3</exec_depend>
1920
<exec_depend>geometry_msgs</exec_depend>
2021
<exec_depend>rclcpp</exec_depend>
22+
<exec_depend>rclcpp_action</exec_depend>
2123
<exec_depend>rclcpp_lifecycle</exec_depend>
2224
<exec_depend>task_msgs</exec_depend>
2325

src/task_behavior_tree/plugins/src/action/initialize_navigation.cpp renamed to src/task_behavior_tree/plugins/action/initialize_navigation.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ InitializeNavigation::InitializeNavigation(
1717
void InitializeNavigation::on_tick()
1818
{
1919
// TODO: Make a template to check that we get the goal_ parameters correctly
20-
if (!getInput("pose", goal_.pose)) {
21-
RCLCPP_ERROR(
22-
node_->get_logger(),
23-
"InitializeNavigation: initial pose not provided");
24-
return;
25-
}
2620
if (!getInput("timeout_s", goal_.timeout_s)) {
2721
RCLCPP_ERROR(
2822
node_->get_logger(),

src/task_behavior_tree/src/bt_navigate_and_find.cpp

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,43 @@
1111

1212
#include "behaviortree_cpp_v3/utils/shared_library.h"
1313

14+
#define LOG(level, ...) RCLCPP_##level(this->get_logger(), __VA_ARGS__)
15+
1416
namespace bt_navigate_and_find {
1517

16-
BtNavigateAndFind::BtNavigateAndFind()
17-
: rclcpp_lifecycle::LifecycleNode("bt_navigate_and_find") {
18-
RCLCPP_INFO(get_logger(), "Constructor");
18+
BtNavigateAndFind::BtNavigateAndFind() : rclcpp::Node("bt_navigate_and_find") {
19+
LOG(INFO, "Constructor");
1920

2021
const std::vector<std::string> plugin_libs = {
22+
"nav2_pipeline_sequence_bt_node",
2123
"task_initialize_navigation_action_bt_node",
2224
};
2325

2426
// Declare this node's parameters
2527
declare_parameter("default_bt_xml_filename");
2628
declare_parameter("plugin_lib_names", plugin_libs);
2729

28-
// TODO: Make this an util later
29-
// How to construct a nav2::SimpleActionServer
30-
// initialize_navigation_action_server_ = std::make_unique<T>(
31-
// get_node_base_interface(), get_node_clock_interface(), get_node_logging_interface(),
32-
// get_node_waitables_interface(), name, std::bind(&Class::Member, this));
30+
if (ConfigureBT()) {
31+
LOG(INFO, "BT configured, starting the run.");
32+
RunBT();
33+
} else {
34+
LOG(ERROR, "Fail to configure BT.");
35+
}
3336
}
3437

35-
BtNavigateAndFind::~BtNavigateAndFind()
36-
{
37-
RCLCPP_INFO(get_logger(), "Destructor");
38+
BtNavigateAndFind::~BtNavigateAndFind() {
39+
LOG(INFO, "Destructor");
3840
}
3941

40-
CallbackReturn BtNavigateAndFind::on_configure(const rclcpp_lifecycle::State & /*state*/)
41-
{
42-
RCLCPP_INFO(get_logger(), "Configuring");
42+
bool BtNavigateAndFind::ConfigureBT() {
43+
LOG(INFO, "Configuring");
44+
45+
// Support for handling the topic-based goal pose from rviz
46+
client_node_ = std::make_shared<rclcpp::Node>("_");
4347

4448
// Get the BT filename to use from the node parameter
4549
get_parameter("default_bt_xml_filename", default_bt_xml_filename_);
4650

47-
if (!loadBehaviorTree(default_bt_xml_filename_)) {
48-
RCLCPP_ERROR(get_logger(), "Error loading XML file: %s", default_bt_xml_filename_.c_str());
49-
return CallbackReturn::FAILURE;
50-
}
51-
5251
// Register the plugins at the factory
5352
auto plugin_lib_names = get_parameter("plugin_lib_names").as_string_array();
5453
BT::SharedLibrary loader;
@@ -58,53 +57,36 @@ CallbackReturn BtNavigateAndFind::on_configure(const rclcpp_lifecycle::State & /
5857

5958
// Create the blackboard that will be shared by all of the nodes in the tree
6059
blackboard_ = BT::Blackboard::create();
61-
6260
// Put items on the blackboard
63-
// Example:
64-
// blackboard_->set<int>("number_recoveries", 0);
61+
blackboard_->set<double>("timeout_s", 10.0);
62+
blackboard_->set<rclcpp::Node::SharedPtr>("node", client_node_);
63+
blackboard_->set<std::chrono::milliseconds>("server_timeout", std::chrono::milliseconds(1000));
64+
65+
// Load the behavior tree
66+
if (!loadBehaviorTree(default_bt_xml_filename_)) {
67+
LOG(ERROR, "Error loading XML file: %s", default_bt_xml_filename_.c_str());
68+
return false;
69+
}
6570

66-
return CallbackReturn::SUCCESS;
71+
return true;
6772
}
6873

69-
CallbackReturn BtNavigateAndFind::on_activate(const rclcpp_lifecycle::State & /*state*/)
70-
{
71-
RCLCPP_INFO(get_logger(), "Activating");
74+
void BtNavigateAndFind::RunBT() {
75+
LOG(INFO, "Activating");
7276
rclcpp::WallRate loopRate(std::chrono::milliseconds(10)); // TODO: Make it a cfg
7377
BT::NodeStatus result = BT::NodeStatus::RUNNING;
7478
// Loop until something happens with ROS or the node completes
7579
while (rclcpp::ok() && result == BT::NodeStatus::RUNNING) {
7680
result = tree_.tickRoot();
7781
loopRate.sleep();
7882
}
79-
80-
return (result == BT::NodeStatus::SUCCESS) ? CallbackReturn::SUCCESS : CallbackReturn::FAILURE;
81-
}
82-
83-
CallbackReturn BtNavigateAndFind::on_deactivate(const rclcpp_lifecycle::State & /*state*/)
84-
{
85-
RCLCPP_INFO(get_logger(), "Deactivating");
86-
return CallbackReturn::SUCCESS;
8783
}
8884

89-
CallbackReturn BtNavigateAndFind::on_shutdown(const rclcpp_lifecycle::State & /*state*/)
90-
{
91-
RCLCPP_INFO(get_logger(), "Shutting down");
92-
return CallbackReturn::SUCCESS;
93-
}
94-
95-
CallbackReturn BtNavigateAndFind::on_cleanup(const rclcpp_lifecycle::State & /*state*/)
96-
{
97-
RCLCPP_INFO(get_logger(), "Cleaning up");
98-
return CallbackReturn::SUCCESS;
99-
}
100-
101-
102-
bool BtNavigateAndFind::loadBehaviorTree(const std::string & bt_xml_filename)
103-
{
85+
bool BtNavigateAndFind::loadBehaviorTree(const std::string & bt_xml_filename) {
10486
// Read the input BT XML from the specified file into a string
10587
std::ifstream xml_file(bt_xml_filename);
10688
if (!xml_file.good()) {
107-
RCLCPP_ERROR(get_logger(), "Couldn't open input XML file: %s", bt_xml_filename.c_str());
89+
LOG(ERROR, "Couldn't open input XML file: %s", bt_xml_filename.c_str());
10890
return false;
10991
}
11092
auto xml_string = std::string(

src/task_monitor/include/task_monitor.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
#include "rclcpp/rclcpp.hpp"
22

33
#include "geometry_msgs/msg/pose_with_covariance_stamped.hpp"
4+
#include "lifecycle_msgs/srv/change_state.hpp"
5+
#include "lifecycle_msgs/srv/get_state.hpp"
46
#include "nav2_util/simple_action_server.hpp"
57
#include "task_msgs/action/initialize_navigation.hpp"
68

79
namespace brain {
10+
using namespace std::chrono_literals;
811

9-
class TaskMonitor {
12+
class TaskMonitor : public rclcpp::Node {
1013
public:
1114
/**
1215
* @brief Default constructor
@@ -16,9 +19,6 @@ class TaskMonitor {
1619
* @brief Default destructor
1720
*/
1821
~TaskMonitor();
19-
20-
// The local node
21-
rclcpp::Node::SharedPtr rclcpp_node_; // TODO: Make it private and access a method from main
2222

2323
private:
2424
/**
@@ -54,6 +54,9 @@ class TaskMonitor {
5454
using InitializeNavigationAction = task_msgs::action::InitializeNavigation;
5555
using InitializeNavigationActionServer = nav2_util::SimpleActionServer<InitializeNavigationAction>;
5656
std::unique_ptr<InitializeNavigationActionServer> initialize_navigation_action_server_;
57+
58+
std::shared_ptr<rclcpp::Client<lifecycle_msgs::srv::GetState>> client_get_bt_state_;
59+
std::shared_ptr<rclcpp::Client<lifecycle_msgs::srv::ChangeState>> client_change_bt_state_;
5760
};
5861

5962
} // namespace brain

src/task_monitor/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ int main(int argc, char * argv[])
77
// Need to wait some time for the publisher to stablish connection, bug ?
88
std::chrono::seconds sleep_time_sec(1);
99
rclcpp::sleep_for(std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_time_sec));
10-
rclcpp::spin(task_monitor_node->rclcpp_node_);
10+
rclcpp::spin(task_monitor_node->get_node_base_interface());
1111
rclcpp::shutdown();
1212

1313
return 0;

0 commit comments

Comments
 (0)