-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMAGraphTest.py
57 lines (51 loc) · 1.5 KB
/
MMAGraphTest.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
import MMA8451 as mma
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from collections import deque
TAILLEN = 2
# Plot Class
class AnalogPlot:
# constructor
def __init__(self, accel, maxLen):
self.accel = accel
self.maxLen = maxLen
self.ax = deque([0.0] * maxLen)
self.ay = deque([0.0] * maxLen)
self.az = deque([0.0] * maxLen)
# add to buffer
def addToBuf(self, buf, val):
if len(buf) < self.maxLen:
buf.append(val)
else:
buf.pop()
buf.appendleft(val)
# add data
def add(self, x, y, z):
self.addToBuf(self.ax, x)
self.addToBuf(self.ay, y)
self.addToBuf(self.az, z)
# update plot
def update(self, frameNum, a0, a1, a2, a3, a4, a5):
x, y, z = self.accel.readScaledData()
self.add(x, y, z)
a0.set_data(self.ax, self.ay)
a1.set_data(self.ax, self.az)
a2.set_data(self.ay, self.ax)
a3.set_data(self.ay, self.az)
a4.set_data(self.az, self.ax)
a5.set_data(self.az, self.ay)
if __name__ == '__main__':
mma = mma.MMA8451()
mma.setup()
analogPlot = AnalogPlot(mma, TAILLEN)
fig = plt.figure()
ax = plt.axes(xlim=(-2, 2), ylim=(-2, 2))
a0, = ax.plot([], [], label='XY')
a1, = ax.plot([], [], label='XZ')
a2, = ax.plot([], [], label='YX')
a3, = ax.plot([], [], label='YZ')
a4, = ax.plot([], [], label='ZX')
a5, = ax.plot([], [], label='ZY')
plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=6, mode="expand", borderaxespad=0.)
anim = animation.FuncAnimation(fig, analogPlot.update, fargs=(a0, a1, a2, a3, a4, a5), interval=1)
plt.show()