make liveplotter not steal focus and terminate as expected

This commit is contained in:
Samuel Sadok
2018-03-15 16:36:04 -07:00
parent 82313713d8
commit eab7cda53e
Regular → Executable
+16 -4
View File
@@ -19,20 +19,32 @@ plt.ion()
global vals
vals = []
# Make sure the script terminates when the user closes the plotter
cancellation_token = threading.Event()
def handle_close(evt):
cancellation_token.set()
fig = plt.figure()
fig.canvas.mpl_connect('close_event', handle_close)
def fetch_data():
global vals
while True:
vals.append(my_odrive.motor0.encoder.pll_pos)
global cancellation_token
while not cancellation_token.is_set():
vals.append(my_odrive.motor0.timing_log.TIMING_LOG_FOC_CURRENT)
if len(vals) > num_samples:
vals = vals[-num_samples:]
time.sleep(1/data_rate)
# TODO: use animation for better UI performance, see:
# https://matplotlib.org/examples/animation/simple_anim.html
def plot_data():
global vals
while True:
global cancellation_token
while not cancellation_token.is_set():
plt.clf()
plt.plot(vals)
plt.pause(1/plot_rate)
#time.sleep(1/plot_rate)
fig.canvas.flush_events()
fetch_thread = threading.Thread(target=fetch_data, daemon=True)
fetch_thread.start()