|
1 | 1 | import numpy as np |
2 | 2 | import matplotlib.pyplot as plt |
3 | | -import control as ctl |
| 3 | +import scipy.signal as scipysig |
4 | 4 | from vrft import * |
5 | 5 |
|
6 | 6 | def generateNoise(t): |
7 | 7 | omega = 2*np.pi*100 |
8 | 8 | xi = 0.9 |
| 9 | + dt = t[1] - t[0] |
9 | 10 | noise = np.random.normal(0,0.1,t.size) |
| 11 | + tf = scipysig.TransferFunction([10*omega**2], [1, 2*xi*omega, omega**2]) |
10 | 12 | # Second order system |
11 | | - yn,t,x = ctl.lsim(ctl.tf([10*omega**2], [1, 2*xi*omega, omega**2]), \ |
12 | | - noise, t) |
| 13 | + _, yn, _ = scipysig.lsim(tf, noise, t) |
13 | 14 | return yn |
14 | 15 |
|
15 | 16 | #Generate time and u(t) signals |
16 | 17 | t_start = 0 |
17 | 18 | t_end = 10 |
18 | 19 | t_step = 1e-2 |
19 | 20 | t = np.arange(t_start, t_end, t_step) |
20 | | -u = np.ones(len(t)).tolist() |
| 21 | +u = np.ones(len(t)) |
21 | 22 | u[200:400] = np.zeros(200) |
22 | 23 | u[600:800] = np.zeros(200) |
23 | 24 |
|
24 | 25 | #Experiment |
25 | 26 | num = [0.5] |
26 | 27 | den = [1, -0.9] |
27 | | -sys = ctl.tf(num, den, t_step) |
28 | | -y,t,x= ctl.lsim(sys, u, t) |
| 28 | +sys = ExtendedTF(num, den, dt=t_step) |
| 29 | +t, y = scipysig.dlsim(sys, u, t) |
| 30 | +y = y[:, 0] |
29 | 31 | y += generateNoise(t) |
30 | | -data = vrft.iddata(y[0],u,t_step,[0]) |
| 32 | +data = iddata(y, u, t_step, [0]) |
| 33 | + |
31 | 34 |
|
32 | 35 | #Reference Model |
33 | | -refModel = ctl.tf([0.6], [1, -0.4], t_step) |
| 36 | +refModel = ExtendedTF([0.6], [1, -0.4], dt=t_step) |
34 | 37 |
|
35 | 38 | #PI Controller |
36 | | -base = [ctl.tf([1], [1],t_step), |
37 | | - ctl.tf([1, 0], [1, -1],t_step)] |
| 39 | +base = [ExtendedTF([1], [1, -1], dt=t_step), |
| 40 | + ExtendedTF([1, 0], [1, -1], dt=t_step)] |
38 | 41 |
|
39 | 42 | #Experiment filter |
40 | | -omega = 2*np.pi*0.1 |
41 | | -L = ctl.tf([1], [1/omega, 1]) |
42 | | -L = ctl.sample_system(L, 0.01) |
43 | | - |
| 43 | +L = refModel * (1 - refModel) |
44 | 44 | #VRFT |
45 | | -C, theta, r = vrft.vrftAlgorithm(data, refModel, base, L) |
| 45 | +theta, r, _, _, C = compute_vrft(data, refModel, base, L) |
46 | 46 |
|
47 | 47 | #Obtained controller |
48 | | -print "Controller:", C |
49 | | -L = C*sys |
50 | | -L = L/(1+L) |
| 48 | +print("Controller: {}".format(C)) |
51 | 49 |
|
52 | | -L = L.minreal() |
53 | | -#this is due to a bug in the control library |
54 | | -L = ctl.tf(L.num, L.den, t_step) |
| 50 | +L = C * sys |
| 51 | +L = feedback(L) |
55 | 52 |
|
56 | | -print "Theta:", theta |
| 53 | +print("Theta: {}".format(theta)) |
57 | 54 |
|
58 | 55 | #Analysis |
59 | | -yr,t = ctl.step(refModel, t) |
60 | | -yc,t = ctl.step(L, t) |
61 | | -ys,t = ctl.step(sys, t) |
62 | | - |
63 | | - |
| 56 | +t = t[:len(r)] |
| 57 | +u = np.ones(len(t)) |
| 58 | +_, yr = scipysig.dlsim(refModel, u, t) |
| 59 | +_, yc = scipysig.dlsim(L, u, t) |
| 60 | +_, ys = scipysig.dlsim(sys, u, t) |
| 61 | + |
| 62 | +yr = np.array(yr).flatten() |
| 63 | +ys = np.array(ys).flatten() |
| 64 | +yc = np.array(yc).flatten() |
64 | 65 | fig, ax = plt.subplots(4, sharex=True) |
65 | | -ax[0].plot(t,yr[0],label='Ref System') |
66 | | -ax[0].plot(t,yc[0], label='CL System') |
| 66 | +ax[0].plot(t, yr,label='Ref System') |
| 67 | +ax[0].plot(t, yc, label='CL System') |
67 | 68 | ax[0].set_title('Systems response') |
68 | 69 | ax[0].grid(True) |
69 | | -ax[1].plot(t,ys[0], label='OL System') |
| 70 | +ax[1].plot(t, ys, label='OL System') |
70 | 71 | ax[1].set_title('OL Systems response') |
71 | 72 | ax[1].grid(True) |
72 | | -ax[2].plot(t,y[0]) |
| 73 | +ax[2].plot(t, y[:len(r)]) |
73 | 74 | ax[2].grid(True) |
74 | 75 | ax[2].set_title('Experiment data') |
75 | | -ax[3].plot(t,r) |
| 76 | +ax[3].plot(t, r) |
76 | 77 | ax[3].grid(True) |
77 | 78 | ax[3].set_title('Virtual Reference') |
78 | 79 |
|
|
0 commit comments