This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Node

Start ROS node and connect to ROS master

Description

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

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

Creation

Syntax

N = robotics.ros.Node(Name)
N = robotics.ros.Node(Name,Host)
N = robotics.ros.Node(Name,Host,Port)
N = robotics.ros.Node(Name,MasterURI,Port)
N = robotics.ros.Node(___,'NodeHost',HostName)

Description

example

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

N = robotics.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 = robotics.ros.Node(Name,Host,Port)tries to connect to the ROS master with port number, Port.

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

N = robotics.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 with which node you are connecting to.

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 separate operations and send separate 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
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 separate information across the network.

Create two ROS masters on different ports.

m1 = robotics.ros.Core; % Default port of 11311 
m2 = robotics.ros.Core(12000);

Connect separate ROS nodes to each ROS master.

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

Clear the ROS nodes. Shut down the ROS masters.

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

See Also

|

External Websites

Introduced in R2015a