5050import scipy .signal # signal processing toolbox
5151import pylab # plotting routines
5252import control .xferfcn as xferfcn
53+ from functools import partial
5354
5455# Main function: compute a root locus diagram
55- def root_locus (sys , kvect , xlim = None , ylim = None , plotstr = '-' , Plot = True ):
56+ def root_locus (sys , kvect , xlim = None , ylim = None , plotstr = '-' , Plot = True ,
57+ PrintGain = True ):
5658 """Calculate the root locus by finding the roots of 1+k*TF(s)
5759 where TF is self.num(s)/self.den(s) and each k is an element
5860 of kvect.
@@ -65,7 +67,9 @@ def root_locus(sys, kvect, xlim=None, ylim=None, plotstr='-', Plot=True):
6567 List of gains to use in computing diagram
6668 Plot : boolean (default = True)
6769 If True, plot magnitude and phase
68-
70+ PrintGain: boolean (default = True)
71+ If True, report mouse clicks when close to the root-locus branches,
72+ calculate gain, damping and print
6973 Return values
7074 -------------
7175 rlist : list of computed root locations
@@ -80,6 +84,10 @@ def root_locus(sys, kvect, xlim=None, ylim=None, plotstr='-', Plot=True):
8084
8185 # Create the plot
8286 if (Plot ):
87+ f = pylab .figure ()
88+ if PrintGain :
89+ cid = f .canvas .mpl_connect (
90+ 'button_release_event' , partial (_RLFeedbackClicks , sys = sys ))
8391 ax = pylab .axes ();
8492
8593 # plot open loop poles
@@ -165,3 +173,12 @@ def _RLSortRoots(sys, mymat):
165173 sorted [n ,ind ] = elem
166174 prevrow = sorted [n ,:]
167175 return sorted
176+
177+ def _RLFeedbackClicks (event , sys ):
178+ """Print root-locus gain feedback for clicks on the root-locus plot
179+ """
180+ s = complex (event .xdata , event .ydata )
181+ K = - 1. / sys .horner (s )
182+ if abs (K .real ) > 1e-8 and abs (K .imag / K .real ) < 0.04 :
183+ print ("Clicked at %10.4g%+10.4gj gain %10.4g damp %10.4g" %
184+ (s .real , s .imag , K .real , - 1 * s .real / abs (s )))
0 commit comments