In ROS 2, messages can be accessed by "subscriber.LatestMessage" but cannot be obtained via the MATLAB function "receive(subscriber, timeout)"
Show older comments
Hello everyone,
I am learning to use ROS 2 to communicate between agents. The problem I encountered is that the subscriber cannot receive any messages using the function receive() regardless how big the timeout I set, but in its property LatestMessage I can indeed see the latest message sent by the publisher.
I created a simple but complete example to showcase this:
% create ROS 2 nodes
node_1 = ros2node("/node_1");
node_2 = ros2node("/node_2");
pub = ros2publisher(node_1,"/topic_1","std_msgs/String"); % create publisher
msg = ros2message("std_msgs/String"); % create message structure
msg.data = 'Hello world';
ros2 topic list
ros2 node list
pause(2) % wait to ensure nodes and publisher are created
sub = ros2subscriber(node_2,"/topic_1"); % create subscriber
pause(2) % wait to ensure subscriber is created
send(pub,msg) % send message
pause(2)
disp(sub.LatestMessage) % message can be accessed in this way
[msg_received, status, statustext] = receive(sub,10) % but this way does not work
Any help would be appreciated.
(Note: I am using MATLAB R2022a in Windows 10 with version 21H2.)
Best,
Jianye Xu
Accepted Answer
More Answers (0)
Categories
Find more on ROS Network Access in MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!