Skip to content

Commit c0b3698

Browse files
Tacha-SSakshayMahna
authored andcommitted
Create subscriber on constructor (ros-navigation#4859)
* Create subscriber on constructor Signed-off-by: Tatsuro Sakaguchi <tatsuro.sakaguchi@g.softbank.co.jp> * Get BT input topic name Signed-off-by: Tatsuro Sakaguchi <tatsuro.sakaguchi@g.softbank.co.jp> * Add createROSInterfaces() Signed-off-by: Tatsuro Sakaguchi <tatsuro.sakaguchi@g.softbank.co.jp> * Add initialize() Signed-off-by: Tatsuro Sakaguchi <tatsuro.sakaguchi@g.softbank.co.jp> * Fix bug Signed-off-by: Tatsuro Sakaguchi <tatsuro.sakaguchi@g.softbank.co.jp> * Call initialize where input port updates occur Signed-off-by: Tatsuro Sakaguchi <tatsuro.sakaguchi@g.softbank.co.jp> * Call initialize() on constructor Signed-off-by: Tatsuro Sakaguchi <tatsuro.sakaguchi@g.softbank.co.jp> --------- Signed-off-by: Tatsuro Sakaguchi <tatsuro.sakaguchi@g.softbank.co.jp> Signed-off-by: Sakshay Mahna <sakshum19@gmail.com>
1 parent e6d6b7a commit c0b3698

17 files changed

+355
-122
lines changed

nav2_behavior_tree/include/nav2_behavior_tree/bt_service_node.hpp

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,7 @@ 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-
node_ = config().blackboard->template get<rclcpp::Node::SharedPtr>("node");
54-
callback_group_ = node_->create_callback_group(
55-
rclcpp::CallbackGroupType::MutuallyExclusive,
56-
false);
57-
callback_group_executor_.add_callback_group(callback_group_, node_->get_node_base_interface());
58-
59-
// Get the required items from the blackboard
60-
auto bt_loop_duration =
61-
config().blackboard->template get<std::chrono::milliseconds>("bt_loop_duration");
62-
getInputOrBlackboard("server_timeout", server_timeout_);
63-
wait_for_service_timeout_ =
64-
config().blackboard->template get<std::chrono::milliseconds>("wait_for_service_timeout");
65-
66-
// timeout should be less than bt_loop_duration to be able to finish the current tick
67-
max_timeout_ = std::chrono::duration_cast<std::chrono::milliseconds>(bt_loop_duration * 0.5);
68-
69-
// Now that we have node_ to use, create the service client for this BT service
70-
getInput("service_name", service_name_);
71-
service_client_ = node_->create_client<ServiceT>(
72-
service_name_,
73-
rclcpp::SystemDefaultsQoS(),
74-
callback_group_);
53+
initialize();
7554

7655
// Make a request for the service without parameter
7756
request_ = std::make_shared<typename ServiceT::Request>();
@@ -101,6 +80,48 @@ class BtServiceNode : public BT::ActionNodeBase
10180
{
10281
}
10382

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+
102+
/**
103+
* @brief Function to create ROS interfaces
104+
*/
105+
void createROSInterfaces()
106+
{
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+
}
123+
}
124+
104125
/**
105126
* @brief Any subclass of BtServiceNode that accepts parameters must provide a
106127
* providedPorts method and call providedBasicPorts in it.
@@ -133,6 +154,10 @@ class BtServiceNode : public BT::ActionNodeBase
133154
*/
134155
BT::NodeStatus tick() override
135156
{
157+
if (!BT::isStatusActive(status())) {
158+
initialize();
159+
}
160+
136161
if (!request_sent_) {
137162
// reset the flag to send the request or not,
138163
// 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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ class ControllerSelector : public BT::SyncActionNode
7171
}
7272

7373
private:
74+
/**
75+
* @brief Function to read parameters and initialize class variables
76+
*/
77+
void initialize();
78+
/**
79+
* @brief Function to create ROS interfaces
80+
*/
81+
void createROSInterfaces();
82+
7483
/**
7584
* @brief Function to perform some user-defined operation on tick
7685
*/

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

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

7373
private:
74+
/**
75+
* @brief Function to read parameters and initialize class variables
76+
*/
77+
void initialize();
78+
/**
79+
* @brief Function to create ROS interfaces
80+
*/
81+
void createROSInterfaces();
82+
7483
/**
7584
* @brief Function to perform some user-defined operation on tick
7685
*/

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

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

7373
private:
74+
/**
75+
* @brief Function to read parameters and initialize class variables
76+
*/
77+
void initialize();
78+
/**
79+
* @brief Function to create ROS interfaces
80+
*/
81+
void createROSInterfaces();
82+
7483
/**
7584
* @brief Function to perform some user-defined operation on tick
7685
*/

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

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

7272
private:
73+
/**
74+
* @brief Function to read parameters and initialize class variables
75+
*/
76+
void initialize();
77+
/**
78+
* @brief Function to create ROS interfaces
79+
*/
80+
void createROSInterfaces();
81+
7382
/**
7483
* @brief Function to perform some user-defined operation on tick
7584
*/

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

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

7373
private:
74+
/**
75+
* @brief Function to read parameters and initialize class variables
76+
*/
77+
void initialize();
78+
/**
79+
* @brief Function to create ROS interfaces
80+
*/
81+
void createROSInterfaces();
82+
7483
/**
7584
* @brief Function to perform some user-defined operation on tick
7685
*/

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

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

6565
private:
66+
/**
67+
* @brief Function to read parameters and initialize class variables
68+
*/
69+
void initialize();
70+
/**
71+
* @brief Function to create ROS interfaces
72+
*/
73+
void createROSInterfaces();
74+
6675
/**
6776
* @brief Callback function for battery topic
6877
* @param msg Shared pointer to sensor_msgs::msg::BatteryState message

nav2_behavior_tree/include/nav2_behavior_tree/plugins/condition/is_battery_low_condition.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class IsBatteryLowCondition : public BT::ConditionNode
5656
*/
5757
void initialize();
5858

59+
/**
60+
* @brief Function to create ROS interfaces
61+
*/
62+
void createROSInterfaces();
63+
5964
/**
6065
* @brief Creates list of BT ports
6166
* @return BT::PortsList Containing node-specific ports

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

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

6565
private:
66+
/**
67+
* @brief Function to read parameters and initialize class variables
68+
*/
69+
void initialize();
70+
/**
71+
* @brief Function to create ROS interfaces
72+
*/
73+
void createROSInterfaces();
6674
/**
6775
* @brief The main override required by a BT action
6876
* @return BT::NodeStatus Status of tick execution
@@ -92,6 +100,8 @@ class GoalUpdater : public BT::DecoratorNode
92100
rclcpp::Node::SharedPtr node_;
93101
rclcpp::CallbackGroup::SharedPtr callback_group_;
94102
rclcpp::executors::SingleThreadedExecutor callback_group_executor_;
103+
std::string goal_updater_topic_;
104+
std::string goals_updater_topic_;
95105
};
96106

97107
} // namespace nav2_behavior_tree

nav2_behavior_tree/plugins/action/controller_selector_node.cpp

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,46 @@ ControllerSelector::ControllerSelector(
3232
const BT::NodeConfiguration & conf)
3333
: BT::SyncActionNode(name, conf)
3434
{
35-
node_ = config().blackboard->get<rclcpp::Node::SharedPtr>("node");
36-
callback_group_ = node_->create_callback_group(
37-
rclcpp::CallbackGroupType::MutuallyExclusive,
38-
false);
39-
callback_group_executor_.add_callback_group(callback_group_, node_->get_node_base_interface());
40-
41-
getInput("topic_name", topic_name_);
42-
43-
rclcpp::QoS qos(rclcpp::KeepLast(1));
44-
qos.transient_local().reliable();
45-
46-
rclcpp::SubscriptionOptions sub_option;
47-
sub_option.callback_group = callback_group_;
48-
controller_selector_sub_ = node_->create_subscription<std_msgs::msg::String>(
49-
topic_name_,
50-
qos,
51-
std::bind(&ControllerSelector::callbackControllerSelect, this, _1),
52-
sub_option);
35+
initialize();
36+
}
37+
38+
void ControllerSelector::initialize()
39+
{
40+
createROSInterfaces();
41+
}
42+
43+
void ControllerSelector::createROSInterfaces()
44+
{
45+
std::string topic_new;
46+
getInput("topic_name", topic_new);
47+
if (topic_new != topic_name_ || !controller_selector_sub_) {
48+
topic_name_ = topic_new;
49+
node_ = config().blackboard->get<rclcpp::Node::SharedPtr>("node");
50+
callback_group_ = node_->create_callback_group(
51+
rclcpp::CallbackGroupType::MutuallyExclusive,
52+
false);
53+
callback_group_executor_.add_callback_group(callback_group_, node_->get_node_base_interface());
54+
55+
56+
rclcpp::QoS qos(rclcpp::KeepLast(1));
57+
qos.transient_local().reliable();
58+
59+
rclcpp::SubscriptionOptions sub_option;
60+
sub_option.callback_group = callback_group_;
61+
controller_selector_sub_ = node_->create_subscription<std_msgs::msg::String>(
62+
topic_name_,
63+
qos,
64+
std::bind(&ControllerSelector::callbackControllerSelect, this, _1),
65+
sub_option);
66+
}
5367
}
5468

5569
BT::NodeStatus ControllerSelector::tick()
5670
{
71+
if (!BT::isStatusActive(status())) {
72+
initialize();
73+
}
74+
5775
callback_group_executor_.spin_some();
5876

5977
// This behavior always use the last selected controller received from the topic input.

0 commit comments

Comments
 (0)