Main Content

ros2node

Create a ROS 2 node on the specified network

Description

The ros2node object represents a ROS 2 node, and allows you to communicate with the rest of the ROS 2 network. You have to create a node before you can create publishers and subscribers.

Creation

Description

example

node = ros2node(Name) initializes a ROS 2 node with the given Name. The node will be on the network specified by the domain identification 0, unless otherwise specified by the ROS_DOMAIN_ID environment variable.

By default the node uses the 'rmw_fastrtps_cpp' ROS middleware (RMW) implementation unless otherwise specified by the RMW_IMPLEMENTATION environment variable. Set the RMW_IMPLEMENTATION environment variable before creating the ros2node object. For example, setenv('RMW_IMPLEMENTATION','rmw_cyclonedds_cpp') sets the RMW implementation to 'rmw_cyclonedds_cpp'. For more information on RMW implementations see Switching Between ROS Middleware Implementations.

example

node = ros2node(Name,ID) will initialize the ROS 2 node with Name and connect to the network using domain ID.

example

node = ros2node(___,Parameters=params) specifies parameters to be declared during the node startup using the name-value argument Parameters, using any of the arguments from the previous syntaxes. Specify params as a structure that contains the parameters as its fields. Each parameter in params can be a scalar or an array of uint8, int64, logical, string, char or double datatype.

Input Arguments

expand all

The name of the node on the ROS 2 network.

Note

In ROS 1, node names are unique and this is being enforced by shutting down existing nodes when a new node with the same name is started. In ROS 2, the uniqueness of node names is not enforced. When creating a new node, use ros2 function to list existing nodes.

The domain identification of the ROS 2 network.

Data Types: double

Properties

expand all

This property is read-only.

The name of the node on the ROS 2 network.

Example: "/node_1"

Data Types: char

This property is read-only.

The domain identification of the ROS 2 network, specified as a non-negative scalar integer between 0 and 232.

Example: 2

Data Types: double

Object Functions

deleteRemove reference to ROS 2 node
getParameterGet value of parameter declared in ROS 2 node
setParameterSet value of parameter declared in ROS 2 node

Examples

collapse all

Initialize the node, '/node_1_default', on the default ROS 2 network.

node1 = ros2node('node_1_default')
node1 = 
  ros2node with properties:

    Name: '/node_1_default'
      ID: 0

Initialize the node, '/node_2_specified', on the ROS 2 network identified with domain 2.

node2 = ros2node('node_2_specified', 2)
node2 = 
  ros2node with properties:

    Name: '/node_2_specified'
      ID: 2

Create a structure that contains all the parameters for the ROS 2 node.

nodeParams.my_double = 2.0;
nodeParams.my_namespace.my_int = int64(1);
nodeParams.my_double_array = [1.1 2.2 3.3];
nodeParams.my_string = "Keyparams";

Create a ROS 2 node and specify nodeParams as the parameters.

node1 = ros2node("/node1",Parameters=nodeParams);

Set the parameter my_double to a new value.

setParameter(node1,"my_double",5.2);

Obtain the new value of the parameter my_double.

doubleValue = getParameter(node1,"my_double")
doubleValue = 5.2000

Extended Capabilities

Version History

Introduced in R2019b