Skip to content

Commit f4545dd

Browse files
committed
changed matplotlib event handler
1 parent 2e1bf7d commit f4545dd

2 files changed

Lines changed: 38 additions & 19 deletions

File tree

control/rlocus.py

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,15 @@ def root_locus(sys, kvect=None, xlim=None, ylim=None, plotstr='-', Plot=True,
124124
bode_plot_params = kwargs['bode_plot_params']
125125
tvect = kwargs['tvect']
126126
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)
128-
129-
if PrintGain:
127+
_RLFeedbackClicksSisotool(event, sys, f,bode_plot_params,tvect)
130128

129+
if PrintGain and sisotool == False:
130+
f.canvas.mpl_connect(
131+
'button_release_event', partial(_RLFeedbackClicksPoint,sys=sys,fig=f))
132+
elif sisotool == True:
133+
print('this is run')
131134
f.canvas.mpl_connect(
132-
'button_release_event', partial(_RLFeedbackClicks,sys=sys,fig=f,sisotool=sisotool,bode_plot_params=bode_plot_params,tvect=tvect))
135+
'button_release_event',partial(_RLFeedbackClicksSisotool,sys=sys, fig=f, bode_plot_params=bode_plot_params,tvect=tvect))
133136

134137
# plot open loop poles
135138
poles = array(denp.r)
@@ -357,40 +360,56 @@ def _RLSortRoots(mymat):
357360
prevrow = sorted[n, :]
358361
return sorted
359362

363+
def _RLFeedbackClicksSisotool(event,sys,fig,bode_plot_params,tvect):
364+
"""Update Sisotool plots if a new point on the root locus plot is clicked
365+
"""
366+
s = complex(event.xdata, event.ydata)
367+
K = -1./sys.horner(s)
368+
ax_rlocus = fig.axes[1]
369+
if _RLFeedbackClicksPoint(event,sys,fig,ax_rlocus):
370+
_SisotoolUpdate(sys,fig,K.real[0][0],bode_plot_params,tvect)
371+
360372

361-
def _RLFeedbackClicks(event,sys,fig,sisotool,bode_plot_params,tvect):
362-
"""Print root-locus gain feedback for clicks on the root-locus plot
373+
def _RLFeedbackClicksPoint(event,sys,fig,ax_rlocus=None):
374+
"""Display root-locus gain feedback point for clicks on the root-locus plot
363375
"""
376+
if ax_rlocus is None:
377+
ax_rlocus = fig.axes[0]
378+
sisotool = False
379+
else:
380+
sisotool = True
381+
364382
(nump, denp) = _systopoly1d(sys)
365383
s = complex(event.xdata, event.ydata)
366-
K = -1./sys.horner(s)
367-
if abs(K.real) > 1e-8 and abs(K.imag/K.real) < 0.04:
384+
K = -1. / sys.horner(s)
385+
if abs(K.real) > 1e-8 and abs(K.imag / K.real) < 0.04:
368386
print("Clicked at %10.4g%+10.4gj gain %10.4g damp %10.4g" %
369387
(s.real, s.imag, K.real, -1 * s.real / abs(s)))
370388
fig.suptitle("Clicked at: %10.4g%+10.4gj gain: %10.4g damp: %10.4g" %
371389
(s.real, s.imag, K.real, -1 * s.real / abs(s)))
372390

373-
if sisotool:
374-
ax_rlocus = fig.axes[1]
375-
else:
376-
ax_rlocus = fig.axes[0]
377-
391+
# Remove the previous points
378392
for line in reversed(ax_rlocus.lines):
379393
if len(line.get_xdata()) == 1:
380394
line.remove()
381395
del line
382396
#else:
383397
# break
384398

399+
# Visualise clicked point, display all roots for sisotool mode
385400
if sisotool:
386401
mymat = _RLFindRoots(nump, denp, K.real)
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)
402+
ax_rlocus.plot([root.real for root in mymat], [root.imag for root in mymat], 'm.', marker='s', markersize=8,
403+
zorder=20)
389404
else:
390405
ax_rlocus.plot(s.real, s.imag, 'k.', marker='s', markersize=8, zorder=20)
391406

392407
fig.canvas.draw()
393408

409+
return True
410+
411+
412+
394413
def _sgrid_func(fig=None, zeta=None, wn=None):
395414
if fig is None:
396415
fig = pylab.gcf()

control/sisotool.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ def sisotool(sys, kvect = None, xlim = None, ylim = None, plotstr_rlocus = '-',r
3131
'fig': fig,
3232
}
3333

34-
#To-do find out clever way to pass correct settings to other plots
35-
_SisotoolUpdate(sys, fig, 1,bode_plot_params,tvect)
34+
# First time call to setup the bode and step response plots
35+
_SisotoolUpdate(sys, fig,1 if kvect is None else kvect[0],bode_plot_params)
3636

3737
# Setup the root-locus plot window
3838
root_locus(sys,kvect=kvect,xlim=xlim,ylim = ylim,plotstr=plotstr_rlocus,grid = rlocus_grid,fig=fig,bode_plot_params=bode_plot_params,tvect=tvect,sisotool=True)
3939

40-
def _SisotoolUpdate(sys,fig,K,bode_plot_params,tvect):
40+
def _SisotoolUpdate(sys,fig,K,bode_plot_params,tvect=None):
4141

4242
# Get the subaxes and clear them
4343
ax_mag,ax_rlocus,ax_phase,ax_step = fig.axes[0],fig.axes[1],fig.axes[2],fig.axes[3]
@@ -55,7 +55,7 @@ def _SisotoolUpdate(sys,fig,K,bode_plot_params,tvect):
5555
bode_plot_params['syslist'] = sys*K.real
5656
bode_plot(**bode_plot_params)
5757

58-
# Generate the step response
58+
# Generate the step response and plot it
5959
sys_closed = (K*sys).feedback(1)
6060
if tvect is None:
6161
tvect, yout = step_response(sys_closed)

0 commit comments

Comments
 (0)