mirror of
https://github.com/odriverobotics/ODrive.git
synced 2026-08-20 22:14:34 +08:00
make liveplotter not steal focus and terminate as expected
This commit is contained in:
Regular → Executable
+16
-4
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user