Guest User

Untitled

a guest
Feb 13th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. clc
  2. clear
  3. format shortg
  4. addpath(genpath('src'))
  5.  
  6. global t y r e u pwm k
  7.  
  8. %% CONFIGURAÇÃO
  9.  
  10. % Adicione o nome de variáveis que queira salvar
  11. toSave = {'t', 'y', 'r', 'e', 'u', 'pwm'};
  12.  
  13. T = 0.15; %tempo de amostragem
  14. n = 101; %número de amostras
  15. t = (0:(n-1))*T; %vetor de tempo
  16.  
  17. %% I/O
  18.  
  19. %caso não ache a planta, o programa simula pela função de transferência Gz
  20. z = tf('z', T, 'variable', 'z^-1');
  21. Gz = z^-1*(0.744 + 0.995*z^-1)/(1 - 0.5812*z^-1 - 0.02333*z^-2);
  22.  
  23. %ajuste a COM e o baud rate de 19200, em Gerenciador de Dispositivos
  24. [stop, read, write] = startcom('COM20', Gz);
  25.  
  26. %% ESTADO INCIAL
  27.  
  28. w = 2/8;
  29. Gjw = evalfr(Gz, exp(1j*w*pi));
  30. kp = dcgain(Gz);
  31. ref = 200;
  32. set = ref/kp;
  33. d = 20;
  34. a = 4*d/pi*abs(Gjw);
  35. eps = -4*d/pi*imag(Gjw);
  36.  
  37. D = 0.4;
  38. eo = (D - 0.5)*pi*a*sqrt(1 - (eps/a)^2);
  39. uo = (2*D - 1)*d;
  40. ko = uo/eo;
  41. ro = eo + kp*uo;
  42.  
  43. %notch filter
  44. Qf = 10;
  45. BW = w/Qf;
  46. [bf, af] = iirnotch(w, BW);
  47.  
  48. %limit cycles
  49. cycle = false;
  50. spin = 1;
  51.  
  52. [r, y, e, u, pwm] = deal(zeros(n, 1));
  53. ping = nan(n, 1);
  54. t0 = tic;
  55.  
  56. %% LOOP DE CONTROLE
  57.  
  58. % ONLY FOR SIMULATION!!!
  59. for k = 1:2
  60. y(k) = ref + ro;
  61. r(k) = ref + ro;
  62. e(k) = 0;
  63. u(k) = set;
  64. pwm(k) = u(k);
  65. write(pwm(k));
  66. end
  67.  
  68. for k = 3:n
  69. %LEITURA
  70. time = tic;
  71. y(k) = read();
  72.  
  73. %REFERÊNCIA E ERRO
  74. % r(k) = ref;
  75. r(k) = -af(2:end)*r(k-1:-1:k-2) + bf*y(k:-1:k-2);
  76. e(k) = r(k) - y(k);
  77.  
  78. %CONTROLE
  79. if e(k) + eo >= eps && spin == -1
  80. cycle = true;
  81. spin = 1;
  82. elseif e(k) + eo <= -eps && spin == 1
  83. cycle = true;
  84. spin = -1;
  85. elseif ~(mod(k, 1/w) || cycle)
  86. spin = -spin;
  87. end
  88. u(k) = set + d*spin;
  89.  
  90. %SATURAÇÃO
  91. if u(k) > 100
  92. pwm(k) = 100;
  93. elseif u(k) < 0
  94. pwm(k) = 0;
  95. else
  96. pwm(k) = u(k);
  97. end
  98.  
  99. %ESCRITA
  100. write(pwm(k));
  101. ping(k) = toc(time);
  102.  
  103. %DELAY
  104. if isa(stop, 'function_handle')
  105. while toc(time) < T
  106. end
  107. end
  108. end
  109. stop();
  110. fprintf('Duração: %f seconds\n', toc(t0) - toc(time));
  111. if sum(ping(1:end-1)' > T)
  112. disp('In-loop latency is too high! Increase your sampling time.')
  113. end
  114.  
  115. %% PLOT & SAVE
  116.  
  117. fig = plotudo(t, y, r, e, u, pwm, 0, 0);
  118.  
  119. if isa(stop, 'function_handle')
  120. folder = 'pratica';
  121. else
  122. folder = 'teoria';
  123. end
  124. if ~exist(folder, 'dir')
  125. mkdir(folder);
  126. end
  127. date = datestr(datetime('now'));
  128. date(date == '-' | date == ':') = '_';
  129. path = [folder '/' date];
  130. save([path '.mat'], toSave{:})
  131. saveas(fig, [path '.fig'])
  132. disp(['Plant: ' folder ' Saved at: ' path])
Add Comment
Please, Sign In to add comment