rclpy.publisher module

class rclpy.publisher.Publisher(publisher_impl: rpyutils.import_c_library.Publisher, msg_type: MsgType, topic: str, qos_profile: rclpy.qos.QoSProfile, event_callbacks: PublisherEventCallbacks, callback_group: CallbackGroup)

Bases: object

Create a container for a ROS publisher.

Warning

Users should not create a publisher with this constuctor, instead they should call Node.create_publisher().

A publisher is used as a primary means of communication in a ROS system by publishing messages on a ROS topic.

Parameters:
  • publisher_impl – Publisher wrapping the underlying rcl_publisher_t object.

  • msg_type – The type of ROS messages the publisher will publish.

  • topic – The name of the topic the publisher will publish to.

  • qos_profile – The quality of service profile to apply to the publisher.

assert_liveliness() None

Manually assert that this Publisher is alive.

If the QoS Liveliness policy is set to MANUAL_BY_TOPIC, the application must call this at least as often as QoSProfile.liveliness_lease_duration.

destroy()
get_subscription_count() int

Get the amount of subscribers that this publisher has.

property handle
publish(msg: MsgType | bytes) None

Send a message to the topic for the publisher.

Parameters:

msg – The ROS message to publish.

Raises:

TypeError if the type of the passed message isn’t an instance of the provided type when the publisher was constructed.

property topic_name: str
wait_for_all_acked(timeout: rclpy.duration.Duration = rclpy.duration.Duration) bool

Wait until all published message data is acknowledged or until the timeout elapses.

If the timeout is negative then this function will block indefinitely until all published message data is acknowledged. If the timeout is 0 then it will check if all published message has been acknowledged without waiting. If the timeout is greater than 0 then it will return after that period of time has elapsed or all published message data is acknowledged. This function only waits for acknowledgments if the publisher’s QOS profile is RELIABLE. Otherwise this function will immediately return true.

Parameters:

timeout – the duration to wait for all published message data to be acknowledged.

Returns:

true if all published message data is acknowledged before the timeout, otherwise false.