HW2: Assignment 2 added

This commit is contained in:
2025-10-26 00:28:24 +03:00
parent de85e9cf10
commit 5e17867857
20 changed files with 522 additions and 2 deletions
+3
View File
@@ -0,0 +1,3 @@
# Matlab auxiliary files
*.asv
+73
View File
@@ -0,0 +1,73 @@
[System]
Name='CarFLC'
Type='mamdani'
Version=2.0
NumInputs=3
NumOutputs=1
NumRules=27
AndMethod='min'
OrMethod='max'
ImpMethod='min'
AggMethod='sum'
DefuzzMethod='centroid'
[Input1]
Name='dh'
Range=[0 1]
NumMFs=3
MF1='S':'trimf',[0 0 0.5]
MF2='M':'trimf',[0 0.5 1]
MF3='L':'trimf',[0.5 1 1]
[Input2]
Name='dv'
Range=[0 1]
NumMFs=3
MF1='S':'trimf',[0 0 0.5]
MF2='M':'trimf',[0 0.5 1]
MF3='L':'trimf',[0.5 1 1]
[Input3]
Name='theta'
Range=[-180 180]
NumMFs=3
MF1='N':'trimf',[-180 -180 0]
MF2='ZE':'trimf',[-180 0 180]
MF3='P':'trimf',[0 180 180]
[Output1]
Name='dtheta'
Range=[-130 130]
NumMFs=3
MF1='N':'trimf',[-130 -130 0]
MF2='ZE':'trimf',[-130 0 130]
MF3='P':'trimf',[0 130 130]
[Rules]
1 1 1, 3 (1) : 1
1 2 1, 3 (1) : 1
1 3 1, 3 (1) : 1
2 1 1, 3 (1) : 1
2 2 1, 2 (1) : 1
2 3 1, 2 (1) : 1
3 1 1, 3 (1) : 1
3 2 1, 3 (1) : 1
3 3 1, 3 (1) : 1
1 1 2, 3 (1) : 1
1 2 2, 3 (1) : 1
1 3 2, 3 (1) : 1
2 1 2, 2 (1) : 1
2 2 2, 2 (1) : 1
2 3 2, 2 (1) : 1
3 1 2, 2 (1) : 1
3 2 2, 2 (1) : 1
3 3 2, 2 (1) : 1
1 1 3, 2 (1) : 1
1 2 3, 2 (1) : 1
1 3 3, 2 (1) : 1
2 1 3, 2 (1) : 1
2 2 3, 2 (1) : 1
2 3 3, 1 (1) : 1
3 1 3, 2 (1) : 1
3 2 3, 2 (1) : 1
3 3 3, 1 (1) : 1
Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 277 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

