Skip to content

Commit d460fc7

Browse files
committed
Add initialize()
1 parent 99c1359 commit d460fc7

19 files changed

+278
-154
lines changed

nav2_behavior_tree/include/nav2_behavior_tree/bt_service_node.hpp

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,6 @@ class BtServiceNode : public BT::ActionNodeBase
5050
: BT::ActionNodeBase(service_node_name, conf), service_name_(service_name), service_node_name_(
5151
service_node_name)
5252
{
53-
// Get the required items from the blackboard
54-
auto bt_loop_duration =
55-
config().blackboard->template get<std::chrono::milliseconds>("bt_loop_duration");
56-
getInputOrBlackboard("server_timeout", server_timeout_);
57-
wait_for_service_timeout_ =
58-
config().blackboard->template get<std::chrono::milliseconds>("wait_for_service_timeout");
59-
60-
// timeout should be less than bt_loop_duration to be able to finish the current tick
61-
max_timeout_ = std::chrono::duration_cast<std::chrono::milliseconds>(bt_loop_duration * 0.5);
62-
63-
// Now that we have node_ to use, create the service client for this BT service
6453
createROSInterfaces();
6554

6655
// Make a request for the service without parameter
@@ -91,22 +80,46 @@ class BtServiceNode : public BT::ActionNodeBase
9180
{
9281
}
9382

83+
/**
84+
* @brief Function to read parameters and initialize class variables
85+
*/
86+
void initialize()
87+
{
88+
// Get the required items from the blackboard
89+
auto bt_loop_duration =
90+
config().blackboard->template get<std::chrono::milliseconds>("bt_loop_duration");
91+
getInputOrBlackboard("server_timeout", server_timeout_);
92+
wait_for_service_timeout_ =
93+
config().blackboard->template get<std::chrono::milliseconds>("wait_for_service_timeout");
94+
95+
// timeout should be less than bt_loop_duration to be able to finish the current tick
96+
max_timeout_ = std::chrono::duration_cast<std::chrono::milliseconds>(bt_loop_duration * 0.5);
97+
98+
// Now that we have node_ to use, create the service client for this BT service
99+
createROSInterfaces();
100+
}
101+
94102
/**
95103
* @brief Function to create ROS interfaces
96104
*/
97105
void createROSInterfaces()
98106
{
99-
node_ = config().blackboard->template get<rclcpp::Node::SharedPtr>("node");
100-
callback_group_ = node_->create_callback_group(
101-
rclcpp::CallbackGroupType::MutuallyExclusive,
102-
false);
103-
callback_group_executor_.add_callback_group(callback_group_, node_->get_node_base_interface());
104-
105-
getInput("service_name", service_name_);
106-
service_client_ = node_->create_client<ServiceT>(
107-
service_name_,
108-
rclcpp::SystemDefaultsQoS(),
109-
callback_group_);
107+
std::string service_new;
108+
getInput("service_name", service_new);
109+
if (service_new != service_name_ || !service_client_) {
110+
service_name_ = service_new;
111+
node_ = config().blackboard->template get<rclcpp::Node::SharedPtr>("node");
112+
callback_group_ = node_->create_callback_group(
113+
rclcpp::CallbackGroupType::MutuallyExclusive,
114+
false);
115+
callback_group_executor_.add_callback_group(callback_group_,
116+
node_->get_node_base_interface());
117+
118+
service_client_ = node_->create_client<ServiceT>(
119+
service_name_,
120+
rclcpp::SystemDefaultsQoS(),
121+
callback_group_);
122+
}
110123
}
111124

112125
/**
@@ -141,6 +154,10 @@ class BtServiceNode : public BT::ActionNodeBase
141154
*/
142155
BT::NodeStatus tick() override
143156
{
157+
if (!BT::isStatusActive(status())) {
158+
initialize();
159+
}
160+
144161
if (!request_sent_) {
145162
// reset the flag to send the request or not,
146163
// allowing the user the option to set it in on_tick

nav2_behavior_tree/include/nav2_behavior_tree/plugins/action/controller_selector_node.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class ControllerSelector : public BT::SyncActionNode
7171
}
7272

7373
private:
74+
/**
75+
* @brief Function to read parameters and initialize class variables
76+
*/
77+
void initialize();
7478
/**
7579
* @brief Function to create ROS interfaces
7680
*/

nav2_behavior_tree/include/nav2_behavior_tree/plugins/action/goal_checker_selector_node.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class GoalCheckerSelector : public BT::SyncActionNode
7171
}
7272

7373
private:
74+
/**
75+
* @brief Function to read parameters and initialize class variables
76+
*/
77+
void initialize();
7478
/**
7579
* @brief Function to create ROS interfaces
7680
*/

nav2_behavior_tree/include/nav2_behavior_tree/plugins/action/planner_selector_node.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class PlannerSelector : public BT::SyncActionNode
7171
}
7272

