-
Notifications
You must be signed in to change notification settings - Fork 0
/
lidar.py
151 lines (108 loc) · 3.23 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import cv2
import serial
#import datetime
import RPi.GPIO as GPIO
import math
ANGLE_OFFSET = 0
DIST_RANGE = 2
def printData(data):
tmpString = ""
for d in data:
tmpString += d.to_bytes(1, 'big').hex() + " "
print(tmpString)
def parseData(data):
retValue = []
prevIndex = 0
for i in range(1, len(data) - 1):
if data[i] == 0xaa and data[i + 1] == 0x55:
retValue.append(data[prevIndex:i])
prevIndex = i
retValue.append(data[prevIndex:])
return retValue
def checkSum(data):
check = [0, 0]
for i in range(int(len(data)/2)):
if i != 4:
check[0] ^= data[2*i]
check[1] ^= data[2*i + 1]
return (check[0] == data[8] and check[1] == data[9])
def processData(data):
distances = []
dataNum = int(data[3])
for i in range(dataNum):
distances.append(int((data[2*(i + 5)] | data[2*(i + 5) + 1] << 8) >> 2))
#distances.append(int(data[2*(i + 5)] | data[2*(i + 5) + 1] << 8)/4)
angleCorrect = [math.atan(19.16 * (d - 90.15)/(90.15 * d)) if d != 0 else 0.0 for d in distances]
startAngleByte = (data[4] | data[5] << 8) >> 1
endAngleByte = (data[6] | data[7] << 8) >> 1
startAngle = int(startAngleByte) * math.pi / 64 / 180
endAngle = int(endAngleByte) * math.pi / 64 / 180
if dataNum > 1:
angleStep = (endAngle - startAngle) / (dataNum - 1)
else:
angleStep = 0
angles = [startAngle + angleStep * i + angleCorrect[i] for i in range(len(angleCorrect))]
return distances, angles
def drawPoints(img, distances, angles):
global ANGLE_OFFSET
global DIST_RANGE
width, height = img.shape[:2]
centerX = width / 2
centerY = height / 2
POINT_R = 2
for (d, a) in zip(distances, angles):
if d > 0:
ang = a + ANGLE_OFFSET
xPos = centerX * (1 + (math.cos(ang) * d) / (DIST_RANGE * 1000))
yPos = centerY * (1 + (math.sin(ang) * d) / (DIST_RANGE * 1000))
cv2.circle(img,(int(xPos), int(yPos)), POINT_R, (0, 255, 0), -1)
#print(cv2.__version__)
originalImg = cv2.imread("lena.png")
width, height = originalImg.shape[:2]
ser = serial.Serial('/dev/ttyS0', 115200, timeout = 0.001)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, 1)
data = []
while True:
img = originalImg.copy()
cv2.line(img, (0, int(height/2)), (width, int(height/2)), (255,0,0), 1)
cv2.line(img, (int(width/2), 0), (int(width/2), height), (255,0,0), 1)
R = min([width, height])/(DIST_RANGE * 2)
for i in range(1, DIST_RANGE + 1):
cv2.circle(img,(int(width/2), int(height/2)), int(i*R), (255, 0, 0), 1)
data += ser.read(100000)
startIndex = 0
nextIndex = 0
for i in range(len(data) - 2):
if data[i] == 0xaa:
if data[i + 1] == 0x55 and data[i + 2] == 0x01:
#Sometimes, a check sum 0xAA and 0x55
if i > nextIndex + 10:
startIndex = nextIndex
nextIndex = i
if startIndex > 0:
pData = parseData(data[startIndex : nextIndex])
distances = []
angles = []
for pd in pData:
if len(pd) > 10:
if checkSum(pd):
dist, ang = processData(pd)
distances += dist
angles += ang
drawPoints(img, distances, angles)
data = data[nextIndex:]
elif nextIndex > 0:
data = data[nextIndex:]
cv2.imshow("Lidar", img)
#print("Reload : " + str(datetime.datetime.now()))
k = cv2.waitKey(1)
if k >= 0:
break
else:
loopFlag = True
GPIO.output(18, 0)
GPIO.cleanup()
ser.close()
cv2.destroyAllWindows()