Main Content

Get Started with ROS 2

This example shows how to set up ROS 2 within MATLAB, and get information about ROS 2 network and ROS 2 messages.

Robot Operating System 2 (ROS 2) is the second version of ROS, which is a communication interface that enables different parts of a robot system to discover, send, and receive data. MATLAB® support for ROS 2 is a library of functions that allows you to exchange data with ROS 2 enabled physical robots or robot simulators such as Gazebo®. ROS 2 is built on Data Distribution Service (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. DDS uses Real Time Publish-Subscribe (RTPS) protocol which provides communication over unreliable network protocols such as UDP. For more information, see RTPS.

To learn about ROS, see Get Started with ROS.

ROS 2 Terminology

  • A ROS 2 network comprises different parts of a robot system (such as a planner or a camera interface) that communicate over ROS 2 network. The network can be distributed over several machines.

  • A ROS 2 node is an entity that contains a collection of related ROS 2 capabilities (such as publishers and subscribers). A ROS 2 network can have many ROS 2 nodes.

  • Publishers and subscribers are different kinds of ROS 2 entities that process data. They exchange data using messages.

  • A publisher sends messages to a specific topic (such as "odometry"), and subscribers to that topic receive those messages. There can be multiple publishers and subscribers associated with a single topic.

  • A Domain is the physical segmentation of network. It is identified by a unique integer value known as Domain ID. By default the Domain ID is 0.

  • Every node in ROS 2 network on creation advertises its presence to other nodes in the same Domain ID only.

  • ROS 2 network is built on Data Distribution Service (DDS) which makes it possible to connect multiple nodes across distributed network. For more information, see Switching Between ROS Middleware Implementations.

  • RTPS (Real Time publisher-subscriber) protocol provides ROS 2 network with capabilities to send messages in unreliable network conditions.

  • ROS 2 offers variety of Quality of Service (QoS) policies that allow you to tune your communication between nodes. For more information, see Manage Quality of Service Policies in ROS 2.

For more information, see Robot Operating System (ROS 2) and the Concepts section on the ROS 2 website.

Initialize ROS 2 Network

Unlike ROS, ROS 2 does not require initialization in MATLAB. The ROS 2 network automatically starts with creation of nodes.

Use ros2node to create a node.

test1 = ros2node("/test1")
test1 = 
  ros2node with properties:

    Name: '/test1'
      ID: 0

Use ros2 node list to see all nodes in the ROS 2 network.

ros2 node list
/test1

Use clear to shutdown the node in ROS 2 network.

clear test1

Use exampleHelperROS2CreateSampleNetwork to populate the ROS network with three additional nodes with sample publishers and subscribers.

exampleHelperROS2CreateSampleNetwork

Use ros2 node list again and observe the new nodes.

ros2 node list
/node_1
/node_2
/node_3

This is a visual representation of the ROS 2 network after the creation of the sample nodes. Use it as a reference to explore the ROS 2 network in the remainder of the example.

Topics and Quality of Service Policies

Run the command ros2 topic list to see available topics in the ROS 2 network. This command returns three active topics: /pose, /parameter_events, and /scan. The topic /parameter_events is a global topic which is always present in the ROS 2 network. The nodes use the /paramater_events topic to monitor or change parameters in the network. You created the /scan and /pose topics as part of the sample network.

ros2 topic list
/parameter_events
/pose
/rosout
/scan

Each topic is associated with a message type. Run the command ros2 topic list -t to see the message type of each topic.

ros2 topic list -t
            Topic                       MessageType           
    _____________________    _________________________________

    {'/parameter_events'}    {'rcl_interfaces/ParameterEvent'}
    {'/pose'            }    {'geometry_msgs/Twist'          }
    {'/rosout'          }    {'rcl_interfaces/Log'           }
    {'/scan'            }    {'sensor_msgs/LaserScan'        }

The Quality of Service (QoS) policy options change the way the publishers and subscribers handle and exchange messages.

1. Use the History and Depth QoS policies to determine the behavior of communication objects based on how you want to place messages in the processing queue. Specify History as one of these options.

  • "keeplast" — Drop messages and retain only the most up-to-date information.

  • "keepall" — Keep all the messages received in the queue until they are processed.

2. Use the Reliability QoS policy to guarantee delivery of the messages to the subscriber. Specify Reliability as one of these options.

  • "reliable" — Ensure that the publisher continuously sends the message to the subscriber until the subscriber confirms receipt of the message.

  • "besteffort" — Allow the publisher to send the message only once.

3. Use the Durability QoS policy with the Depth input to control the persistence of messages for late-joining connections. Specify Durability as one of these options.

  • "transientlocal" — The publisher sends the persisted messages to the subscriber if the subscriber joins the network after the publisher initially sends the messages.

  • "volatile" — The publisher does not persist messages after sending them. Subscribers do not request persisted messages from publishers.

Messages

Publishers and subscribers use ROS 2 messages to exchange information. Each ROS 2 message has an associated message type that defines the datatypes and layout of information in that message. For more information, see Work with Basic ROS 2 Messages.

Use ros2 msg show to view the properties of a message type. The geometry_msgs/Twist message type has two properties, Linear and Angular. Each property is a message of type geometry_msgs/Vector3, which in turn has three properties of type double.

ros2 msg show geometry_msgs/Twist
# This expresses velocity in free space broken into its linear and angular parts.

Vector3  linear
Vector3  angular
ros2 msg show geometry_msgs/Vector3
# This represents a vector in free space.

# This is semantically different than a point.
# A vector is always anchored at the origin.
# When a transform is applied to a vector, only the rotational component is applied.

float64 x
float64 y
float64 z

Use ros2 msg list to see the full list of message types available in MATLAB.

Disconnect From ROS 2 Network

Use exampleHelperROS2ShutDownSampleNetwork to remove the sample nodes, publishers, and subscribers from the ROS 2 network. To remove your own nodes, use clear with the node, publisher, or subscriber object.

exampleHelperROS2ShutDownSampleNetwork

DDS and RMW Implementations

You can switch between RMW Implementations for using the data distribution service (DDS), when you work with ROS 2 in MATLAB® and Simulink®. To configure the required RMW Implementation, follow these steps.

1. Under the Home tab in the Environment section of the MATLAB toolstrip, open Preferences.

2. Configure the Python™ environment in the ROS Toolbox Preferences dialog box and select an implementation from the ROS Middleware (RMW) Implementation dropdown list. The default implementation is rmw_fastrtps_cpp.

3. Switch to a custom RMW implementation by clicking Configure and Switch to Custom RMW Implementation, which launches the ROS Middleware Configuration dialogue box.

4. Install and build the custom RMW Implementation package to validate the creation of a ROS 2 node with the selected custom RMW implementation.

Next Steps