|
59 | 59 |
|
60 | 60 | # Main function: compute a root locus diagram |
61 | 61 | def root_locus(sys, kvect=None, xlim=None, ylim=None, plotstr='-', Plot=True, |
62 | | - PrintGain=True, grid=False): |
| 62 | + PrintGain=True, grid=False, sisotool = False, f=None, ax =None): |
63 | 63 | """Root locus plot |
64 | 64 |
|
65 | 65 | Calculate the root locus by finding the roots of 1+k*TF(s) |
@@ -103,21 +103,21 @@ def root_locus(sys, kvect=None, xlim=None, ylim=None, plotstr='-', Plot=True, |
103 | 103 |
|
104 | 104 | # Create the Plot |
105 | 105 | if Plot: |
106 | | - figure_number = pylab.get_fignums() |
107 | | - figure_title = [pylab.figure(numb).canvas.get_window_title() for numb in figure_number] |
108 | | - new_figure_name = "Root Locus" |
109 | | - rloc_num = 1 |
110 | | - while new_figure_name in figure_title: |
111 | | - new_figure_name = "Root Locus " + str(rloc_num) |
112 | | - rloc_num += 1 |
113 | | - f = pylab.figure(new_figure_name) |
| 106 | + if sisotool == False: |
| 107 | + figure_number = pylab.get_fignums() |
| 108 | + figure_title = [pylab.figure(numb).canvas.get_window_title() for numb in figure_number] |
| 109 | + new_figure_name = "Root Locus" |
| 110 | + rloc_num = 1 |
| 111 | + while new_figure_name in figure_title: |
| 112 | + new_figure_name = "Root Locus " + str(rloc_num) |
| 113 | + rloc_num += 1 |
| 114 | + f = pylab.figure(new_figure_name) |
| 115 | + ax = pylab.axes() |
114 | 116 |
|
115 | | - ax = pylab.axes() |
116 | 117 |
|
117 | 118 | if PrintGain: |
118 | | - click_point, = ax.plot([0], [0],color='k',markersize = 0,marker='s',zorder=20) |
119 | 119 | f.canvas.mpl_connect( |
120 | | - 'button_release_event', partial(_RLFeedbackClicks, sys=sys,fig=f,point=click_point)) |
| 120 | + 'button_release_event', partial(_RLFeedbackClicks,sys=sys,fig=f,sisotool=sisotool)) |
121 | 121 |
|
122 | 122 | # plot open loop poles |
123 | 123 | poles = array(denp.r) |
@@ -344,21 +344,33 @@ def _RLSortRoots(mymat): |
344 | 344 | return sorted |
345 | 345 |
|
346 | 346 |
|
347 | | -def _RLFeedbackClicks(event, sys,fig,point): |
| 347 | +def _RLFeedbackClicks(event,sys,fig,sisotool): |
348 | 348 | """Print root-locus gain feedback for clicks on the root-locus plot |
349 | 349 | """ |
| 350 | + (nump, denp) = _systopoly1d(sys) |
350 | 351 | s = complex(event.xdata, event.ydata) |
351 | 352 | K = -1./sys.horner(s) |
352 | 353 | if abs(K.real) > 1e-8 and abs(K.imag/K.real) < 0.04: |
353 | 354 | print("Clicked at %10.4g%+10.4gj gain %10.4g damp %10.4g" % |
354 | 355 | (s.real, s.imag, K.real, -1 * s.real / abs(s))) |
355 | | - point.set_ydata(s.imag) |
356 | | - point.set_xdata(s.real) |
357 | | - point.set_markersize(8) |
358 | 356 | fig.suptitle("Clicked at: %10.4g%+10.4gj gain: %10.4g damp: %10.4g" % |
359 | | - (s.real, s.imag, K.real, -1 * s.real / abs(s))) |
360 | | - fig.canvas.draw() |
| 357 | + (s.real, s.imag, K.real, -1 * s.real / abs(s))) |
| 358 | + |
| 359 | + ax = fig.axes[0] |
| 360 | + for line in reversed(ax.lines): |
| 361 | + if len(line.get_xdata()) == 1: |
| 362 | + line.remove() |
| 363 | + del line |
| 364 | + else: |
| 365 | + break |
| 366 | + |
| 367 | + if sisotool: |
| 368 | + 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) |
| 370 | + else: |
| 371 | + ax.plot(s.real, s.imag, 'k.', marker='s', markersize=8, zorder=20) |
361 | 372 |
|
| 373 | + fig.canvas.draw() |
362 | 374 |
|
363 | 375 | def _sgrid_func(fig=None, zeta=None, wn=None): |
364 | 376 | if fig is None: |
|
0 commit comments