diff --git a/tools/liveplotter.py b/tools/liveplotter.py old mode 100644 new mode 100755 index d432062b..1c4ba849 --- a/tools/liveplotter.py +++ b/tools/liveplotter.py @@ -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()