Display incoming data from ROS topic

5 views (last 30 days)
Hallo,
I'm new to ROS within Matlab and trying to set up a program, which can plot data from a Topic. I installed the Robotics System Toolbox.
This is my program using an example Network:
% start the ROS Master and connect to network
rosinit; % connect to ROS and initialize
exampleHelperROSCreateSampleNetwork; %create an example ros network
rostopic list % display all topics
robotpose = rossubscriber('/pose',@exampleHelperROSPoseCallback); %create 'robotpose' as subscriber to topic '/pose'
global pos % global variable pos
global orient % global variable orient
% current values of pose and orientation will be stored in pos and orient
pause(1);
A = pos;
B = orient;
counter_1 = 1;
counter_2 = 1;
x_width = 100;
% figure(1) setup
figure(1)
subplot(1,2,1)
title('Position');
xlabel('Time');ylabel('Value');
grid;
hold on;
% figure(2) setup
%figure(2)
subplot(1,2,2)
title('Orientation');
xlabel('Time');ylabel('Value');
grid;
hold on;
while 1
pause(0.25); % wait 0.25s then continue
%figure(1)
subplot(1,2,1)
if (size(A) <= (x_width)) % in the beginning A is newly build up
A = [A;pos]; % fill new line of A with data from pos
%pos % show pos for control
plot([1:counter_1]',A(1:counter_1,1), 'Color',[1,0,0]); % plot x achsis
plot([1:counter_1]',A(1:counter_1,2), 'Color',[0,1,0]); % plot y achsis
plot([1:counter_1]',A(1:counter_1,3), 'Color',[0,0,1]); % plot z achsis
if counter_1 == 1
legend('x','y','z','Location','southoutside'); % legend is under the plot
end
else % A is expanded and first element erased
A = [A(2:size(A),:);pos]; % fill new line of A with data from pos
%pos % show pos für control
xlim([(counter_1 - x_width),counter_1]);
plot([(counter_1 - x_width + 1):counter_1 + 1]',A(:,1), 'Color',[1,0,0]);
plot([(counter_1 - x_width + 1):counter_1 + 1]',A(:,2), 'Color',[0,1,0]);
plot([(counter_1 - x_width + 1):counter_1 + 1]',A(:,3), 'Color',[0,0,1]);
end
counter_1 = counter_1 + 1; % number of steps gone
%figure(2)
subplot(1,2,2)
if (size(B) <= (x_width)) % same like A
B = [B;orient]; % orient not pos
plot([1:counter_2]',B(1:counter_2,1), 'Color',[1,0,0]);
plot([1:counter_2]',B(1:counter_2,2), 'Color',[0,1,0]);
plot([1:counter_2]',B(1:counter_2,3), 'Color',[0,0,1]);
if (counter_2 == 1)
legend('x','y','z','Location','southoutside');
end
else
B = [B(2:size(B),:);orient];
xlim([(counter_2 - x_width),counter_2]);
plot([(counter_2 - x_width + 1):counter_2 + 1]',B(:,1), 'Color',[1,0,0]);
plot([(counter_2 - x_width + 1):counter_2 + 1]',B(:,2), 'Color',[0,1,0]);
plot([(counter_2 - x_width + 1):counter_2 + 1]',B(:,3), 'Color',[0,0,1]);
end
counter_2 = counter_2 + 1;
end
When the program runs for a while (about 2 min, depends on the computer) it is responding very slow and the plots don't work properly. Is there an alternative for the infinite loop?

Accepted Answer

Anish Mitra
Anish Mitra on 24 Feb 2016
If you can create a .bag file from a running ROS, you can then read it into MATLAB and extract the message data as a timeseries. This would be offline analysis though.
To speed up the plotting part a little bit - you can initialize the A and B matrices, with trailing zeros and then change the XData and YData of the line handles inside the loop instead of using the plot commands.
For example :
X = 1:100;
Y1 = randn(1,100);
Y2 = randn(1,100);
figure(1); hold on;
h1 = plot(X,Y1,'Color',[1,0,0]);
h2 = plot(X,Y2,'Color',[0,1,0]);
while(1)
pause(0.25);
Y1 = [Y1(2:100), randn(1)];
Y2 = [Y2(2:100), randn(1)];
h1.YData = Y1;
h2.YData = Y2;
end
If the 'pause' command is not being used, then 'drawnow' will need to be put in the loop to force MATLAB to redraw the lines at every iteration.
  1 Comment
Ignacio Arriscado
Ignacio Arriscado on 11 Apr 2019
Hi,
is it possible to display data in real time, when this data is coming from a ros node subscriber? I want to design a desktop application to display data in real time from turtlebot, is this possible?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!