Subscriber
mrs_lib::Subscriber
-
template<typename MessageType>
class Subscriber Wrapper for rclcpp::Subscription with callback and polling interface.
- Template Parameters:
MessageType – Type of the message to subscribe to.
Public Types
-
using MessageCallback = std::function<void(std::shared_ptr<const MessageType>)>
Type of the accepted callback.
-
using MessageCoroCallback = CoroCallback<void(std::shared_ptr<const MessageType>)>
Type of the accepted coroutine callback.
Public Functions
-
inline explicit Subscriber(const SubscriberOptions &options, std::string_view topic_name, MessageCallback callback)
Construct a Subscriber with the specified callback.
- Parameters:
options – Options to configure the subscriber.
topic_name – Name of the topic to subscribe to.
callback – Callback to call when a message is received.
-
inline explicit Subscriber(const SubscriberOptions &options, std::string_view topic_name, MessageCoroCallback callback)
Construct a Subscriber with the specified coroutine callback.
- Parameters:
options – Options to configure the subscriber.
topic_name – Name of the topic to subscribe to.
callback – Coroutine callback to call when a message is received.
-
inline explicit Subscriber(const SubscriberOptions &options, std::string_view topic_name, PolledSubscriberTag tag)
Construct a Subscriber without any callback.
- Parameters:
options – Options to configure the subscriber.
topic_name – Name of the topic to subscribe to.
tag – Tag to explicitly specify that this subscriber has no callback.
-
std::optional<std::shared_ptr<const MessageType>> get_message()
Get the latest received message, if there is one.
Calling this function marks the message as used.
- Returns:
The latest message, if there is any, nullopt otherwise.
-
std::optional<std::shared_ptr<const MessageType>> get_new_message()
Get the latest new message, if there is one.
A new message is a message that was not yet used. A message is always marked as used if there is a callback associated with the subscriber. Additionally, it is marked as used by obtaining it using get_*message.
Calling this function marks the message as used.
- Returns:
The latest new message, if there is any, nullopt otherwise.
-
std::optional<std::shared_ptr<const MessageType>> peek_message() const
Get the latest received message, if there is one.
Calling this function does NOT mark the message as used.
- Returns:
The latest message, if there is any, nullopt otherwise.
-
std::optional<std::shared_ptr<const MessageType>> peek_new_message() const
Get the latest new message, if there is one.
A new message is a message that was not yet used. A message is always marked as used if there is a callback associated with the subscriber. Additionally, it is marked as used by obtaining it using get_*message.
Calling this function does NOT mark the message as used.
- Returns:
The latest new message, if there is any, nullopt otherwise.
-
rclcpp::Time get_last_message_time() const
Returns time of the last received message on the topic, handled by this Subscriber.
- Returns:
time when the last message was received.
-
std::string get_topic_name() const
Returns the resolved (full) name of the topic handled by this Subscriber.
- Returns:
The name of the handled topic.
-
std::string get_unresolved_topic_name() const
Returns the unresolved name of the topic handled by this Subscriber.
- Returns:
The name of the handled topic.
-
uint32_t get_publisher_count() const
Returns number of publishers registered at the topic.
- Returns:
number of publishers.
mrs_lib::SubscriberOptions
-
struct SubscriberOptions
Options for configuring the Subscriber.
Public Members
-
SubscriberNodeInterfaces node_interfaces
ROS node interfaces used by the subscriber.
-
rclcpp::QoS qos = rclcpp::SystemDefaultsQoS()
QOS settings for the subscriber.
-
std::shared_ptr<rclcpp::CallbackGroup> callback_group = nullptr
ROS callback group used by the subscriber callbacks.
-
std::optional<std::chrono::nanoseconds> no_message_timeout = std::nullopt
Time after which timeout callback is called if no message is received.
-
std::shared_ptr<mrs_lib::TimeoutManager2> timeout_manager = nullptr
Timeout manager used for the no message timeout.
If nullptr and timeout is set, the Subscriber will create its own timeout manager.
-
std::function<void(std::string_view topic_name, rclcpp::Time last_msg)> timeout_callback = {}
Timeout manager used for the no message timeout.
If not set, a default will be used.
-
SubscriberNodeInterfaces node_interfaces
Example
1class ExampleNode : public rclcpp::Node
2{
3public:
4 ExampleNode(const rclcpp::NodeOptions& options = rclcpp::NodeOptions{})
5 : Node("example_node", options),
6 logger_(*this),
7 subscriber_(mrs_lib::SubscriberOptions{.node_interfaces = *this}, "example_topic", std::bind_front(&ExampleNode::message_callback, this))
8 {
9 }
10
11 [[nodiscard]] int64_t get_last_val() const
12 {
13 return last_val_;
14 }
15
16private:
17 void message_callback(std::shared_ptr<const std_msgs::msg::Int64> msg)
18 {
19 logger_.info("Message received with payload: {}", msg->data);
20 last_val_ = msg->data;
21 }
22
23 mrs_lib::Logger logger_;
24
25 std::atomic<int64_t> last_val_ = 0;
26
27 mrs_lib::Subscriber<std_msgs::msg::Int64> subscriber_;
28};