ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
rclcpp::PublisherBase Class Reference
Inheritance diagram for rclcpp::PublisherBase:
Inheritance graph
[legend]
Collaboration diagram for rclcpp::PublisherBase:
Collaboration graph
[legend]

Public Types

using IntraProcessManagerSharedPtr = std::shared_ptr< rclcpp::experimental::IntraProcessManager >
 

Public Member Functions

RCLCPP_PUBLIC PublisherBase (rclcpp::node_interfaces::NodeBaseInterface *node_base, const std::string &topic, const rosidl_message_type_support_t &type_support, const rcl_publisher_options_t &publisher_options)
 Default constructor. More...
 
RCLCPP_PUBLIC const char * get_topic_name () const
 Get the topic that this publisher publishes on. More...
 
RCLCPP_PUBLIC size_t get_queue_size () const
 Get the queue size for this publisher. More...
 
RCLCPP_PUBLIC const rmw_gid_t & get_gid () const
 Get the global identifier for this publisher (used in rmw and by DDS). More...
 
RCLCPP_PUBLIC std::shared_ptr< rcl_publisher_tget_publisher_handle ()
 Get the rcl publisher handle. More...
 
RCLCPP_PUBLIC std::shared_ptr< const rcl_publisher_tget_publisher_handle () const
 Get the rcl publisher handle. More...
 
RCLCPP_PUBLIC const std::unordered_map< rcl_publisher_event_type_t, std::shared_ptr< rclcpp::QOSEventHandlerBase > > & get_event_handlers () const
 Get all the QoS event handlers associated with this publisher. More...
 
RCLCPP_PUBLIC size_t get_subscription_count () const
 Get subscription count. More...
 
RCLCPP_PUBLIC size_t get_intra_process_subscription_count () const
 Get intraprocess subscription count. More...
 
RCLCPP_PUBLIC RCUTILS_WARN_UNUSED bool assert_liveliness () const
 Manually assert that this Publisher is alive (for RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC). More...
 
RCLCPP_PUBLIC rclcpp::QoS get_actual_qos () const
 Get the actual QoS settings, after the defaults have been determined. More...
 
RCLCPP_PUBLIC bool can_loan_messages () const
 Check if publisher instance can loan messages. More...
 
RCLCPP_PUBLIC bool operator== (const rmw_gid_t &gid) const
 Compare this publisher to a gid. More...
 
RCLCPP_PUBLIC bool operator== (const rmw_gid_t *gid) const
 Compare this publisher to a pointer gid. More...
 
RCLCPP_PUBLIC void setup_intra_process (uint64_t intra_process_publisher_id, IntraProcessManagerSharedPtr ipm)
 Implementation utility function used to setup intra process publishing after creation.
 
RCLCPP_PUBLIC std::vector< rclcpp::NetworkFlowEndpointget_network_flow_endpoints () const
 Get network flow endpoints. More...
 
template<typename DurationRepT = int64_t, typename DurationT = std::milli>
bool wait_for_all_acked (std::chrono::duration< DurationRepT, DurationT > timeout=std::chrono::duration< DurationRepT, DurationT >(-1)) const
 Wait until all published messages are acknowledged or until the specified timeout elapses. More...
 
void set_on_new_qos_event_callback (std::function< void(size_t)> callback, rcl_publisher_event_type_t event_type)
 Set a callback to be called when each new qos event instance occurs. More...
 
void clear_on_new_qos_event_callback (rcl_publisher_event_type_t event_type)
 Unset the callback registered for new qos events, if any.
 

Protected Types

using IntraProcessManagerWeakPtr = std::weak_ptr< rclcpp::experimental::IntraProcessManager >
 

Protected Member Functions

template<typename EventCallbackT >
void add_event_handler (const EventCallbackT &callback, const rcl_publisher_event_type_t event_type)
 
RCLCPP_PUBLIC void default_incompatible_qos_callback (QOSOfferedIncompatibleQoSInfo &info) const
 

Protected Attributes

std::shared_ptr< rcl_node_trcl_node_handle_
 
std::shared_ptr< rcl_publisher_tpublisher_handle_
 
std::unordered_map< rcl_publisher_event_type_t, std::shared_ptr< rclcpp::QOSEventHandlerBase > > event_handlers_
 
bool intra_process_is_enabled_
 
IntraProcessManagerWeakPtr weak_ipm_
 
uint64_t intra_process_publisher_id_
 
rmw_gid_t rmw_gid_
 
const rosidl_message_type_support_t type_support_
 

Detailed Description

Definition at line 60 of file publisher_base.hpp.

Constructor & Destructor Documentation

◆ PublisherBase()

PublisherBase::PublisherBase ( rclcpp::node_interfaces::NodeBaseInterface node_base,
const std::string &  topic,
const rosidl_message_type_support_t &  type_support,
const rcl_publisher_options_t publisher_options 
)

