-
Notifications
You must be signed in to change notification settings - Fork 0
/
lidar.py
86 lines (65 loc) · 1.93 KB
/
lidar.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
83
84
85
86
import serial
import time
line = 50 # 멀고 가깝고 기준
maxDist = 130 # 센서 벗어나는 거리
testCounter = 1
closeCounter = 1
farCounter = 1
state = 0 # 유니티 인풋 default=False
ser = serial.Serial("COM7", 115200)
# def checkUnity():
# if():
# send = 1
# elif:
# send = 2
# elif:
# state=0
def getTFminiData():
while True:
# time.sleep(0.1)
count = ser.in_waiting
if count > 8:
recv = ser.read(9)
ser.reset_input_buffer()
if recv[0] == 0x59 and recv[1] == 0x59: # python3
distance = recv[2] + recv[3] * 256
strength = recv[4] + recv[5] * 256
# print(distance)
if(distance < maxDist):
if(distance > line):
global closeCounter
closeCounter += 1
else:
global farCounter
farCounter += 1
# 테스트용. 차후에 지우셈
global testCounter
testCounter += 1
if(testCounter > 500):
break
# 대체물
# checkUnity()
# if(send !=1):
# break
# print('(', distance, ',', strength, ')')
ser.reset_input_buffer()
while __name__ == '__main__':
try:
if ser.is_open == False:
ser.open()
if(state == 0):
testCounter = 1
closeCounter = 1
farCounter = 1
elif(state == 1):
getTFminiData()
elif(state == 2):
if(closeCounter > farCounter):
result = "FAR"
else:
result = "CLOSE"
state = 0
print(result)
except KeyboardInterrupt: # Ctrl+C
if ser != None:
ser.close()