-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot_euler.py
More file actions
56 lines (42 loc) · 1.62 KB
/
plot_euler.py
File metadata and controls
56 lines (42 loc) · 1.62 KB
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
#!/usr/bin/env python3
import matplotlib.pyplot as plt
from pyvmu.vmu931 import VMU931Parser
from pyvmu import messages
# We want to be able to update the plot in real-time, plt.ion() is non-blocking.
plt.ion()
figure = plt.figure()
euler_axes = figure.add_subplot(111)
euler_axes.set_title("Euler Angles")
euler_axes.set_xlabel("Timestamp (ms)")
euler_axes.set_ylabel("Angle (°)")
euler_axes.set_ylim([-180, 180]) # -8 <= g <= 8
x_line, = euler_axes.plot([], [], label="X")
y_line, = euler_axes.plot([], [], label="Y")
z_line, = euler_axes.plot([], [], label="Z")
plt.legend()
ts_points = []
x_points = []
y_points = []
z_points = []
with VMU931Parser(euler=True) as vp:
while True:
pkt = vp.parse()
if isinstance(pkt, messages.Status):
print(pkt)
if isinstance(pkt, messages.Euler):
ts, x, y, z = pkt
ts_points.append(ts)
x_points.append(x)
y_points.append(y)
z_points.append(z)
# Only plot every 10 points (still quite smooth), otherwise we risk being bottle-necked by matplotlib.
if len(ts_points) % 10 == 0:
# set_data is faster than drawing a whole new line
x_line.set_data(ts_points[-1000:], x_points[-1000:])
y_line.set_data(ts_points[-1000:], y_points[-1000:])
z_line.set_data(ts_points[-1000:], z_points[-1000:])
euler_axes.relim()
euler_axes.autoscale_view()
figure.canvas.draw()
# Pause to force redraw. Actually blocks until re-draw is complete, 0.00001 is an arbitrary small number.
plt.pause(0.00001)