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 robotics.ros.Publisher:

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

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

Creation

Syntax

pub = rospublisher(topicname)
pub = rospublisher(topicname,msgtype)
pub = rospublisher(___,Name,Value)
[pub,msg] = rospublisher(___)
pub = robotics.ros.Publisher(node,topicname)
pub = robotics.ros.Publisher(node,topicname,type)
pub = robotics.ros.Publisher(___, "IsLatching",value)

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 = robotics.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 = robotics.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 = robotics.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

This property is read-only.

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.

Example: "/chatter"

Data Types: char

This property is read-only.

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.

Example: "std_msgs/String"

Data Types: char

This property is read-only.

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.

Data Types: logical

This property is read-only.

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

Data Types: double

Object Functions

sendPublish ROS message to topic
rosmessageCreate ROS messages

Examples

collapse all

Connect to a ROS network.

rosinit
Initializing ROS master on http://bat800207glnxa64:43273/.
Initializing global node /matlab_global_node_55461 with NodeURI http://bat800207glnxa64:45081/

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 message via the publisher.

send(chatpub,msg);

Shutdown ROS network.

rosshutdown
Shutting down global node /matlab_global_node_55461 with NodeURI http://bat800207glnxa64:45081/
Shutting down ROS master on http://bat800207glnxa64:43273/.

Create a ROS publisher and view the associated properties for the robotics.ros.Publisher object. Add a subscriber to view the updated properties.

Start ROS master.

rosinit
Initializing ROS master on http://bat800213glnxa64:45121/.
Initializing global node /matlab_global_node_89657 with NodeURI http://bat800213glnxa64:39885/

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
rosshutdown
Shutting down global node /matlab_global_node_89657 with NodeURI http://bat800213glnxa64:39885/
Shutting down ROS master on http://bat800213glnxa64:45121/.

Connect to a ROS network.

rosinit
Initializing ROS master on http://bat800616glnxa64:35163/.
Initializing global node /matlab_global_node_88541 with NodeURI http://bat800616glnxa64:37411/

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 ROS network.

rosshutdown
Shutting down global node /matlab_global_node_88541 with NodeURI http://bat800616glnxa64:37411/
Shutting down ROS master on http://bat800616glnxa64:35163/.

Create a Publisher object using the class constructor.

Start the ROS master.

master = robotics.ros.Core;

Create a ROS node, which connects to the master.

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

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

pub = robotics.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 R2015a