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 ROS 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 nodes present outside the subnet, configure the DDS middleware used by MATLAB® by creating a DEFAULT_FASTRTPS_PROFILE.xml file. Add the IP addresses of systems outside the subnet with which to communicate inside the <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.

Note

The required configuration depends on the DDS middleware implementation in use. MATLAB uses Fast DDS by default.

Keep this file in the MATLAB current working directory. Systems using ROS 2 outside MATLAB must place this file in the same directory from which the ROS 2 application is launched.

The code snippet 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. If this file is not present or does not contain the correct IP addresses, ROS 2 does not receive information from nodes outside the subnet.

ROS 2 communication subnet

Troubleshoot ROS 2 Communication Across Remote Networks

Use these troubleshooting steps when MATLAB is unable to discover or communicate with ROS 2 nodes running on a remote machine or outside the local subnet.

 Verify Network Configuration

 Verify ROS 2 Communication Between MATLAB and External Environment

 Verify ROS 2 Distribution, ROS Domain ID, and ROS Middleware

 Solve ROS Middleware-Specific Issues

 Ensure QoS Settings and Message Definitions Compatibility

 Enable Local Network Access (Firewall)

ROS 2 Communication Interfaces

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