mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
mrs_lib::Subscriber< MessageType > Class Template Reference

Wrapper for rclcpp::Subscription with callback and polling interface. More...

#include <subscriber.hpp>

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 Member Functions

 Subscriber (const SubscriberOptions &options, std::string_view topic_name, MessageCallback callback)
 Construct a Subscriber with the specified callback.
 Subscriber (const SubscriberOptions &options, std::string_view topic_name, MessageCoroCallback callback)
 Construct a Subscriber with the specified coroutine callback.
 Subscriber (const SubscriberOptions &options, std::string_view topic_name, PolledSubscriberTag tag)
 Construct a Subscriber without any callback.
std::optional< std::shared_ptr< const MessageType > > get_message ()
 Get the latest received message, if there is one.
std::optional< std::shared_ptr< const MessageType > > get_new_message ()
 Get the latest new message, if there is one.
std::optional< std::shared_ptr< const MessageType > > peek_message () const
 Get the latest received message, if there is one.
std::optional< std::shared_ptr< const MessageType > > peek_new_message () const
 Get the latest new message, if there is one.
rclcpp::Time get_last_message_time () const
 Returns time of the last received message on the topic, handled by this Subscriber.
std::string get_topic_name () const
 Returns the resolved (full) name of the topic handled by this Subscriber.
std::string get_unresolved_topic_name () const
 Returns the unresolved name of the topic handled by this Subscriber.
uint32_t get_publisher_count () const
 Returns number of publishers registered at the topic.

Detailed Description

template<typename MessageType>
class mrs_lib::Subscriber< MessageType >

Wrapper for rclcpp::Subscription with callback and polling interface.

Template Parameters
MessageTypeType of the message to subscribe to.

Constructor & Destructor Documentation

◆ Subscriber() [1/3]

template<typename MessageType>
mrs_lib::Subscriber< MessageType >::Subscriber ( const SubscriberOptions & options,
std::string_view topic_name,
MessageCallback callback )
inlineexplicit

Construct a Subscriber with the specified callback.

Parameters
optionsOptions to configure the subscriber.
topic_nameName of the topic to subscribe to.
callbackCallback to call when a message is received.

◆ Subscriber() [2/3]

template<typename MessageType>
mrs_lib::Subscriber< MessageType >::Subscriber ( const SubscriberOptions & options,
std::string_view topic_name,
MessageCoroCallback callback )
inlineexplicit

Construct a Subscriber with the specified coroutine callback.

Parameters
optionsOptions to configure the subscriber.
topic_nameName of the topic to subscribe to.
callbackCoroutine callback to call when a message is received.

◆ Subscriber() [3/3]

template<typename MessageType>
mrs_lib::Subscriber< MessageType >::Subscriber ( const SubscriberOptions & options,
std::string_view topic_name,
PolledSubscriberTag tag )
inlineexplicit

Construct a Subscriber without any callback.

Parameters
optionsOptions to configure the subscriber.
topic_nameName of the topic to subscribe to.
tagTag to explicitly specify that this subscriber has no callback.

Member Function Documentation

◆ get_last_message_time()

template<typename MessageType>
rclcpp::Time mrs_lib::Subscriber< MessageType >::get_last_message_time ( ) const
nodiscard

Returns time of the last received message on the topic, handled by this Subscriber.

Returns
time when the last message was received.

◆ get_message()

template<typename MessageType>
std::optional< std::shared_ptr< const MessageType > > mrs_lib::Subscriber< MessageType >::get_message ( )
nodiscard

Get the latest received message, if there is one.

Returns
The latest message, if there is any, nullopt otherwise.

Calling this function marks the message as used.

◆ get_new_message()

template<typename MessageType>
std::optional< std::shared_ptr< const MessageType > > mrs_lib::Subscriber< MessageType >::get_new_message ( )
nodiscard

Get the latest new message, if there is one.

Returns
The latest new message, if there is any, nullopt otherwise.

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.

◆ get_publisher_count()

template<typename MessageType>
uint32_t mrs_lib::Subscriber< MessageType >::get_publisher_count ( ) const
nodiscard

Returns number of publishers registered at the topic.

Returns
number of publishers.

◆ get_topic_name()

template<typename MessageType>
std::string mrs_lib::Subscriber< MessageType >::get_topic_name ( ) const
nodiscard

Returns the resolved (full) name of the topic handled by this Subscriber.

Returns
The name of the handled topic.

◆ get_unresolved_topic_name()

template<typename MessageType>
std::string mrs_lib::Subscriber< MessageType >::get_unresolved_topic_name ( ) const
nodiscard

Returns the unresolved name of the topic handled by this Subscriber.

Returns
The name of the handled topic.

◆ peek_message()

template<typename MessageType>
std::optional< std::shared_ptr< const MessageType > > mrs_lib::Subscriber< MessageType >::peek_message ( ) const
nodiscard

Get the latest received message, if there is one.

Returns
The latest message, if there is any, nullopt otherwise.

Calling this function does NOT mark the message as used.

◆ peek_new_message()

template<typename MessageType>
std::optional< std::shared_ptr< const MessageType > > mrs_lib::Subscriber< MessageType >::peek_new_message ( ) const
nodiscard

Get the latest new message, if there is one.

Returns
The latest new message, if there is any, nullopt otherwise.

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.


The documentation for this class was generated from the following files: