Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
pjago committed Apr 15, 2019
0 parents commit 48f2935
Show file tree
Hide file tree
Showing 77 changed files with 7,872 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
src/astron.m
src/zigler_nichols.m
src/relay.m
*.m~
*.asv
survey
src/easylife.m
sim/
dc fan/
web fan/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Pedro Martins

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 38 additions & 0 deletions basic_set.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
%% read and press ctrl + enter for each block!
return
clear

%% libraries (must be at current folder!)
addpath('jsonlab-1.5')

%% Triangular Pulse Input
nlang = 3; %number of linguistic variables
mixing = 0.5; %from 0 to 1, there are at most two variables (required)
span = 400; %distance beetween edges (input span)

b = linspace(-1.0, 1.0, nlang)';
a = b - 1/(nlang - 1)*(1 + mixing)';
c = b + 1/(nlang - 1)*(1 + mixing)';
a(1) = -inf; c(end) = inf; %edges
s = struct('params', mat2cell([a b c]*span/2, ones([1 nlang])),...
'type', 'triangularPulse');

%% Triangular Pulse Output
nlang = 5; %number of linguistic variables
mixing = 0.5; %from 0 to 1, there are at most two variables (required)
span = 100; %distance beetween edges (input span)

b = linspace((1 + mixing)/(2*nlang), 1.0 - (1 + mixing)/(2*nlang), nlang)';
a = b - (1 + mixing)/(2*nlang)';
c = b + (1 + mixing)/(2*nlang)';
s = struct('params', mat2cell([a b c]*span, ones([1 nlang])),...
'type', 'triangularPulse');

%% other distributions @todo

%% Save JSON
json = savejson('', s);
json([1:2 end-1:end]) = [];
file = fopen('.\fuzzy\membership\fan\u.json', 'w');
fprintf(file, json);
fclose(file);
27 changes: 27 additions & 0 deletions centroid_area.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function out = centroid_area( params )
%CENTROID x = centroid( params )
switch params{end}
case 'triangularPulse'
[h, a, b, c, x] = params{:};
if isnan(x(1)) && isnan(x(2))
area = 0;
centroid = 0;
else
S(1) = (b - x(1))*h;
C(1) = (b + x(1))/2;
S(2) = (x(2) - b)*h;
C(2) = (b + x(2))/2;
S(3) = (x(1) - a)*h/2;
C(3) = (a + 2*x(1))/3;
S(4) = (c - x(2))*h/2;
C(4) = (c + 2*x(2))/3;
area = sum(S);
if area ~= 0
centroid = sum(C.*S)/area;
else
centroid = 0;
end
end
end
out = [centroid area];
end
Binary file added degrau.slx
Binary file not shown.
14 changes: 14 additions & 0 deletions fuzzy/fan/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%fan fuzzy uses ../membership/fan json data
%and K is the plant model dc gain

u(k) = fuzzy(e(k), de(k)); %no model
u(k) = (r(k)/K - 50) + fuzzy(e(k), de(k)); %with model

Gz =

0.07751 z^-1 + 0.1716 z^-2
------------------------------
1 - 0.8539 z^-1 - 0.02473 z^-2

Sample time: 0.1 seconds
Discrete-time transfer function.
Binary file added fuzzy/fan/no_model.fig
Binary file not shown.
Binary file added fuzzy/fan/no_model.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fuzzy/fan/with_model.fig
Binary file not shown.
Binary file added fuzzy/fan/with_model.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions fuzzy/fan1/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
%fan1 fuzzy uses ../membership/fan1 json data
%and K is the plant model dc gain

u(k) = fuzzy(e(k), de(k)); %no model
u(k) = (r(k)/K - 50) + fuzzy(e(k), de(k)); %with model

Gz =

0.07751 z^-1 + 0.1716 z^-2
------------------------------
1 - 0.8539 z^-1 - 0.02473 z^-2

Sample time: 0.1 seconds
Discrete-time transfer function.
Binary file added fuzzy/fan1/no_model.fig
Binary file not shown.
Binary file added fuzzy/fan1/no_model.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fuzzy/fan1/with_model.fig
Binary file not shown.
Binary file added fuzzy/fan1/with_model.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions fuzzy/membership/fan/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"params": ["-_Inf_",-120,-10],
"type": "triangularPulse"
},
{
"params": [-30,0,30],
"type": "triangularPulse"
},
{
"params": [10,120,"_Inf_"],
"type": "triangularPulse"
}
]
14 changes: 14 additions & 0 deletions fuzzy/membership/fan/e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"params": ["-_Inf_",-50,-5],
"type": "triangularPulse"
},
{
"params": [-10,0,10],
"type": "triangularPulse"
},
{
"params": [5,50,"_Inf_"],
"type": "triangularPulse"
}
]
22 changes: 22 additions & 0 deletions fuzzy/membership/fan/u.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"params": [0,15,30],
"type": "triangularPulse"
},
{
"params": [17.5,32.5,47.5],
"type": "triangularPulse"
},
{
"params": [35,50,65],
"type": "triangularPulse"
},
{
"params": [52.5,67.5,82.5],
"type": "triangularPulse"
},
{
"params": [70,85,100],
"type": "triangularPulse"
}
]
14 changes: 14 additions & 0 deletions fuzzy/membership/fan1/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"params": ["-_Inf_",-250,-10],
"type": "triangularPulse"
},
{
"params": [-100,0,100],
"type": "triangularPulse"
},
{
"params": [10,250,"_Inf_"],
"type": "triangularPulse"
}
]
14 changes: 14 additions & 0 deletions fuzzy/membership/fan1/e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"params": ["-_Inf_",-200,-5],
"type": "triangularPulse"
},
{
"params": [-25,0,25],
"type": "triangularPulse"
},
{
"params": [5,200,"_Inf_"],
"type": "triangularPulse"
}
]
22 changes: 22 additions & 0 deletions fuzzy/membership/fan1/u.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"params": [0,15,30],
"type": "triangularPulse"
},
{
"params": [17.5,32.5,47.5],
"type": "triangularPulse"
},
{
"params": [35,50,65],
"type": "triangularPulse"
},
{
"params": [52.5,67.5,82.5],
"type": "triangularPulse"
},
{
"params": [70,85,100],
"type": "triangularPulse"
}
]
116 changes: 116 additions & 0 deletions fuzzy_fan.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
%1. Right-click the editor's tab and select "Change current folder to..."
%2. Run the code either with Command Window, F5 or Ctrl+Enter
%3. Modify the configuration and repeat from (2)
clc
clear
format shortg
addpath(genpath('src'))
global t y r e u pwm k

