-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeraldoLineFollower.py
70 lines (57 loc) · 1.46 KB
/
GeraldoLineFollower.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
try:
from pyb import DAC
from pyb import LED
from pyb import Pin
from pyb import USB_VCP
sensitivity, maxn, minn = .5, 255, 0
com = USB_VCP()
light = DAC(2)
y1, y2, y3 = Pin('Y1', Pin.IN), Pin('Y2', Pin.IN), Pin('Y3', Pin.IN)
intensity, oldStr = maxn, str(y1.value()) + str(y2.value())
directTab = {
'1000' : 1,
'0001' : 1,
'0111' : 1,
'1110' : 1,
'1011' : -1,
'1101' : -1,
'0100' : -1,
'0010' : -1
}
def setByte(i):
return i.to_bytes(1)
def limitNums(num, maxn, minn):
return max(minn, min(maxn, num))
def handleLEDs(intensity, value):
if intensity == maxn:
LED(4).on()
LED(1).off()
elif intensity == minn:
LED(1).on()
LED(4).off()
else:
LED(1).off()
LED(4).off()
if value > 0 and intensity != maxn:
LED(2).on()
LED(3).off()
elif value < 0 and intensity != minn:
LED(3).on()
LED(2).off()
else:
LED(2).off()
LED(3).off()
while True:
v1, v2 = y1.value(), y2.value()
currentStr = str(v1) + str(v2)
usedStr = oldStr + currentStr
oldStr = currentStr if directTab.get(usedStr) else oldStr
additive = 0 if not directTab.get(usedStr) else directTab[usedStr] * sensitivity * (maxn - minn)/255
intensity = limitNums(intensity + additive, maxn, minn)
light.write_timed(setByte(round(intensity)), 1)
print('Additive:', additive, 'Value:', intensity, eol = '\n')
handleLEDs(intensity, additive)
except BaseException as errStr:
f = open('error.txt', 'w')
f.write(str(errStr))
f.close()