mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
subscriber.hpp
1#ifndef MRS_LIB_SUBSCRIBER_HPP_
2#define MRS_LIB_SUBSCRIBER_HPP_
3
4
5#include <chrono>
6#include <memory>
7#include <optional>
8#include <string_view>
9
10#include <rclcpp/callback_group.hpp>
11#include <rclcpp/node_interfaces/node_base_interface.hpp>
12#include <rclcpp/node_interfaces/node_clock_interface.hpp>
13#include <rclcpp/node_interfaces/node_interfaces.hpp>
14#include <rclcpp/node_interfaces/node_logging_interface.hpp>
15#include <rclcpp/node_interfaces/node_parameters_interface.hpp>
16#include <rclcpp/node_interfaces/node_timers_interface.hpp>
17#include <rclcpp/node_interfaces/node_topics_interface.hpp>
18
19#include "mrs_lib/timeout_manager.hpp"
22
23
24namespace mrs_lib
25{
26
31 rclcpp::node_interfaces::NodeInterfaces<rclcpp::node_interfaces::NodeBaseInterface, rclcpp::node_interfaces::NodeTimersInterface,
32 rclcpp::node_interfaces::NodeClockInterface, rclcpp::node_interfaces::NodeLoggingInterface,
33 rclcpp::node_interfaces::NodeParametersInterface, rclcpp::node_interfaces::NodeTopicsInterface>;
34
39 {
42
44 rclcpp::QoS qos = rclcpp::SystemDefaultsQoS();
46 std::shared_ptr<rclcpp::CallbackGroup> callback_group = nullptr;
47
49 std::optional<std::chrono::nanoseconds> no_message_timeout = std::nullopt;
55 std::shared_ptr<mrs_lib::TimeoutManager2> timeout_manager = nullptr;
61 std::function<void(std::string_view topic_name, rclcpp::Time last_msg)> timeout_callback = {};
62 };
63
68 {
69 };
70
76 template <typename MessageType>
78 {
79 private:
80 class Impl;
81
82 public:
84 using MessageCallback = std::function<void(std::shared_ptr<const MessageType>)>;
85
87 using MessageCoroCallback = CoroCallback<void(std::shared_ptr<const MessageType>)>;
88
96 explicit Subscriber(const SubscriberOptions& options, std::string_view topic_name, MessageCallback callback);
97
105 explicit Subscriber(const SubscriberOptions& options, std::string_view topic_name, MessageCoroCallback callback);
106
114 explicit Subscriber(const SubscriberOptions& options, std::string_view topic_name, PolledSubscriberTag tag);
115
123 [[nodiscard]] std::optional<std::shared_ptr<const MessageType>> get_message();
124
137 [[nodiscard]] std::optional<std::shared_ptr<const MessageType>> get_new_message();
138
146 [[nodiscard]] std::optional<std::shared_ptr<const MessageType>> peek_message() const;
147
160 [[nodiscard]] std::optional<std::shared_ptr<const MessageType>> peek_new_message() const;
161
162
168 [[nodiscard]] rclcpp::Time get_last_message_time() const;
169
175 [[nodiscard]] std::string get_topic_name() const;
176
182 [[nodiscard]] std::string get_unresolved_topic_name() const;
183
189 [[nodiscard]] uint32_t get_publisher_count() const;
190
191 private:
192 Pimpl<Impl> impl_;
193 };
194
195} // namespace mrs_lib
196
197#ifndef MRS_LIB_SUBSCRIBER_IMPL_HPP_
198#include "mrs_lib/subscriber.impl.hpp"
199#endif
200
201#endif // MRS_LIB_SUBSCRIBER_HPP_
Base template for CoroCallback.
Definition callback.hpp:166
Owning pointer wrapper with move only value semantics. Usefull for implementing the pointer to implem...
Definition pimpl.hpp:39
Subscriber(const SubscriberOptions &options, std::string_view topic_name, MessageCallback callback)
Construct a Subscriber with the specified callback.
Definition subscriber.impl.hpp:252
uint32_t get_publisher_count() const
Returns number of publishers registered at the topic.
Definition subscriber.impl.hpp:312
std::optional< std::shared_ptr< const MessageType > > get_new_message()
Get the latest new message, if there is one.
Definition subscriber.impl.hpp:276
std::string get_unresolved_topic_name() const
Returns the unresolved name of the topic handled by this Subscriber.
Definition subscriber.impl.hpp:306
std::optional< std::shared_ptr< const MessageType > > peek_new_message() const
Get the latest new message, if there is one.
Definition subscriber.impl.hpp:288
std::optional< std::shared_ptr< const MessageType > > peek_message() const
Get the latest received message, if there is one.
Definition subscriber.impl.hpp:282
std::function< void(std::shared_ptr< const MessageType >)> MessageCallback
Type of the accepted callback.
Definition subscriber.hpp:84
CoroCallback< void(std::shared_ptr< const MessageType >)> MessageCoroCallback
Type of the accepted coroutine callback.
Definition subscriber.hpp:87
std::optional< std::shared_ptr< const MessageType > > get_message()
Get the latest received message, if there is one.
Definition subscriber.impl.hpp:270
rclcpp::Time get_last_message_time() const
Returns time of the last received message on the topic, handled by this Subscriber.
Definition subscriber.impl.hpp:294
std::string get_topic_name() const
Returns the resolved (full) name of the topic handled by this Subscriber.
Definition subscriber.impl.hpp:300
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
rclcpp::node_interfaces::NodeInterfaces< rclcpp::node_interfaces::NodeBaseInterface, rclcpp::node_interfaces::NodeTimersInterface, rclcpp::node_interfaces::NodeClockInterface, rclcpp::node_interfaces::NodeLoggingInterface, rclcpp::node_interfaces::NodeParametersInterface, rclcpp::node_interfaces::NodeTopicsInterface > SubscriberNodeInterfaces
ROS node interfaces required by the Subscriber.
Definition subscriber.hpp:30
Tag for explicitly marking Subscriber that has no callback.
Definition subscriber.hpp:68
Options for configuring the Subscriber.
Definition subscriber.hpp:39
SubscriberNodeInterfaces node_interfaces
ROS node interfaces used by the subscriber.
Definition subscriber.hpp:41
std::optional< std::chrono::nanoseconds > no_message_timeout
Time after which timeout callback is called if no message is received.
Definition subscriber.hpp:49
std::function< void(std::string_view topic_name, rclcpp::Time last_msg)> timeout_callback
Timeout manager used for the no message timeout.
Definition subscriber.hpp:61
std::shared_ptr< rclcpp::CallbackGroup > callback_group
ROS callback group used by the subscriber callbacks.
Definition subscriber.hpp:46
std::shared_ptr< mrs_lib::TimeoutManager2 > timeout_manager
Timeout manager used for the no message timeout.
Definition subscriber.hpp:55
rclcpp::QoS qos
QOS settings for the subscriber.
Definition subscriber.hpp:44