Connect to a ROS 2 Network
A ROS 2 network consists of a multiple ROS 2 nodes. Unlike ROS where the ROS master facilitates the communication by keeping track of all active ROS entities, ROS 2 is based on Data Distribution Standard (DDS) which is an end-to-end middleware that provides features such as discovery, serialization, and transportation. These features align with the design principles of ROS 2 such as distributed discovery and control over different "Quality of Service" options for transportation.
To connect to a ROS 1 network, see Connect to a ROS Network.
When you work with ROS 2, you typically follow these steps:
Connect to a ROS 2 network. To connect to a ROS 2 network, you have to create a ROS 2 node in MATLAB specifying the network domain ID.
Exchange Data. Once connected, MATLAB exchanges data with other ROS 2 nodes in the same domain ID through publishers and subscribers.
Disconnect from the ROS 2 network. Clearing all references to the nodes, publishers, and subscribers removes MATLAB from the ROS 2 network.
Create a ROS 2 Node in the Default Domain
ros2node to create a node in the default domain, which uses the ID of
0. Nodes communicate with other nodes in the same domain, and are unaware of nodes in other domains.
defaultNode = ros2node("/default_node")
defaultNode = ros2node with properties: Name: '/default_node' ID: 0
clear to remove the reference to the node, allowing it to be deleted from the ROS 2 network.
Create a ROS 2 Node on a Different Domain
To create a node in non-default domain, explicitly specify the domain ID as a second input argument to
newDomainNode is created in the domain specified by ID
newDomainNode = ros2node("/new_domain_node",25)
newDomainNode = ros2node with properties: Name: '/new_domain_node' ID: 25
To view network information on a specific domain, provide the ID as a parameter to the
ros2 function. The following command displays all nodes with domain ID
Change Default Domain ID
If the domain ID is not provided explicitly to the node or
ros2 command, they use the value of the
ROS_DOMAIN_ID environment variable by default. Use
getenv to see the current value. If that environment variable is unset, or not set to a valid value, the default domain ID of
0 will be used.
ans = 0x0 empty char array
You can set
ROS_DOMAIN_ID using the
setenv("ROS_DOMAIN_ID","25") envDomainNode = ros2node("/env_domain_node")
envDomainNode = ros2node with properties: Name: '/env_domain_node' ID: 25
The ros2 function provides information on the network specified by that environment variable. Use
ros2 node list to view nodes with domain ID
ros2 node list
ROS_DOMAIN_ID to default.
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 in a mechanism called discovery.
Upon starting, each node in ROS 2 advertises its presence to other nodes in the same domain. The other nodes respond to this advertisement by providing their information to the new node. Nodes with communication objects like publishers and subscribers establish connections with other nodes if they have corresponding objects with compatible Quality of Service (QoS) settings. For more information on QoS settings, see Manage Quality of Service Policies in ROS 2.
Discovery is an ongoing process, which enables new nodes to join the network as they are created. Each node is monitoring the ROS 2 network and act similarly to the ROS master in a ROS network. Nodes also advertise their absence to other nodes when they go offline.
The new ROS 2 node sends its advertisement to the existing nodes. The existing nodes respond to the advertisement and then set up for ongoing communication.
ROS 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
domainId 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 DEFAULT_FASTRTPS_PROFILES.xml file.
<?xml version="1.0" encoding="UTF-8" ?> <profiles> <participant profile_name="participant_win" is_default_profile="true"> <rtps> <builtin> <domainId>0</domainId> <initialPeersList> <locator> <kind>UDPv4</kind> <address>22.214.171.124</address> </locator> <locator> <kind>UDPv4</kind> <address>126.96.36.199</address> </locator> <locator> <kind>UDPv4</kind> <address>188.8.131.52</address> </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_PROFILES.xml. No information from the nodes in the other machine outside the subnet will be received if DEFAULT_FASTRTPS_PROFILES.xml is either not present or does not contain the correct IP addresses.