-
Notifications
You must be signed in to change notification settings - Fork 110
/
rrt.go
191 lines (162 loc) · 4.88 KB
/
rrt.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
package motionplan
import (
"context"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/spatialmath"
)
const (
// Number of planner iterations before giving up.
defaultPlanIter = 20000
)
type rrtParallelPlanner interface {
motionPlanner
rrtBackgroundRunner(context.Context, spatialmath.Pose, []referenceframe.Input, *rrtParallelPlannerShared)
}
type rrtParallelPlannerShared struct {
maps *rrtMaps
endpointPreview chan node
solutionChan chan *rrtPlanReturn
}
type rrtOptions struct {
// Number of planner iterations before giving up.
PlanIter int `json:"plan_iter"`
}
func newRRTOptions() *rrtOptions {
return &rrtOptions{
PlanIter: defaultPlanIter,
}
}
type rrtMap map[node]node
type rrtPlanReturn struct {
steps []node
planerr error
maps *rrtMaps
}
func (plan *rrtPlanReturn) toInputs() [][]referenceframe.Input {
inputs := make([][]referenceframe.Input, 0, len(plan.steps))
for _, step := range plan.steps {
inputs = append(inputs, step.Q())
}
return inputs
}
func (plan *rrtPlanReturn) err() error {
return plan.planerr
}
type rrtMaps struct {
startMap rrtMap
goalMap rrtMap
optNode *costNode // The highest quality IK solution
}
// initRRTsolutions will create the maps to be used by a RRT-based algorithm. It will generate IK solutions to pre-populate the goal
// map, and will check if any of those goals are able to be directly interpolated to.
func initRRTSolutions(ctx context.Context, mp motionPlanner, goal spatialmath.Pose, seed []referenceframe.Input) *rrtPlanReturn {
rrt := &rrtPlanReturn{
maps: &rrtMaps{
startMap: map[node]node{},
goalMap: map[node]node{},
},
}
seedNode := newCostNode(seed, 0)
rrt.maps.startMap[seedNode] = nil
// get many potential end goals from IK solver
solutions, err := mp.getSolutions(ctx, goal, seed)
if err != nil {
rrt.planerr = err
return rrt
}
// the smallest interpolated distance between the start and end input represents a lower bound on cost
_, optimalCost := mp.opt().DistanceFunc(&ConstraintInput{StartInput: seed, EndInput: solutions[0].Q()})
rrt.maps.optNode = newCostNode(solutions[0].Q(), optimalCost)
// Check for direct interpolation for the subset of IK solutions within some multiple of optimal
// Since solutions are returned ordered, we check until one is out of bounds, then skip remaining checks
canInterp := true
// initialize maps and check whether direct interpolation is an option
for _, solution := range solutions {
if canInterp {
_, cost := mp.opt().DistanceFunc(&ConstraintInput{StartInput: seed, EndInput: solution.Q()})
if cost < optimalCost*defaultOptimalityMultiple {
if mp.checkPath(seed, solution.Q()) {
rrt.steps = []node{seedNode, solution}
return rrt
}
} else {
canInterp = false
}
}
rrt.maps.goalMap[newCostNode(solution.Q(), 0)] = nil
}
return rrt
}
func shortestPath(maps *rrtMaps, nodePairs []*nodePair) *rrtPlanReturn {
if len(nodePairs) == 0 {
return &rrtPlanReturn{planerr: errPlannerFailed, maps: maps}
}
minIdx := 0
minDist := nodePairs[0].sumCosts()
for i := 1; i < len(nodePairs); i++ {
if dist := nodePairs[i].sumCosts(); dist < minDist {
minDist = dist
minIdx = i
}
}
return &rrtPlanReturn{steps: extractPath(maps.startMap, maps.goalMap, nodePairs[minIdx]), maps: maps}
}
// node interface is used to wrap a configuration for planning purposes.
type node interface {
// return the configuration associated with the node
Q() []referenceframe.Input
}
type basicNode struct {
q []referenceframe.Input
}
func (n *basicNode) Q() []referenceframe.Input {
return n.q
}
type costNode struct {
node
cost float64
}
func newCostNode(q []referenceframe.Input, cost float64) *costNode {
return &costNode{&basicNode{q: q}, cost}
}
// nodePair groups together nodes in a tuple
// TODO(rb): in the future we might think about making this into a list of nodes.
type nodePair struct{ a, b node }
func (np *nodePair) sumCosts() float64 {
a, aok := np.a.(*costNode)
if !aok {
return 0
}
b, bok := np.b.(*costNode)
if !bok {
return 0
}
return a.cost + b.cost
}
func extractPath(startMap, goalMap map[node]node, pair *nodePair) []node {
// need to figure out which of the two nodes is in the start map
var startReached, goalReached node
if _, ok := startMap[pair.a]; ok {
startReached, goalReached = pair.a, pair.b
} else {
startReached, goalReached = pair.b, pair.a
}
// extract the path to the seed
path := make([]node, 0)
for startReached != nil {
path = append(path, startReached)
startReached = startMap[startReached]
}
// reverse the slice
for i, j := 0, len(path)-1; i < j; i, j = i+1, j-1 {
path[i], path[j] = path[j], path[i]
}
// skip goalReached node and go directly to its parent in order to not repeat this node
goalReached = goalMap[goalReached]
// extract the path to the goal
for goalReached != nil {
path = append(path, goalReached)
goalReached = goalMap[goalReached]
}
return path
}