|
54 | 54 | import control.xferfcn as xferfcn |
55 | 55 | from control.freqplot import bode |
56 | 56 | from control.lti import isdtime |
| 57 | +import scipy as sp |
| 58 | + |
57 | 59 |
|
58 | 60 | # gain and phase margins |
59 | 61 | # contributed by Sawyer B. Fuller <minster@caltech.edu> |
60 | 62 | #! TODO - need to add unit test functions |
61 | | -def stability_margins(sysdata, deg=True): |
62 | | - """Calculate gain, phase and stability margins and associated |
63 | | - crossover frequencies. |
| 63 | +# def stability_margins(sysdata, deg=True): |
| 64 | +# """Calculate gain, phase and stability margins and associated |
| 65 | +# crossover frequencies. |
64 | 66 |
|
65 | | - Usage |
66 | | - ----- |
67 | | - gm, pm, sm, wg, wp, ws = stability_margins(sysdata, deg=True) |
| 67 | +# Usage |
| 68 | +# ----- |
| 69 | +# gm, pm, sm, wg, wp, ws = stability_margins(sysdata, deg=True) |
68 | 70 |
|
69 | | - Parameters |
70 | | - ---------- |
71 | | - sysdata: linsys or (mag, phase, omega) sequence |
72 | | - sys : linsys |
73 | | - Linear SISO system |
74 | | - mag, phase, omega : sequence of array_like |
75 | | - Input magnitude, phase, and frequencies (rad/sec) sequence from |
76 | | - bode frequency response data |
77 | | - deg=True: boolean |
78 | | - If true, all input and output phases in degrees, else in radians |
| 71 | +# Parameters |
| 72 | +# ---------- |
| 73 | +# sysdata: linsys or (mag, phase, omega) sequence |
| 74 | +# sys : linsys |
| 75 | +# Linear SISO system |
| 76 | +# mag, phase, omega : sequence of array_like |
| 77 | +# Input magnitude, phase, and frequencies (rad/sec) sequence from |
| 78 | +# bode frequency response data |
| 79 | +# deg=True: boolean |
| 80 | +# If true, all input and output phases in degrees, else in radians |
79 | 81 |
|
80 | | - Returns |
81 | | - ------- |
82 | | - gm, pm, sm, wg, wp, ws: float |
83 | | - Gain margin gm, phase margin pm, stability margin sm, and |
84 | | - associated crossover |
85 | | - frequencies wg, wp, and ws of SISO open-loop. If more than |
86 | | - one crossover frequency is detected, returns the lowest corresponding |
87 | | - margin. |
88 | | - """ |
89 | | - #TODO do this precisely without the effects of discretization of frequencies? |
90 | | - #TODO assumes SISO |
91 | | - #TODO unit tests, margin plot |
| 82 | +# Returns |
| 83 | +# ------- |
| 84 | +# gm, pm, sm, wg, wp, ws: float |
| 85 | +# Gain margin gm, phase margin pm, stability margin sm, and |
| 86 | +# associated crossover |
| 87 | +# frequencies wg, wp, and ws of SISO open-loop. If more than |
| 88 | +# one crossover frequency is detected, returns the lowest corresponding |
| 89 | +# margin. |
| 90 | +# """ |
| 91 | +# #TODO do this precisely without the effects of discretization of frequencies? |
| 92 | +# #TODO assumes SISO |
| 93 | +# #TODO unit tests, margin plot |
| 94 | + |
| 95 | +# if (not getattr(sysdata, '__iter__', False)): |
| 96 | +# sys = sysdata |
| 97 | + |
| 98 | +# # TODO: implement for discrete time systems |
| 99 | +# if (isdtime(sys, strict=True)): |
| 100 | +# raise NotImplementedError("Function not implemented in discrete time") |
| 101 | + |
| 102 | +# mag, phase, omega = bode(sys, deg=deg, Plot=False) |
| 103 | +# elif len(sysdata) == 3: |
| 104 | +# # TODO: replace with FRD object type? |
| 105 | +# mag, phase, omega = sysdata |
| 106 | +# else: |
| 107 | +# raise ValueError("Margin sysdata must be either a linear system or a 3-sequence of mag, phase, omega.") |
| 108 | + |
| 109 | +# if deg: |
| 110 | +# cycle = 360. |
| 111 | +# crossover = 180. |
| 112 | +# else: |
| 113 | +# cycle = 2 * np.pi |
| 114 | +# crossover = np.pi |
| 115 | + |
| 116 | +# wrapped_phase = -np.mod(phase, cycle) |
| 117 | + |
| 118 | +# # phase margin from minimum phase among all gain crossovers |
| 119 | +# neg_mag_crossings_i = np.nonzero(np.diff(mag < 1) > 0)[0] |
| 120 | +# mag_crossings_p = wrapped_phase[neg_mag_crossings_i] |
| 121 | +# if len(neg_mag_crossings_i) == 0: |
| 122 | +# if mag[0] < 1: # gain always less than one |
| 123 | +# wp = np.nan |
| 124 | +# pm = np.inf |
| 125 | +# else: # gain always greater than one |
| 126 | +# print("margin: no magnitude crossings found") |
| 127 | +# wp = np.nan |
| 128 | +# pm = np.nan |
| 129 | +# else: |
| 130 | +# min_mag_crossing_i = neg_mag_crossings_i[np.argmin(mag_crossings_p)] |
| 131 | +# wp = omega[min_mag_crossing_i] |
| 132 | +# pm = crossover + phase[min_mag_crossing_i] |
| 133 | +# if pm < 0: |
| 134 | +# print("warning: system unstable: negative phase margin") |
| 135 | + |
| 136 | +# # gain margin from minimum gain margin among all phase crossovers |
| 137 | +# neg_phase_crossings_i = np.nonzero(np.diff(wrapped_phase < -crossover) > 0)[0] |
| 138 | +# neg_phase_crossings_g = mag[neg_phase_crossings_i] |
| 139 | +# if len(neg_phase_crossings_i) == 0: |
| 140 | +# wg = np.nan |
| 141 | +# gm = np.inf |
| 142 | +# else: |
| 143 | +# min_phase_crossing_i = neg_phase_crossings_i[ |
| 144 | +# np.argmax(neg_phase_crossings_g)] |
| 145 | +# wg = omega[min_phase_crossing_i] |
| 146 | +# gm = abs(1/mag[min_phase_crossing_i]) |
| 147 | +# if gm < 1: |
| 148 | +# print("warning: system unstable: gain margin < 1") |
| 149 | + |
| 150 | +# # stability margin from minimum abs distance from -1 point |
| 151 | +# if deg: |
| 152 | +# phase_rad = phase * np.pi / 180. |
| 153 | +# else: |
| 154 | +# phase_rad = phase |
| 155 | +# L = mag * np.exp(1j * phase_rad) # complex loop response to -1 pt |
| 156 | +# min_Lplus1_i = np.argmin(np.abs(L + 1)) |
| 157 | +# sm = np.abs(L[min_Lplus1_i] + 1) |
| 158 | +# ws = phase[min_Lplus1_i] |
| 159 | + |
| 160 | +# return gm, pm, sm, wg, wp, ws |
| 161 | + |
| 162 | +# largely copied/adapted from |
| 163 | +# https://github.com/alchemyst/Skogestad-Python/blob/master/BODE.py |
| 164 | +# by RvP |
| 165 | +def stability_margins(sysdata, deg=True): |
92 | 166 |
|
| 167 | + # calculate gain of system |
93 | 168 | if (not getattr(sysdata, '__iter__', False)): |
94 | 169 | sys = sysdata |
95 | | - |
96 | | - # TODO: implement for discrete time systems |
97 | | - if (isdtime(sys, strict=True)): |
98 | | - raise NotImplementedError("Function not implemented in discrete time") |
99 | | - |
100 | | - mag, phase, omega = bode(sys, deg=deg, Plot=False) |
101 | 170 | elif len(sysdata) == 3: |
102 | 171 | # TODO: replace with FRD object type? |
103 | 172 | mag, phase, omega = sysdata |
| 173 | + sys = FRD(mag*exp((1j/360.)*phase), omega) |
104 | 174 | else: |
105 | | - raise ValueError("Margin sysdata must be either a linear system or a 3-sequence of mag, phase, omega.") |
| 175 | + raise ValueError("Margin sysdata must be either a linear system or " |
| 176 | + "a 3-sequence of mag, phase, omega.") |
106 | 177 |
|
107 | | - if deg: |
108 | | - cycle = 360. |
109 | | - crossover = 180. |
110 | | - else: |
111 | | - cycle = 2 * np.pi |
112 | | - crossover = np.pi |
113 | | - |
114 | | - wrapped_phase = -np.mod(phase, cycle) |
115 | | - |
116 | | - # phase margin from minimum phase among all gain crossovers |
117 | | - neg_mag_crossings_i = np.nonzero(np.diff(mag < 1) > 0)[0] |
118 | | - mag_crossings_p = wrapped_phase[neg_mag_crossings_i] |
119 | | - if len(neg_mag_crossings_i) == 0: |
120 | | - if mag[0] < 1: # gain always less than one |
121 | | - wp = np.nan |
122 | | - pm = np.inf |
123 | | - else: # gain always greater than one |
124 | | - print("margin: no magnitude crossings found") |
125 | | - wp = np.nan |
126 | | - pm = np.nan |
127 | | - else: |
128 | | - min_mag_crossing_i = neg_mag_crossings_i[np.argmin(mag_crossings_p)] |
129 | | - wp = omega[min_mag_crossing_i] |
130 | | - pm = crossover + phase[min_mag_crossing_i] |
131 | | - if pm < 0: |
132 | | - print("warning: system unstable: negative phase margin") |
| 178 | + def mod(w): |
| 179 | + """to give the function to calculate |G(jw)| = 1""" |
| 180 | + print(w) |
| 181 | + return np.abs(sys.evalfr(w)) - 1 |
| 182 | + |
| 183 | + def arg(w): |
| 184 | + """function to calculate the phase angle at -180 deg""" |
| 185 | + return np.angle(sys.evalfr(w)) + np.pi |
| 186 | + |
| 187 | + # how to calculate the frequency at which |G(jw)| = 1 |
| 188 | + wc = sp.optimize.newton(mod, 0.00001) |
| 189 | + w_180 = sp.optimize.newton(arg, 0.00001) |
133 | 190 |
|
134 | | - # gain margin from minimum gain margin among all phase crossovers |
135 | | - neg_phase_crossings_i = np.nonzero(np.diff(wrapped_phase < -crossover) > 0)[0] |
136 | | - neg_phase_crossings_g = mag[neg_phase_crossings_i] |
137 | | - if len(neg_phase_crossings_i) == 0: |
138 | | - wg = np.nan |
139 | | - gm = np.inf |
140 | | - else: |
141 | | - min_phase_crossing_i = neg_phase_crossings_i[ |
142 | | - np.argmax(neg_phase_crossings_g)] |
143 | | - wg = omega[min_phase_crossing_i] |
144 | | - gm = abs(1/mag[min_phase_crossing_i]) |
145 | | - if gm < 1: |
146 | | - print("warning: system unstable: gain margin < 1") |
147 | | - |
148 | | - # stability margin from minimum abs distance from -1 point |
149 | | - if deg: |
150 | | - phase_rad = phase * np.pi / 180. |
151 | | - else: |
152 | | - phase_rad = phase |
153 | | - L = mag * np.exp(1j * phase_rad) # complex loop response to -1 pt |
154 | | - min_Lplus1_i = np.argmin(np.abs(L + 1)) |
155 | | - sm = np.abs(L[min_Lplus1_i] + 1) |
156 | | - ws = phase[min_Lplus1_i] |
157 | | - |
158 | | - return gm, pm, sm, wg, wp, ws |
| 191 | + PM = np.angle(Gw(wc), deg=True) + 180 |
| 192 | + GM = 1/(np.abs(Gw(w_180))) |
| 193 | + |
| 194 | + return GM, PM, wc, w_180 |
159 | 195 |
|
160 | 196 | # Contributed by Steffen Waldherr <waldherr@ist.uni-stuttgart.de> |
161 | 197 | #! TODO - need to add test functions |
|
0 commit comments