forked from jorticus/pymate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot.py
83 lines (67 loc) · 2.37 KB
/
plot.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#from matecom import MateCom # For use with MATE RS232 interface
from pymate.matenet import MateNET, MateMXDevice # For use with proprietry MateNET protocol
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import time
from threading import Thread, Lock
from collections import deque
N = 1000 # History length, in samples
class DynamicAxes:
def __init__(self, axes, windowSize):
self.mate = mate
self.windowSize = windowSize
self.i = 0
self.x = np.arange(windowSize)
self.yi = []
self.axes = axes
for ai in axes:
self.yi.append(deque([0.0]*windowSize))
self.fig = None
self.ax = None
self.mutex = Lock()
def _addToBuf(self, buf, val):
if len(buf) < self.windowSize:
buf.append(val)
else:
buf.pop()
buf.appendleft(val)
def update(self, data):
"""
Call this in a separate thread to add new samples
to the internal buffers. By keeping this separate
from the animation function, the GUI is not blocked.
"""
assert len(data) == len(self.axes)
with self.mutex:
for i in range(len(self.axes)):
self._addToBuf(self.yi[i], data[i])
def anim(self, *args):
""" Used by matplotlib's FuncAnimation controller """
# Update the plot data, even if it hasn't changed
with self.mutex:
for i, ai in enumerate(self.axes):
ai.set_data(self.x, self.yi[i])
if __name__ == "__main__":
#bus = MateNET('COM2') # RS232
bus = MateNET('COM2') # MateNET
mate = MateMXDevice(bus, port=0)
# Set up plot
fig = plt.figure()
ax = plt.axes(xlim=(0, N), ylim=(0, 30))
a1, = ax.plot([],[])
a2, = ax.plot([],[])
plt.legend([a1, a2], ["Battery V", "PV V"])
data = DynamicAxes([a1, a2])
# Set up acquisition thread
def acquire():
while True:
#status = mate.read_status() # RS232
status = mate.get_status() # MateNET
print "BV:%s, PV:%s" % (status.bat_voltage, status.pv_voltage)
data.update([float(status.bat_voltage), float(status.pv_voltage)])
thread = Thread(target=acquire)
thread.start()
# Show plot
anim = animation.FuncAnimation(fig, data.anim, interval=1000/25)
plt.show()