-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
61 lines (47 loc) · 1.47 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sys
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import comm.telemetry as telemetry
import comm
robot = comm.make_asserv()
class TelemetryPlot:
def __init__(self, telem):
self.telem = telem
pretty_name = telem.name.replace("_"," ").capitalize()
self.fig = plt.figure(pretty_name)
self.ax = self.fig.gca()
self.ax.set_ylim(-1000,1000)
self.ax.set_title(pretty_name)
self.ax.set_xlabel("Time (s)")
self.plots = {name:self.ax.plot([], [], label=name)[0] for name in telem.fields()}
self.time_data = []
self.plot_data = {name:[] for name in telem.fields()}
self.fig.legend()
self.anim = FuncAnimation(self.fig, self.update, interval=16, blit=True)
def update(self, i):
for name in self.plots.keys():
self.plots[name].set_data(self.time_data, self.plot_data[name])
if len(self.time_data) > 0:
mval = self.time_data[-1]
self.ax.set_xlim(mval-10.0, mval)
return self.plots.values()
def handle_data(self, dat):
pkt = self.telem.to_packet(dat)
ts = pkt.timestamp
if ts == 0:
self.time_data = []
for name in self.plot_data.keys():
self.plot_data[name] = []
self.time_data.append(ts)
for name, val in pkt.vals().items():
self.plot_data[name].append(val)
plots = {}
for idx, telem in robot.telems.items():
plots[idx] = TelemetryPlot(telem)
def cb_func(idx, dat):
plots[idx].handle_data(dat)
if len(sys.argv) < 2:
print("Give IP")
exit()
cl = telemetry.Client(sys.argv[1], 1337, cb_func)
plt.show()