Plot multiple vehicles with multiple data in parallel
2 views (last 30 days)
Show older comments
I need to plot multiple vehicles in parallel for this reason I created this test script for two vehicles:
%% Create a MotionData object
motionData_1 = MotionData('dateTime.csv','XYtheta.csv');
%% set fake data for second vehicle
motionData_2 = motionData_1;
motionData_2.X(motionData_2.X ~= 0) = motionData_2.X(motionData_2.X ~= 0) - 10;
motionData_2.Y(motionData_2.Y ~= 0) = motionData_2.Y(motionData_2.Y ~= 0) - 5;
motionData_2.theta(motionData_2.theta ~= 0) = motionData_2.theta(motionData_2.theta ~= 0) + 90;
%% test fake data
motionData_2.X;
%% Create Vehicle objects
vehicle_1 = Vehicle2('V1', 'WS', motionData_1, 'b');
vehicle_2 = Vehicle2('V2', 'WS', motionData_2, 'r');
%% Set the start and end times for the simulation
startTime = '03.05.2023 18:00:00.000';
endTime = '03.05.2023 19:00:59.100';
%% Set the size and frame length for the graph data
size = 20;
frameLength = 0.55;
figure
% Plot the graph data with customized settings
vehicle_1.plotGraphData3(startTime, endTime, size, frameLength);
vehicle_2.plotGraphData3(startTime, endTime, size, frameLength);
More specifically these two functions I need to be executed in parallel because they work perfectly for one vehicle
vehicle_1.plotGraphData3(startTime, endTime, size, frameLength);
vehicle_2.plotGraphData3(startTime, endTime, size, frameLength);
The content of the plotGraphData3 function in the Vehicle class is as follows:
function plotGraphData3(obj, startTime, endTime, size, frameLength)
% Call the plotGraphData method of the GraphData object
[allXYA, startIndex, endIndex, numRows] = obj.graphData.getGraphDataInRange(startTime, endTime);
%obj.graphData.getGraphDataInRange(startTime, endTime);
obj.graphData.plotGraphData3(allXYA, numRows, size, frameLength);
end
where getGraphDataInRange and plotGraphData3 are in the GraphData class as follows:
function [allXYA, startIndex, endIndex, numRows] = getGraphDataInRange(obj,startTime, endTime)
if isempty(startTime) && isempty(endTime)
% Retrieve the motion data for the whole range
X = obj.motionData.position(:, 1); Y = obj.motionData.position(:, 2);
theta = obj.motionData.orientation;
allXYA = [X,Y,theta]; startIndex = 1; endIndex = length(allXYA(:, 1));
else
% Retrieve the motion data within the specified time range
[allXYA, startIndex, endIndex] = obj.motionData.getDataInRange(startTime, endTime);
end
numRows = length(allXYA(:, 1));
end
function plotGraphData3(obj,allXYA, numRows, size, frameLength)
%set Vehicle and angle texts which will be displayed on top off
% the vehicle
%initialize text first time only
vehicleTxt = text(allXYA(1, 1), allXYA(1, 2)- 0.55, obj.name, ...
'HorizontalAlignment', 'center', 'FontSize', 5, 'Color', 'blue');
angleTxt = text(allXYA(1, 1) + 0.4, allXYA(1, 2), char(968), ...
'HorizontalAlignment', 'left','FontSize', 5, 'Color', 'blue'); % psi symbol = char(968)
%intialization before loop
lastNonZ_XYA = [0, 0, 0]; prevXYA = [0, 0, 0]; hvehicle = [];
hold on
for i = 1:numRows
currXYA = [allXYA(i,1), allXYA(i,2), allXYA(i,3)];
if (i-1 > 0) %check if i is not the very first value
prevXYA = [allXYA(i-1,1), allXYA(i-1,2), allXYA(i-1,3)];
end
if (~isequal(currXYA, [0, 0, 0]) && ~isequal(currXYA , prevXYA))
if (~isempty(hvehicle))
delete(hvehicle); delete(framex); delete(framey);
end
hvehicle = plot(currXYA(1), currXYA(2), 'o', 'MarkerSize', size, 'Color', obj.color);
hdot = plot(currXYA(1), currXYA(2), '.', 'MarkerSize', size/3, 'Color', obj.color);
if(~isequal(prevXYA, [0, 0, 0]))
hpath = plot([prevXYA(1),currXYA(1)], [prevXYA(2), currXYA(2)], 'Color', obj.color);
end
% update text
set(vehicleTxt, 'Position', ...
[currXYA(1), currXYA(2)-0.3],'FontSize', 5, 'Color', obj.color);
set(angleTxt, 'Position', ...
[currXYA(1) + 0.25, currXYA(2)], 'String', sprintf(' %s=\n %.2f^o',char(968), currXYA(3) ), ...
'FontSize', 5, 'Color', obj.color); % psi = char(968)
A = currXYA(3); % set angle
R = [cosd(A) sind(A);
-sind(A) cosd(A)];
R= frameLength * R; %adjust length
framex = quiver(currXYA(1), currXYA(2), R(1,1) , R(1,2), 0, obj.color, 'LineWidth', 1, 'MaxHeadSize', 0.5);
framey = quiver(currXYA(1), currXYA(2), R(2,1), R(2,2), 0, obj.color, 'LineWidth', 1, 'MaxHeadSize', 0.5);
drawnow;
pause(0.05);
lastNonZ_XYA = currXYA;
elseif mod(i,500) == 0 % skip each 500 samples
%plot the last non zero value
if (~isequal(lastNonZ_XYA, [0, 0, 0])) % if previous = current values
if (~isempty(hvehicle))
delete(hvehicle);
end
hvehicle = plot(lastNonZ_XYA(1), lastNonZ_XYA(2), 'o', 'MarkerSize', size, 'Color', obj.color);
% x sign to denote that the vehicle is not moving
hpathStop = plot(lastNonZ_XYA(1), lastNonZ_XYA(2), 'x', 'MarkerSize', size/3, 'Color', 'r');
drawnow;
end
end
end
end
Also to give more prespective the main aim is to plot multiple vehicles from multiple data files and I don't know if there is a more optimized solution because data extraction is taking too much time.
0 Comments
Answers (1)
Shubham
on 28 Aug 2023
Hello mgf_04,
I understand that you have a query regarding data extraction, processing it and finally plotting it, all to be done more efficiently to save time.
In this case you can use the “parpool” function in MATLAB to create parallel pool of process/thread workers and assign the task to be done in parallel using a “parfor” loop. To achieve this, you can make the code modular where each vehicle can be plotted independently and parallelly.
Here is a working example relevant to your query:
% Define the inputs for the function
inputs = [1, 2, 3, 4, 5];
% Create a parallel pool
parpool();
% Execute the function calls in parallel using parfor loop
parfor i = 1:length(inputs)
result = times2(inputs(i));
disp(result);
end
% Close the parallel pool
delete(gcp);
% Define the function to be executed in parallel
function result = times2(input)
% Perform some computation
result = input * 2;
end
To read more about “parpool” using MATLAB, please refer the following documentation –
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!