@@ -52,10 +52,31 @@ class RosTopicSubNode : public BT::ConditionNode
52
52
protected:
53
53
struct SubscriberInstance
54
54
{
55
+ void init (std::shared_ptr<rclcpp::Node> node, const std::string& topic_name)
56
+ {
57
+ // create a callback group for this particular instance
58
+ callback_group =
59
+ node->create_callback_group (rclcpp::CallbackGroupType::MutuallyExclusive, false );
60
+ callback_group_executor.add_callback_group (
61
+ callback_group, node->get_node_base_interface ());
62
+
63
+ rclcpp::SubscriptionOptions option;
64
+ option.callback_group = callback_group;
65
+
66
+ // The callback will broadcast to all the instances of RosTopicSubNode<T>
67
+ auto callback = [this ](const std::shared_ptr<TopicT> msg)
68
+ {
69
+ broadcaster (msg);
70
+ };
71
+ subscriber = node->create_subscription <TopicT>(topic_name, 1 , callback, option);
72
+ }
73
+
55
74
std::shared_ptr<Subscriber> subscriber;
56
75
rclcpp::CallbackGroup::SharedPtr callback_group;
57
76
rclcpp::executors::SingleThreadedExecutor callback_group_executor;
58
- boost::signals2::signal<void (const std::shared_ptr<TopicT>&)> broadcaster;
77
+ boost::signals2::signal<void (const std::shared_ptr<TopicT>)> broadcaster;
78
+
79
+
59
80
};
60
81
61
82
static std::mutex& registryMutex ()
@@ -75,6 +96,7 @@ class RosTopicSubNode : public BT::ConditionNode
75
96
SubscriberInstance* sub_instance_ = nullptr ;
76
97
std::shared_ptr<TopicT> last_msg_;
77
98
std::string topic_name_;
99
+ boost::signals2::connection signal_connection_;
78
100
79
101
rclcpp::Logger logger ()
80
102
{
@@ -94,7 +116,10 @@ class RosTopicSubNode : public BT::ConditionNode
94
116
const BT::NodeConfig& conf,
95
117
const RosNodeParams& params);
96
118
97
- virtual ~RosTopicSubNode () = default ;
119
+ virtual ~RosTopicSubNode ()
120
+ {
121
+ signal_connection_.disconnect ();
122
+ }
98
123
99
124
/* *
100
125
* @brief Any subclass of RosTopicNode that accepts parameters must provide a
@@ -129,7 +154,7 @@ class RosTopicSubNode : public BT::ConditionNode
129
154
*
130
155
* @return the new status of the Node, based on last_msg
131
156
*/
132
- virtual NodeStatus onTick (const std::shared_ptr<TopicT>& last_msg) = 0;
157
+ virtual NodeStatus onTick (const std::shared_ptr<TopicT> last_msg) = 0;
133
158
134
159
private:
135
160
@@ -206,33 +231,17 @@ template<class T> inline
206
231
// just created (subscriber is not initialized)
207
232
if (!sub_instance_->subscriber )
208
233
{
209
- // create a callback group for this particular instance
210
- sub_instance_->callback_group =
211
- node_->create_callback_group (rclcpp::CallbackGroupType::MutuallyExclusive, false );
212
- sub_instance_->callback_group_executor .add_callback_group (
213
- sub_instance_->callback_group , node_->get_node_base_interface ());
214
-
215
- rclcpp::SubscriptionOptions sub_option;
216
- sub_option.callback_group = sub_instance_->callback_group ;
217
-
218
- // The callback will broadcast to all the instances of RosTopicSubNode<T>
219
- auto callback = [this ](const std::shared_ptr<T> msg) {
220
- sub_instance_->broadcaster (msg);
221
- };
222
- sub_instance_->subscriber =
223
- node_->create_subscription <T>(topic_name, 1 , callback, sub_option);
234
+ sub_instance_->init (node_, topic_name);
224
235
225
236
RCLCPP_INFO (logger (),
226
237
" Node [%s] created Subscriber to topic [%s]" ,
227
238
name ().c_str (), topic_name.c_str () );
228
239
}
229
240
230
241
// add "this" as received of the broadcaster
231
- sub_instance_->broadcaster .connect ([this ](const std::shared_ptr<T> msg)
232
- {
233
- last_msg_ = msg;
234
- });
235
-
242
+ signal_connection_ = sub_instance_->broadcaster .connect (
243
+ [this ](const std::shared_ptr<T> msg) { last_msg_ = msg; } );
244
+
236
245
topic_name_ = topic_name;
237
246
return true ;
238
247
}
0 commit comments