Default constructor.

Typically, a publisher is not created through this method, but instead is created through a call to Node::create_publisher.

Parameters
[in]node_baseA pointer to the NodeBaseInterface for the parent node.
[in]topicThe topic that this publisher publishes on.
[in]type_supportThe type support structure for the type to be published.
[in]publisher_optionsQoS settings for this publisher.

Definition at line 44 of file publisher_base.cpp.

References rclcpp::expand_topic_or_service_name(), rclcpp::Logger::get_child(), rclcpp::get_node_logger(), rcl_get_zero_initialized_publisher(), rcl_node_get_name(), rcl_node_get_namespace(), rcl_publisher_fini(), rcl_publisher_get_rmw_handle(), rcl_publisher_init(), RCL_RET_OK, and RCL_RET_TOPIC_NAME_INVALID.

Here is the call graph for this function:

Member Function Documentation

◆ assert_liveliness()

bool PublisherBase::assert_liveliness ( ) const

Manually assert that this Publisher is alive (for RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC).

If the rmw Liveliness policy is set to RMW_QOS_POLICY_LIVELINESS_MANUAL_BY_TOPIC, the creator of this publisher may manually call assert_liveliness at some point in time to signal to the rest of the system that this Node is still alive.

Returns
true if the liveliness was asserted successfully, otherwise false

Definition at line 228 of file publisher_base.cpp.

References rcl_publisher_assert_liveliness(), and RCL_RET_OK.

Here is the call graph for this function:

◆ can_loan_messages()

bool PublisherBase::can_loan_messages ( ) const

Check if publisher instance can loan messages.

Depending on the middleware and the message type, this will return true if the middleware can allocate a ROS message instance.

Definition at line 234 of file publisher_base.cpp.

References rcl_publisher_can_loan_messages().

Referenced by rclcpp::LoanedMessage< MessageT, AllocatorT >::LoanedMessage(), rclcpp::Publisher< MessageT, AllocatorT >::publish(), rclcpp::LoanedMessage< MessageT, AllocatorT >::release(), and rclcpp::LoanedMessage< MessageT, AllocatorT >::~LoanedMessage().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_actual_qos()

rclcpp::QoS PublisherBase::get_actual_qos ( ) const

Get the actual QoS settings, after the defaults have been determined.

The actual configuration applied when using RMW_QOS_POLICY_*_SYSTEM_DEFAULT can only be resolved after the creation of the publisher, and it depends on the underlying rmw implementation. If the underlying setting in use can't be represented in ROS terms, it will be set to RMW_QOS_POLICY_*_UNKNOWN. May throw runtime_error when an unexpected error occurs.

Returns
The actual qos settings.

Definition at line 215 of file publisher_base.cpp.

References rclcpp::QoSInitialization::from_rmw(), and rcl_publisher_get_actual_qos().

Referenced by rclcpp::Publisher< MessageT, AllocatorT >::post_init_setup().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_event_handlers()

const std::unordered_map< rcl_publisher_event_type_t, std::shared_ptr< rclcpp::QOSEventHandlerBase > > & PublisherBase::get_event_handlers ( ) const

Get all the QoS event handlers associated with this publisher.

Returns
The map of QoS event handlers.

Definition at line 167 of file publisher_base.cpp.

◆ get_gid()

const rmw_gid_t & PublisherBase::get_gid ( ) const

Get the global identifier for this publisher (used in rmw and by DDS).

Returns
The gid.

Definition at line 148 of file publisher_base.cpp.

Referenced by operator==().

Here is the caller graph for this function:

◆ get_intra_process_subscription_count()

size_t PublisherBase::get_intra_process_subscription_count ( ) const

Get intraprocess subscription count.

Returns
The number of intraprocess subscriptions.

Definition at line 198 of file publisher_base.cpp.

Referenced by rclcpp::Publisher< MessageT, AllocatorT >::publish().

Here is the caller graph for this function:

◆ get_network_flow_endpoints()

std::vector< rclcpp::NetworkFlowEndpoint > PublisherBase::get_network_flow_endpoints ( ) const

Get network flow endpoints.

Describes network flow endpoints that this publisher is sending messages out on

Returns
vector of NetworkFlowEndpoint

Definition at line 282 of file publisher_base.cpp.

References RCL_RET_OK.

◆ get_publisher_handle() [1/2]

std::shared_ptr< rcl_publisher_t > PublisherBase::get_publisher_handle ( )

Get the rcl publisher handle.

Returns
The rcl publisher handle.

Definition at line 154 of file publisher_base.cpp.

Referenced by rclcpp::LoanedMessage< MessageT, AllocatorT >::LoanedMessage(), rclcpp::GenericPublisher::publish(), and rclcpp::LoanedMessage< MessageT, AllocatorT >::~LoanedMessage().

