send
Publish ROS message to topic
Syntax
Description
Examples
Set up a publisher and subscriber to send and receive a message on a ROS network.
Connect to a ROS network.
rosinit
Launching ROS Core... Done in 0.52367 seconds. Initializing ROS master on http://172.20.222.142:50872. Initializing global node /matlab_global_node_23886 with NodeURI http://dcc2548638glnxa64:40021/ and MasterURI http://localhost:50872.
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_23886 with NodeURI http://dcc2548638glnxa64:40021/ and MasterURI http://localhost:50872. Shutting down ROS master on http://172.20.222.142:50872.
Input Arguments
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
Usage notes and limitations:
Supported only for
structmessages.
Version History
Introduced in R2019bYou can now create messages as structures with fields matching the message object properties. Using structures typically improves performance of creating, updating, and using ROS messages, but message fields are no longer validated when set. Message types and corresponding field values from the structures are validated when sent across the network.
To use ROS messages as structures, use the "DataFormat" name-value
argument when creating your publishers, subscribers, or other ROS objects. Any messages
generated from these objects will utilize structures.
pub = rospublisher("/scan","sensor_msgs/LaserScan","DataFormat","struct") msg = rosmessage(pub)
You can also create messages as structures directly, but make sure to specify the data
format as "struct" for the publisher, subscriber, or other ROS objects as
well. ROS objects still use message objects by default.
msg = rosmessage("/scan","sensor_msgs/LaserScan","DataFormat","struct") ... pub = rospublisher("/scan","sensor_msgs/LaserScan","DataFormat","struct")
In a future release, ROS messages will use structures by default and ROS message objects will be removed.
For more information, see Improve Performance of ROS Using Message Structures.
See Also
receive | rosmessage | rostopic | rossubscriber | rospublisher
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)