-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracker.py
72 lines (63 loc) · 2.23 KB
/
tracker.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
import serial
import time
import thread
import numpy as np
class Retina(object):
def __init__(self, port='/dev/ttyUSB0'):
self.serial = serial.Serial(port, baudrate=8000000, rtscts=True)
time.sleep(0.5)
self.serial.write("E+\nZ+\n")
time.sleep(0.5)
self.serial.write("E+\nZ+\n")
self.image = np.zeros((128, 128), dtype=float)
self.last_on = np.zeros((128, 128), dtype=float) + time.time()
self.last_off = np.zeros((128, 128), dtype=float) + time.time()
self.deltas = []
thread.start_new_thread(self.update_loop, ())
def clear_image(self):
self.image *= 0.5
del self.deltas[:]
def update_loop(self):
events = 0
while True:
byte0 = ord(self.serial.read())
if byte0 & 0x80 == 0:
continue
byte0 = byte0 & 0x7F # strip the top byte
byte1 = ord(self.serial.read())
sign = byte1 >= 0x7F
byte1 = byte1 & 0x7F # strip the top byte
self.image[byte1, byte0] += 1 if sign else -1
now = time.time()
if sign:
delta = now - self.last_off[byte1, byte0]
self.last_on[byte1, byte0] = now
else:
delta = now - self.last_on[byte1, byte0]
self.last_off[byte1, byte0] = now
self.deltas.append(delta)
events += 1
class RetinaView(object):
def __init__(self, retina):
self.retina = retina
thread.start_new_thread(self.update_loop, ())
def update_loop(self):
import pylab
pylab.ion()
self.fig = pylab.figure()
vmin, vmax = -1, 1
#self.img = pylab.imshow(self.retina.image, vmin=vmin, vmax=vmax,
# cmap='gray', interpolation='none')
while True:
#self.img.set_data(self.retina.image)
self.fig.clear()
if len(self.retina.deltas) > 0:
pylab.hist(self.retina.deltas, 20, range=(0.0, 0.01))
self.retina.clear_image()
pylab.draw()
time.sleep(0.02)
if __name__ == '__main__':
retina = Retina()
view = RetinaView(retina)
while True:
time.sleep(1)