mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
publisher_handler.h
Go to the documentation of this file.
1// clang: TomasFormat
6#ifndef PUBLISHER_HANDLER_H
7#define PUBLISHER_HANDLER_H
8
9#include <memory>
10
11#include <rclcpp/rclcpp.hpp>
12#include <rclcpp/node.hpp>
13#include <rclcpp/time_source.hpp>
14
15#include <atomic>
16#include <cassert>
17#include <string>
18#include <mutex>
19#include <optional>
20
21#include <mrs_lib/internal/rate_throttle.hpp>
22
23namespace mrs_lib
24{
25
26 struct PublisherHandlerOptions
27 {
28 PublisherHandlerOptions(const rclcpp::Node::SharedPtr& node) : node(node)
29 {
30 }
31
32 PublisherHandlerOptions() = default;
33
34 rclcpp::Node::SharedPtr node;
35
39 double throttle_rate = 0;
40
41 rclcpp::QoS qos = rclcpp::SystemDefaultsQoS();
42 };
43
44 /* class PublisherHandler_impl //{ */
45
49 template <class TopicType>
51 {
52
53 public:
58
63
70 PublisherHandler_impl(const PublisherHandlerOptions& options, const std::string& address);
71
78 void publish(const TopicType& msg);
79
85 void publish(const std::shared_ptr<TopicType>& msg);
86
92 /* void publish(const std::shared_ptr<TopicType const>& msg); */
93
99 void publish(typename TopicType::ConstSharedPtr msg);
100
106 void publish(std::unique_ptr<TopicType> msg);
107
113 unsigned int getNumSubscribers(void) const;
114
120 std::string topicName(void) const;
121
122 private:
123 rclcpp::Node::SharedPtr node_;
124
125 std::shared_ptr<rclcpp::Publisher<TopicType>> publisher_;
126
127 mutable std::mutex mutex_publisher_;
128
129 std::atomic<bool> publisher_initialized_;
130
131 std::string address_;
132
133 std::optional<internal::RateThrottle> throttle_;
134
138 bool throttleAccept(void);
139 };
140
141 //}
142
143 /* class PublisherHandler //{ */
144
148 template <class TopicType>
150 {
151
152 public:
157
162
171
178
185 PublisherHandler(const rclcpp::Node::SharedPtr& node, const std::string& address);
186
193 PublisherHandler(const PublisherHandlerOptions& options, const std::string& address);
194
200 void publish(const TopicType& msg);
201
207 void publish(const std::shared_ptr<TopicType>& msg);
208
214 /* void publish(const std::shared_ptr<TopicType const>& msg); */
215
221 void publish(typename TopicType::ConstSharedPtr msg);
222
228 void publish(std::unique_ptr<TopicType> msg);
229
235 unsigned int getNumSubscribers(void) const;
236
242 std::string topicName(void) const;
243
244 private:
245 std::shared_ptr<PublisherHandler_impl<TopicType>> impl_;
246 };
247
248 //}
249
250} // namespace mrs_lib
251
252#ifndef PUBLISHER_HANDLER_HPP
253#include <mrs_lib/impl/publisher_handler.hpp>
254#endif // PUBLISHER_HANDLER_H
255
256#endif // PUBLISHER_HANDLER_H
void publish(const TopicType &msg)
publish message
Definition publisher_handler.hpp:52
~PublisherHandler_impl(void)
default destructor
Definition publisher_handler.h:62
unsigned int getNumSubscribers(void) const
get number of subscribers
Definition publisher_handler.hpp:167
std::string topicName(void) const
get the resolved name of the topic
Definition publisher_handler.hpp:182
PublisherHandler_impl(void)
default constructor
Definition publisher_handler.hpp:17
user wrapper of the publisher handler implementation
Definition publisher_handler.h:150
void publish(const TopicType &msg)
publish message
Definition publisher_handler.hpp:260
PublisherHandler & operator=(const PublisherHandler &other)
operator=
Definition publisher_handler.hpp:199
std::string topicName(void) const
get the resolved name of the topic
Definition publisher_handler.hpp:337
unsigned int getNumSubscribers(void) const
get number of subscribers
Definition publisher_handler.hpp:324
~PublisherHandler(void)
generic destructor
Definition publisher_handler.h:161
PublisherHandler(void)
generic constructor
Definition publisher_handler.h:156
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
Definition publisher_handler.h:27
double throttle_rate
when > 0, the publisher output is limited to this rate in [Hz]
Definition publisher_handler.h:39