|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import control as ctl |
| 4 | +from vrft import * |
| 5 | + |
| 6 | +def generateNoise(t): |
| 7 | + omega = 2*np.pi*100 |
| 8 | + xi = 0.9 |
| 9 | + noise = np.random.normal(0,0.1,t.size) |
| 10 | + # Second order system |
| 11 | + yn,t,x = ctl.lsim(ctl.tf([omega**2], [1, 2*xi*omega, omega**2]), \ |
| 12 | + noise, t) |
| 13 | + return yn |
| 14 | + |
| 15 | +#Generate time and u(t) signals |
| 16 | +t_start = 0 |
| 17 | +t_end = 10 |
| 18 | +t_step = 1e-2 |
| 19 | +t = np.arange(t_start, t_end, t_step) |
| 20 | +u = np.ones(len(t)).tolist() |
| 21 | +u[200:400] = np.zeros(200) |
| 22 | +u[600:800] = np.zeros(200) |
| 23 | + |
| 24 | +#Experiment |
| 25 | +num = [0.5] |
| 26 | +den = [1, -1.6, 0.8] |
| 27 | +sys = ctl.tf(num, den, t_step) |
| 28 | +y,t,x= ctl.lsim(sys, u, t) |
| 29 | +y += generateNoise(t) |
| 30 | +data = vrft.iddata(y[0],u,t_step,[0]) |
| 31 | + |
| 32 | +#Reference Model |
| 33 | +refModel = ctl.tf([0.2], [1, -0.8], t_step) |
| 34 | + |
| 35 | +#PI Controller |
| 36 | +base = [ctl.tf([1], [1],t_step), |
| 37 | + ctl.tf([1, 0], [1, -1],t_step), |
| 38 | + ctl.tf([1, -1], [1, 0],t_step)] |
| 39 | + |
| 40 | +#Experiment filter |
| 41 | +omega = 2*np.pi*1 |
| 42 | +L = ctl.tf([1], [1/omega, 1]) |
| 43 | +L = ctl.sample_system(L, 0.01) |
| 44 | + |
| 45 | +#VRFT |
| 46 | +C, theta, r = vrft.vrftAlgorithm(data, refModel, base, L) |
| 47 | + |
| 48 | +#Obtained controller |
| 49 | +print "Controller:", C |
| 50 | +L = C*sys |
| 51 | +L = L/(1+L) |
| 52 | + |
| 53 | +L = L.minreal() |
| 54 | +#this is due to a bug in the control library |
| 55 | +L = ctl.tf(L.num, L.den, t_step) |
| 56 | + |
| 57 | +print "Theta:", theta |
| 58 | + |
| 59 | +#Analysis |
| 60 | +yr,t = ctl.step(refModel, t) |
| 61 | +yc,t = ctl.step(L, t) |
| 62 | +ys,t = ctl.step(sys, t) |
| 63 | + |
| 64 | + |
| 65 | +fig, ax = plt.subplots(4, sharex=True) |
| 66 | +ax[0].plot(t,yr[0],label='Ref System') |
| 67 | +ax[0].plot(t,yc[0], label='CL System') |
| 68 | +ax[0].set_title('Systems response') |
| 69 | +ax[0].grid(True) |
| 70 | +ax[1].plot(t,ys[0], label='OL System') |
| 71 | +ax[1].set_title('OL Systems response') |
| 72 | +ax[1].grid(True) |
| 73 | +ax[2].plot(t,y[0]) |
| 74 | +ax[2].grid(True) |
| 75 | +ax[2].set_title('Experiment data') |
| 76 | +ax[3].plot(t,r) |
| 77 | +ax[3].grid(True) |
| 78 | +ax[3].set_title('Virtual Reference') |
| 79 | + |
| 80 | +# Now add the legend with some customizations. |
| 81 | +legend = ax[0].legend(loc='lower right', shadow=True) |
| 82 | + |
| 83 | +# The frame is matplotlib.patches.Rectangle instance surrounding the legend. |
| 84 | +frame = legend.get_frame() |
| 85 | +frame.set_facecolor('0.90') |
| 86 | + |
| 87 | +# Set the fontsize |
| 88 | +for label in legend.get_texts(): |
| 89 | + label.set_fontsize('large') |
| 90 | + |
| 91 | +for label in legend.get_lines(): |
| 92 | + label.set_linewidth(1.5) # the legend line width |
| 93 | +plt.show() |
0 commit comments