% Adicione o nome de variáveis que queira salvar
salvar = {'t', 'y', 'r', 'e', 'u', 'pwm', 'ping', 'o', 'k', 'n', 'T'};

T = 0.1; %tempo de amostragens
n = 300; %numero de amostras
o = 3; %início de amostragem
t = (0:(n-1))*T; %vetor de tempo

%% I/O
%tenta conectar com a planta, simula caso aconteça algum erro
z = tf('z', T, 'variable', 'z^-1');
Gz = z^-1*(0.077508 + 0.17161*z^-1)/(1 - 0.8539*z^-1 - 0.02473*z^-2);

%ajuste a COM e o baud rate de 19200, em Gerenciador de Dispositivos
[termina, leitura, escrita, planta] = startcom('COM20', Gz);

%% CONFIGURACAO
pasta = planta*'pratica' + ~planta*'teorica';
salvar_em = ['ventilador/' pasta '/fuzzy'];
saturacao = 100;
referencia = 100;
escala = 200;

%ESTADO INCIAL
[r, y, e, u, pwm] = deal(zeros(n, 1));
ping = nan(n, 1);
t0 = tic;

%% FUZZY UZI
addpath('.\jsonlab-1.5')
pasta_fuzzy = '.\fuzzy\membership\fan1\';
mfe = membership(loadjson([pasta_fuzzy 'e.json']));
mfde = membership(loadjson([pasta_fuzzy 'de.json']));
mfu = membership(loadjson([pasta_fuzzy 'u.json']));

AND = @(x1, x2) min(x1, x2);
OR = @(x1, x2) max(x1, x2);
IF = @(y, member) centroid_area([y member(y)]);

% AND min
% OR max
% IF centroid
% -e & -de = --u
% -e & +de | 0e & -de = -u
% -e & 0de | +e & 0de | 0e & 0de = 0u
% 0e & +de | +e & -de = +u
% +e & +de = ++u

%% CONTROL LOOP
K = dcgain(Gz);
dr = (escala - referencia)/4;

for k = 3:n
%MEDIR SENSOR
time = tic;
y(k) = leitura();

%REFERÊNCIA
r(k) = referencia + dr*(-1*(k>n/5) + 2*(k>2*n/5) - 3*(k>3*n/5) + 4*(k>4*n/5));
e(k) = r(k) - y(k);

%CONTROLE
%fuzzification
fe = mfe.fx(e(k));
fde = mfde.fx((y(k) - y(k-1))/T);
[ne, ze, pe] = fe{:};
[nde, zde, pde] = fde{:};
[nnu, nu, zu, pu, ppu] = mfu.fy{:};
%rule system
rules = ...
[
IF(AND(ne, nde), nnu);
IF(OR(OR(AND(ne, pde), AND(ne, zde)), AND(ze, nde)), nu);
IF(AND(ze, zde), zu);
IF(OR(OR(AND(pe, nde), AND(pe, zde)), AND(ze, pde)), pu);
IF(AND(pe, pde), ppu);
];
%defuzzification
% u(k) = sum(rules(:,1).*rules(:,2))/sum(rules(:,2));
u(k) = (r(k)/K - 50) + sum(rules(:,1).*rules(:,2))/sum(rules(:,2));

%ATUAÇÃO E SATURAÇÃO
pwm(k) = min(max(u(k), 0), saturacao);

escrita(pwm(k));
ping(k) = toc(time);

%DELAY
if planta
while toc(time) < T
end
end
end
termina();
fprintf('Tempo: %f segundos\n\n', toc(t0) - toc(time));

%% PLOT & SALVE
fig = plotudo(t(o:k), y, r, e, u, pwm, 0, 0, 0);
if ~exist(salvar_em, 'dir')
mkdir(salvar_em);
end
date = datestr(datetime('now'));
date(date == '-' | date == ':') = '_';
path = [salvar_em '/' date];
save([path '.mat'], salvar{:})
saveas(fig, [path '.fig'])
disp(['Plant: ' salvar_em ' Saved at: ' path])
Loading

0 comments on commit 48f2935

Please sign in to comment.