Here is the caller graph for this function:

◆ get_publisher_handle() [2/2]

std::shared_ptr< const rcl_publisher_t > PublisherBase::get_publisher_handle ( ) const

Get the rcl publisher handle.

Returns
The rcl publisher handle.

Definition at line 160 of file publisher_base.cpp.

◆ get_queue_size()

size_t PublisherBase::get_queue_size ( ) const

Get the queue size for this publisher.

Returns
The queue size.

Definition at line 135 of file publisher_base.cpp.

References rcl_publisher_options_s::qos, and rcl_publisher_get_options().

Here is the call graph for this function:

◆ get_subscription_count()

size_t PublisherBase::get_subscription_count ( ) const

Get subscription count.

Returns
The number of subscriptions.

Definition at line 173 of file publisher_base.cpp.

References rcl_context_is_valid(), rcl_publisher_get_context(), rcl_publisher_get_subscription_count(), rcl_publisher_is_valid_except_context(), RCL_RET_OK, and RCL_RET_PUBLISHER_INVALID.

Referenced by rclcpp::Publisher< MessageT, AllocatorT >::publish().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_topic_name()

const char * PublisherBase::get_topic_name ( ) const

Get the topic that this publisher publishes on.

Returns
The topic name.

Definition at line 129 of file publisher_base.cpp.

References rcl_publisher_get_topic_name().

Here is the call graph for this function:

◆ operator==() [1/2]

bool PublisherBase::operator== ( const rmw_gid_t &  gid) const

Compare this publisher to a gid.

Note that this function calls the next function.

Parameters
[in]gidReference to a gid.
Returns
True if the publisher's gid matches the input.

Definition at line 240 of file publisher_base.cpp.

◆ operator==() [2/2]

bool PublisherBase::operator== ( const rmw_gid_t *  gid) const

Compare this publisher to a pointer gid.

A wrapper for comparing this publisher's gid to the input using rmw_compare_gids_equal.

Parameters
[in]gidA pointer to a gid.
Returns
True if this publisher's gid matches the input.

Definition at line 246 of file publisher_base.cpp.

References get_gid().

Here is the call graph for this function:

◆ set_on_new_qos_event_callback()

void rclcpp::PublisherBase::set_on_new_qos_event_callback ( std::function< void(size_t)>  callback,
rcl_publisher_event_type_t  event_type 
)
inline

Set a callback to be called when each new qos event instance occurs.

The callback receives a size_t which is the number of events that occurred since the last time this callback was called. Normally this is 1, but can be > 1 if events occurred before any callback was set.

Since this callback is called from the middleware, you should aim to make it fast and not blocking. If you need to do a lot of work or wait for some other event, you should spin it off to another thread, otherwise you risk blocking the middleware.

Calling it again will clear any previously set callback.

An exception will be thrown if the callback is not callable.

This function is thread-safe.

If you want more information available in the callback, like the qos event or other information, you may use a lambda with captures or std::bind.

See also
rclcpp::QOSEventHandlerBase::set_on_ready_callback
Parameters
[in]callbackfunctor to be called when a new event occurs
[in]event_typeidentifier for the qos event we want to attach the callback to

Definition at line 278 of file publisher_base.hpp.

References rclcpp::get_logger().

Here is the call graph for this function:

◆ wait_for_all_acked()

template<typename DurationRepT = int64_t, typename DurationT = std::milli>
bool rclcpp::PublisherBase::wait_for_all_acked ( std::chrono::duration< DurationRepT, DurationT >  timeout = std::chrono::duration<DurationRepT, DurationT>(-1)) const
inline

Wait until all published messages are acknowledged or until the specified timeout elapses.

This method waits until all published messages are acknowledged by all matching subscriptions or the given timeout elapses.

If the timeout is negative then this method will block indefinitely until all published messages are acknowledged. If the timeout is zero then this method will not block, it will check if all published messages are acknowledged and return immediately. If the timeout is greater than zero, this method will wait until all published messages are acknowledged or the timeout elapses.

This method only waits for acknowledgments if the publisher's QoS profile is RELIABLE. Otherwise this method will immediately return true.

Parameters
[in]timeoutthe duration to wait for all published messages to be acknowledged.
Returns
true if all published messages were acknowledged before the given timeout elapsed, otherwise false.
Exceptions
rclcpp::exceptions::RCLErrorif middleware doesn't support or internal error occurs
std::invalid_argumentif timeout is greater than std::chrono::nanoseconds::max() or less than std::chrono::nanoseconds::min()

Definition at line 235 of file publisher_base.hpp.

References rcl_publisher_wait_for_all_acked(), RCL_RET_OK, and RCL_RET_TIMEOUT.

Here is the call graph for this function:

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