Skip to content

Commit

Permalink
Add a view plot.
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoconni committed Feb 17, 2024
1 parent e90c83e commit b804a11
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
63 changes: 60 additions & 3 deletions python/UI.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
import tkinter as tk
from tkinter import ttk

import matplotlib
import numpy as np

matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure


class PropertyList(ttk.Treeview):
def __init__(self, master, properties, get_property_value):
Expand Down Expand Up @@ -99,6 +106,35 @@ def remove_selected_properties(self):
self.delete((selected_item,))


class Graph:
def __init__(self, master):
figure = Figure(figsize=(8, 4), dpi=100)
self.axes = figure.add_subplot()
self.lines = {}
self.axes.set_xlabel("time [s]")
self.axes.grid()

self.canvas = FigureCanvasTkAgg(figure, master)
NavigationToolbar2Tk(self.canvas, master, pack_toolbar=True)

self.canvas.get_tk_widget().pack()

def update(self, time, history):
for name in history.keys():
data = history[name]

if name in self.lines:
line = self.lines[name]
line.set_data(time, data)
line.set_label(name)
else:
(self.lines[name],) = self.axes.plot(time, data, label=name)
self.axes.legend()
self.axes.relim()
self.axes.autoscale_view()
self.canvas.draw()


class App(tk.Tk):
def __init__(self, fdm):
super().__init__()
Expand All @@ -109,6 +145,11 @@ def __init__(self, fdm):
frame = ttk.Frame(self)
frame.pack()

self.graph = Graph(frame)

frame = ttk.Frame(self)
frame.pack()

label = ttk.Label(frame, text="Properties")
label.grid(column=0, row=0)
label = ttk.Label(frame, text="Watch List")
Expand All @@ -127,7 +168,7 @@ def __init__(self, fdm):
self.watch_list.grid(column=1, row=1)

btn = ttk.Button(frame, text="Watch")
btn.bind("<Button>", self.watch_properties)
btn.bind("<Button>", self.watch_selected_properties)
btn.grid(column=0, row=2)

btn = ttk.Button(frame, text="Unwatch")
Expand All @@ -152,13 +193,28 @@ def __init__(self, fdm):

ttk.Button(self, text="Quit", command=lambda: self.quit()).pack()

def watch_properties(self, event):
self.time = []
self.prop_history = {}

def watch_selected_properties(self, event):
for item in self.property_list.get_selected_properties():
self.watch_list.add_property(item[0], item[1])
self.prop_history[item[0]] = np.zeros((len(self.time),))

def step(self, event):
self.fdm.run()
self.watch_list.update(self.fdm.get_property_value)
self.update_graph()

def update_graph(self):
self.time.append(self.fdm.get_sim_time())

if self.prop_history:
for prop in self.prop_history:
self.prop_history[prop] = np.append(
self.prop_history[prop], self.fdm.get_property_value(prop)
)
self.graph.update(self.time, self.prop_history)

def run(self, event):
self.update_id = self.watch_list.after(250, self.update)
Expand All @@ -167,7 +223,8 @@ def pause(self, event):
self.watch_list.after_cancel(self.update_id)

def update(self):
for i in range(1000):
for i in range(50):
self.fdm.run()
self.watch_list.update(self.fdm.get_property_value)
self.update_graph()
self.update_id = self.watch_list.after(250, self.update)
15 changes: 11 additions & 4 deletions tests/TestUI.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/python
import math

from UI import App

if __name__ == "__main__":
Expand All @@ -13,12 +15,13 @@ def get_aircraft_name(self):
class dummyFDM:
def __init__(self):
self.properties = {
"a/c": 2.0,
"a/b": 0.0,
"a/c": -1.0,
"a/b": 2.0,
"c/d/e": 4.0,
"b/c": 1.0,
"c/d/a": 3.0,
}
self.time = 0.0

def query_property_catalog(self, _):
prop_names = list(self.properties.keys()) + [""]
Expand All @@ -31,8 +34,12 @@ def get_aircraft(self):
return dummyAircraft("Test")

def run(self):
self.properties["a/b"] += 0.25
self.properties["c/d/e"] -= 0.1
self.properties["a/b"] = 2.0 * math.cos(0.15 * math.pi * self.time)
self.properties["c/d/e"] = 4.0 * math.cos(0.5 * math.pi * self.time)
self.time += 0.01

def get_sim_time(self):
return self.time

app = App(dummyFDM())
app.mainloop()

0 comments on commit b804a11

Please sign in to comment.