Main Content

Node

Start ROS node and connect to ROS master

Description

The ros.Node object represents a ROS node in the ROS network. The object enables you to communicate with the rest of the ROS network. You must create a node before you can use other ROS functionality, such as publishers, subscribers, and services.

You can create a ROS node using the rosinit function, or by calling ros.Node:

  • rosinit — Creates a single ROS node in MATLAB®. You can specify an existing ROS master, or the function creates one for you. The Node object is not visible.

  • ros.Node— Creates multiple ROS nodes for use on the same ROS network in MATLAB.

Creation

Description

N = ros.Node(Name) initializes the ROS node with Name and tries to connect to the ROS master at default URI, http://localhost:11311.

example

N = ros.Node(Name,Host) tries to connect to the ROS master at the specified IP address or host name, Host using the default port number, 11311.

N = ros.Node(Name,Host,Port)tries to connect to the ROS master with port number, Port.

N = ros.Node(Name,MasterURI,Port) tries to connect to the ROS master at the specified IP address, MasterURI.

N = ros.Node(___,'NodeHost',HostName) specifies the IP address or host name that the node uses to advertise itself to the ROS network. Examples include "192.168.1.1" or "comp-home". You can use any of the arguments from the previous syntaxes.

Properties

expand all

Name of the node, specified as a string scalar or character vector. The node name must be a valid ROS graph name. See ROS Names.

URI of the ROS master, specified as a string scalar or character vector. The node is connected to the ROS master with the given URI.

URI for the node, specified as a string scalar or character vector. The node uses this URI to advertise itself on the ROS network for others to connect to it.

Current ROS network time, specified as a Time object. For more information, see rostime.

Examples

collapse all

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

Create a ROS master.

master = ros.Core;
Launching ROS Core...
Done in 0.85405 seconds.

Initialize multiple nodes.

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

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

pub = ros.Publisher(node1,'/chatter','std_msgs/String');
sub = 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
ans = 
  ROS String message with properties:

    MessageType: 'std_msgs/String'
           Data: 'Message from Node 1'

  Use showdetails to show the contents of the message

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')

Connecting to multiple ROS masters is possible using MATLAB®. These separate ROS masters do not share information and must have different port numbers. Connect ROS nodes to each master based on how you want to distribute information across the network.

Create two ROS masters on different ports.

m1 = ros.Core; % Default port of 11311 
Launching ROS Core...
Done in 0.43944 seconds.
m2 = ros.Core(12000);
Launching ROS Core...
Done in 0.44573 seconds.

Connect separate ROS nodes to each ROS master.

node1 = ros.Node('/test_node_1','localhost');
node2 = ros.Node('/test_node_2','localhost',12000);

Clear the ROS nodes. Shut down the ROS masters.

clear('node1','node2')
clear('m1','m2')

Version History

Introduced in R2019b

See Also

|

External Websites