%
% 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);
