-
Notifications
You must be signed in to change notification settings - Fork 110
/
nloptInverseKinematics.go
383 lines (342 loc) · 10.7 KB
/
nloptInverseKinematics.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
//go:build !windows && !no_cgo
package ik
import (
"context"
"math"
"math/rand"
"strings"
"sync"
"github.com/go-nlopt/nlopt"
"github.com/pkg/errors"
"go.uber.org/multierr"
"go.viam.com/utils"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/referenceframe"
)
var (
errNoSolve = errors.New("kinematics could not solve for position")
errBadBounds = errors.New("cannot set upper or lower bounds for nlopt, slice is empty. Are you trying to move a static frame?")
errTooManyVals = errors.New("passed in too many joint positions")
)
const (
constrainedTries = 30
nloptStepsPerIter = 4001
defaultJump = 1e-8
)
// NloptIK TODO.
type NloptIK struct {
id int
model referenceframe.Frame
lowerBound []float64
upperBound []float64
maxIterations int
epsilon float64
logger logging.Logger
// Nlopt will try to minimize a configuration for whatever is passed in. If exact is false, then the solver will emit partial
// solutions where it was not able to meet the goal criteria but still was able to improve upon the seed.
exact bool
// useRelTol specifies whether the SetXtolRel and SetFtolRel values will be set for nlopt.
// If true, this will terminate solving when nlopt alg iterations change the distance to goal by less than some proportion of calculated
// distance. This can cause premature terminations when the distances are large.
useRelTol bool
}
type optimizeReturn struct {
solution []float64
score float64
err error
}
// CreateNloptIKSolver creates an nloptIK object that can perform gradient descent on metrics for Frames. The parameters are the Frame on
// which Transform() will be called, a logger, and the number of iterations to run. If the iteration count is less than 1, it will be set
// to the default of 5000.
func CreateNloptIKSolver(mdl referenceframe.Frame, logger logging.Logger, iter int, exact, useRelTol bool) (*NloptIK, error) {
ik := &NloptIK{logger: logger}
ik.model = mdl
ik.id = 0
// Stop optimizing when iterations change by less than this much
// Also, how close we want to get to the goal region. The metric should reflect any buffer.
ik.epsilon = defaultEpsilon * defaultEpsilon
if iter < 1 {
// default value
iter = 5000
}
ik.maxIterations = iter
ik.lowerBound, ik.upperBound = limitsToArrays(mdl.DoF())
ik.exact = exact
ik.useRelTol = useRelTol
return ik, nil
}
// Solve runs the actual solver and sends any solutions found to the given channel.
func (ik *NloptIK) Solve(ctx context.Context,
solutionChan chan<- *Solution,
seed []referenceframe.Input,
solveMetric StateMetric,
rseed int,
) error {
//nolint: gosec
randSeed := rand.New(rand.NewSource(int64(rseed)))
var err error
mInput := &State{Frame: ik.model}
// Determine optimal jump values; start with default, and if gradient is zero, increase to 1 to try to avoid underflow.
jump, err := ik.calcJump(defaultJump, seed, solveMetric)
if err != nil {
return err
}
tries := 1
iterations := 0
solutionsFound := 0
startingPos := seed
opt, err := nlopt.NewNLopt(nlopt.LD_SLSQP, uint(len(ik.model.DoF())))
defer opt.Destroy()
if err != nil {
return errors.Wrap(err, "nlopt creation error")
}
if len(ik.lowerBound) == 0 || len(ik.upperBound) == 0 {
return errBadBounds
}
var activeSolvers sync.WaitGroup
jumpVal := 0.
// x is our set of inputs
// Gradient is, under the hood, a unsafe C structure that we are meant to mutate in place.
nloptMinFunc := func(x, gradient []float64) float64 {
iterations++
inputs := referenceframe.FloatsToInputs(x)
eePos, err := ik.model.Transform(inputs)
if eePos == nil || (err != nil && !strings.Contains(err.Error(), referenceframe.OOBErrString)) {
ik.logger.CErrorw(ctx, "error calculating eePos in nlopt", "error", err)
err = opt.ForceStop()
ik.logger.CErrorw(ctx, "forcestop error", "error", err)
return 0
}
mInput.Configuration = inputs
mInput.Position = eePos
dist := solveMetric(mInput)
if len(gradient) > 0 {
// Yes, the for loop below is logically equivalent to not having this if statement. But CPU branch prediction means having the
// if statement is faster.
for i := range gradient {
jumpVal = jump[i]
flip := false
inputs[i].Value += jumpVal
ub := ik.upperBound[i]
if inputs[i].Value >= ub {
flip = true
inputs[i].Value -= 2 * jumpVal
}
eePos, err := ik.model.Transform(inputs)
if eePos == nil || (err != nil && !strings.Contains(err.Error(), referenceframe.OOBErrString)) {
ik.logger.CErrorw(ctx, "error calculating eePos in nlopt", "error", err)
err = opt.ForceStop()
ik.logger.CErrorw(ctx, "forcestop error", "error", err)
return 0
}
mInput.Configuration = inputs
mInput.Position = eePos
dist2 := solveMetric(mInput)
gradient[i] = (dist2 - dist) / jumpVal
if flip {
inputs[i].Value += jumpVal
gradient[i] *= -1
} else {
inputs[i].Value -= jumpVal
}
}
}
return dist
}
err = multierr.Combine(
opt.SetFtolAbs(ik.epsilon),
opt.SetLowerBounds(ik.lowerBound),
opt.SetStopVal(ik.epsilon),
opt.SetUpperBounds(ik.upperBound),
opt.SetXtolAbs1(ik.epsilon),
opt.SetMinObjective(nloptMinFunc),
opt.SetMaxEval(nloptStepsPerIter),
)
if ik.useRelTol {
err = multierr.Combine(
err,
opt.SetFtolRel(ik.epsilon),
opt.SetXtolRel(ik.epsilon),
)
}
if ik.id > 0 {
// Solver with ID 1 seeds off current angles
if ik.id == 1 {
if len(seed) > len(ik.model.DoF()) {
return errTooManyVals
}
startingPos = seed
// Set initial restrictions on joints for more intuitive movement
err = ik.updateBounds(startingPos, tries, opt)
if err != nil {
return err
}
} else {
// Solvers whose ID is not 1 should skip ahead directly to trying random seeds
startingPos = ik.GenerateRandomPositions(randSeed)
tries = constrainedTries
}
}
solveChan := make(chan *optimizeReturn, 1)
defer close(solveChan)
for iterations < ik.maxIterations {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
var solutionRaw []float64
var result float64
var nloptErr error
iterations++
activeSolvers.Add(1)
utils.PanicCapturingGo(func() {
defer activeSolvers.Done()
solutionRaw, result, nloptErr := opt.Optimize(referenceframe.InputsToFloats(startingPos))
solveChan <- &optimizeReturn{solutionRaw, result, nloptErr}
})
select {
case <-ctx.Done():
err = multierr.Combine(err, opt.ForceStop())
activeSolvers.Wait()
return multierr.Combine(err, ctx.Err())
case solution := <-solveChan:
solutionRaw = solution.solution
result = solution.score
nloptErr = solution.err
}
if nloptErr != nil {
// This just *happens* sometimes due to weirdnesses in nonlinear randomized problems.
// Ignore it, something else will find a solution
err = multierr.Combine(err, nloptErr)
}
if result < ik.epsilon || (solutionRaw != nil && !ik.exact) {
select {
case <-ctx.Done():
return err
default:
}
solutionChan <- &Solution{
Configuration: referenceframe.FloatsToInputs(solutionRaw),
Score: result,
Exact: result < ik.epsilon,
}
solutionsFound++
}
tries++
if ik.id > 0 && tries < constrainedTries {
err = ik.updateBounds(seed, tries, opt)
if err != nil {
return err
}
} else {
err = multierr.Combine(
opt.SetLowerBounds(ik.lowerBound),
opt.SetUpperBounds(ik.upperBound),
)
if err != nil {
return err
}
startingPos = ik.GenerateRandomPositions(randSeed)
}
}
if solutionsFound > 0 {
return nil
}
return multierr.Combine(err, errNoSolve)
}
// GenerateRandomPositions generates a random set of positions within the limits of this solver.
func (ik *NloptIK) GenerateRandomPositions(randSeed *rand.Rand) []referenceframe.Input {
pos := make([]referenceframe.Input, len(ik.model.DoF()))
for i, l := range ik.lowerBound {
u := ik.upperBound[i]
// Default to [-999,999] as range if limits are infinite
if l == math.Inf(-1) {
l = -999
}
if u == math.Inf(1) {
u = 999
}
jRange := math.Abs(u - l)
// Note that rand is unseeded and so will produce the same sequence of floats every time
// However, since this will presumably happen at different positions to different joints, this shouldn't matter
pos[i] = referenceframe.Input{randSeed.Float64()*jRange + l}
}
return pos
}
// Frame returns the associated referenceframe.
func (ik *NloptIK) Frame() referenceframe.Frame {
return ik.model
}
// DoF returns the DoF of the associated referenceframe.
func (ik *NloptIK) DoF() []referenceframe.Limit {
return ik.model.DoF()
}
// updateBounds will set the allowable maximum/minimum joint angles to disincentivise large swings before small swings
// have been tried.
func (ik *NloptIK) updateBounds(seed []referenceframe.Input, tries int, opt *nlopt.NLopt) error {
rangeStep := 0.1
newLower := make([]float64, len(ik.lowerBound))
newUpper := make([]float64, len(ik.upperBound))
for i, pos := range seed {
newLower[i] = math.Max(ik.lowerBound[i], pos.Value-(rangeStep*float64(tries*(i+1))))
newUpper[i] = math.Min(ik.upperBound[i], pos.Value+(rangeStep*float64(tries*(i+1))))
// Allow full freedom of movement for the two most distal joints
if i > len(seed)-2 {
newLower[i] = ik.lowerBound[i]
newUpper[i] = ik.upperBound[i]
}
}
return multierr.Combine(
opt.SetLowerBounds(newLower),
opt.SetUpperBounds(newUpper),
)
}
func (ik *NloptIK) calcJump(testJump float64, seed []referenceframe.Input, solveMetric StateMetric) ([]float64, error) {
mInput := &State{Frame: ik.model}
jump := make([]float64, 0, len(seed))
eePos, err := ik.model.Transform(seed)
if err != nil {
return nil, err
}
mInput.Configuration = seed
mInput.Position = eePos
seedDist := solveMetric(mInput)
for i, testVal := range seed {
seedTest := append(make([]referenceframe.Input, 0, len(seed)), seed...)
for jumpVal := testJump; jumpVal < 0.1; jumpVal *= 10 {
seedTest[i] = referenceframe.Input{testVal.Value + jumpVal}
if seedTest[i].Value > ik.upperBound[i] {
seedTest[i] = referenceframe.Input{testVal.Value - jumpVal}
if seedTest[i].Value < ik.lowerBound[i] {
jump = append(jump, testJump)
break
}
}
eePos, err = ik.model.Transform(seedTest)
if err != nil {
return nil, err
}
mInput.Configuration = seedTest
mInput.Position = eePos
checkDist := solveMetric(mInput)
// Use the smallest value that yields a change in distance
if checkDist != seedDist {
jump = append(jump, jumpVal)
break
}
}
if len(jump) != i+1 {
jump = append(jump, testJump)
}
}
return jump, nil
}
func limitsToArrays(limits []referenceframe.Limit) ([]float64, []float64) {
var min, max []float64
for _, limit := range limits {
min = append(min, limit.Min)
max = append(max, limit.Max)
}
return min, max
}