Main Content

send

R2026b

Publish ROS message to topic

Description

send(pub,msg) publishes a message to the topic specified by the publisher, pub. This message can be received by all subscribers in the ROS network that are subscribed to the topic specified by pub.

example

Examples

collapse all

Set up a publisher and subscriber to send and receive a message on a ROS network.

Connect to a ROS network.

rosinit
The value of the ROS_MASTER_URI environment variable, http://172.23.65.36:59698, will be used to connect to the ROS master.
Initializing global node /matlab_global_node_90452 with NodeURI http://172.23.65.36:39755/ and MasterURI http://172.23.65.36:59698.

Create a publisher with a specific topic and message type. You can also return a default message to send using this publisher.

[pub,msg] = rospublisher('position','geometry_msgs/Point');

Modify the message before sending it over the network.

msg.X = 1;
msg.Y = 2;
send(pub,msg);

Create a subscriber and wait for the latest message. Verify the message is the one you sent.

sub = rossubscriber('position')
sub = 
  Subscriber with properties:

                      TopicName: '/position'
                  LatestMessage: [1×1 Point]
                    MessageType: 'geometry_msgs/Point'
                     BufferSize: 1
    MessagePreprocessingEnabled: 0
                  NewMessageFcn: []
                     DataFormat: 'object'

pause(1);
sub.LatestMessage
ans = 
  ROS Point message with properties:

    MessageType: 'geometry_msgs/Point'
              X: 1
              Y: 2
              Z: 0

  Use showdetails to show the contents of the message

Shut down the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_90452 with NodeURI http://172.23.65.36:39755/ and MasterURI http://172.23.65.36:59698.

Input Arguments

collapse all

ROS publisher, specified as a Publisher object handle. You can create the object using rospublisher.

ROS message, specified as a Message object handle or structure. You can create object using rosmessage.

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.

Extended Capabilities

expand all

Version History

Introduced in R2019b

expand all