forked from hmflash/tikitank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
treads.py
executable file
·76 lines (55 loc) · 1.7 KB
/
treads.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
#!/usr/bin/env python
import beaglebone_pru_adc as adc
import subprocess, os.path
import sys, time
class SpeedSensor(adc.Capture):
def __init__(self, firmware, *k, **kw):
super(SpeedSensor, self).__init__(*k, **kw)
fw = os.path.splitext(firmware)[0]
cmd = [ 'pasm', '-V3', '-b', firmware, fw ]
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
(stdout, stderr) = p.communicate()
if p.returncode != 0:
print stdout
raise IOError('Failed to compile "%s"' % firmware)
self._firmware = fw = os.path.abspath(fw + '.bin')
self.encoder0_pin = kw.get('pin', 0)
def calibrate(self):
print 'Running encoder for 10 seconds...'
self.encoder0_threshold = 4096
self.encoder0_delay = 0
adc._pru_adc.Capture.start(self, self._firmware)
time.sleep(10)
timer = s.timer
_, min0, max0, _, _ = s.encoder0_values
self.close()
print 'Capture runs at %d readings per second' % (timer // 10)
print 'Time value of one timer unit is %d nanosec' % (1000000000 // timer)
print 'Range for the encoder:', min0, '-', max0
print 'Recommended threshold value for encoder is:', int(0.9*(max0-min0))
def start(self):
s.encoder0_threshold = 425
s.encoder0_delay = 1
adc._pru_adc.Capture.start(self, self._firmware)
def close(self):
self.stop()
self.wait()
super(SpeedSensor, self).close()
if __name__ == '__main__':
s = SpeedSensor('tank/firmware.p')
if '--test' in sys.argv:
s.calibrate()
sys.exit()
try:
s.start()
if '-v' in sys.argv:
while True:
print s.timer, s.encoder0_values
num = 0
while True:
delta = s.encoder0_ticks - num
if delta:
print 'Tick: %s (%s)' % (delta, num)
num += delta
except KeyboardInterrupt:
s.close()