Main Content

Get Started with ROS

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

Robot Operating System (ROS) is a communication interface that enables different parts of a robot system to discover each other, and send and receive data between them. MATLAB® supports ROS with a library of functions that enables you to exchange data with ROS-enabled physical robots or robot simulators such as Gazebo®.

ROS Terminology

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

  • A ROS master coordinates the different parts of a ROS network. It is identified by a Master URI (Uniform Resource Identifier) that specifies the hostname or IP address of the machine where the master is running.

  • A ROS node contains a collection of related ROS capabilities (such as publishers, subscribers, and services). A ROS network can have many ROS nodes.

  • Publishers, subscribers, and services are different kinds of ROS 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. A single topic can be associated with multiple publishers and subscribers.

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

Initialize ROS Network

Use rosinit to initialize ROS. By default, rosinit creates a ROS master in MATLAB and starts a global node that is connected to the master. The global node is automatically used by other ROS functions.

rosinit
Launching ROS Core...
.....Done in 5.7786 seconds.
Initializing ROS master on http://172.21.16.85:51063.
Initializing global node /matlab_global_node_13423 with NodeURI http://ah-avijayar:52604/ and MasterURI http://localhost:51063.

Use rosnode list to see all nodes in the ROS network. Note that the only available node is the global node created by rosinit.

rosnode list
/matlab_global_node_13423

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

exampleHelperROSCreateSampleNetwork

Use rosnode list again to see the three new nodes (node_1, node_2, and node_3).

rosnode list
/matlab_global_node_13423
/node_1
/node_2
/node_3

The figure shows the current state of the ROS network. The MATLAB global node is disconnected since it currently does not have any publishers, subscribers or services.

Topics

Use rostopic list to see available topics in the ROS network. There are four active topics: /pose, /rosout, /scan and /tf. The default topics: rosout and tf are always present in the ROS network. The other two topics were created as part of the sample network.

rostopic list
/pose  
/rosout
/scan  
/tf    

Use rostopic info <topicname> to get specific information about a specific topic. The command below shows that /node_1 publishes (sends messages to) the /pose topic, and /node_2 subscribes (receives messages from) to that topic. See Exchange Data with ROS Publishers and Subscribers for more information.

rostopic info /pose
Type: geometry_msgs/Twist
 
Publishers:
* /node_1 (http://ah-avijayar:52609/)
 
Subscribers:
* /node_2 (http://ah-avijayar:52614/)

Use rosnode info <nodename> to get information about a specific node. The command below shows that node_1 publishes to /pose, /rosout and /tf topics, subscribes to the /scan topic and provides services: /node_1/get_loggers and /node_1/set_logger_level. The default logging services: get_loggers and set_logger_level are provided by all the nodes created in ROS network.

rosnode info /node_1
Node: [/node_1]
URI: [http://ah-avijayar:52609/]
 
Publications (3 Active Topics): 
 * /pose
 * /rosout
 * /tf
 
Subscriptions (1 Active Topics): 
 * /scan
 
Services (2 Active): 
 * /node_1/get_loggers
 * /node_1/set_logger_level

Services

ROS services provide a mechanism for procedure calls across the ROS network. A service client sends a request message to a service server, which processes the information in the request and returns with a response message (see Call and Provide ROS Services).

Use rosservice list to see all available service servers in the ROS network. The command below shows that two services (/add and /reply) are available along with the default logger services of all the nodes.

rosservice list
/add
/matlab_global_node_13423/get_loggers
/matlab_global_node_13423/set_logger_level
/node_1/get_loggers
/node_1/set_logger_level
/node_2/get_loggers
/node_2/set_logger_level
/node_3/get_loggers
/node_3/set_logger_level
/reply

Use rosservice info <servicename> to get information about a specific service.

rosservice info /add
Node: /node_3
URI: rosrpc://ah-avijayar:52618
Type: roscpp_tutorials/TwoInts
Args: MessageType A B

Messages

Publishers, subscribers, and services use ROS messages to exchange information. Each ROS message has an associated message type that defines the datatypes and layout of information in that message (See Work with Basic ROS Messages).

Use rostopic type <topicname> to see the message type used by a topic. The command below shows that the /pose topic uses messages of type geometry_msgs/Twist.

rostopic type /pose
geometry_msgs/Twist

Use rosmsg show <messagetype> 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.

rosmsg show geometry_msgs/Twist
% This expresses velocity in free space broken into its Linear and Angular parts.
Vector3  Linear
Vector3  Angular
rosmsg show geometry_msgs/Vector3
% This represents a vector in free space. 
% It is only meant to represent a direction. Therefore, it does not
% make sense to apply a translation to it (e.g., when applying a 
% generic rigid transformation to a Vector3, tf2 will only apply the
% rotation). If you want your data to be translatable too, use the
% geometry_msgs/Point message instead.

double X
double Y
double Z

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

Shut Down ROS Network

Use exampleHelperROSShutDownSampleNetwork to remove the sample nodes, publishers, and subscribers from the ROS network. This command is only needed if the sample network was created earlier using exampleHelperROSStartSampleNetwork.

exampleHelperROSShutDownSampleNetwork

Use rosshutdown to shut down the ROS network in MATLAB. This shuts down the ROS master that was started by rosinit and deletes the global node. Using rosshutdown is the recommended procedure once you are done working with the ROS network.

rosshutdown
Shutting down global node /matlab_global_node_13423 with NodeURI http://ah-avijayar:52604/ and MasterURI http://localhost:51063.
Shutting down ROS master on http://172.21.16.85:51063.

Next Steps