Skip to content

Commit 89093b6

Browse files
Add files via upload
1 parent 40c54f1 commit 89093b6

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
% This file produces plots for the results of simulations produced by means
2+
% of PyChrono and stored under the folder_1
3+
4+
% Sanjana Sanjay Dhulla
5+
% Mattia Gramuglia
6+
% Andrea L'Afflitto
7+
8+
% 09/21/2023
9+
10+
clear
11+
clc
12+
close all
13+
14+
%% Initializing the folder
15+
% Name of the folder containing the .csv files
16+
folder_1= 'my_ball_density_MRACwithBASELINE';
17+
% folder_1= 'my_ball_density_MRACwithBaseline';
18+
19+
% Scan the folder to get the file names
20+
data = dir(fullfile('my_ball_density_MRACwithBASELINE\*.csv'));
21+
22+
%% Iterating through the files
23+
% Initialize an empty cell array to store the names
24+
file_name_csv = cell(1, numel(data));
25+
26+
% Loop through the elements and extract the 'name' field
27+
for ii=1:numel(data)
28+
file_name_csv{ii} = data(ii).name;
29+
end
30+
31+
% Display the list of names
32+
% disp(file_name_csv);
33+
34+
%% Extracting the required position error data from each file and taking the L2-norm,
35+
% and extracting densities from the file name
36+
norm_tracking_error_at_some_density = zeros(size(data));
37+
Total_thrust_at_some_density = zeros(size(data));
38+
density = zeros(numel(data),1);
39+
computation_time = zeros(size(data));
40+
% Iterating through each file
41+
for ii=1:numel(data)
42+
% Reading the required potion error and time data from each file
43+
file_name = folder_1 + "\" + file_name_csv{ii};
44+
45+
% For versions of MATLAB older than 2020, readlines command cannot be used
46+
% to read headers, hence the below code needs to be implemented to extract
47+
% controller type and density
48+
fileID = fopen(file_name, 'r');
49+
50+
% Read the header lines (assuming there are three header lines)
51+
headerLines = cell(3, 1);
52+
for i = 1:3
53+
headerLines{i} = fgetl(fileID);
54+
end
55+
dataTable = cell2table(cellfun(@(x) strsplit(x, ','), headerLines, 'UniformOutput', false));
56+
% Extracting controller name and ball density
57+
controller_type = dataTable.Var1{2}{2};
58+
density(ii,1) = str2double(dataTable.Var1{3}{3});
59+
60+
Wrapper_data = readtable(file_name);
61+
Wrapper_data = table2array(Wrapper_data);
62+
63+
% Deleting the heading line
64+
Wrapper_data = Wrapper_data(4:end,:);
65+
66+
% Extracting simulation time vector
67+
simulation_time_vector = Wrapper_data(:,1);
68+
% Convert the cell array of strings to a numeric vector
69+
simulation_time_vector = str2double(simulation_time_vector);
70+
71+
% Extracting computation time to determine the total time taken for the
72+
% simulation to run for a particular density
73+
computation_time_at_current_density = Wrapper_data(:,2);
74+
computation_time_at_current_density = str2double(computation_time_at_current_density);
75+
computation_time(ii) = computation_time_at_current_density(end-1,end);
76+
77+
% Extracting translational_position_in_I and
78+
% translational_position_in_I_user and subtracting them to get position
79+
% error. The data vector of PID and MRAC is different hence checking
80+
% the controller and then extracting the required data
81+
translational_position_I = Wrapper_data(:,3:5);
82+
83+
if contains(controller_type, 'MRAC')
84+
translational_position_I_user = Wrapper_data(:,33:35);
85+
Thrust = Wrapper_data(:,49:56);
86+
87+
elseif strcmp(controller_type, 'PID')
88+
translational_position_I_user = Wrapper_data(:,24:26);
89+
Thrust = Wrapper_data(:,40:47);
90+
end
91+
92+
% Determining the total thrust
93+
Thrust = cellfun(@str2double, Thrust, 'UniformOutput', false);
94+
Thrust_mat = cell2mat(Thrust);
95+
Total_thrust = sum(Thrust_mat,2);
96+
97+
%Computing position error
98+
translational_position_I = cellfun(@str2double, translational_position_I, 'UniformOutput', false);
99+
translational_position_I_user = cellfun(@str2double, translational_position_I_user, 'UniformOutput', false);
100+
101+
translational_position_error = cellfun(@minus, translational_position_I, translational_position_I_user, 'UniformOutput', 0);
102+
%translational_position_error = translational_position_I(:,:,:) - translational_position_I_user(:,:,:);
103+
% Convert the cell array result to a numeric array
104+
translational_position_error = cell2mat(translational_position_error);
105+
norm_postion_error = zeros(length(translational_position_error),1);
106+
norm_t_sq = zeros(length(translational_position_error),1);
107+
for jj=1:length(translational_position_error)
108+
% Taking norm at each timestep
109+
norm_postion_error(jj,1) = norm(translational_position_error(jj,:));
110+
111+
% Taking square of norm at each timestep
112+
norm_t_sq(jj,1) = norm_postion_error(jj,1)*norm_postion_error(jj,1);
113+
end
114+
115+
% Integrating the norms using trapz(T,Norm) or cumtrapz(T,Norm)
116+
integral_norm = trapz(simulation_time_vector',norm_t_sq');
117+
118+
% Assing norm of position error for each desnity to a variable (array)
119+
norm_tracking_error_at_some_density(ii,1) = sqrt(integral_norm);
120+
Total_thrust_at_some_density(ii,1) = trapz(simulation_time_vector',Total_thrust');
121+
122+
message = [round(num2str(ii/numel(data)*100,4)),'% of data processed'];
123+
disp(message)
124+
end
125+
126+
%% Creating a dictionary to pair the density and tracking error and sorting them in ascending order
127+
% The map auto-sorts the keys and values with respect to the keys
128+
mapObj = containers.Map(density,norm_tracking_error_at_some_density);
129+
130+
% Converting the sorted values into array
131+
densities = cell2mat(keys(mapObj));
132+
tracking_L2_error_norm = cell2mat(values(mapObj));
133+
134+
135+
136+
% Following the same as above for Thrust
137+
mapObj = containers.Map(density,Total_thrust_at_some_density);
138+
Total_thrust_every_density = cell2mat(values(mapObj));
139+
140+
141+
% Same for computation time
142+
mapObj = containers.Map(density,computation_time);
143+
computation_time = cell2mat(values(mapObj));
144+
%% Interpolation of data
145+
% Use an interpolating polynomial
146+
order_interpolation = 3;
147+
coefficients_L2_error_norm_vs_density = polyfit(densities, tracking_L2_error_norm,order_interpolation);
148+
interpolated_L2_error_norm = polyval(coefficients_L2_error_norm_vs_density, densities);
149+
150+
151+
152+
order_interpolation = 3;
153+
coefficients_Total_thrust_vs_density = polyfit(densities, Total_thrust_every_density,order_interpolation);
154+
interpolated_Total_thrust = polyval(coefficients_Total_thrust_vs_density, densities);
155+
156+
%% Plotting
157+
% Tracking error vs density
158+
set(figure,'Color','white','WindowState','maximized')
159+
plot(densities, tracking_L2_error_norm, '-o','LineWidth',2)
160+
hold on
161+
plot(densities,interpolated_L2_error_norm,'k-','LineWidth',2)
162+
xlabel('Ball density','interpreter','latex','fontsize',22)
163+
ylabel('$\Vert e(\cdot) \Vert_{L_2}$','interpreter','latex','fontsize',22)
164+
l = legend('Raw data',['Interpolating polynomial order ', num2str(order_interpolation)]);
165+
set(l,'interpreter','latex','fontsize',22)
166+
axis tight
167+
168+
variable_title = ['Control technique:',' ', controller_type];
169+
% variable_title = [variable_title,techique_used];
170+
171+
title(variable_title,'interpreter','latex','fontsize',22)
172+
173+
174+
175+
176+
% Thrust vs density
177+
set(figure,'Color','white','WindowState','maximized')
178+
plot(densities, Total_thrust_every_density, '-o','LineWidth',2)
179+
hold on
180+
plot(densities,interpolated_Total_thrust,'k-','LineWidth',2)
181+
xlabel('Ball density','interpreter','latex','fontsize',22)
182+
ylabel('Total Thrust','interpreter','latex','fontsize',22)
183+
l = legend('Raw data',['Interpolating polynomial order ', num2str(order_interpolation)]);
184+
set(l,'interpreter','latex','fontsize',22)
185+
axis tight
186+
187+
variable_title = ['Control technique:',' ', controller_type];
188+
% variable_title = [variable_title,techique_used];
189+
190+
title(variable_title,'interpreter','latex','fontsize',22)
191+
192+
193+
194+
195+
% Computation time vs density
196+
set(figure,'Color','white','WindowState','maximized')
197+
plot(densities, computation_time, '-o','LineWidth',2)
198+
xlabel('Ball density','interpreter','latex','fontsize',22)
199+
ylabel('Computation Time [s]','interpreter','latex','fontsize',22)
200+
201+
axis tight
202+
203+
variable_title = ['Control technique:',' ', controller_type];
204+
% variable_title = [variable_title,techique_used];
205+
206+
title(variable_title,'interpreter','latex','fontsize',22)

0 commit comments

Comments
 (0)