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.

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.

example

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.

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 = ros.Publisher(node,topicname) creates a publisher for a topic with name, topicname. node is the robotics.ros.Node object handle that this publisher attaches to. If node is specified as [], the publisher tries to attach to the global node.

example

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.

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.

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

Object Functions

sendPublish ROS message to topic
rosmessageCreate ROS messages

Examples

collapse all

Start ROS master.

rosinit
Initializing ROS master on http://bat584805glnxa64:43157/.
Initializing global node /matlab_global_node_00452 with NodeURI http://bat584805glnxa64:38913/

Create publisher for the '/chatter' topic with the 'std_msgs/String' message type.

chatpub = rospublisher('/chatter','std_msgs/String');

Create a message to send. Specify the Data property.

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_00452 with NodeURI http://bat584805glnxa64:38913/
Shutting down ROS master on http://bat584805glnxa64:43157/.

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
Initializing ROS master on http://bat1074506glnxa64:44889/.
Initializing global node /matlab_global_node_46244 with NodeURI http://bat1074506glnxa64:36827/

Create a publisher and view its properties.

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

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');
pause(1)

subCount = pub.NumSubscribers
subCount = 1

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_46244 with NodeURI http://bat1074506glnxa64:36827/
Shutting down ROS master on http://bat1074506glnxa64:44889/.

Connect to a ROS network.

rosinit
Initializing ROS master on http://bat1073807glnxa64:33461/.
Initializing global node /matlab_global_node_66222 with NodeURI http://bat1073807glnxa64:45637/

Create a message to send. Specify the Data property.

msg = rosmessage('std_msgs/String');
msg.Data = 'test phrase';

Send message via the '/chatter' topic.

rospublisher('/chatter',msg)
ans =

     []

Shutdown the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_66222 with NodeURI http://bat1073807glnxa64:45637/
Shutting down ROS master on http://bat1073807glnxa64:33461/.

Create a Publisher object using the class constructor.

Start the ROS master.

master = ros.Core;

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');
msg = rosmessage('std_msgs/String');
msg.Data = 'robot1';
send(pub,msg);

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

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

Introduced in R2019b