RclcppSubscriberInterface Class Template
rclcpp implementation of the one-shot subscriber interface. More...
Declaration
class moveit_pro::behaviors::RclcppSubscriberInterface<MessageT> { ... }
Included Headers
Base class
| class | SubscriberInterface<MessageT> |
|
One-shot subscriber interface that receives a single message from a topic, then cleans up. More... | |
Public Constructors Index
template <typename MessageT> | |
| RclcppSubscriberInterface (const std::shared_ptr< BehaviorContext > &behavior_context) | |
Public Destructor Index
template <typename MessageT> | |
| ~RclcppSubscriberInterface () override | |
Public Member Functions Index
template <typename MessageT> | |
| auto | initialize (const std::string &topic_name, const std::chrono::duration< double > &wait_for_message_timeout, const std::chrono::duration< double > &wait_for_publisher_timeout, bool bypass_qos_negotiation=false) override -> tl::expected< void, std::string > |
|
Initializes the rclcpp subscriber interface to listen on the provided topic. More... | |
template <typename MessageT> | |
| auto | initialize (const std::string &topic_name, const std::chrono::duration< double > &wait_for_message_timeout, const std::chrono::duration< double > &wait_for_publisher_timeout, std::optional< std::function< void()> > on_haltable, bool bypass_qos_negotiation=false) -> tl::expected< void, std::string > |
|
An overloaded form of initialize with an additional callback parameter that is called when the current task can be halted. More... | |
template <typename MessageT> | |
| auto | getNextMessage () override -> std::future< tl::expected< MessageT, std::string > > |
|
Get a future for the next message on the topic. More... | |
template <typename MessageT> | |
| auto | syncGetNextMessage () override -> tl::expected< MessageT, std::string > |
|
Block until a message is received on the topic. More... | |
template <typename MessageT> | |
| void | halt () override |
|
Stops any publisher or message checking. More... | |
Private Member Functions Index
template <typename MessageT> | |
| void | messageCallback (const std::shared_ptr< MessageT > message) |
|
Callback function passed to the subscriber when it is created. More... | |
template <typename MessageT> | |
| void | setMessagePromise (tl::expected< MessageT, std::string > message) |
|
Sets the latest message promise with a message or an error. More... | |
template <typename MessageT> | |
| auto | waitForPublisher (const std::string &topic_name, const std::chrono::duration< double > &wait_for_publisher_timeout) -> tl::expected< rclcpp::TopicEndpointInfo, std::string > |
|
Waits for a publisher to be advertised on the provided topic, and gets the TopicEndpointInfo for the publisher once it appears. More... | |
Private Member Attributes Index
template <typename MessageT> | |
| std::shared_ptr< rclcpp::Node > | node_ |
|
Node used to create the subscriber. More... | |
template <typename MessageT> | |
| std::shared_ptr< rclcpp::CallbackGroup > | reentrant_callback_group_ |
|
Reentrant callback group used to create the subscriber. More... | |
template <typename MessageT> | |
| std::shared_ptr< rclcpp::Subscription< MessageT > > | subscriber_ |
|
The subscriber, which is created when initialize() is called. More... | |
template <typename MessageT> | |
| std::chrono::duration< double > | wait_for_message_timeout_ |
|
Duration to wait for the message to be available before failing. More... | |
template <typename MessageT> | |
| std::atomic_flag | halted_flag_ {} |
|
Indicates if the Subscriber was halted. More... | |
template <typename MessageT> | |
| std::atomic_flag | message_received_flag_ {} |
|
Flag set to false while syncGetNextMessage() is blocking while waiting for a message. More... | |
template <typename MessageT> | |
| std::promise< tl::expected< MessageT, std::string > > | latest_message_promise_ |
|
This promise is set to the received message (or error) when messageCallback() is called. More... | |
template <typename MessageT> | |
| std::shared_ptr< std::mutex > | callback_guard_ = std::make_shared<std::mutex>() |
|
Mutex shared with subscription callbacks to prevent use-after-free. More... | |
template <typename MessageT> | |
| std::shared_ptr< std::atomic_bool > | callback_active_ = std::make_shared<std::atomic_bool>(true) |
|
Flag shared with subscription callbacks, set to false in the destructor. More... | |
Description
rclcpp implementation of the one-shot subscriber interface.
- Template Parameters
-
MessageT ROS message type used to specialize this implementation.
Definition at line 84 of file subscriber_interface.hpp.
Public Constructors
RclcppSubscriberInterface()
| explicit |
Declaration at line 87 of file subscriber_interface.hpp, definition at line 26 of file subscriber_interface_impl.hpp.
Public Destructor
~RclcppSubscriberInterface()
|
Declaration at line 88 of file subscriber_interface.hpp, definition at line 32 of file subscriber_interface_impl.hpp.
Public Member Functions
getNextMessage()
| virtual |
Get a future for the next message on the topic.
- Returns
A future that will contain the message when it's received, or an error result if the message could not be received.
Declaration at line 140 of file subscriber_interface.hpp, definition at line 143 of file subscriber_interface_impl.hpp.
halt()
| virtual |
Stops any publisher or message checking.
Declaration at line 152 of file subscriber_interface.hpp, definition at line 164 of file subscriber_interface_impl.hpp.
initialize()
| virtual |
Initializes the rclcpp subscriber interface to listen on the provided topic.
By default, this blocks for a brief duration until a publisher is available on the topic. If no publisher appears before the timeout, returns an error result.
When bypass_qos_negotiation is false (default), after the publisher appears, get its current QoS settings, and set the subscriber's QoS settings to have the same reliability setting as the publisher. This ensures that the subscriber QoS always matches the publisher in situations where the same behavior may need to get messages from different sources which use different QoS settings (for example, retrieving images from a simulated camera publisher vs. a real-hardware camera driver).
When bypass_qos_negotiation is true, use default QoS settings without waiting for or negotiating with a publisher. This is useful for transient topics or situations where the publisher's presence cannot be waited for.
- Parameters
-
topic_name The topic name to use when creating the subscriber.
wait_for_message_timeout The timeout duration to use when waiting for a message to be received on the topic.
wait_for_publisher_timeout The timeout duration to use when waiting for a publisher to advertise on the topic. Ignored if bypass_qos_negotiation is true.
bypass_qos_negotiation If true, use default QoS instead of negotiating with publisher.
- Returns
Void if the subscriber interface was initialized successfully. Returns an error result if no publisher appeared on the topic or the subscriber could not be created.
Declaration at line 114 of file subscriber_interface.hpp, definition at line 70 of file subscriber_interface_impl.hpp.
initialize()
|
An overloaded form of initialize with an additional callback parameter that is called when the current task can be halted.
- Parameters
-
topic_name The topic name to use when creating the subscriber.
wait_for_message_timeout The timeout duration to use when waiting for a message to be received on the topic.
wait_for_publisher_timeout The timeout duration to use when waiting for a publisher to advertise on the topic. Ignored if bypass_qos_negotiation is true.
on_haltable Optional callback that is called when the current task can be halted.
bypass_qos_negotiation If true, use default QoS instead of negotiating with publisher.
Declaration at line 130 of file subscriber_interface.hpp, definition at line 80 of file subscriber_interface_impl.hpp.
syncGetNextMessage()
| virtual |
Block until a message is received on the topic.
- Returns
If the message was received, returns the message. Returns an error result if the timeout duration was exceeded before a message was received.
Declaration at line 147 of file subscriber_interface.hpp, definition at line 149 of file subscriber_interface_impl.hpp.
Private Member Functions
messageCallback()
|
Callback function passed to the subscriber when it is created.
Declaration at line 158 of file subscriber_interface.hpp, definition at line 172 of file subscriber_interface_impl.hpp.
setMessagePromise()
|
Sets the latest message promise with a message or an error.
Declaration at line 163 of file subscriber_interface.hpp, definition at line 178 of file subscriber_interface_impl.hpp.
waitForPublisher()
|
Waits for a publisher to be advertised on the provided topic, and gets the TopicEndpointInfo for the publisher once it appears.
- Parameters
-
topic_name of where we expect the publisher to advertise.
wait_for_publisher_timeout duration to wait before giving up on a publisher advertising. If negative, wait forever.
Declaration at line 173 of file subscriber_interface.hpp, definition at line 44 of file subscriber_interface_impl.hpp.
Private Member Attributes
callback_active_
|
Flag shared with subscription callbacks, set to false in the destructor.
Callbacks check this flag (under callback_guard_ lock) before accessing members.
Definition at line 210 of file subscriber_interface.hpp.
callback_guard_
|
Mutex shared with subscription callbacks to prevent use-after-free.
The callback lambda captures a shared_ptr to this mutex, so it remains valid even after this object is destroyed. The destructor locks this mutex to wait for any in-flight callbacks.
Definition at line 204 of file subscriber_interface.hpp.
halted_flag_
|
Indicates if the Subscriber was halted.
Definition at line 191 of file subscriber_interface.hpp.
latest_message_promise_
|
This promise is set to the received message (or error) when messageCallback() is called.
Definition at line 197 of file subscriber_interface.hpp.
message_received_flag_
|
Flag set to false while syncGetNextMessage() is blocking while waiting for a message.
Definition at line 194 of file subscriber_interface.hpp.
node_
|
Node used to create the subscriber.
Definition at line 176 of file subscriber_interface.hpp.
reentrant_callback_group_
|
Reentrant callback group used to create the subscriber.
This ensures that only one call to messageCallback() is handled at a time.
Definition at line 182 of file subscriber_interface.hpp.
subscriber_
|
The subscriber, which is created when initialize() is called.
Definition at line 185 of file subscriber_interface.hpp.
wait_for_message_timeout_
|
Duration to wait for the message to be available before failing.
Definition at line 188 of file subscriber_interface.hpp.
The documentation for this class was generated from the following files:
Generated via doxygen2docusaurus 2.2.2 by Doxygen 1.9.8.