1111
1212#include " behaviortree_cpp_v3/utils/shared_library.h"
1313
14+ #define LOG (level, ...) RCLCPP_##level(this ->get_logger (), __VA_ARGS__)
15+
1416namespace 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 (
0 commit comments