ros2publisher

Publish messages on a topic

Description

Use the ros2publisher object to publish messages on a topic. When messages are published on that topic, ROS 2 nodes that subscribe to that topic receive those messages directly.

Creation

Description

pub = ros2publisher(node,topic) creates a publisher, pub, for a topic with name topic that already exists on the ROS 2 network. node is the ros2node object handle to which the publisher should attach. The publisher gets the topic message type from the network topic list.

Note

The topic must be on the network topic list.

example

pub = ros2publisher(node,topic,type) creates a publisher for a topic and adds that topic to the network topic list. If the topic list already contains a matching topic, pub will be added to the list of publishers for that topic.

pub = ros2publisher(___,Name,Value) specifies additional options using one or more name-value pair arguments. Specify name-value pair arguments after all other input arguments.

Input Arguments

expand all

A ros2node object on the network.

Name of the published topic, specified as a string scalar or character vector. If the topic does not exist, the object creates the topic using its associated message type.

This property is set at creating by the TopicName argument. The value cannot be changed after creation.

Example: "/chatter"

Data Types: char

Message type of published messages, specified as a string scalar or character vector. This message type remains associated with the topic and must be used for new messages published.

This property is set at creation by the MessageType argument. The value cannot be changed after creation.

Example: "std_msgs/String"

Data Types: char

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Determines the mode of storing messages in the queue. The queued messages will be sent to late-joining subscribers. If the queue fills with messages waiting to be processed, then old messages will be dropped to make room for new. If set to "keeplast", the queue stores the number of messages set by the Depth property. If set to "keepall", the queue stores all messages up to the resource limits of MATLAB®.

Data Types: double

Number of messages stored in the message queue when History is set to "keeplast".

Example: 42

Data Types: double

Affects the guarantee of message delivery. If "reliable", then delivery is guaranteed, but may retry multiple times. If "besteffort", then delivery is attempt, but retried.

Example: "reliable"

Data Types: char | string

Affects persistence of messages in publishers, which allows late-joining subscribers to receive the number of old messages specified by Depth. If "volatile", then messages do not persist. If "transientlocal", then publisher will persist most recent messages.

Example: "volatile"

Data Types: char | string

Properties

expand all

Name of the published topic, specified as a string scalar or character vector. If the topic does not exist, the object creates the topic using its associated message type.

This property is set at creating by the TopicName argument. The value cannot be changed after creation.

Example: "/chatter"

Data Types: char

Message type of published messages, specified as a string scalar or character vector. This message type remains associated with the topic and must be used for new messages published.

This property is set at creation by the MessageType argument. The value cannot be changed after creation.

Example: "std_msgs/String"

Data Types: char

This property is read-only.

Determines the mode of storing messages in the queue. The queued messages will be sent to late-joining subscribers. If the queue fills with messages waiting to be processed, then old messages will be dropped to make room for new. When set to "keeplast", the queue stores the number of messages set by the Depth property. Otherwise, when set to "keepall", the queue stores all messages up to the resource limits of MATLAB.

Example: "keeplast"

Data Types: char | string

This property is read-only.

Number of messages stored in the message queue when History is set to "keeplast".

Example: 42

Data Types: double

This property is read-only.

Affects the guarantee of message delivery. If "reliable", then delivery is guaranteed, but may retry multiple times. If "besteffort", then delivery is attempt, but retried.

Example: "reliable"

Data Types: char | string

This property is read-only.

Affects persistence of messages in publishers, which allows late-joining subscribers to receive the number of old messages specified by Depth. If "volatile", then messages do not persist. If "transientlocal", then publisher will persist most recent messages.

Example: "volatile"

Data Types: char | string

Object Functions

ros2messageCreate ROS 2 message structures
sendPublish ROS 2 message to topic

Examples

collapse all

Create a ROS 2 node.

node = ros2node("/node_1");

Create publisher and message.

chatPub = ros2publisher(node,"/chatter","std_msgs/String")
chatPub = 
  ros2publisher with properties:

      TopicName: '/chatter'
    MessageType: 'std_msgs/String'
        History: 'keeplast'
          Depth: 10
    Reliability: 'reliable'
     Durability: 'volatile'

msg = ros2message(chatPub)
msg = struct with fields:
    data: ''

Introduced in R2019b