-
Notifications
You must be signed in to change notification settings - Fork 110
/
motionPlanner.go
397 lines (349 loc) · 11.2 KB
/
motionPlanner.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
// Package motionplan is a motion planning library.
package motionplan
import (
"context"
"fmt"
"math"
"math/rand"
"sort"
"time"
"github.com/edaniels/golog"
"github.com/pkg/errors"
commonpb "go.viam.com/api/common/v1"
"go.viam.com/utils"
frame "go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/robot"
"go.viam.com/rdk/robot/framesystem"
"go.viam.com/rdk/spatialmath"
vutil "go.viam.com/rdk/utils"
)
// motionPlanner provides an interface to path planning methods, providing ways to request a path to be planned, and
// management of the constraints used to plan paths.
type motionPlanner interface {
// Plan will take a context, a goal position, and an input start state and return a series of state waypoints which
// should be visited in order to arrive at the goal while satisfying all constraints
Plan(context.Context, spatialmath.Pose, []frame.Input, *plannerOptions) ([][]frame.Input, error)
Frame() frame.Frame // Frame will return the frame used for planning
}
type seededPlannerConstructor func(frame frame.Frame, nCPU int, seed *rand.Rand, logger golog.Logger) (motionPlanner, error)
// PlanMotion plans a motion to destination for a given frame. It takes a given frame system, wraps it with a SolvableFS, and solves.
func PlanMotion(ctx context.Context,
logger golog.Logger,
dst *frame.PoseInFrame,
f frame.Frame,
seedMap map[string][]frame.Input,
fs frame.FrameSystem,
worldState *commonpb.WorldState,
planningOpts map[string]interface{},
) ([]map[string][]frame.Input, error) {
return PlanWaypoints(ctx, logger, []*frame.PoseInFrame{dst}, f, seedMap, fs, worldState, []map[string]interface{}{planningOpts})
}
// PlanRobotMotion plans a motion to destination for a given frame. A robot object is passed in and current position inputs are determined.
func PlanRobotMotion(ctx context.Context,
dst *frame.PoseInFrame,
f frame.Frame,
r robot.Robot,
fs frame.FrameSystem,
worldState *commonpb.WorldState,
planningOpts map[string]interface{},
) ([]map[string][]frame.Input, error) {
seedMap, _, err := framesystem.RobotFsCurrentInputs(ctx, r, fs)
if err != nil {
return nil, err
}
return PlanWaypoints(ctx, r.Logger(), []*frame.PoseInFrame{dst}, f, seedMap, fs, worldState, []map[string]interface{}{planningOpts})
}
// PlanFrameMotion plans a motion to destination for a given frame with no frame system. It will create a new FS just for the plan.
// WorldState is not supported in the absence of a real frame system.
func PlanFrameMotion(ctx context.Context,
logger golog.Logger,
dst spatialmath.Pose,
f frame.Frame,
seed []frame.Input,
planningOpts map[string]interface{},
) ([][]frame.Input, error) {
// ephemerally create a framesystem containing just the frame for the solve
fs := frame.NewEmptySimpleFrameSystem("")
err := fs.AddFrame(f, fs.World())
if err != nil {
return nil, err
}
destination := frame.NewPoseInFrame(frame.World, dst)
seedMap := map[string][]frame.Input{f.Name(): seed}
solutionMap, err := PlanWaypoints(
ctx,
logger,
[]*frame.PoseInFrame{destination},
f,
seedMap,
fs,
nil,
[]map[string]interface{}{planningOpts},
)
if err != nil {
return nil, err
}
return FrameStepsFromRobotPath(f.Name(), solutionMap)
}
// PlanWaypoints plans motions to a list of destinations in order for a given frame. It takes a given frame system, wraps it with a
// SolvableFS, and solves. It will generate a list of intermediate waypoints as well to pass to the solvable framesystem if possible.
func PlanWaypoints(ctx context.Context,
logger golog.Logger,
dst []*frame.PoseInFrame,
f frame.Frame,
seedMap map[string][]frame.Input,
fs frame.FrameSystem,
worldState *commonpb.WorldState,
planningOpts []map[string]interface{},
) ([]map[string][]frame.Input, error) {
solvableFS := NewSolvableFrameSystem(fs, logger)
if len(dst) == 0 {
return nil, errors.New("no destinations passed to PlanWaypoints")
}
return solvableFS.SolveWaypointsWithOptions(ctx, seedMap, dst, f.Name(), worldState, planningOpts)
}
// FrameStepsFromRobotPath is a helper function which will extract the waypoints of a single frame from the map output of a robot path.
func FrameStepsFromRobotPath(frameName string, path []map[string][]frame.Input) ([][]frame.Input, error) {
solution := make([][]frame.Input, 0, len(path))
for _, step := range path {
frameStep, ok := step[frameName]
if !ok {
return nil, fmt.Errorf("frame named %s not found in solved motion path", frameName)
}
solution = append(solution, frameStep)
}
return solution, nil
}
type planner struct {
solver InverseKinematics
frame frame.Frame
logger golog.Logger
randseed *rand.Rand
start time.Time
}
func newPlanner(frame frame.Frame, nCPU int, seed *rand.Rand, logger golog.Logger) (*planner, error) {
ik, err := CreateCombinedIKSolver(frame, logger, nCPU)
if err != nil {
return nil, err
}
mp := &planner{
solver: ik,
frame: frame,
logger: logger,
randseed: seed,
}
return mp, nil
}
func (mp *planner) Frame() frame.Frame {
return mp.frame
}
func (mp *planner) checkInputs(planOpts *plannerOptions, inputs []frame.Input) bool {
frame := mp.Frame()
position, err := frame.Transform(inputs)
if err != nil {
return false
}
ok, _ := planOpts.CheckConstraints(&ConstraintInput{
StartPos: position,
EndPos: position,
StartInput: inputs,
EndInput: inputs,
Frame: frame,
})
return ok
}
func (mp *planner) checkPath(planOpts *plannerOptions, seedInputs, target []frame.Input) bool {
ok, _ := planOpts.CheckConstraintPath(
&ConstraintInput{
StartInput: seedInputs,
EndInput: target,
Frame: mp.Frame(),
},
planOpts.Resolution,
)
return ok
}
// node interface is used to wrap a configuration for planning purposes.
type node interface {
// return the configuration associated with the node
Q() []frame.Input
}
type planReturn interface {
// return the steps in Input form
toInputs() [][]frame.Input
err() error
}
type basicNode struct {
q []frame.Input
}
func (n *basicNode) Q() []frame.Input {
return n.q
}
type costNode struct {
node
cost float64
}
func newCostNode(q []frame.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
}
// EvaluatePlan assigns a numeric score to a plan that corresponds to the cumulative distance between input waypoints in the plan.
func EvaluatePlan(plan planReturn, planOpts *plannerOptions) (totalCost float64) {
if errors.Is(plan.err(), errPlannerFailed) {
return math.Inf(1)
}
steps := plan.toInputs()
for i := 0; i < len(steps)-1; i++ {
_, cost := planOpts.DistanceFunc(&ConstraintInput{StartInput: steps[i], EndInput: steps[i+1]})
totalCost += cost
}
return totalCost
}
// GetSteps will determine the number of steps which should be used to get from the seed to the goal.
// The returned value is guaranteed to be at least 1.
// stepSize represents both the max mm movement per step, and max R4AA degrees per step.
func GetSteps(seedPos, goalPos spatialmath.Pose, stepSize float64) int {
// use a default size of 1 if zero is passed in to avoid divide-by-zero
if stepSize == 0 {
stepSize = 1.
}
mmDist := seedPos.Point().Distance(goalPos.Point())
rDist := spatialmath.OrientationBetween(seedPos.Orientation(), goalPos.Orientation()).AxisAngles()
nSteps := math.Max(math.Abs(mmDist/stepSize), math.Abs(vutil.RadToDeg(rDist.Theta)/stepSize))
return int(nSteps) + 1
}
// getSolutions will initiate an IK solver for the given position and seed, collect solutions, and score them by constraints.
// If maxSolutions is positive, once that many solutions have been collected, the solver will terminate and return that many solutions.
// If minScore is positive, if a solution scoring below that amount is found, the solver will terminate and return that one solution.
func getSolutions(ctx context.Context,
planOpts *plannerOptions,
solver InverseKinematics,
goal spatialmath.Pose,
seed []frame.Input,
f frame.Frame,
rseed int,
) ([]*costNode, error) {
// Linter doesn't properly handle loop labels
nSolutions := planOpts.MaxSolutions
if nSolutions == 0 {
nSolutions = defaultSolutionsToSeed
}
seedPos, err := f.Transform(seed)
if err != nil {
return nil, err
}
goalPos := fixOvIncrement(goal, seedPos)
solutionGen := make(chan []frame.Input)
ikErr := make(chan error, 1)
defer func() { <-ikErr }()
ctxWithCancel, cancel := context.WithCancel(ctx)
defer cancel()
// Spawn the IK solver to generate solutions until done
utils.PanicCapturingGo(func() {
defer close(ikErr)
ikErr <- solver.Solve(ctxWithCancel, solutionGen, goalPos, seed, planOpts.metric, rseed)
})
solutions := map[float64][]frame.Input{}
// Solve the IK solver. Loop labels are required because `break` etc in a `select` will break only the `select`.
IK:
for {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
select {
case step := <-solutionGen:
cPass, cScore := planOpts.CheckConstraints(&ConstraintInput{
seedPos,
goalPos,
seed,
step,
f,
})
endPass, _ := planOpts.CheckConstraints(&ConstraintInput{
goalPos,
goalPos,
step,
step,
f,
})
if cPass && endPass {
if cScore < planOpts.MinScore && planOpts.MinScore > 0 {
solutions = map[float64][]frame.Input{}
solutions[cScore] = step
// good solution, stopping early
break IK
}
solutions[cScore] = step
if len(solutions) >= nSolutions {
// sufficient solutions found, stopping early
break IK
}
}
// Skip the return check below until we have nothing left to read from solutionGen
continue IK
default:
}
select {
case <-ikErr:
// If we have a return from the IK solver, there are no more solutions, so we finish processing above
// until we've drained the channel
break IK
default:
}
}
if len(solutions) == 0 {
return nil, errIKSolve
}
keys := make([]float64, 0, len(solutions))
for k := range solutions {
keys = append(keys, k)
}
sort.Float64s(keys)
orderedSolutions := make([]*costNode, 0)
for _, key := range keys {
orderedSolutions = append(orderedSolutions, newCostNode(solutions[key], key))
}
return orderedSolutions, nil
}
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
}