forked from r4stered/VexIQPathPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_planner.py
355 lines (266 loc) · 11 KB
/
path_planner.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# VEX IQ Python-Project
import sys
import vexiq
import math
import timer
#region config
leftMotor = vexiq.Motor(1)
rightMotor = vexiq.Motor(2)
gyro = vexiq.Gyro(3)
#endregion config
import math
class PathPlanner:
originalPath = None
nodeOnlyPath = None
smoothPath = None
leftPath = None
rightPath = None
origCenterVelocity = None
origRightVelocity = None
origLeftVelocity = None
smoothCenterVelocity = None
smoothRightVelocity = None
smoothLeftVelocity = None
accumHeading = None
totalTime = 0
totalDistance = 0
numFinalPoints = 0
pathAlpha = 0.7
pathBeta = 0.3
pathTolerance = 0.0001
velocityAlpha = 0.1
velocityBeta = 0.3
velocityTolerance = 0.0001
def __init__(self, path):
self.originalPath = [row[:] for row in path]
def printArr(self, path):
print("X:, Y:")
for val in path:
print(val)
def print2DArr(self, path):
print("X:, Y:")
for val in path:
print(str(val[0]) + "," + str(val[1]))
def inject(self, orig, numToInject):
lengthOfArray = len(orig) + ((numToInject)*(len(orig)-1))
morePoints = [[0 for w in range(2)] for h in range(lengthOfArray)]
index = 0
for i in range(len(orig)-1):
morePoints[index][0] = orig[i][0]
morePoints[index][1] = orig[i][1]
index = index + 1
for j in range(1, numToInject + 1):
morePoints[index][0] = j * ((orig[i+1][0]-orig[i][0])/(numToInject+1)) + orig[i][0]
morePoints[index][1] = j * ((orig[i+1][1]-orig[i][1])/(numToInject+1)) + orig[i][1]
index = index + 1
morePoints[index][0] = orig[-1][0]
morePoints[index][1] = orig[-1][1]
index = index + 1
return morePoints
def smoother(self, path, weight_data, weight_smooth, tolerance):
newPath = [row[:] for row in path]
change = tolerance
while change >= tolerance:
change = 0.0
for i in range(1, len(path)-1):
for j in range(len(path[i])):
aux = newPath[i][j]
newPath[i][j] += weight_data * (path[i][j] - newPath[i][j]) + weight_smooth * (newPath[i-1][j] + newPath[i+1][j] - (2.0 * newPath[i][j]))
change += abs(aux - newPath[i][j])
return newPath
def nodeOnlyWaypoints(self, path):
li = []
li.append(path[0])
for i in range(1, len(path) - 1):
vector1 = math.atan2((path[i][1]-path[i-1][1]), path[i][0] - path[i-1][0])
vector2 = math.atan2((path[i+1][1]-path[i][1]), path[i+1][0] - path[i][0])
if abs(vector2-vector1) >= 0.01:
li.append(path[i])
li.append(path[-1])
return li
def velocity(self, smoothPath, timestep):
dxdt = [None] * len(smoothPath);
dydt = [None] * len(smoothPath);
velocity = [[0 for w in range(2)] for h in range(len(smoothPath))]
dxdt[0] = 0
dydt[0] = 0
velocity[0][0] = 0
velocity[0][1] = 0
self.accumHeading[0][1] = 0
for i in range(1, len(smoothPath)):
dxdt[i] = (smoothPath[i][0]-smoothPath[i-1][0])/timestep
dydt[i] = (smoothPath[i][1]-smoothPath[i-1][1])/timestep
velocity[i][0] = velocity[i-1][0] + timestep
self.accumHeading[i][0] = self.accumHeading[i-1][0] + timestep
velocity[i][1] = math.sqrt(math.pow(dxdt[i], 2) + math.pow(dydt[i], 2))
return velocity
def velocityFix(self, smoothVelocity, origVelocity, tolerance):
difference = self.errorSum(origVelocity, smoothVelocity)
fixVel = [[0 for w in range(2)] for h in range(len(smoothVelocity))]
for i in range(len(smoothVelocity)):
fixVel[i][0] = smoothVelocity[i][0]
fixVel[i][1] = smoothVelocity[i][1]
increase = 0
while abs(difference[-1]) > tolerance:
increase = difference[-1]/1/50
for i in range(1, len(fixVel)):
fixVel[i][1] = fixVel[i][1] - increase
difference = self.errorSum(origVelocity, fixVel)
return fixVel
def errorSum(self, origVelocity, smoothVelocity):
tempOrigDist = [None] * len(origVelocity)
tempSmoothDist = [None] * len(smoothVelocity)
difference = [None ] * len(smoothVelocity)
timeStep = origVelocity[1][0] - origVelocity[0][0]
tempOrigDist[0] = origVelocity[0][1]
tempSmoothDist[0] = smoothVelocity[0][1]
for i in range(1, len(origVelocity)):
tempOrigDist[i] = origVelocity[i][1] * timeStep + tempOrigDist[i-1]
tempSmoothDist[i] = smoothVelocity[i][1] * timeStep + tempSmoothDist[i-1]
difference[i] = tempSmoothDist[i] - tempOrigDist[i]
return difference
def injectionCounter2Step(self, numNodeOnlyPoints, maxTimeToCompelete, timestep):
first = 0
second = 0
third = 0
oldPointsTotal = 0
numFinalPoints = 0
ret = []
totalPoints = maxTimeToCompelete / timestep
if totalPoints < 100:
pointsFirst = 0
pointsTotal = 0
for i in range(4, 7):
for j in range(1, 9):
pointsFirst = i * (numNodeOnlyPoints-1) + numNodeOnlyPoints
pointsTotal = (j*(pointsFirst-1)+pointsFirst)
if pointsTotal <= totalPoints and pointsTotal > oldPointsTotal:
first = i
second = j
numFinalPoints = pointsTotal
oldPointsTotal = pointsTotal
ret.append(first)
ret.append(second)
ret.append(third)
else:
pointsFirst = 0
pointsSecond = 0
pointsTotal = 0
for i in range(1, 6):
for j in range(1, 9):
for k in range(1, 8):
pointsFirst = i * (numNodeOnlyPoints-1) + numNodeOnlyPoints;
pointsSecond = (j * (pointsFirst - 1) + pointsFirst)
pointsTotal = (k * (pointsSecond - 1) + pointsSecond)
if pointsTotal <= totalPoints:
first = i
second = j
third = k
numFinalPoints = pointsTotal
ret.append(first)
ret.append(second)
ret.append(third)
return ret
def leftRight(self, smoothPath, robotTrackWidth):
leftPath = [[0 for w in range(2)] for h in range(len(smoothPath))]
rightPath = [[0 for w in range(2)] for h in range(len(smoothPath))]
gradient = [[0 for w in range(2)] for h in range(len(smoothPath))]
for i in range(len(smoothPath) - 1):
gradient[i][1] = math.atan2(smoothPath[i+1][1] - smoothPath[i][1], smoothPath[i+1][0] - smoothPath[i][0])
gradient[-1][1] = gradient[-2][1]
for i in range(len(gradient)):
leftPath[i][0] = (robotTrackWidth / 2 * math.cos(gradient[i][1] + math.pi/2)) + smoothPath[i][0]
leftPath[i][1] = (robotTrackWidth / 2 * math.sin(gradient[i][1] + math.pi/2)) + smoothPath[i][1]
rightPath[i][0] = (robotTrackWidth / 2 * math.cos(gradient[i][1] - math.pi/2)) + smoothPath[i][0]
rightPath[i][1] = (robotTrackWidth / 2 * math.sin(gradient[i][1] - math.pi/2)) + smoothPath[i][1]
deg = math.degrees(gradient[i][1])
gradient[i][1] = deg
if i > 0:
if deg-gradient[i-1][1] > 180:
gradient[i][1] = -360+deg
if deg-gradient[i-1][1] < -180:
gradient[i][1] = 360+deg
self.accumHeading = gradient
self.leftPath = leftPath
self.rightPath = rightPath
def calculate(self, totalTime, timestep, robotTrackWidth):
print("hello")
self.nodeOnlyPath = self.nodeOnlyWaypoints(self.originalPath)
inject = self.injectionCounter2Step(len(self.nodeOnlyPath), totalTime, timestep)
print("got past injection counter")
for i in range(len(inject)):
if i == 0:
self.smoothPath = self.inject(self.nodeOnlyPath, inject[0])
self.smoothPath = self.smoother(self.smoothPath, self.pathAlpha, self.pathBeta, self.pathTolerance)
else:
self.smoothPath = self.inject(self.smoothPath, inject[i])
self.smoothPath = self.smoother(self.smoothPath, 0.1, 0.3, 0.0001)
print("got past inject and smooth")
self.leftRight(self.smoothPath, robotTrackWidth)
self.origCenterVelocity = self.velocity(self.smoothPath, timestep)
self.origLeftVelocity = self.velocity(self.leftPath, timestep)
self.origRightVelocity = self.velocity(self.rightPath, timestep)
self.smoothCenterVelocity = [row[:] for row in self.origCenterVelocity]
self.smoothLeftVelocity = [row[:] for row in self.origLeftVelocity]
self.smoothRightVelocity = [row[:] for row in self.origRightVelocity]
self.smoothCenterVelocity[-1][1] = 0.0
self.smoothLeftVelocity[-1][1] = 0.0
self.smoothRightVelocity[-1][1] = 0.0
self.smoothCenterVelocity = self.smoother(self.smoothCenterVelocity, self.velocityAlpha, self.velocityBeta, self.velocityTolerance)
self.smoothLeftVelocity = self.smoother(self.smoothLeftVelocity, self.velocityAlpha,self.velocityBeta, self.velocityTolerance)
self.smoothRightVelocity = self.smoother(self.smoothRightVelocity, self.velocityAlpha, self.velocityBeta, self.velocityTolerance)
self.smoothCenterVelocity = self.velocityFix(self.smoothCenterVelocity, self.origCenterVelocity, 0.0001)
self.smoothLeftVelocity = self.velocityFix(self.smoothLeftVelocity, self.origLeftVelocity, 0.0001)
self.smoothRightVelocity = self.velocityFix(self.smoothRightVelocity, self.origRightVelocity, 0.0001)
print("done")
waypoints = []
waypoints.append([7,16])
waypoints.append([11,16])
waypoints.append([17,28])
waypoints.append([23,28])
totalTime = 4
timestep = 0.1
robotTrackWidth = 7
planner = PathPlanner(waypoints)
planner.calculate(totalTime, timestep, robotTrackWidth)
print("Smooth Path")
planner.print2DArr(planner.smoothPath)
print("Velocity Path")
planner.print2DArr(planner.smoothCenterVelocity)
print("Left Velocity Path")
planner.print2DArr(planner.smoothLeftVelocity)
print("Right Velocity Path")
planner.print2DArr(planner.smoothRightVelocity)
timeCounter = timer.Timer()
timeCounter.start()
wheel_radius = 1.25319
wheel_radius_meters = wheel_radius * .0254
i = 0
def translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
while(True):
currentTime = timeCounter.elapsed_time()
if currentTime >= 0.1:
if i < len(planner.smoothRightVelocity):
leftMotorMPS = planner.smoothLeftVelocity[i][1] * 0.0254
rightMotorMPS = planner.smoothRightVelocity[i][1] * 0.0254
leftmotorRadiansPerSecond = leftMotorMPS / wheel_radius_meters
rightmotorRadiansPerSecond = rightMotorMPS / wheel_radius_meters
leftMotorRPM = leftmotorRadiansPerSecond * 9.549
rightMotorRPM = rightmotorRadiansPerSecond * 9.549
leftMotorSpeed = translate(leftMotorRPM, -120, 120, -100, 100)
rightMotorSpeed = translate(rightMotorRPM, -120, 120, -100, 100)
leftMotor.run(-leftMotorSpeed)
rightMotor.run(rightMotorSpeed)
else:
leftMotor.run(0)
rightMotor.run(0)
timeCounter.reset()
timeCounter.start()
i = i + 1