Main Content

Connect to ROS 2 Network and Establish Communication

ROS 2 Nodes

Nodes are the basic building blocks of ROS applications, which takes care of the computation. A ROS 2 network can have multiple nodes running on a single computer or across multiple computers. Nodes are independent processes that communicate with each other by sending and receiving messages. In a robot system, nodes include sensors (camera), motion controllers (motors), and algorithm components (route planner).

Initialize ROS 2 Network

To connect with a ROS 2 network, you can create a ROS 2 node. A ROS 2 network is identified with a Domain ID. To create a ROS 2 node in the default domain (with domain ID of 0), use ros2node command.

defaultNode = ros2node("/default_node")
defaultNode = 
  ros2node with properties:

    Name: '/default_node'
      ID: 0

View ROS 2 Network Information

Use ros2 node list to view the network information on the default domain.

ros2 node list
/default_node
/node_1

Create ROS 2 Node on Different Domain

Pass the domain ID as a parameter to create a node in a domain other than the default one.

newDomainNode = ros2node("/new_domain_node",25)
newDomainNode = 
  ros2node with properties:

    Name: '/new_domain_node'
      ID: 25

This will create a node and connect to the network with domain ID of 25.

Provide the domain ID as shown below for non-default domains.

ros2("node","list","DomainID",25)
/new_domain_node

Shutdown ROS 2 Network

Use clear to remove the reference to the node and remove the node from the network.

clear defaultNode
clear newDomainNode

You can create multiple such nodes and establish communication between them by sending and receiving messages of different interface types.

Communication in ROS 2 Network

To connect to an existing ROS 2 network, create a node in the desired domain. The ROS 2 network automatically detects any new nodes created in the same domain which you studied in Discovery.

ROS 2 Communication Outside Subnet

A subnet is a logical partition of an IP network into multiple, smaller network segments. ROS 2 nodes can communicate with other nodes within the same subnet. To detect the nodes present outside the subnet, create a DEFAULT_FASTRTPS_PROFILE.xml file to configure the specific DDS implementation MATLAB uses. Add the list of IP address of systems outside of the subnet with which to communicate inside address elements. Note that for both systems to communicate, they each must specify the other system's address in their respective DEFAULT_FASTRTPS_PROFILE.xml files. Set the domain ID element to the appropriate value for the network that is used for communication.

Keep this file in the MATLAB® Current Working Directory. Systems using ROS 2 outside of MATLAB should place this file in the same directory from which the ROS 2 application is run. Below is an example of DEFAULT_FASTRTPS_PROFILE.xml file.

<?xml version="1.0" encoding="UTF-8" ?>
<profiles>
    <participant profile_name="participant_win" is_default_profile="true">
        <rtps>
            <builtin>
                <metatrafficUnicastLocatorList>
                     <locator/>
                </metatrafficUnicastLocatorList>
                 <initialPeersList>
                     <locator>
                         <udpv4>
                         <address>192.34.17.36</address>
                         </udpv4>
                     </locator>
                     <locator>
                         <udpv4>
                         <address>182.30.45.12</address>
                         </udpv4>
                     </locator>
                     <locator>
                         <udpv4>
                         <address>194.158.78.29</address>
                         </udpv4>
                     </locator>
                 </initialPeersList>
             </builtin>
         </rtps>
     </participant>
</profiles> 

ROS 2 advertises information to the nodes present in the systems with IP addresses listed inside the DEFAULT_FASTRTPS_PROFILE.xml. No information from the nodes in the other machine outside the subnet will be received if DEFAULT_FASTRTPS_PROFILE.xml is either not present or does not contain the correct IP addresses.

ROS 2 communication subnet

ROS 2 Communication Interfaces

You can communicate between ROS 2 nodes using different interface types: