Skip to content

Commit 8954fce

Browse files
committed
Add 2 example. 2nd Order system
1 parent 3ce7514 commit 8954fce

3 files changed

Lines changed: 108 additions & 6 deletions

File tree

examples/1_example.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def generateNoise(t):
3434

3535
#PI Controller
3636
base = [ctl.tf([1], [1],t_step),
37-
ctl.tf([1, 0], [1, -1],t_step)]
37+
ctl.tf([1, 0], [1, -1],t_step)]
3838

3939
#Experiment filter
4040
omega = 2*np.pi*0.1
@@ -49,23 +49,32 @@ def generateNoise(t):
4949
L = C*sys
5050
L = L/(1+L)
5151

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)
55+
5256
print "Theta:", theta
5357

5458
#Analysis
5559
yr,t = ctl.step(refModel, t)
5660
yc,t = ctl.step(L, t)
61+
ys,t = ctl.step(sys, t)
62+
5763

58-
fig, ax = plt.subplots(3, sharex=True)
64+
fig, ax = plt.subplots(4, sharex=True)
5965
ax[0].plot(t,yr[0],label='Ref System')
6066
ax[0].plot(t,yc[0], label='CL System')
6167
ax[0].set_title('Systems response')
6268
ax[0].grid(True)
63-
ax[1].plot(t,y[0])
69+
ax[1].plot(t,ys[0], label='OL System')
70+
ax[1].set_title('OL Systems response')
6471
ax[1].grid(True)
65-
ax[1].set_title('Experiment data')
66-
ax[2].plot(t,r)
72+
ax[2].plot(t,y[0])
6773
ax[2].grid(True)
68-
ax[2].set_title('Virtual Reference')
74+
ax[2].set_title('Experiment data')
75+
ax[3].plot(t,r)
76+
ax[3].grid(True)
77+
ax[3].set_title('Virtual Reference')
6978

7079
# Now add the legend with some customizations.
7180
legend = ax[0].legend(loc='lower right', shadow=True)

examples/2_example.png

48.5 KB
Loading

examples/2_example.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)