5252import pylab # plotting routines
5353from .xferfcn import _convertToTransferFunction
5454from .exception import ControlMIMONotImplemented
55+ from .sisotool import _SisotoolUpdate
5556from functools import partial
5657
5758__all__ = ['root_locus' , 'rlocus' ]
5859
5960
6061# Main function: compute a root locus diagram
6162def root_locus (sys , kvect = None , xlim = None , ylim = None , plotstr = '-' , Plot = True ,
62- PrintGain = True , grid = False , sisotool = False , f = None , ax = None ):
63+ PrintGain = True , grid = False , ** kwargs ):
64+
6365 """Root locus plot
6466
6567 Calculate the root locus by finding the roots of 1+k*TF(s)
@@ -96,14 +98,16 @@ def root_locus(sys, kvect=None, xlim=None, ylim=None, plotstr='-', Plot=True,
9698 (nump , denp ) = _systopoly1d (sys )
9799
98100 if kvect is None :
101+ start_mat = _RLFindRoots (nump , denp , [1 ])
99102 kvect , mymat , xlim , ylim = _default_gains (nump , denp , xlim , ylim )
100103 else :
104+ start_mat = _RLFindRoots (nump , denp , kvect [0 ])
101105 mymat = _RLFindRoots (nump , denp , kvect )
102106 mymat = _RLSortRoots (mymat )
103107
104108 # Create the Plot
105109 if Plot :
106- if sisotool == False :
110+ if ' sisotool' not in kwargs :
107111 figure_number = pylab .get_fignums ()
108112 figure_title = [pylab .figure (numb ).canvas .get_window_title () for numb in figure_number ]
109113 new_figure_name = "Root Locus"
@@ -113,11 +117,19 @@ def root_locus(sys, kvect=None, xlim=None, ylim=None, plotstr='-', Plot=True,
113117 rloc_num += 1
114118 f = pylab .figure (new_figure_name )
115119 ax = pylab .axes ()
116-
120+ else :
121+ sisotool = kwargs ['sisotool' ]
122+ f = kwargs ['fig' ]
123+ ax = f .axes [1 ]
124+ bode_plot_params = kwargs ['bode_plot_params' ]
125+ tvect = kwargs ['tvect' ]
126+ event = type ('event' , (object ,), {'xdata' : start_mat [0 ][0 ].real ,'ydata' :start_mat [0 ][0 ].imag })()
127+ _RLFeedbackClicks (event , sys , f , sisotool ,bode_plot_params ,tvect )
117128
118129 if PrintGain :
130+
119131 f .canvas .mpl_connect (
120- 'button_release_event' , partial (_RLFeedbackClicks ,sys = sys ,fig = f ,sisotool = sisotool ))
132+ 'button_release_event' , partial (_RLFeedbackClicks ,sys = sys ,fig = f ,sisotool = sisotool , bode_plot_params = bode_plot_params , tvect = tvect ))
121133
122134 # plot open loop poles
123135 poles = array (denp .r )
@@ -139,7 +151,9 @@ def root_locus(sys, kvect=None, xlim=None, ylim=None, plotstr='-', Plot=True,
139151 ax .set_ylim (ylim )
140152 ax .set_xlabel ('Real' )
141153 ax .set_ylabel ('Imaginary' )
142- if grid :
154+ if grid and sisotool :
155+ _sgrid_func (f )
156+ elif grid :
143157 _sgrid_func ()
144158 return mymat , kvect
145159
@@ -344,7 +358,7 @@ def _RLSortRoots(mymat):
344358 return sorted
345359
346360
347- def _RLFeedbackClicks (event ,sys ,fig ,sisotool ):
361+ def _RLFeedbackClicks (event ,sys ,fig ,sisotool , bode_plot_params , tvect ):
348362 """Print root-locus gain feedback for clicks on the root-locus plot
349363 """
350364 (nump , denp ) = _systopoly1d (sys )
@@ -356,26 +370,33 @@ def _RLFeedbackClicks(event,sys,fig,sisotool):
356370 fig .suptitle ("Clicked at: %10.4g%+10.4gj gain: %10.4g damp: %10.4g" %
357371 (s .real , s .imag , K .real , - 1 * s .real / abs (s )))
358372
359- ax = fig .axes [0 ]
360- for line in reversed (ax .lines ):
373+ if sisotool :
374+ ax_rlocus = fig .axes [1 ]
375+ else :
376+ ax_rlocus = fig .axes [0 ]
377+
378+ for line in reversed (ax_rlocus .lines ):
361379 if len (line .get_xdata ()) == 1 :
362380 line .remove ()
363381 del line
364- else :
365- break
382+ # else:
383+ # break
366384
367385 if sisotool :
368386 mymat = _RLFindRoots (nump , denp , K .real )
369- ax .plot ([root .real for root in mymat ],[root .imag for root in mymat ],'m.' ,marker = 's' ,markersize = 8 , zorder = 20 )
387+ ax_rlocus .plot ([root .real for root in mymat ],[root .imag for root in mymat ],'m.' ,marker = 's' ,markersize = 8 , zorder = 20 )
388+ _SisotoolUpdate (sys ,fig ,K .real [0 ][0 ],bode_plot_params ,tvect )
370389 else :
371- ax .plot (s .real , s .imag , 'k.' , marker = 's' , markersize = 8 , zorder = 20 )
390+ ax_rlocus .plot (s .real , s .imag , 'k.' , marker = 's' , markersize = 8 , zorder = 20 )
372391
373392 fig .canvas .draw ()
374393
375394def _sgrid_func (fig = None , zeta = None , wn = None ):
376395 if fig is None :
377396 fig = pylab .gcf ()
378- ax = fig .gca ()
397+ ax = fig .gca ()
398+ else :
399+ ax = fig .axes [1 ]
379400 xlocator = ax .get_xaxis ().get_major_locator ()
380401
381402 ylim = ax .get_ylim ()
0 commit comments