7373
private:
74+
/**
75+
* @brief Function to read parameters and initialize class variables
76+
*/
77+
void initialize();
7478
/**
7579
* @brief Function to create ROS interfaces
7680
*/

nav2_behavior_tree/include/nav2_behavior_tree/plugins/action/progress_checker_selector_node.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class ProgressCheckerSelector : public BT::SyncActionNode
7070
}
7171

7272
private:
73+
/**
74+
* @brief Function to read parameters and initialize class variables
75+
*/
76+
void initialize();
7377
/**
7478
* @brief Function to create ROS interfaces
7579
*/

nav2_behavior_tree/include/nav2_behavior_tree/plugins/action/smoother_selector_node.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class SmootherSelector : public BT::SyncActionNode
7171
}
7272

7373
private:
74+
/**
75+
* @brief Function to read parameters and initialize class variables
76+
*/
77+
void initialize();
7478
/**
7579
* @brief Function to create ROS interfaces
7680
*/

nav2_behavior_tree/include/nav2_behavior_tree/plugins/condition/is_battery_charging_condition.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ class IsBatteryChargingCondition : public BT::ConditionNode
6363
}
6464

6565
private:
66+
/**
67+
* @brief Function to read parameters and initialize class variables
68+
*/
69+
void initialize();
6670
/**
6771
* @brief Function to create ROS interfaces
6872
*/

nav2_behavior_tree/include/nav2_behavior_tree/plugins/condition/is_path_valid_condition.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ class IsPathValidCondition : public BT::ConditionNode
6868
}
6969

7070
private:
71-
/**
72-
* @brief Function to create ROS interfaces
73-
*/
74-
void createROSInterfaces();
7571
rclcpp::Node::SharedPtr node_;
7672
rclcpp::Client<nav2_msgs::srv::IsPathValid>::SharedPtr client_;
7773
// The timeout value while waiting for a response from the

nav2_behavior_tree/include/nav2_behavior_tree/plugins/condition/is_stuck_condition.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ class IsStuckCondition : public BT::ConditionNode
8484
static BT::PortsList providedPorts() {return {};}
8585

8686
private:
87-
/**
88-
* @brief Function to create ROS interfaces
89-
*/
90-
void createROSInterfaces();
9187
// The node that will be used for any ROS operations
9288
rclcpp::Node::SharedPtr node_;
9389
rclcpp::CallbackGroup::SharedPtr callback_group_;

nav2_behavior_tree/include/nav2_behavior_tree/plugins/decorator/goal_updater_node.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ class GoalUpdater : public BT::DecoratorNode
6363
}
6464

6565
private:
66+
/**
67+
* @brief Function to read parameters and initialize class variables
68+
*/
69+
void initialize();
6670
/**
6771
* @brief Function to create ROS interfaces
6872
*/
@@ -96,6 +100,8 @@ class GoalUpdater : public BT::DecoratorNode
96100
rclcpp::Node::SharedPtr node_;
97101
rclcpp::CallbackGroup::SharedPtr callback_group_;
98102
rclcpp::executors::SingleThreadedExecutor callback_group_executor_;
103+
std::string goal_updater_topic_;
104+
std::string goals_updater_topic_;
99105
};
100106

101107
} // namespace nav2_behavior_tree

0 commit comments

Comments
 (0)