-
Notifications
You must be signed in to change notification settings - Fork 110
/
nloptInverseKinematics.go
310 lines (274 loc) · 8.68 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
//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
)
// NloptIK TODO.
type NloptIK struct {
id int
model referenceframe.Frame
lowerBound []float64
upperBound []float64
maxIterations int
epsilon float64
logger logging.Logger
jump float64
// 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
}
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 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())
// How much to adjust joints to determine slope
ik.jump = 0.00000001
ik.exact = exact
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
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
}
mInput := &State{Frame: ik.model}
var activeSolvers sync.WaitGroup
// x is our joint positions
// Gradient is, under the hood, a unsafe C structure that we are meant to mutate in place.
nloptMinFunc := func(x, gradient []float64) float64 {
iterations++
// Requesting an out-of-bounds transform will result in a non-nil error but will optionally return a correct if invalid pose.
// Thus we check if eePos is nil, and if not, continue as normal and ignore errors.
// As confirmation, the "input out of bounds" string is checked for in the error text.
inputs := referenceframe.FloatsToInputs(x)
eePos, err := ik.model.Transform(inputs)
if eePos == nil || (err != nil && !strings.Contains(err.Error(), referenceframe.OOBErrString)) {
ik.logger.Errorw("error calculating eePos in nlopt", "error", err)
err = opt.ForceStop()
ik.logger.Errorw("forcestop error", "error", err)
return 0
}
mInput.Configuration = inputs
mInput.Position = eePos
dist := solveMetric(mInput)
if len(gradient) > 0 {
for i := range gradient {
x[i] += ik.jump
inputs = referenceframe.FloatsToInputs(x)
eePos, err := ik.model.Transform(inputs)
x[i] -= ik.jump
if eePos == nil || (err != nil && !strings.Contains(err.Error(), referenceframe.OOBErrString)) {
ik.logger.Errorw("error calculating eePos in nlopt", "error", err)
err = opt.ForceStop()
ik.logger.Errorw("forcestop error", "error", err)
return 0
}
mInput.Configuration = inputs
mInput.Position = eePos
dist2 := solveMetric(mInput)
gradient[i] = (dist2 - dist) / ik.jump
}
}
return dist
}
err = multierr.Combine(
opt.SetFtolAbs(ik.epsilon),
opt.SetFtolRel(ik.epsilon),
opt.SetLowerBounds(ik.lowerBound),
opt.SetStopVal(ik.epsilon),
opt.SetUpperBounds(ik.upperBound),
opt.SetXtolAbs1(ik.epsilon),
opt.SetXtolRel(ik.epsilon),
opt.SetMinObjective(nloptMinFunc),
opt.SetMaxEval(nloptStepsPerIter),
)
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
}
// 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 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
}