|
| 1 | +"""Demonstrate mixed-sensitivity H-infinity design for a MIMO plant. |
| 2 | +
|
| 3 | +Based on Example 3.8 from Multivariable Feedback Control, Skogestad |
| 4 | +and Postlethwaite, 1st Edition. |
| 5 | +""" |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import matplotlib.pyplot as plt |
| 9 | + |
| 10 | +from control import tf, ss, mixsyn, feedback, step_response |
| 11 | + |
| 12 | +def weighting(wb,m,a): |
| 13 | + """weighting(wb,m,a) -> wf |
| 14 | + wb - design frequency (where |wf| is approximately 1) |
| 15 | + m - high frequency gain of 1/wf; should be > 1 |
| 16 | + a - low frequency gain of 1/wf; should be < 1 |
| 17 | + wf - SISO LTI object |
| 18 | + """ |
| 19 | + s = tf([1,0],[1]) |
| 20 | + return (s/m+wb)/(s+wb*a) |
| 21 | + |
| 22 | + |
| 23 | +def plant(): |
| 24 | + """plant() -> g |
| 25 | + g - LTI object; 2x2 plant with a RHP zero, at s=0.5. |
| 26 | + """ |
| 27 | + den = [0.2,1.2,1] |
| 28 | + gtf=tf([[[1],[1]], |
| 29 | + [[2,1],[2]]], |
| 30 | + [[den,den], |
| 31 | + [den,den]]) |
| 32 | + return ss(gtf) |
| 33 | + |
| 34 | + |
| 35 | +# as of this writing (2017-07-01), python-control doesn't have an |
| 36 | +# equivalent to Matlab's sigma function, so use a trivial stand-in. |
| 37 | +def triv_sigma(g,w): |
| 38 | + """triv_sigma(g,w) -> s |
| 39 | + g - LTI object, order n |
| 40 | + w - frequencies, length m |
| 41 | + s - (m,n) array of singular values of g(1j*w)""" |
| 42 | + m,p,_ = g.freqresp(w) |
| 43 | + sjw = (m * np.exp(1j*p*np.pi/180)).transpose(2,0,1) |
| 44 | + sv = np.linalg.svd(sjw,compute_uv=False) |
| 45 | + return sv |
| 46 | + |
| 47 | + |
| 48 | +def analysis(): |
| 49 | + """Plot open-loop responses for various inputs""" |
| 50 | + g=plant() |
| 51 | + |
| 52 | + t = np.linspace(0,10,101) |
| 53 | + _, yu1 = step_response(g,t,input=0) |
| 54 | + _, yu2 = step_response(g,t,input=1) |
| 55 | + |
| 56 | + yu1 = yu1 |
| 57 | + yu2 = yu2 |
| 58 | + |
| 59 | + # linear system, so scale and sum previous results to get the |
| 60 | + # [1,-1] response |
| 61 | + yuz = yu1 - yu2 |
| 62 | + |
| 63 | + plt.figure(1) |
| 64 | + plt.subplot(1,3,1) |
| 65 | + plt.plot(t,yu1[0],label='$y_1$') |
| 66 | + plt.plot(t,yu1[1],label='$y_2$') |
| 67 | + plt.xlabel('time') |
| 68 | + plt.ylabel('output') |
| 69 | + plt.ylim([-1.1,2.1]) |
| 70 | + plt.legend() |
| 71 | + plt.title('o/l response to input [1,0]') |
| 72 | + |
| 73 | + plt.subplot(1,3,2) |
| 74 | + plt.plot(t,yu2[0],label='$y_1$') |
| 75 | + plt.plot(t,yu2[1],label='$y_2$') |
| 76 | + plt.xlabel('time') |
| 77 | + plt.ylabel('output') |
| 78 | + plt.ylim([-1.1,2.1]) |
| 79 | + plt.legend() |
| 80 | + plt.title('o/l response to input [0,1]') |
| 81 | + |
| 82 | + plt.subplot(1,3,3) |
| 83 | + plt.plot(t,yuz[0],label='$y_1$') |
| 84 | + plt.plot(t,yuz[1],label='$y_2$') |
| 85 | + plt.xlabel('time') |
| 86 | + plt.ylabel('output') |
| 87 | + plt.ylim([-1.1,2.1]) |
| 88 | + plt.legend() |
| 89 | + plt.title('o/l response to input [1,-1]') |
| 90 | + |
| 91 | + |
| 92 | +def synth(wb1,wb2): |
| 93 | + """synth(wb1,wb2) -> k,gamma |
| 94 | + wb1: S weighting frequency |
| 95 | + wb2: KS weighting frequency |
| 96 | + k: controller |
| 97 | + gamma: H-infinity norm of 'design', that is, of evaluation system |
| 98 | + with loop closed through design |
| 99 | + """ |
| 100 | + g = plant() |
| 101 | + wu = ss([],[],[],np.eye(2)) |
| 102 | + wp1 = ss(weighting(wb=wb1, m=1.5, a=1e-4)) |
| 103 | + wp2 = ss(weighting(wb=wb2, m=1.5, a=1e-4)) |
| 104 | + wp = wp1.append(wp2) |
| 105 | + k,_,info = mixsyn(g,wp,wu) |
| 106 | + return k, info.gamma |
| 107 | + |
| 108 | + |
| 109 | +def step_opposite(g,t): |
| 110 | + """reponse to step of [-1,1]""" |
| 111 | + _, yu1 = step_response(g,t,input=0) |
| 112 | + _, yu2 = step_response(g,t,input=1) |
| 113 | + return yu1 - yu2 |
| 114 | + |
| 115 | + |
| 116 | +def design(): |
| 117 | + """Show results of designs""" |
| 118 | + # equal weighting on each output |
| 119 | + k1, gam1 = synth(0.25,0.25) |
| 120 | + # increase "bandwidth" of output 2 by moving crossover weighting frequency 100 times higher |
| 121 | + k2, gam2 = synth(0.25,25) |
| 122 | + # now weight output 1 more heavily |
| 123 | + # won't plot this one, just want gamma |
| 124 | + _, gam3 = synth(25,0.25) |
| 125 | + |
| 126 | + print('design 1 gamma {:.3g} (Skogestad: 2.80)'.format(gam1)) |
| 127 | + print('design 2 gamma {:.3g} (Skogestad: 2.92)'.format(gam2)) |
| 128 | + print('design 3 gamma {:.3g} (Skogestad: 6.73)'.format(gam3)) |
| 129 | + |
| 130 | + # do the designs |
| 131 | + g = plant() |
| 132 | + w = np.logspace(-2,2,101) |
| 133 | + I = ss([],[],[],np.eye(2)) |
| 134 | + s1 = I.feedback(g*k1) |
| 135 | + s2 = I.feedback(g*k2) |
| 136 | + |
| 137 | + # frequency response |
| 138 | + sv1 = triv_sigma(s1,w) |
| 139 | + sv2 = triv_sigma(s2,w) |
| 140 | + |
| 141 | + plt.figure(2) |
| 142 | + |
| 143 | + plt.subplot(1,2,1) |
| 144 | + plt.semilogx(w, 20*np.log10(sv1[:,0]), label=r'$\sigma_1(S_1)$') |
| 145 | + plt.semilogx(w, 20*np.log10(sv1[:,1]), label=r'$\sigma_2(S_1)$') |
| 146 | + plt.semilogx(w, 20*np.log10(sv2[:,0]), label=r'$\sigma_1(S_2)$') |
| 147 | + plt.semilogx(w, 20*np.log10(sv2[:,1]), label=r'$\sigma_2(S_2)$') |
| 148 | + plt.ylim([-60,10]) |
| 149 | + plt.ylabel('magnitude [dB]') |
| 150 | + plt.xlim([1e-2,1e2]) |
| 151 | + plt.xlabel('freq [rad/s]') |
| 152 | + plt.legend() |
| 153 | + plt.title('Singular values of S') |
| 154 | + |
| 155 | + # time response |
| 156 | + |
| 157 | + # in design 1, both outputs have an inverse initial response; in |
| 158 | + # design 2, output 2 does not, and is very fast, while output 1 |
| 159 | + # has a larger initial inverse response than in design 1 |
| 160 | + time = np.linspace(0,10,301) |
| 161 | + t1 = (g*k1).feedback(I) |
| 162 | + t2 = (g*k2).feedback(I) |
| 163 | + |
| 164 | + y1 = step_opposite(t1,time) |
| 165 | + y2 = step_opposite(t2,time) |
| 166 | + |
| 167 | + plt.subplot(1,2,2) |
| 168 | + plt.plot(time, y1[0], label='des. 1 $y_1(t))$') |
| 169 | + plt.plot(time, y1[1], label='des. 1 $y_2(t))$') |
| 170 | + plt.plot(time, y2[0], label='des. 2 $y_1(t))$') |
| 171 | + plt.plot(time, y2[1], label='des. 2 $y_2(t))$') |
| 172 | + plt.xlabel('time [s]') |
| 173 | + plt.ylabel('response [1]') |
| 174 | + plt.legend() |
| 175 | + plt.title('c/l response to reference [1,-1]') |
| 176 | + |
| 177 | + |
| 178 | +analysis() |
| 179 | +design() |
| 180 | +plt.show() |
0 commit comments