Skip to content

Commit 74d7e0d

Browse files
committed
adjust for integrator excess in marginplot
1 parent 280ed45 commit 74d7e0d

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

lib/ControlSystemsBase/src/plotting.jl

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ end
293293
else
294294
sbal = s
295295
end
296+
if plotphase && adjust_phase_start && isrational(sbal)
297+
intexcess = integrator_excess(sbal)
298+
end
296299
mag, phase = bode(sbal, w; unwrap=false)
297300
if _PlotScale == "dB" # Set by setPlotScale(str) globally
298301
mag = 20*log10.(mag)
@@ -330,7 +333,6 @@ end
330333
plotphase || continue
331334

332335
if adjust_phase_start == true && isrational(sbal)
333-
intexcess = integrator_excess(sbal)
334336
if intexcess != 0
335337
# Snap phase so that it starts at -90*intexcess
336338
nineties = round(Int, phasedata[1] / 90)
@@ -725,11 +727,12 @@ Plot all the amplitude and phase margins of the system(s) `sys`.
725727
726728
- A frequency vector `w` can be optionally provided.
727729
- `balance`: Call [`balance_statespace`](@ref) on the system before plotting.
730+
- `adjust_phase_start`: If true, the phase will be adjusted so that it starts at -90*intexcess degrees, where `intexcess` is the integrator excess of the system.
728731
729732
`kwargs` is sent as argument to RecipesBase.plot.
730733
"""
731734
marginplot
732-
@recipe function marginplot(p::Marginplot; plotphase=true, hz=false, balance=true)
735+
@recipe function marginplot(p::Marginplot; plotphase=true, hz=false, balance=true, adjust_phase_start=true)
733736
systems, w = _processfreqplot(Val{:bode}(), p.args...)
734737
ny, nu = size(systems[1])
735738
s2i(i,j) = LinearIndices((nu,(plotphase ? 2 : 1)*ny))[j,i]
@@ -746,6 +749,11 @@ marginplot
746749
s = balance_statespace(s)[1]
747750
end
748751
bmag, bphase = bode(s, w)
752+
753+
if plotphase && adjust_phase_start && isrational(s)
754+
intexcess = integrator_excess(s)
755+
end
756+
749757
for j=1:nu
750758
for i=1:ny
751759
wgm, gm, wpm, pm, fullPhase = sisomargin(s[i,j],w, full=true, allMargins=true)
@@ -801,6 +809,15 @@ marginplot
801809
[wgm wgm]', [ones(length(mag)) mag]'
802810
end
803811
plotphase || continue
812+
813+
phasedata = bphase[i, j, :]
814+
if plotphase && adjust_phase_start && isrational(s)
815+
if intexcess != 0
816+
# Snap phase so that it starts at -90*intexcess
817+
nineties = round(Int, phasedata[1] / 90)
818+
phasedata .+= ((90*(-intexcess-nineties)) ÷ 360) * 360
819+
end
820+
end
804821

805822
# Phase margins
806823
subplot := s2i(2i,j)
@@ -810,7 +827,7 @@ marginplot
810827
@series begin
811828
primary := true
812829
seriestype := :bodephase
813-
w, bphase[i, j, :]
830+
w, phasedata
814831
end
815832
@series begin
816833
color --> :gray

0 commit comments

Comments
 (0)