-
Notifications
You must be signed in to change notification settings - Fork 110
/
dubins.go
419 lines (357 loc) · 13.2 KB
/
dubins.go
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package motionplan
import (
"errors"
"math"
"sort"
"go.viam.com/rdk/referenceframe"
)
// paper: https://arxiv.org/pdf/2206.10533.pdf
// python dubins-rrt package used for reference: https://github.com/FelicienC/RRT-Dubins
// Dubins describes the parameters for a specific Dubin's problem.
type Dubins struct {
Radius float64 // Turning radius of car
PointSeparation float64 // Separation of points on path to check for collision
}
// DubinPathAttr describes a Dubins path that can be taken from one point to another.
type DubinPathAttr struct {
TotalLen float64 // Total length of all segments making up a single Dubin's path
DubinsPath []float64 // Length array of the six possible Dubin's path combiantions
Straight bool // True if Dubin's Path segment is straight
}
// NewDubins creates a new Dubins instance given a valid radius and point separation.
func NewDubins(radius, pointSeparation float64) (*Dubins, error) {
if radius <= 0 {
return nil, errors.New("radius must be greater than 0")
}
if pointSeparation <= 0 {
return nil, errors.New("point Separation must be greater than 0")
}
dubins := &Dubins{Radius: radius, PointSeparation: pointSeparation}
return dubins, nil
}
func (d *Dubins) findCenter(point []float64, isLeft bool) []float64 {
angle := point[2]
if isLeft {
angle += math.Pi / 2
} else {
angle -= math.Pi / 2
}
center := []float64{point[0] + math.Cos(angle)*d.Radius, point[1] + math.Sin(angle)*d.Radius}
return center
}
// Calculates a DubinPathAttr for moving from start to end using a Left, Straight, Left path.
func (d *Dubins) lsl(start, end, center0, center2 []float64) DubinPathAttr {
straightDist := dist(center0, center2)
alpha := math.Atan2(sub(center2, center0)[1], sub(center2, center0)[0])
beta2 := mod2Pi((end[2] - alpha))
beta0 := mod2Pi((alpha - start[2]))
totalLen := d.Radius*(beta2+beta0) + straightDist // both
path := make([]float64, 3)
path[0] = beta0
path[1] = beta2
path[2] = straightDist
dubin := DubinPathAttr{TotalLen: totalLen, DubinsPath: path, Straight: true}
return dubin
}
// Calculates a DubinPathAttr for moving from start to end using a Right, Straight, Right path.
func (d *Dubins) rsr(start, end, center0, center2 []float64) DubinPathAttr {
alpha := math.Atan2(sub(center2, center0)[1], sub(center2, center0)[0])
beta2 := mod2Pi((-end[2] + alpha))
beta0 := mod2Pi((-alpha + start[2]))
straightDist := dist(center0, center2)
totalLen := d.Radius*(beta2+beta0) + straightDist
path := make([]float64, 3)
path[0] = -beta0
path[1] = -beta2
path[2] = straightDist
dubin := DubinPathAttr{TotalLen: totalLen, DubinsPath: path, Straight: true}
return dubin
}
// Calculates a DubinPathAttr for moving from start to end using a Right, Straight, Left path.
func (d *Dubins) rsl(start, end, center0, center2 []float64) DubinPathAttr {
medianPoint := []float64{sub(center2, center0)[0] / 2, sub(center2, center0)[1] / 2}
psia := math.Atan2(medianPoint[1], medianPoint[0])
halfIntercenter := norm(medianPoint)
if halfIntercenter < d.Radius {
zeros := []float64{0., 0., 0.}
dubin := DubinPathAttr{TotalLen: math.Inf(1), DubinsPath: zeros, Straight: true}
return dubin
}
alpha := math.Acos(d.Radius / halfIntercenter)
beta0 := mod2Pi(-(psia + alpha - start[2] - math.Pi/2))
beta2 := mod2Pi(math.Pi + end[2] - math.Pi/2 - alpha - psia)
straightDist := 2 * (math.Sqrt((math.Pow(halfIntercenter, 2) - math.Pow(d.Radius, 2))))
totalLen := d.Radius*(beta2+beta0) + straightDist
path := make([]float64, 3)
path[0] = -beta0
path[1] = beta2
path[2] = straightDist
dubin := DubinPathAttr{TotalLen: totalLen, DubinsPath: path, Straight: true}
return dubin
}
// Calculates a DubinPathAttr for moving from start to end using a Left, Straight, Right path.
func (d *Dubins) lsr(start, end, center0, center2 []float64) DubinPathAttr {
medianPoint := []float64{sub(center2, center0)[0] / 2, sub(center2, center0)[1] / 2}
psia := math.Atan2(medianPoint[1], medianPoint[0])
halfIntercenter := norm(medianPoint)
if halfIntercenter < d.Radius {
zeros := []float64{0., 0., 0.}
dubin := DubinPathAttr{TotalLen: math.Inf(1), DubinsPath: zeros, Straight: true}
return dubin
}
alpha := math.Acos(d.Radius / halfIntercenter)
beta0 := mod2Pi((psia - alpha - start[2] + math.Pi/2))
beta2 := mod2Pi(0.5*math.Pi - end[2] - alpha + psia)
straightDist := 2 * (math.Sqrt((math.Pow(halfIntercenter, 2) - math.Pow(d.Radius, 2))))
totalLen := d.Radius*(beta2+beta0) + straightDist
path := make([]float64, 3)
path[0] = beta0
path[1] = -beta2
path[2] = straightDist
dubin := DubinPathAttr{TotalLen: totalLen, DubinsPath: path, Straight: true}
return dubin
}
// Calculates a DubinPathAttr for moving from start to end using a Left, Right, Left path.
func (d *Dubins) lrl(start, end, center0, center2 []float64) DubinPathAttr {
distIntercenter := dist(center0, center2)
intercenter := []float64{sub(center2, center0)[0] / 2, sub(center2, center0)[1] / 2}
psia := math.Atan2(intercenter[1], intercenter[0])
if 2*d.Radius < distIntercenter && distIntercenter > 4*d.Radius {
zeros := []float64{0., 0., 0.}
dubin := DubinPathAttr{TotalLen: math.Inf(1), DubinsPath: zeros, Straight: false}
return dubin
}
gamma := 2 * math.Asin(distIntercenter/(4*d.Radius))
beta0 := mod2Pi((psia - start[2] + math.Pi/2 + (math.Pi-gamma)/2))
beta1 := mod2Pi((-psia + math.Pi/2 + end[2] + (math.Pi-gamma)/2))
totalLen := (2*math.Pi - gamma + math.Abs(beta0) + math.Abs(beta1)) * d.Radius
path := make([]float64, 3)
path[0] = beta0
path[1] = beta1
path[2] = 2*math.Pi - gamma
dubin := DubinPathAttr{TotalLen: totalLen, DubinsPath: path, Straight: false}
return dubin
}
// Calculates a DubinPathAttr for moving from start to end using a Right, Left, Right path.
func (d *Dubins) rlr(start, end, center0, center2 []float64) DubinPathAttr {
distIntercenter := dist(center0, center2)
intercenter := []float64{sub(center2, center0)[0] / 2, sub(center2, center0)[1] / 2}
psia := math.Atan2(intercenter[1], intercenter[0])
if 2*d.Radius < distIntercenter && distIntercenter > 4*d.Radius {
zeros := []float64{0., 0., 0.}
dubin := DubinPathAttr{TotalLen: math.Inf(1), DubinsPath: zeros, Straight: false}
return dubin
}
gamma := 2 * math.Asin(distIntercenter/(4*d.Radius))
beta0 := -mod2Pi((-psia + (start[2] + math.Pi/2) + (math.Pi-gamma)/2))
beta1 := -mod2Pi((psia + math.Pi/2 - end[2] + (math.Pi-gamma)/2))
totalLen := (2*math.Pi - gamma + math.Abs(beta0) + math.Abs(beta1)) * d.Radius
path := make([]float64, 3)
path[0] = beta0
path[1] = beta1
path[2] = 2*math.Pi - gamma
dubin := DubinPathAttr{TotalLen: totalLen, DubinsPath: path, Straight: false}
return dubin
}
// AllPaths finds every possible Dubins path from start to end and returns them as DubinPathAttrs.
func (d *Dubins) AllPaths(start, end []float64, sorts bool) []DubinPathAttr {
center0Left := d.findCenter(start, true) // "L"
center0Right := d.findCenter(start, false) // "R"
center2Left := d.findCenter(end, true) // "L"
center2Right := d.findCenter(end, false) // "R"
paths := []DubinPathAttr{
d.lsl(start, end, center0Left, center2Left),
d.rsr(start, end, center0Right, center2Right),
d.rsl(start, end, center0Right, center2Left),
d.lsr(start, end, center0Left, center2Right),
d.rlr(start, end, center0Right, center2Right),
d.lrl(start, end, center0Left, center2Left),
}
if sorts {
// sort by first element in paths
sort.SliceStable(paths, func(i, j int) bool {
return paths[i].TotalLen < paths[j].TotalLen
})
}
return paths
}
func (d *Dubins) generatePointsStraight(start, end, path []float64) [][]float64 {
total := d.Radius*(math.Abs(path[1])+math.Abs(path[0])) + path[2]
center0 := d.findCenter(start, false) // "R"
center2 := d.findCenter(end, false) // "R"
if path[0] > 0 {
center0 = d.findCenter(start, true) // "L"
center2 = d.findCenter(end, true) // "L"
}
// start of straight
ini := start[:2]
if math.Abs(path[0]) > 0 {
angle := start[2]
if path[0] > 0 {
angle += (math.Abs(path[0]) - math.Pi/2)
} else if path[0] < 0 {
angle -= (math.Abs(path[0]) - math.Pi/2)
}
sides := []float64{math.Cos(angle), math.Sin(angle)}
ini = add(center0, mul(sides, d.Radius))
}
// end of straight
fin := end[:2]
if math.Abs(path[1]) > 0 {
angle := end[2] + (-math.Abs(path[1]) - math.Pi/2)
if path[1] > 0 {
angle += (-math.Abs(path[1]) - math.Pi/2)
} else if path[1] < 0 {
angle -= (-math.Abs(path[1]) - math.Pi/2)
}
sides := []float64{math.Cos(angle), math.Sin(angle)}
fin = add(center2, sides)
}
distStraight := dist(ini, fin)
// generate all points
points := make([][]float64, 0)
x := 0.0
for x < total {
switch {
case x < math.Abs(path[0])*d.Radius:
points = append(points, d.circleArc(start, path[0], center0, x))
case x > total-math.Abs(path[1])*d.Radius:
points = append(points, d.circleArc(end, path[1], center2, x-total))
default:
coeff := (x - math.Abs(path[0])*d.Radius) / distStraight
points = append(points, add(mul(fin, coeff), mul(ini, (1-coeff))))
}
x += d.PointSeparation
}
points = append(points, end[:2])
return points
}
func (d *Dubins) generatePointsCurve(start, end, path []float64) [][]float64 {
total := d.Radius*(math.Abs(path[1])+math.Abs(path[0])) + path[2]
center0 := d.findCenter(start, false) // "R"
center2 := d.findCenter(end, false) // "R"
if path[0] > 0 {
center0 = d.findCenter(start, true) // "L"
center2 = d.findCenter(end, true) // "L"
}
intercenter := dist(center0, center2)
center1 := mul(add(center0, center2), 0.5)
if path[0] > 0 {
add(center1, mul(ortho(mul(sub(center2, center0), 1/intercenter)), math.Sqrt((4*math.Pow(d.Radius, 2)-math.Pow((intercenter/2), 2)))))
} else if path[0] < 0 {
sub(center1, mul(ortho(mul(sub(center2, center0), 1/intercenter)), math.Sqrt((4*math.Pow(d.Radius, 2)-math.Pow((intercenter/2), 2)))))
}
psi0 := math.Atan2(sub(center1, center0)[1], sub(center1, center0)[0]) - math.Pi
// generate all points
points := make([][]float64, 0)
x := 0.0
for x < total {
switch {
case x < math.Abs(path[0])*d.Radius:
points = append(points, d.circleArc(start, path[0], center0, x))
case x > total-math.Abs(path[1])*d.Radius:
points = append(points, d.circleArc(end, path[1], center2, x-total))
default:
angle := psi0
if path[0] > 0 {
angle += (x/d.Radius - math.Abs(path[0]))
} else if path[0] < 0 {
angle -= (x/d.Radius - math.Abs(path[0]))
}
sides := []float64{math.Cos(angle), math.Sin(angle)}
points = append(points, add(center1, mul(sides, d.Radius)))
}
x += d.PointSeparation
}
points = append(points, end[:2])
return points
}
func (d *Dubins) circleArc(reference []float64, beta float64, center []float64, x float64) []float64 {
angle := reference[2]
if beta > 0 {
angle += ((x / d.Radius) - math.Pi/2)
} else if beta < 0 {
angle -= ((x / d.Radius) - math.Pi/2)
}
sides := []float64{math.Cos(angle), math.Sin(angle)}
point := add(center, mul(sides, d.Radius))
return point
}
func (d *Dubins) generatePoints(start, end, dubinsPath []float64, straight bool) [][]float64 {
if straight {
return d.generatePointsStraight(start, end, dubinsPath)
}
return d.generatePointsCurve(start, end, dubinsPath)
}
// DubinsPath returns a list of points along the shortest Dubins path from start to end.
func (d *Dubins) DubinsPath(start, end []float64) [][]float64 {
paths := d.AllPaths(start, end, true)
DubinsPath, straight := paths[0].DubinsPath, paths[0].Straight
return d.generatePoints(start, end, DubinsPath, straight)
}
// Helper functions
// In python, the modulo computes n%m = (n+m)%n. For example: -1%10 is 9 in python, and -1 in Go/C
// python mod is desirable here so convert to python mod.
func mod2Pi(n float64) float64 {
const twoPi = 2 * math.Pi
return math.Mod(math.Mod(n, twoPi)+twoPi, twoPi)
}
func dist(p1, p2 []float64) float64 {
dist := math.Sqrt(math.Pow((p1[0]-p2[0]), 2) + math.Pow((p1[1]-p2[1]), 2))
return dist
}
func norm(p1 []float64) float64 {
return math.Sqrt(math.Pow(p1[0], 2) + math.Pow(p1[1], 2))
}
func ortho(vect []float64) []float64 {
orth := []float64{-vect[1], vect[0]}
return orth
}
// element wise subtraction.
func sub(vect1, vect2 []float64) []float64 {
subv := make([]float64, len(vect1))
for i := range vect1 {
subv[i] = vect1[i] - vect2[i]
}
return subv
}
// element wise addition.
func add(vect1, vect2 []float64) []float64 {
addv := make([]float64, len(vect1))
for i := range vect1 {
addv[i] = vect1[i] + vect2[i]
}
return addv
}
// element wise multiplication by a scalar.
func mul(vect1 []float64, scalar float64) []float64 {
mulv := make([]float64, len(vect1))
for i, x := range vect1 {
mulv[i] = x * scalar
}
return mulv
}
// GetDubinTrajectoryFromPath takes a path of waypoints that can be followed using Dubins paths and returns
// a list of DubinPathAttrs describing the Dubins paths to get between waypoints.
func GetDubinTrajectoryFromPath(waypoints [][]referenceframe.Input, d Dubins) []DubinPathAttr {
traj := make([]DubinPathAttr, 0)
current := make([]float64, 3)
next := make([]float64, 3)
for i, wp := range waypoints {
if i == 0 {
for j := 0; j < 3; j++ {
current[j] = wp[j].Value
}
} else {
for j := 0; j < 3; j++ {
next[j] = wp[j].Value
}
allPaths := d.AllPaths(current, next, true)[0]
traj = append(traj, allPaths)
for j := 0; j < 3; j++ {
current[j] = next[j]
}
}
}
return traj
}