Main Content

rospublisher

Publish message on a topic

Description

Use rospublisher to create a ROS publisher for sending messages via a ROS network. To create ROS messages, use rosmessage. Send these messages via the ROS publisher with the send function.

The Publisher object created by the function represents a publisher on the ROS network. The object publishes a specific message type on a given topic. When the Publisher object publishes a message to the topic, all subscribers to the topic receive this message. The same topic can have multiple publishers and subscribers.

Note

In a future release, ROS Toolbox will use message structures instead of objects for ROS messages.

To use message structures now, set the "DataFormat" name-value argument to "struct". For more information, see ROS Message Structures.

The publisher gets the topic message type from the topic list on the ROS master. When the MATLAB® global node publishes messages on that topic, ROS nodes that subscribe to that topic receive those messages. If the topic is not on the ROS master topic list, this function displays an error message. If the ROS master topic list already contains a matching topic, the ROS master adds the MATLAB global node to the list of publishers for that topic. To see a list of available topic names, at the MATLAB command prompt, type rostopic list.

You can create a Publisher object using the rospublisher function, or by calling ros.Publisher:

  • rospublisher only works with the global node using rosinit. It does not require a node object handle as an argument.

  • ros.Publisher works with additional nodes that are created using ros.Node. It requires a node object handle as the first argument.

Creation

Description

pub = rospublisher(topicname) creates a publisher for a specific topic name and sets the TopicName property. The topic must already exist on the ROS master topic list with an established MessageType.

pub = rospublisher(topicname,msgtype) creates a publisher for a topic and adds that topic to the ROS master topic list. The inputs are set to the TopicName and MessageType properties of the publisher. If the topic already exists and msgtype differs from the topic type on the ROS master topic list, the function displays an error message.

example

pub = rospublisher(___,Name,Value) provides additional options specified by one or more Name,Value pair arguments using any of the arguments from previous syntaxes. Name is the property name and Value is the corresponding value.

[pub,msg] = rospublisher(___) returns a message, msg, that you can send with the publisher, pub. The message is initialized with default values. You can also get the ROS message using the rosmessage function.

[pub,msg] = rospublisher(___,"DataFormat","struct") uses message structures instead of objects. For more information, see ROS Message Structures

pub = ros.Publisher(node,topicname) creates a publisher for a topic with name, topicname. node is the ros.Node object handle that this publisher attaches to. If node is specified as [], the publisher tries to attach to the global node.

pub = ros.Publisher(node,topicname,type) creates a publisher with specified message type, type. If the topic already exists, MATLAB checks the message type and displays an error if the input type differs. If the ROS master topic list already contains a matching topic, the ROS master adds the MATLAB global node to the list of publishers for that topic.

example

pub = ros.Publisher(___,"IsLatching",value) specifies if the publisher is latching with a Boolean, value. If a publisher is latching, it saves the last sent message and sends it to any new subscribers. By default, IsLatching is enabled.

[pub,msg] = ros.Publisher(___,"DataFormat","struct") uses message structures instead of objects. For more information, see ROS Message Structures

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

Indicator of whether publisher is latching, specified as true or false. A publisher that is latching saves the last sent message and resends it to any new subscribers.

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

Data Types: logical

Number of subscribers to the published topic, specified as an integer.

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

Data Types: double

Message format, specified as "object" or "struct". You must set this property on creation using the name-value input. For more information, see ROS Message Structures.

Object Functions

sendPublish ROS message to topic
rosmessageCreate ROS messages

Examples

collapse all

Start ROS master.

rosinit
Launching ROS Core...
Done in 0.82557 seconds.
Initializing ROS master on http://172.20.148.53:50298.
Initializing global node /matlab_global_node_39153 with NodeURI http://dcc867993glnxa64:39767/ and MasterURI http://localhost:50298.

Create publisher for the /chatter topic with the std_msgs/String message type. Set the "DataFormat" name-value argument to structure ROS messages.

chatpub = rospublisher("/chatter","std_msgs/String","DataFormat","struct");

Create a message to send. Specify the Data property with a character vector.

msg = rosmessage(chatpub);
msg.Data = 'test phrase';

Send the message via the publisher.

send(chatpub,msg);

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_39153 with NodeURI http://dcc867993glnxa64:39767/ and MasterURI http://localhost:50298.
Shutting down ROS master on http://172.20.148.53:50298.

Create a ROS publisher and view the associated properties for the rospublisher object. Add a subscriber and view the updated properties.

Start ROS master.

rosinit
Launching ROS Core...
Done in 0.49306 seconds.
Initializing ROS master on http://172.20.148.53:49710.
Initializing global node /matlab_global_node_64713 with NodeURI http://dcc867993glnxa64:43689/ and MasterURI http://localhost:49710.

Create a publisher and view its properties.

pub = rospublisher('/chatter','std_msgs/String','DataFormat','struct');

topic = pub.TopicName
topic = 
'/chatter'
subCount = pub.NumSubscribers
subCount = 
0

Subscribe to the publisher topic and view the changes in the NumSubscribers property.

sub = rossubscriber('/chatter','DataFormat','struct');
pause(1)

subCount = pub.NumSubscribers
subCount = 
1

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_64713 with NodeURI http://dcc867993glnxa64:43689/ and MasterURI http://localhost:49710.
Shutting down ROS master on http://172.20.148.53:49710.

Create a Publisher object using the class constructor.

Start the ROS core.

core = ros.Core;
Launching ROS Core...
.Done in 1.6481 seconds.

Create a ROS node, which connects to the master.

node = ros.Node('/test1');

Create a publisher and send string data. The publisher attaches to the node object in the first argument.

pub = ros.Publisher(node,'/robotname','std_msgs/String','DataFormat','struct');
msg = rosmessage(pub);
msg.Data = 'robot1';
send(pub,msg);

Clear the publisher and ROS node. Shut down the ROS master.

clear('pub','node')
clear('master')

Extended Capabilities

Version History

Introduced in R2019b

expand all