Main Content

Node

Start ROS node and connect to ROS master

Description

Theros.Node对象代表一个活性氧ROS节点网络。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 therosinitfunction, or by callingros.Node:

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

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

Creation

Description

example

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

N = ros.Node(Name,Host)tries to connect to the ROS master at the specified IP address or host name,Hostusing 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. SeeROS 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 aTimeobject. For more information, seerostime.

Examples

collapse all

Create multiple ROS nodes. Use theNodeobject 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.89732 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 bynode1can be accessed by a subscriber running innode2.

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 fromnode1. The subscriber attached tonode2will receive the message.

send(pub,msg)% Sent from node 1pause(1)% Wait for message to updatesub.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 theCoreobject 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.85317 seconds.
m2 = ros.Core(12000);
Launching ROS Core... Done in 0.92636 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')

See Also

|

External Websites

Introduced in R2019b