-
Notifications
You must be signed in to change notification settings - Fork 1
/
dutch roll hinf.py
160 lines (141 loc) · 4.26 KB
/
dutch roll hinf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Dutch roll Hinf and H2 control example
"""
import numpy as np
import matplotlib.pyplot as plt
from control import tf, ss, hinfsyn, h2syn, step_response, augw, minreal
def triv_sigma(g, w):
"""triv_sigma(g,w) -> s
g - LTI object, order n
w - frequencies, length m
s - (m,n) array of singular values of g(1j*w)"""
m, p, _ = g.freqresp(w)
sjw = (m*np.exp(1j*p*np.pi/180)).transpose(2, 0, 1)
sv = np.linalg.svd(sjw, compute_uv=False)
return sv
w = np.logspace(-3, 3, 101)
def weighting(wb, m, a):
"""weighting(wb,m,a) -> wf
wb - design frequency (where |wf| is approximately 1)
m - high frequency gain of 1/wf; should be > 1
a - low frequency gain of 1/wf; should be < 1
wf - SISO LTI object
"""
s = tf([1, 0], [1])
return (s/m + wb) / (s + wb*a)
# Lateral stability derivatives and aircraft parameters
Y_b = 43.72 # ft/s^2
Y_r = 0.0
N_b = 4.395 # 1/s^2
N_r = -0.744 # 1/s
Y_dr = 12.17 # ft/s^2
N_dr = -4.495 # 1/s^2
N_da = -0.217 # 1/s^2
u_0 = 176.0 # ft/s
# Aircraft model
A = [[Y_b/u_0, -(1.0-Y_r/u_0)],[N_b, N_r]] # x1 = beta, x2 = r
B = [[0.0,Y_dr/u_0],[N_da,N_dr]] #u1 = delta_a, u2 = delta_r
C = [[1.0,0.0],[0.0,1.0]] # y1 = beta, y2 = r
D = [[0.0,0.0],[0.0,0.0]]
G = ss(A,B,C,D)
#weighting function
w1 = weighting(2, 2, 0.005)
w1 = ss(w1) #needs to be converted for append
wp = w1.append(w1)
wu = ss([], [], [], np.eye(2)) #unwieghted actuator authority size of 2
#Augmented plant need to make by hand for hinf syn
I = ss([], [], [], np.eye(2))
P = augw(G,wp,wu) #generalized plant P
P = minreal(P)
wps = tf(wp)
Gs= tf(G)
Is= tf(I)
# correct generalized plant
p11 = wps
p12 = wps*Gs
p21 = -Is
p22 = -Gs
K2 = h2syn(P, 2, 2)
# H INF CONTROLLER
K, CL, gam, rcond = hinfsyn(P,2,2) #generalized plant incorrect so doing mixsyn
print(gam)
#open loop plot
openloop = triv_sigma(G*K, w)
openloop2 = triv_sigma(G*K2, w)
plt.figure(1)
plt.semilogx(w, 20*np.log10(openloop[:, 0]), label=r'G*K')
plt.semilogx(w, 20*np.log10(openloop2[:, 0]), label=r'G*K2')
plt.ylim([-50, 50])
plt.ylabel('magnitude [dB]')
plt.xlim([1e-3, 1e3])
plt.xlabel('freq [rad/s]')
plt.title('open loop')
plt.grid(1)
plt.legend()
# closed loop tf
I = ss([], [], [], np.eye(2))
s1 = I.feedback(G*K)
s1 = minreal(s1)
s2 = I.feedback(G*K2)
s2 = minreal(s2)
invwp = I.feedback(wp)
t1 = (G*K).feedback(I)
t1 = minreal(t1)
t2 = (G*K2).feedback(I)
t2 = minreal(t2)
# frequency response
sv1 = triv_sigma(s1, w)
sv2 = triv_sigma(s2, w)
iwp = triv_sigma(invwp, w)
plt.figure(2)
plt.semilogx(w, 20*np.log10(sv1[:, 0]), label=r'$\sigma_1(S_1)$')
plt.semilogx(w, 20*np.log10(sv2[:, 0]), label=r'$\sigma_1(S_2)$')
plt.semilogx(w, 20*np.log10(iwp[:, 0]), label=r'invWp')
plt.ylim([-60, 10])
plt.ylabel('magnitude [dB]')
plt.xlim([1e-3, 1e3])
plt.xlabel('freq [rad/s]')
plt.legend()
plt.title('Singular values of S')
#step responses
time = np.linspace(0, 10, 301)
_,y1 = step_response(t1, time,input=0)
_,y2 = step_response(t1, time,input=1)
plt.figure(3)
plt.subplot(1, 2, 1)
plt.plot(time, y1[0], label='in 1 $y_1(t))$')
plt.plot(time, y2[1], label='in 2 $y_2(t))$')
plt.xlabel('time [s]')
plt.ylabel('response [1]')
plt.legend()
plt.title('step response hinf')
_,y1 = step_response(t2, time,input=0)
_,y2 = step_response(t2, time,input=1)
plt.subplot(1, 2, 2)
plt.plot(time, y1[0], label='in 1 $y_1(t))$')
plt.plot(time, y2[1], label='in 2 $y_2(t))$')
plt.xlabel('time [s]')
plt.ylabel('response [1]')
plt.legend()
plt.title('step response h2')
#disturbance step responses
time = np.linspace(0, 10, 301)
_,y1 = step_response(s1, time,input=0)
_,y2 = step_response(s1, time,input=1)
plt.figure(4)
plt.subplot(1, 2, 1)
plt.plot(time, y1[0], label='dist 1 $y_1(t))$')
plt.plot(time, y2[1], label='dist 2 $y_2(t))$')
plt.xlabel('time [s]')
plt.ylabel('response [1]')
plt.legend()
plt.title('step response hinf')
_,y1 = step_response(s2, time,input=0)
_,y2 = step_response(s2, time,input=1)
plt.subplot(1, 2, 2)
plt.plot(time, y1[0], label='dist 1 $y_1(t))$')
plt.plot(time, y2[1], label='dist 2 $y_2(t))$')
plt.xlabel('time [s]')
plt.ylabel('response [1]')
plt.legend()
plt.title('step response h2')