www.gusucode.com > robotics 案例源码程序 matlab代码 > robotics/ConnectingToMultipleROSNodesUsingNodeObjectsExample.m

    %% Create Multiple ROS Nodes
% Create multiple ROS nodes. Use the 
% |Node| object with publishers, subscribers, and other ROS functionality 
% to specify with which node you are connecting to.

% Copyright 2015 The MathWorks, Inc.


%%
% Create a ROS master.
master = robotics.ros.Core;

%%
% Initialize multiple nodes.

node1 = robotics.ros.Node('/test_node_1');
node2 = robotics.ros.Node('/test_node_2');

%%
% Use these nodes to perform seperate operations and send seperate
% messages. A message published by |node1| can be accessed by a subscriber
% running in |node2|.

pub = robotics.ros.Publisher(node1,'/chatter','std_msgs/String');
sub = robotics.ros.Subscriber(node2,'/chatter','std_msgs/String');

msg = rosmessage('std_msgs/String');
msg.Data = 'Message from Node 1';

%%
% Send a message from |node1|. The subscriber attached to |node2| will receive
% the message.

send(pub,msg) % Sent from node 1
pause(1) % Wait for message to update
sub.LatestMessage

%%
% Clear the ROS network of publisher, subscriber, and nodes. Delete the |Core| 
% object to shut down the ROS master.
clear('pub','sub','node1','node2')
clear('master')