+77
View File
@@ -0,0 +1,77 @@
%
% Local script to create FIS (w/o the GUI crap)
%
% FIS control
% R2025 compatibility:
% - newfis->mamfis
% - defuzzMethod='cos' -> AggregationMethod="sum", DefuzzificationMethod="centroid"
fis = mamfis( ...
AndMethod="min", ...
OrMethod="max", ...
ImplicationMethod="min", ...
AggregationMethod="sum", ...
DefuzzificationMethod="centroid" );
% Inputs
fis = addInput(fis,[0 1],'Name','dh');
fis = addMF(fis,'dh','trimf',[0 0 0.5],'Name','S');
fis = addMF(fis,'dh','trimf',[0 0.5 1],'Name','M');
fis = addMF(fis,'dh','trimf',[0.5 1 1],'Name','L');
fis = addInput(fis,[0 1],'Name','dv');
fis = addMF(fis,'dv','trimf',[0 0 0.5],'Name','S');
fis = addMF(fis,'dv','trimf',[0 0.5 1],'Name','M');
fis = addMF(fis,'dv','trimf',[0.5 1 1],'Name','L');
fis = addInput(fis,[-180 180],'Name','theta');
fis = addMF(fis,'theta','trimf',[-180 -180 0],'Name','N');
fis = addMF(fis,'theta','trimf',[-180 0 180],'Name','ZE');
fis = addMF(fis,'theta','trimf',[ 0 180 180],'Name','P');
% Output
fis = addOutput(fis,[-130 130],'Name','dtheta');
fis = addMF(fis,'dtheta','trimf',[-130 -130 0],'Name','N');
fis = addMF(fis,'dtheta','trimf',[-130 0 130],'Name','ZE');
fis = addMF(fis,'dtheta','trimf',[ 0 130 130],'Name','P');
% Rules
fis = addRule(fis, "IF theta IS N dh IS S AND dv IS S AND THEN dtheta IS P");
fis = addRule(fis, "IF theta IS N dh IS S AND dv IS M AND THEN dtheta IS P");
fis = addRule(fis, "IF theta IS N dh IS S AND dv IS L AND THEN dtheta IS P");
fis = addRule(fis, "IF theta IS N dh IS M AND dv IS S AND THEN dtheta IS P");
fis = addRule(fis, "IF theta IS N dh IS M AND dv IS M AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS N dh IS M AND dv IS L AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS N dh IS L AND dv IS S AND THEN dtheta IS P");
fis = addRule(fis, "IF theta IS N dh IS L AND dv IS M AND THEN dtheta IS P");
fis = addRule(fis, "IF theta IS N dh IS L AND dv IS L AND THEN dtheta IS P");
fis = addRule(fis, "IF theta IS ZE dh IS S AND dv IS S AND THEN dtheta IS P");
fis = addRule(fis, "IF theta IS ZE dh IS S AND dv IS M AND THEN dtheta IS P");
fis = addRule(fis, "IF theta IS ZE dh IS S AND dv IS L AND THEN dtheta IS P");
fis = addRule(fis, "IF theta IS ZE dh IS M AND dv IS S AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS ZE dh IS M AND dv IS M AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS ZE dh IS M AND dv IS L AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS ZE dh IS L AND dv IS S AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS ZE dh IS L AND dv IS M AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS ZE dh IS L AND dv IS L AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS P dh IS S AND dv IS S AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS P dh IS S AND dv IS M AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS P dh IS S AND dv IS L AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS P dh IS M AND dv IS S AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS P dh IS M AND dv IS M AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS P dh IS M AND dv IS L AND THEN dtheta IS N");
fis = addRule(fis, "IF theta IS P dh IS L AND dv IS S AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS P dh IS L AND dv IS M AND THEN dtheta IS ZE");
fis = addRule(fis, "IF theta IS P dh IS L AND dv IS L AND THEN dtheta IS N");
writeFIS(fis,'CarFLC');
f1 = figure('Name','FLC surface'); gensurf(fis); grid on;
exportgraphics(f1, sprintf('FLC_surface.png'), 'Resolution', 300);
+15
View File
@@ -0,0 +1,15 @@
function [x_new, y_new, theta_new] = moveCar(x, y, theta, dtheta)
%MOVECAR updates car's position based on previous state
% Detailed explanation goes here
global Speed_mag;
global Ts;
% Stear and then calculate the new position
theta_new = theta + dtheta;
x_new = x + Speed_mag * Ts * cos(toRadians("degrees", theta_new));
y_new = y + Speed_mag * Ts * sin(toRadians("degrees", theta_new));
end
+88
View File
@@ -0,0 +1,88 @@
%% Car - Simulation
%
% Assignment 2 in Fuzzy systems
%
% author:
% Christos Choutouridis ΑΕΜ 8997
% cchoutou@ece.auth.gr
%
clear; clc; close all;
global Speed_mag;
global Ts;
% Configuration
% ----------------------
Speed_mag = 0.05;
Ts = 1;
% Givents
% ----------------------
corners = [5 1 ; 6 2 ; 7 3];
x_init = 3.8;
y_init = 0.5;
x_final = 10;
y_final = 3.2;
theta_init = [-45 0 45];
% Saturation between min: m and max: M
sat = @(x, m, M) max(m, min(M, x));
% Load control
flc = readfis('CarFLC');
t = 0:Ts:250;
for i = 1:length(theta_init)
x = x_init;
y = y_init;
theta = theta_init(i);
X = zeros(size(t));
Y = zeros(size(t));
End=length(t);
for k=1:End
[dh, dv] = wallDistance(corners, x, y);
if isfinite(dh) && ~isfinite(dv) && (dh <=0 || dv <= 0)
End = k-1;
fprintf('Car crashed!! - Stop simulation');
break;
end
dtheta = evalfis(flc, [sat(dh, 0, 1) sat(dv, 0, 1) theta]);
[x, y, theta] = moveCar(x, y, theta, dtheta);
X(k) = x; Y(k) = y;
if x >= x_final
End = k-1;
break;
end
end
figure(i); clf; hold on; grid on; axis equal;
xlim([2 11]); ylim([-1 4]);
xlabel('x [m]'); ylabel('y [m]');
title( ...
"Car movement $(\theta_0 =" + theta_init(i)+ "^{\circ})$", ...
'Interpreter','latex', ...
'FontSize', 16);
% Walls
fill([5 6 6 5],[0 0 1 1],[0.5 0.5 0.5],'FaceAlpha',0.4,'EdgeColor','none');
fill([6 7 7 6],[0 0 2 2],[0.5 0.5 0.5],'FaceAlpha',0.4,'EdgeColor','none');
fill([7 10 10 7],[0 0 3 3],[0.5 0.5 0.5],'FaceAlpha',0.4,'EdgeColor','none');
plot(X(1:End), Y(1:End), '-', 'LineWidth', 1.5);
hold on;
plot(X(1), Y(1), 'go', 'MarkerFaceColor', 'g'); % begin
plot(X(End), Y(End), 'ro', 'MarkerFaceColor', 'r'); % end
hold off;
filename = sprintf('CarMovement_theta%d.png', theta_init(i));
saveas(gcf, filename);
end
+45
View File
@@ -0,0 +1,45 @@
function [dh, dv] = wallDistance(corners, x, y)
% Calculates the distances from the walls.
% - Negative value means colision
% - Inf value means no wall in this direction
%
% Inputs:
% corners[] : corner matrix (each line is a corner point)
% x: Car's x position
% y: Car's y position
% Outputs:
% dh: Horizontal distance from wall
% dv: Vertical distance from wall
% Calculate the distances in the x direction
srt_corners = sortrows(corners, 2);
active_corners = -Inf(size(corners));
for i=1:length(srt_corners)
if srt_corners(i, 2) >= y
active_corners = srt_corners(i:end, :);
break;
end
end
dx = active_corners(:, 1) - x; % Walls are on the right
dh = min(dx); % Minimum distance in x direction
% Calculate the distances in the y direction
srt_corners = sortrows(corners, 1, 'descend');
active_corners = -Inf(size(corners));
for i=1:length(srt_corners)
if srt_corners(i, 1) <= x
active_corners = srt_corners(i:end, :);
break;
end
end
dy = y - active_corners(:, 2); % Walls are on the bottom
dv = min(dy); % Minimum distance in y direction
if ~isfinite(dh) dh = Inf; end
if ~isfinite(dv) dv = Inf; end
end