This example shows how to publish and subscribe to topics in a ROS 2 network. It also shows how to:
Subscribe and Wait for Messages
Create a sample ROS 2 network with several publishers and subscribers.
Use ros2 topic list
to see which topics are available.
/parameter_events
/pose
/rosout
/scan
Assume you want to subscribe to the /scan
topic. Use ros2subscriber
to subscribe to the /scan
topic. Specify the name of the node with the subscriber. If the topic already exists in the ROS 2 network, ros2subscriber
detects its message type automatically, so you do not need to specify it.
Use receive
to wait for a new message. Specify a timeout of 10 seconds. The output scanData
contains the received message data. status
indicates whether a message was received successfully and statustext
provides additional information about the status
.
You can now remove the subscriber laserSub
and the node associated to it.
Subscribe Using Callback Functions
Instead of using receive
to get data, you can specify a function to be called when a new message is received. This allows other MATLAB code to execute while the subscriber is waiting for new messages. Callbacks are essential if you want to use multiple subscribers.
Subscribe to the /pose
topic, using the callback function exampleHelperROS2PoseCallback
, which takes a received message as the input. One way of sharing data between your main workspace and the callback function is to use global variables. Define two global variables pos
and orient
.
The global variables pos
and orient
are assigned in the exampleHelperROS2PoseCallback
function when new message data is received on the /pose
topic.
function exampleHelperROS2PoseCallback(message)
% Declare global variables to store position and orientation
global pos
global orient
% Extract position and orientation from the ROS message and assign the
% data to the global variables.
pos = [message.linear.x message.linear.y message.linear.z];
orient = [message.angular.x message.angular.y message.angular.z];
end
Wait a moment for the network to publish another /pose
message. Display the updated values.
If you type in pos
and orient
a few times in the command line you can see that the values are continuously updated.
Stop the pose subscriber by clearing the subscriber variable
Note: There are other ways to extract information from callback functions besides using globals. For example, you can pass a handle object as additional argument to the callback function. See the Create Callbacks for Graphics Objects documentation for more information about defining callback functions.
Publish Messages
Create a publisher that sends ROS 2 string messages to the /chatter
topic.
Create and populate a ROS 2 message to send to the /chatter
topic.
Use ros2 topic list
to verify that the /chatter
topic is available in the ROS 2 network.
/chatter
/parameter_events
/pose
/rosout
/scan
Define a subscriber for the /chatter
topic. exampleHelperROS2ChatterCallback
is called when a new message is received, and displays the string content in the message.
chatterSub =
ros2subscriber with properties:
TopicName: '/chatter'
LatestMessage: []
MessageType: 'std_msgs/String'
NewMessageFcn: @exampleHelperROS2ChatterCallback
History: 'keeplast'
Depth: 10
Reliability: 'reliable'
Durability: 'volatile'
Publish a message to the /chatter
topic. Observe that the string is displayed by the subscriber callback.
The exampleHelperROS2ChatterCallback
function was called when the subscriber received the string message.
Disconnect From ROS 2 Network
Remove the sample nodes, publishers and subscribers from the ROS 2 network. Also clear the global variables pos
and orient
Next Steps