forked from lionyhw/PlanetX_MicroPython
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tracking.py
63 lines (49 loc) · 1.53 KB
/
tracking.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
from microbit import *
from enum import *
class TRACKING(object):
"""基本描述
两路巡线模块
Args:
RJ_pin (pin): 连接端口
"""
def __init__(self, RJ_pin):
if RJ_pin == J1:
self.__pinL = pin1
self.__pinR = pin8
elif RJ_pin == J2:
self.__pinL = pin2
self.__pinR = pin12
elif RJ_pin == J3:
self.__pinL = pin13
self.__pinR = pin14
elif RJ_pin == J4:
self.__pinL = pin15
self.__pinR = pin16
self.__pinL.set_pull(self.__pinL.PULL_UP)
self.__pinR.set_pull(self.__pinR.PULL_UP)
def get_state(self):
"""基本描述
返回当前巡线头状态
Returns:
00 均在白色
10 左黑右白
01 左白右黑
11 均在黑色
"""
if self.__pinL.read_digital() == 1 and self.__pinR.read_digital() == 1:
return 00
elif self.__pinL.read_digital() == 0 and self.__pinR.read_digital() == 1:
return 10
elif self.__pinL.read_digital() == 1 and self.__pinR.read_digital() == 0:
return 01
elif self.__pinL.read_digital() == 0 and self.__pinR.read_digital() == 0:
return 11
else:
print("Unknown ERROR")
if __name__ == '__main__':
trc = TRACKING(J1)
while True:
if trc.get_state() == 10:
display.show(Image.HAPPY)
elif trc.get_state() == 00:
display.show(Image.SAD)