Guest User

Untitled

a guest
Oct 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. function main()
  2. clear all
  3. clc
  4.  
  5. simulateUsing_lsim()
  6. simulateUsing_loop()
  7.  
  8. end
  9.  
  10.  
  11. %%%%%% Simulating using lsim %%%%%%%
  12. function simulateUsing_lsim()
  13.  
  14. % Define the continuous-time closed-loop system
  15. P = getContPlant();
  16. [Kp,Ki,Kd] = get_PIDgains();
  17. C = pid(Kp,Ki,Kd);
  18. clSys_cont = feedback(C*P,1);
  19.  
  20. % Define the discrete-time closed-loop system
  21. hk = get_sampling_time();
  22. clSys_disc = c2d(clSys_cont,hk);
  23.  
  24. % Generate the reference signal and the time vector
  25. [r,t] = getReference(hk);
  26.  
  27. %% Simulate and plot using lsim
  28. figure
  29. lsim(clSys_disc,r,t)
  30.  
  31. %% Finding and plotting the error
  32. y = lsim(clSys_disc,r);
  33. e = r - y;
  34. figure
  35. p = plot(t,e,'b--');
  36. set(p,'linewidth',2)
  37. legend('error')
  38. xlabel('Time (seconds)')
  39. ylabel('error')
  40. % xlim([-.1 10.1])
  41. end
  42.  
  43. %%%%%% Simulating using loop iteration (difference equations) %%%%%%%
  44. function simulateUsing_loop()
  45.  
  46. % Get the cont-time ol-sys
  47. P = getContPlant();
  48.  
  49. % Get the sampling time
  50. hk = get_sampling_time();
  51.  
  52. % Get the disc-time ol-sys in SS representation
  53. P_disc = ss(c2d(P,hk));
  54. Ad = P_disc.A;
  55. Bd = P_disc.B;
  56. Cd = P_disc.C;
  57.  
  58. % Get the PID gains
  59. [Kp,Ki,Kd] = get_PIDgains();
  60.  
  61. % Generate the reference signal and the time vector
  62. [r,t] = getReference(hk);
  63.  
  64. %% Perform the system simulation
  65. x = [0 0]'; % Set initial states
  66. e = 0; % Set initial errors
  67. integral_sum = 0; % Set initial integral part value
  68.  
  69. for i=2:1:length(t)
  70.  
  71. % Calculate the output signal "y"
  72. y(:,i) = Cd*x;
  73.  
  74. % Calculate the error "e"
  75. e(:,i) = y(:,i) - r(i);
  76.  
  77. % Calculate the control signal vector "u"
  78. integral_sum = integral_sum + Ki*hk*e(i);
  79. u(:,i) = Kp*e(i) + integral_sum + (1/hk)*Kd*(e(:,i)-e(:,i-1));
  80.  
  81. % Saturation. Limit the value of u withing the range [-tol tol]
  82. % tol = 100;
  83. % if abs(u(:,i)) > tol
  84. % u(:,i) = tol * abs(u(:,i))/u(:,i);
  85. % else
  86. % end
  87.  
  88. % Calculate the state vector "x"
  89. x = Ad*x + Bd*u(:,i); % State transitions to time n
  90.  
  91. end
  92.  
  93. %% Subplots
  94. figure
  95. plot(t,y,'b',t,r,'g--')
  96.  
  97. %% Plotting the error
  98. figure
  99. p = plot(t,e,'r');
  100. set(p,'linewidth',2)
  101. legend('error')
  102. xlabel('Time (seconds)')
  103. ylabel('error')
  104.  
  105. end
  106.  
  107. function P = getContPlant()
  108. s = tf('s');
  109. P = 1/(s^2 + 10*s + 20);
  110. end
  111.  
  112. function [Kp,Ki,Kd] = get_PIDgains()
  113. Kp = 350;
  114. Ki = 300;
  115. Kd = 50;
  116. end
  117.  
  118. function hk = get_sampling_time()
  119. hk = 0.01;
  120. end
  121.  
  122. function [r,t] = getReference(hk)
  123. [r,t] = gensig('square',4,10,hk);
  124. end
Add Comment
Please, Sign In to add comment