forked from l3cire/path-planning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlidar.py
300 lines (255 loc) · 8.84 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import random
import numpy as np
from math import *
import cv2
import matplotlib.pyplot as plt
import scipy as sc
import scipy.optimize as opt
from sklearn.linear_model import LinearRegression
lidarGeneratedData = []
pic = cv2.imread("whiteboard.png")
#lidarinput keeps distance to each point at each degree with step of 1 degree
def drawData(coordsArray):
for i in range(len(coordsArray)):
print('')
#print("coordsArray for drawing is " +str(coordsArray))
#print(coordsArray[i][0][0],coordsArray[i][1][0])
#cv2.circle(pic, (int(coordsArray[i][0][0]*1000),int(coordsArray[i][1][0]*1000)), 3, (255,0,0), -1)
#cv2.imshow("frame" , pic)
#cv2.imwrite("result.png", pic)
def generateLine():
print('generated random line')
x0 = np.random.uniform(-5.0,5.0)
y0 = np.random.uniform(-5.0,5.0)
x1 = x0 + np.random.uniform(-10.0,10.0)
y1 = y0 + np.random.uniform(-10.0, 10.0)
xLowerLimit = np.random.uniform(-1.0,1.0)
xUpperLimit = np.random.uniform(xLowerLimit, xLowerLimit+np.random.uniform(0, 50.0))
twoPoints = [x0,y0,x1,y1,xLowerLimit,xUpperLimit] #x0,y0,x1,y1
slope = (y1-y0)/(x1-x0)
const = ((0 - x0) / (x1 - x0)) * (y1 - y0) + y0
print(f'slope is {slope}')
print(f'const is {const}')
twoPoints.append(slope)
twoPoints.append(const)
#print(f"Generated points: {twoPoints}")
#print(f"Func eq is: {slope}x + {const}")
#print('')
return twoPoints
twoPoints = generateLine()
print(f'twoPoints is {twoPoints}')
print(f'limits are {twoPoints[5], twoPoints[6]}')
#print(twoPoints)
#def randFunc(x):
# print(twoPoints[4] , x[0] , twoPoints[5])
# if twoPoints[4]<x[0]<twoPoints[5]:
# x0 = twoPoints[0]
# y0 = twoPoints[1]
# x1 = twoPoints[2]
# y1 = twoPoints[3]
# func = ((x[0] - x0) / (x1 - x0)) * (y1 - y0) + y0
# return func
# else:
# return "out of bounds"
#
#
#def angleFunc(alpha, x):
# y = -alpha[0] + 90
# func = np.tan(y)*x
# return (func)
#
#def difFunc(alpha, x):
# func = randFunc(x[0])-angleFunc(alpha, x[0])
# return func
#
#def sqDifFunc(alpha, x):
# func = difFunc(alpha, x[0])**2
# return func
#
#xIntersection = opt.minimize(sqDifFunc, [0], options={'eps': 0.1})
#print(xIntersection)
def findIntersections():
intersectionsX = []
b = twoPoints[7]
k = twoPoints[6]
print(f'b is {b}')
print(f'k is {k}')
for i in range (360):
j = -i + 90 #angle in normal coords
if -90<=j<=90:
j = radians(j)
if (tan(j) != k):
xIntersect = b / (tan(j) - k)
if twoPoints[4] < xIntersect < twoPoints[5] and xIntersect>=0:
xIntersect = xIntersect
# intersectionsX.append([i,xIntersect])
intersectionsX.append([i, xIntersect])
else:
intersectionsX.append([i, None])
# intersectionsX.append([i,'none'])
if j< -90:
j = radians(j)
if (tan(j) != k):
xIntersect = b / (tan(j) - k)
if twoPoints[4] < xIntersect < twoPoints[5] and xIntersect < 0:
xIntersect = xIntersect
# intersectionsX.append([i,xIntersect])
intersectionsX.append([i, xIntersect])
else:
intersectionsX.append([i, None])
# intersectionsX.append([i,'none'])
return intersectionsX
intersectionsX = findIntersections()
#pointx = [3]
#print(f"Func value in point {pointx} is: {randFunc(pointx)}")
#print('')
#print(f'Intersection points of lidar with obstacles are: {intersectionsX}')
print(len(intersectionsX))
for unit in intersectionsX:
alpha = unit[0]
lengthX = unit[1]
if lengthX is not None and sin(radians(alpha))!=0:
lengthR = lengthX/sin(radians(alpha))
lidarGeneratedData.append(round(lengthR,2))
else:
lidarGeneratedData.append(None)
print('')
def generateNoise(lidarGeneratedData):
noisedData = np.array([])
for datum in lidarGeneratedData:
if datum is None:
if np.random.uniform(0,100)<0.5:
datum = np.random.uniform(0,20)
else:
datum += np.random.uniform(0,0.2)
noisedData = np.append(noisedData, datum)
#print(noisedData)
return noisedData
noisedData = generateNoise(lidarGeneratedData)
"""
THIS WAS THE INFORMATION GENERATION PART
FROM NOW ON, DATA ANALYSIS IS BEING IMPLEMENTED
"""
def data2coords(dist):
overall = []
xS = np.array([])
yS = np.array([])
c = 0
#print('')
#print(f'dist in data2coords is {dist}')
for i in range (len(dist)):
if dist[i] is not None:
#print(f'dist[i] is {dist[i]}')
xCoordinate = dist[i]*sin(radians(i))
xS = np.append(xS, xCoordinate)
#print(f'appended {xCoordinate} to xS: {xS}')
yCoordinate = dist[i]*cos(radians(i))
yS = np.append(yS, yCoordinate)
overall.append([xCoordinate, yCoordinate])
return xS, yS
#return overall
def data2coords4line (line):
xS = np.array([])
yS = np.array([])
c = 0
# print('')
# print(f'dist in data2coords is {dist}')
for point in line:
xCoordinate = point[0] * sin(radians(point[1]))
xS = np.append(xS, xCoordinate)
# print(f'appended {xCoordinate} to xS: {xS}')
yCoordinate = point[0] * cos(radians(point[1]))
yS = np.append(yS, yCoordinate)
return xS, yS
def data2coordsov(dist):
overall = []
xS = np.array([])
yS = np.array([])
c = 0
#print('')
#print(f'dist in data2coords is {dist}')
for i in range (len(dist)):
if dist[i] is not None:
#print(f'dist[i] is {dist[i]}')
xCoordinate = dist[i]*sin(radians(i))
xS = np.append(xS, xCoordinate)
#print(f'appended {xCoordinate} to xS: {xS}')
yCoordinate = dist[i]*cos(radians(i))
yS = np.append(yS, yCoordinate)
overall.append([xCoordinate, yCoordinate])
#return xS, yS
return overall
def LinesSplit(inputData):
lines = []
linesWithoutAngle = []
lineNum = 0
inputData = np.append(inputData, inputData[0])
inputData = np.insert(inputData,0, inputData[-2])
#print(f'inputData in linessplit is {inputData} after appending and inserting 2 basics')
flag = 1
for i in range(1, len(inputData)-1):
if inputData[i] is not None:
if inputData[i+1] is not None or inputData[i-1] is not None:
if flag == 1:
lines.append([])
linesWithoutAngle.append([])
flag = 0
lines[-1].append([inputData[i],i - 1])
linesWithoutAngle[-1].append([inputData[i]])
#print(f'appended {[inputData[i],i - 1]} to lines in linessplit')
#print(f'now lines are {lines}')
else:
lineNum +=1
flag = 1
#if lines[0][0][1] == 0 and lines[-1][-1][1] == 359:
# for k in range(len(lines[0])):
# lines[-1].append(lines[0][k])
# lines.pop(0)
return lines
def process_data(data): #data is an array of length with each degree
lines = LinesSplit(data) #lines are arrays in array thet give radius and angle
print(f'data is {data}')
outData = []
for line in lines:
print(f'line is: {line}')
modelIntercepts = []
modelCoefs = []
X, Y = data2coords4line(line)
#xS = np.array([])
yS = Y
xS = X.reshape(-1,1)
#yS = np.array([])
#lineNP = np.array(line)
#drawData(data2coordsov(lineNP))
#print(f'lineNP is given to data2coords as data. Its value is {lineNP}')
#xS = np.append(xS, data2coords(lineNP)[0])
#print(f'xS = {xS}')
#print(f'xS length is {len(xS)}')
#yS = np.append(yS, data2coords(lineNP)[1])
#xS = np.array(xS).reshape(-1, 1)
model = LinearRegression().fit(xS, yS)
#print(f'intercept: {model.intercept_}')
modelIntercepts.append(model.intercept_)
#print(f'slope: {model.coef_}')
modelCoefs.append(model.coef_[0])
#print(xS)
xSSorted = np.sort(xS, axis = 0)
print(f'xSorted are {xSSorted}')
xLowerLimit = xSSorted[-1][0]
xHigherLimit = xSSorted[0][0]
outData.append([modelIntercepts,modelCoefs, xLowerLimit, xHigherLimit])
#print (f'modelIntercepts are {modelIntercepts}, modelCoefs are {modelCoefs}')
#print('')
print(f'outData is {outData}')
return outData
def displayPoints(dataset):
X, Y = data2coords(dataset)
plt.scatter(X, Y)
print(X)
#print(X,Y, sep = "\n" )
datanp = np.array(dataset)
plt.scatter(datanp, range(360))
plt.scatter(0,0)
plt.show()
displayPoints(noisedData)
process_data(noisedData)