Display incoming data from ROS topic
显示 更早的评论
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?
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Publishers and Subscribers 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!