mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
publisher_handler.hpp
1// clang: TomasFormat
2#ifndef PUBLISHER_HANDLER_HPP
3#define PUBLISHER_HANDLER_HPP
4
6
7namespace mrs_lib
8{
9
10 // --------------------------------------------------------------
11 // | PublisherHandler_impl |
12 // --------------------------------------------------------------
13
14 /* PublisherHandler_impl(void) //{ */
15
16 template <class TopicType>
17 PublisherHandler_impl<TopicType>::PublisherHandler_impl(void) : publisher_initialized_(false)
18 {
19 }
20
21 //}
22
23 /* PublisherHandler_impl(const PublisherHandlerOptions& options, const std::string& address) //{ */
24
25 template <class TopicType>
27 {
28
29 node_ = options.node;
30
31 {
32 std::scoped_lock lock(mutex_publisher_);
33
34 address_ = address;
35 publisher_ = node_->create_publisher<TopicType>(address, options.qos);
36 RCLCPP_INFO(node_->get_logger(), " Created publisher on topic '%s' -> '%s'", address.c_str(), publisher_->get_topic_name());
37 }
38
39 if (options.throttle_rate > 1e-3)
40 {
41 throttle_.emplace(options.throttle_rate);
42 }
43
44 publisher_initialized_ = true;
45 }
46
47 //}
48
49 /* publish(TopicType& msg) //{ */
50
51 template <class TopicType>
53 {
54
55 if (!publisher_initialized_)
56 {
57 return;
58 }
59
60 {
61 std::scoped_lock lock(mutex_publisher_);
62
63 if (!throttleAccept())
64 {
65 return;
66 }
67
68 publisher_->publish(msg);
69 }
70 }
71
72 //}
73
74 /* publish(const std::shared_ptr<TopicType>& msg) //{ */
75
76 template <class TopicType>
77 void PublisherHandler_impl<TopicType>::publish(const std::shared_ptr<TopicType>& msg)
78 {
79
80 if (!publisher_initialized_)
81 {
82 return;
83 }
84
85 {
86 std::scoped_lock lock(mutex_publisher_);
87
88 if (!throttleAccept())
89 {
90 return;
91 }
92
93 publisher_->publish(msg);
94 }
95 }
96
97 //}
98
99 /* publish(TopicType::ConstSharedPtr& msg) //{ */
100
101 template <class TopicType>
102 void PublisherHandler_impl<TopicType>::publish(typename TopicType::ConstSharedPtr msg)
103 {
104
105 if (!publisher_initialized_)
106 {
107 return;
108 }
109
110 {
111 std::scoped_lock lock(mutex_publisher_);
112
113 if (!throttleAccept())
114 {
115 return;
116 }
117
118 publisher_->publish(msg);
119 }
120 }
121
122 //}
123
124 /* publish(std::unique_ptr<TopicType> msg) //{ */
125
126 template <class TopicType>
127 void PublisherHandler_impl<TopicType>::publish(std::unique_ptr<TopicType> msg)
128 {
129 if (!publisher_initialized_)
130 {
131 return;
132 }
133
134 {
135 std::scoped_lock lock(mutex_publisher_);
136
137 if (!throttleAccept())
138 {
139 return;
140 }
141
142 publisher_->publish(std::move(msg));
143 }
144 }
145
146 //}
147
148 /* throttleAccept() //{ */
149
150 template <class TopicType>
151 bool PublisherHandler_impl<TopicType>::throttleAccept(void)
152 {
153
154 if (!throttle_)
155 {
156 return true;
157 }
158
159 return throttle_->accept(node_->get_clock()->now());
160 }
161
162 //}
163
164 /* getNumSubscribers(void) //{ */
165
166 template <class TopicType>
168 {
169
170 {
171 std::scoped_lock lock(mutex_publisher_);
172
173 return publisher_->get_subscription_count();
174 }
175 }
176
177 //}
178
179 // --------------------------------------------------------------
180 // | PublisherHandler |
181 // --------------------------------------------------------------
182
183 /* operator= //{ */
184
185 template <class TopicType>
187 {
188
189 if (this == &other)
190 {
191 return *this;
193
194 if (other.impl_)
195 {
196 this->impl_ = other.impl_;
197 }
198
199 return *this;
200 }
201
202 //}
203
204 /* copy constructor //{ */
205
206 template <class TopicType>
208 {
209
210 if (other.impl_)
211 {
212 this->impl_ = other.impl_;
213 }
214 }
215
216 //}
217
218 /* PublisherHandler(rclcpp::Node::SharedPtr node, const std::string& address) //{ */
219
220 template <class TopicType>
221 PublisherHandler<TopicType>::PublisherHandler(const rclcpp::Node::SharedPtr& node, const std::string& address)
222 {
223
225
226 opts.node = node;
228 impl_ = std::make_shared<PublisherHandler_impl<TopicType>>(opts, address);
229 }
230
231 //}
232
233 /* PublisherHandler(const rclcpp::PublisherOptions& options, const std::string& address) //{ */
234
235 template <class TopicType>
237 {
238
239 impl_ = std::make_shared<PublisherHandler_impl<TopicType>>(options, address);
240 }
241
242 //}
243
244 /* publish(const TopicType& msg) //{ */
245
246 template <class TopicType>
247 void PublisherHandler<TopicType>::publish(const TopicType& msg)
248 {
249
250 impl_->publish(msg);
251 }
252
253 //}
254
255 /* publish(const std::shared_ptr<TopicType>& msg) //{ */
256
257 template <class TopicType>
258 void PublisherHandler<TopicType>::publish(const std::shared_ptr<TopicType>& msg)
259 {
260
261 impl_->publish(msg);
262 }
263
264 //}
265
266
267 /* /1* publish(const std::shared_ptr<TopicType const>& msg) //{ *1/ */
268
269 /* template <class TopicType> */
270 /* void PublisherHandler<TopicType>::publish(const std::shared_ptr<TopicType const>& msg) { */
271
272 /* impl_->publish(msg); */
273 /* } */
274
275 /* //} */
276
277 /* publish(const TopicType::ConstSharedPtr& msg) //{ */
278
279 template <class TopicType>
280 void PublisherHandler<TopicType>::publish(typename TopicType::ConstSharedPtr msg)
281 {
282
283 impl_->publish(msg);
284 }
285
286 //}
287
288
289 /* publish(std::unique_ptr<TopicType> msg) //{ */
290
291 template <class TopicType>
292 void PublisherHandler<TopicType>::publish(std::unique_ptr<TopicType> msg)
293 {
294
295 impl_->publish(std::move(msg));
296 }
297
298 //}
299
300 /* getNumSubscribers(void) //{ */
301
302 template <class TopicType>
304 {
305
306 return impl_->getNumSubscribers();
307 }
308
309 //}
310
311} // namespace mrs_lib
312
313#endif // PUBLISHER_HANDLER_HPP
unsigned int getNumSubscribers(void)
get number of subscribers
Definition publisher_handler.hpp:167
void publish(const TopicType &msg)
publish message
Definition publisher_handler.hpp:52
PublisherHandler_impl(void)
default constructor
Definition publisher_handler.hpp:17
void publish(const TopicType &msg)
publish message
Definition publisher_handler.hpp:247
PublisherHandler & operator=(const PublisherHandler &other)
operator=
Definition publisher_handler.hpp:186
unsigned int getNumSubscribers(void)
get number of subscribers
Definition publisher_handler.hpp:303
PublisherHandler(void)
generic constructor
Definition publisher_handler.h:148
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
Defines PublisherHandler and related convenience classes for upgrading the ROS publisher.
Definition publisher_handler.h:26
double throttle_rate
when > 0, the publisher output is limited to this rate in [Hz]
Definition publisher_handler.h:38