Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. clear all;
  2. close all;
  3. clc;
  4. %% 1
  5. load('SysIdenData.mat')
  6. t = LogData.time;
  7. y_act = LogData.signals(1).values(:,2);
  8. y_actm = LogData.signals(1).values(:,1);
  9. u_act = LogData.signals(2).values;
  10. Ts = 0.75;
  11.  
  12. figure
  13. subplot(2,1,1);
  14. plot(t,y_act,'b');
  15. hold on
  16. plot(t,y_actm,'r');
  17. hold off
  18. grid on;
  19. xlim([0 2000]);
  20. ylim([0 4]);
  21. title('Actual Output Signal');
  22. xlabel('Time (sec)');
  23. ylabel('Water Level (V)');
  24. legend('Noise-Reduced Output', 'Measured Output');
  25.  
  26. subplot(2,1,2);
  27. plot(t,u_act);
  28. grid on;
  29. xlim([0 2000]);
  30. ylim([0 4]);
  31. title('Actual Input Signal');
  32. xlabel('Time (sec)');
  33. ylabel('Pump Voltage (V)');
  34. legend('Actual Input');
  35. %%
  36. u_offset = 2.05;
  37. u = u_act - u_offset;
  38.  
  39. y_offset = mean(y_act((735/0.75):(750/0.75)));
  40. y = y_act - y_offset;
  41.  
  42. y = y(800:1600);
  43. u = u(800:1600);
  44. t = t(800:1600) - t(800);
  45.  
  46. figure
  47. subplot(2,1,1);
  48. plot(t, y, 'r');
  49. grid on;
  50. xlim([0 1000]);
  51. ylim([-1.5 1])
  52. title('Actual Offset-Free, Truncated Output Signal');
  53. xlabel('Time(sec)');
  54. ylabel('Water Level (V)');
  55. legend('Actual Output');
  56.  
  57. subplot(2,1,2);
  58. plot(t, u);
  59. grid on;
  60. xlim([0 1000]);
  61. ylim([-1.5 1])
  62. title('Actual Offset-Free, Truncated Input Signal');
  63. xlabel('Time(sec)');
  64. ylabel('Pump Voltage (V)');
  65. legend('Actual Input');
  66.  
  67. %%
  68. phi = [];
  69. Y = [];
  70. for k=10 : round(length(y)/2)
  71. temp = [y(k-1) y(k-2) u(k-1) u(k-2)];
  72. phi = [phi;temp];
  73. Y = [Y;y(k)];
  74. end
  75.  
  76. theta_h = inv(phi' * phi) * phi' * Y;
  77. a1 = -theta_h(1);
  78. a2 = -theta_h(2);
  79. b1 = theta_h(3);
  80. b2 = theta_h(4);
  81.  
  82. z = tf('z',Ts);
  83. T = (b1*z + b2)/(z^2 + a1*z + a2);
  84.  
  85. G = [0 1; -a2 -a1];
  86. H = [0; 1];
  87. C = [b2 b1];
  88. D = 0;
  89. SS = ss(G,H,C,D,Ts);
  90. %%
  91. figure
  92. subplot(2,1,1);
  93. Y_sim1 = lsim(T,u(k+1:end));
  94. plot(t(k+1:end),Y_sim1);
  95. hold on
  96. plot(t(k+1:end),y(k+1:end));
  97. legend('Simulated Output','Actual Output');
  98. title('Offset-Free Model Verification (2nd Half)');
  99. xlabel('Time (sec)');
  100. ylabel('Water Level (V)');
  101. ylim([-2 2]);
  102.  
  103. subplot(2,1,2);
  104. Y_sim2 = lsim(T,u);
  105. plot(t,Y_sim2);
  106. hold on;
  107. plot(t,y);
  108. legend('Simulated Output','Actual Output');
  109. title('Offset-Free Model Verification (2nd Half)');
  110. xlabel('Time (sec)');
  111. ylabel('Water Level (V)');
  112. ylim([-2 2]);
  113.  
  114. %% 2
  115. eigenvals = eig(G);
  116. G_obs = G';
  117. H_obs = C';
  118. C_obs = H';
  119. D_obs = 0;
  120.  
  121. L_ndb_sp = acker(G_obs,H_obs,[0.92 0.92]);
  122. sys_cl_ndb_sp = ss((G_obs - H_obs*L_ndb_sp),H_obs,C_obs,D_obs,Ts);
  123. dcgain_ndb_sp = dcgain(sys_cl_ndb_sp);
  124.  
  125. K_db = (G_obs^2)*inv(obsv(G_obs,C_obs))*[0 1]';
  126.  
  127. %% 3
  128. load('SFLogData');
  129. treal = SFLogData.time;
  130. yref = SFLogData.signals(1).values(:,1);
  131. yreal = SFLogData.signals(1).values(:,2);
  132. ureal = SFLogData.signals(2).values;
  133.  
  134.  
  135. [Y_sim, t_sim, x_sim] = lsim(sys_cl_ndb_sp,(1/dcgain_ndb_sp) * yref);
  136.  
  137. figure;
  138. subplot(2,1,1);
  139. plot(treal,yreal,'b');
  140. hold on
  141. plot(treal,yref,'r');
  142. hold on
  143. plot(treal,Y_sim,'g');
  144. grid on;
  145. xlim([0 600]);
  146. ylim([0 4]);
  147. title('Output Feedback Control Results');
  148. xlabel('Time (sec)');
  149. ylabel('Water Level (V)');
  150. legend('Actual', 'Reference','Simulated');
  151.  
  152. [Y_sim, t_sim, x_sim] = lsim(sys_cl_ndb_sp,(1/dcgain_ndb_sp) * (yref-y_offset));
  153.  
  154. u_sim = [];
  155. for count = 1:length(treal)
  156. u_sim(count) = (1/dcgain_ndb_sp * (yref(count)-y_offset)) - (L_ndb_sp * x_sim(count,:)') + u_offset;
  157. end
  158.  
  159. subplot(2,1,2);
  160. plot(treal,ureal,'b');
  161. hold on;
  162. plot(treal,u_sim,'r');
  163. grid on;
  164. xlim([0 600]);
  165. ylim([0 3]);
  166. title('Control Input Signal');
  167. xlabel('Time (sec)');
  168. ylabel('Pump Voltage (V)');
  169. legend('Actual Control Input', 'Simuated Control Input');
  170. %%
  171. Gc = Kp + (Ki*h)/(z-1);
  172. Gcl = (Gc*T)/(1+Gc*T);
  173.  
  174. Gu = Gc/(1+Gc*T);
  175. lsim(Gcl,yref);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement