forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstraint_handler.go
355 lines (316 loc) · 12.7 KB
/
constraint_handler.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
//go:build !no_cgo
package motionplan
import (
"fmt"
"go.viam.com/rdk/motionplan/ik"
"go.viam.com/rdk/referenceframe"
)
var defaultMinStepCount = 2
// ConstraintHandler is a convenient wrapper for constraint handling which is likely to be common among most motion
// planners. Including a constraint handler as an anonymous struct member allows reuse.
type ConstraintHandler struct {
segmentConstraints map[string]SegmentConstraint
segmentFSConstraints map[string]SegmentFSConstraint
stateConstraints map[string]StateConstraint
stateFSConstraints map[string]StateFSConstraint
}
// CheckStateConstraints will check a given input against all state constraints.
// Return values are:
// -- a bool representing whether all constraints passed
// -- if failing, a string naming the failed constraint.
func (c *ConstraintHandler) CheckStateConstraints(state *ik.State) (bool, string) {
for name, cFunc := range c.stateConstraints {
pass := cFunc(state)
if !pass {
return false, name
}
}
return true, ""
}
// CheckStateFSConstraints will check a given input against all FS state constraints.
// Return values are:
// -- a bool representing whether all constraints passed
// -- if failing, a string naming the failed constraint.
func (c *ConstraintHandler) CheckStateFSConstraints(state *ik.StateFS) (bool, string) {
for name, cFunc := range c.stateFSConstraints {
pass := cFunc(state)
if !pass {
return false, name
}
}
return true, ""
}
// CheckSegmentConstraints will check a given input against all segment constraints.
// Return values are:
// -- a bool representing whether all constraints passed
// -- if failing, a string naming the failed constraint.
func (c *ConstraintHandler) CheckSegmentConstraints(segment *ik.Segment) (bool, string) {
for name, cFunc := range c.segmentConstraints {
pass := cFunc(segment)
if !pass {
return false, name
}
}
return true, ""
}
// CheckSegmentFSConstraints will check a given input against all FS segment constraints.
// Return values are:
// -- a bool representing whether all constraints passed
// -- if failing, a string naming the failed constraint.
func (c *ConstraintHandler) CheckSegmentFSConstraints(segment *ik.SegmentFS) (bool, string) {
for name, cFunc := range c.segmentFSConstraints {
pass := cFunc(segment)
if !pass {
return false, name
}
}
return true, ""
}
// CheckStateConstraintsAcrossSegment will interpolate the given input from the StartInput to the EndInput, and ensure that all intermediate
// states as well as both endpoints satisfy all state constraints. If all constraints are satisfied, then this will return `true, nil`.
// If any constraints fail, this will return false, and an Segment representing the valid portion of the segment, if any. If no
// part of the segment is valid, then `false, nil` is returned.
func (c *ConstraintHandler) CheckStateConstraintsAcrossSegment(ci *ik.Segment, resolution float64) (bool, *ik.Segment) {
interpolatedConfigurations, err := interpolateSegment(ci, resolution)
if err != nil {
return false, nil
}
var lastGood []referenceframe.Input
for i, interpConfig := range interpolatedConfigurations {
interpC := &ik.State{Frame: ci.Frame, Configuration: interpConfig}
if resolveStatesToPositions(interpC) != nil {
return false, nil
}
pass, _ := c.CheckStateConstraints(interpC)
if !pass {
if i == 0 {
// fail on start pos
return false, nil
}
return false, &ik.Segment{StartConfiguration: ci.StartConfiguration, EndConfiguration: lastGood, Frame: ci.Frame}
}
lastGood = interpC.Configuration
}
return true, nil
}
// interpolateSegment is a helper function which produces a list of intermediate inputs, between the start and end
// configuration of a segment at a given resolution value.
func interpolateSegment(ci *ik.Segment, resolution float64) ([][]referenceframe.Input, error) {
// ensure we have cartesian positions
if err := resolveSegmentsToPositions(ci); err != nil {
return nil, err
}
steps := CalculateStepCount(ci.StartPosition, ci.EndPosition, resolution)
if steps < defaultMinStepCount {
// Minimum step count ensures we are not missing anything
steps = defaultMinStepCount
}
var interpolatedConfigurations [][]referenceframe.Input
for i := 0; i <= steps; i++ {
interp := float64(i) / float64(steps)
interpConfig, err := ci.Frame.Interpolate(ci.StartConfiguration, ci.EndConfiguration, interp)
if err != nil {
return nil, err
}
interpolatedConfigurations = append(interpolatedConfigurations, interpConfig)
}
return interpolatedConfigurations, nil
}
// interpolateSegmentFS is a helper function which produces a list of intermediate inputs, between the start and end
// configuration of a segment at a given resolution value.
func interpolateSegmentFS(ci *ik.SegmentFS, resolution float64) ([]referenceframe.FrameSystemInputs, error) {
// Find the frame with the most steps by calculating steps for each frame
maxSteps := defaultMinStepCount
for frameName, startConfig := range ci.StartConfiguration {
if len(startConfig) == 0 {
// No need to interpolate 0dof frames
continue
}
endConfig, exists := ci.EndConfiguration[frameName]
if !exists {
return nil, fmt.Errorf("frame %s exists in start config but not in end config", frameName)
}
// Get frame from FrameSystem
frame := ci.FS.Frame(frameName)
if frame == nil {
return nil, fmt.Errorf("frame %s exists in start config but not in framesystem", frameName)
}
// Calculate positions for this frame's start and end configs
startPos, err := frame.Transform(startConfig)
if err != nil {
return nil, err
}
endPos, err := frame.Transform(endConfig)
if err != nil {
return nil, err
}
// Calculate steps needed for this frame
steps := CalculateStepCount(startPos, endPos, resolution)
if steps > maxSteps {
maxSteps = steps
}
}
// Create interpolated configurations for all frames
var interpolatedConfigurations []referenceframe.FrameSystemInputs
for i := 0; i <= maxSteps; i++ {
interp := float64(i) / float64(maxSteps)
frameConfigs := make(referenceframe.FrameSystemInputs)
// Interpolate each frame's configuration
for frameName, startConfig := range ci.StartConfiguration {
endConfig := ci.EndConfiguration[frameName]
frame := ci.FS.Frame(frameName)
interpConfig, err := frame.Interpolate(startConfig, endConfig, interp)
if err != nil {
return nil, err
}
frameConfigs[frameName] = interpConfig
}
interpolatedConfigurations = append(interpolatedConfigurations, frameConfigs)
}
return interpolatedConfigurations, nil
}
// CheckSegmentAndStateValidity will check an segment input and confirm that it 1) meets all segment constraints, and 2) meets all
// state constraints across the segment at some resolution. If it fails an intermediate state, it will return the shortest valid segment,
// provided that segment also meets segment constraints.
func (c *ConstraintHandler) CheckSegmentAndStateValidity(segment *ik.Segment, resolution float64) (bool, *ik.Segment) {
valid, subSegment := c.CheckStateConstraintsAcrossSegment(segment, resolution)
if !valid {
if subSegment != nil {
subSegmentValid, _ := c.CheckSegmentConstraints(subSegment)
if subSegmentValid {
return false, subSegment
}
}
return false, nil
}
// all states are valid
valid, _ = c.CheckSegmentConstraints(segment)
return valid, nil
}
// AddStateConstraint will add or overwrite a constraint function with a given name. A constraint function should return true
// if the given position satisfies the constraint.
func (c *ConstraintHandler) AddStateConstraint(name string, cons StateConstraint) {
if c.stateConstraints == nil {
c.stateConstraints = map[string]StateConstraint{}
}
name = name + "_" + fmt.Sprintf("%p", cons)
c.stateConstraints[name] = cons
}
// RemoveStateConstraint will remove the given constraint.
func (c *ConstraintHandler) RemoveStateConstraint(name string) {
delete(c.stateConstraints, name)
}
// StateConstraints will list all state constraints by name.
func (c *ConstraintHandler) StateConstraints() []string {
names := make([]string, 0, len(c.stateConstraints))
for name := range c.stateConstraints {
names = append(names, name)
}
return names
}
// AddSegmentConstraint will add or overwrite a constraint function with a given name. A constraint function should return true
// if the given position satisfies the constraint.
func (c *ConstraintHandler) AddSegmentConstraint(name string, cons SegmentConstraint) {
if c.segmentConstraints == nil {
c.segmentConstraints = map[string]SegmentConstraint{}
}
// Add function address to name to prevent collisions
name = name + "_" + fmt.Sprintf("%p", cons)
c.segmentConstraints[name] = cons
}
// RemoveSegmentConstraint will remove the given constraint.
func (c *ConstraintHandler) RemoveSegmentConstraint(name string) {
delete(c.segmentConstraints, name)
}
// SegmentConstraints will list all segment constraints by name.
func (c *ConstraintHandler) SegmentConstraints() []string {
names := make([]string, 0, len(c.segmentConstraints))
for name := range c.segmentConstraints {
names = append(names, name)
}
return names
}
// AddStateFSConstraint will add or overwrite a constraint function with a given name. A constraint function should return true
// if the given position satisfies the constraint.
func (c *ConstraintHandler) AddStateFSConstraint(name string, cons StateFSConstraint) {
if c.stateFSConstraints == nil {
c.stateFSConstraints = map[string]StateFSConstraint{}
}
name = name + "_" + fmt.Sprintf("%p", cons)
c.stateFSConstraints[name] = cons
}
// RemoveStateFSConstraint will remove the given constraint.
func (c *ConstraintHandler) RemoveStateFSConstraint(name string) {
delete(c.stateFSConstraints, name)
}
// StateFSConstraints will list all FS state constraints by name.
func (c *ConstraintHandler) StateFSConstraints() []string {
names := make([]string, 0, len(c.stateFSConstraints))
for name := range c.stateFSConstraints {
names = append(names, name)
}
return names
}
// AddSegmentFSConstraint will add or overwrite a constraint function with a given name. A constraint function should return true
// if the given position satisfies the constraint.
func (c *ConstraintHandler) AddSegmentFSConstraint(name string, cons SegmentFSConstraint) {
if c.segmentFSConstraints == nil {
c.segmentFSConstraints = map[string]SegmentFSConstraint{}
}
name = name + "_" + fmt.Sprintf("%p", cons)
c.segmentFSConstraints[name] = cons
}
// RemoveSegmentFSConstraint will remove the given constraint.
func (c *ConstraintHandler) RemoveSegmentFSConstraint(name string) {
delete(c.segmentFSConstraints, name)
}
// SegmentFSConstraints will list all FS segment constraints by name.
func (c *ConstraintHandler) SegmentFSConstraints() []string {
names := make([]string, 0, len(c.segmentFSConstraints))
for name := range c.segmentFSConstraints {
names = append(names, name)
}
return names
}
// CheckStateConstraintsAcrossSegmentFS will interpolate the given input from the StartConfiguration to the EndConfiguration, and ensure
// that all intermediate states as well as both endpoints satisfy all state constraints. If all constraints are satisfied, then this will
// return `true, nil`. If any constraints fail, this will return false, and an SegmentFS representing the valid portion of the segment,
// if any. If no part of the segment is valid, then `false, nil` is returned.
func (c *ConstraintHandler) CheckStateConstraintsAcrossSegmentFS(ci *ik.SegmentFS, resolution float64) (bool, *ik.SegmentFS) {
interpolatedConfigurations, err := interpolateSegmentFS(ci, resolution)
if err != nil {
return false, nil
}
var lastGood referenceframe.FrameSystemInputs
for i, interpConfig := range interpolatedConfigurations {
interpC := &ik.StateFS{FS: ci.FS, Configuration: interpConfig}
pass, _ := c.CheckStateFSConstraints(interpC)
if !pass {
if i == 0 {
// fail on start pos
return false, nil
}
return false, &ik.SegmentFS{StartConfiguration: ci.StartConfiguration, EndConfiguration: lastGood, FS: ci.FS}
}
lastGood = interpC.Configuration
}
return true, nil
}
// CheckSegmentAndStateValidityFS will check a segment input and confirm that it 1) meets all segment constraints, and 2) meets all
// state constraints across the segment at some resolution. If it fails an intermediate state, it will return the shortest valid segment,
// provided that segment also meets segment constraints.
func (c *ConstraintHandler) CheckSegmentAndStateValidityFS(segment *ik.SegmentFS, resolution float64) (bool, *ik.SegmentFS) {
valid, subSegment := c.CheckStateConstraintsAcrossSegmentFS(segment, resolution)
if !valid {
if subSegment != nil {
subSegmentValid, _ := c.CheckSegmentFSConstraints(subSegment)
if subSegmentValid {
return false, subSegment
}
}
return false, nil
}
// all states are valid
valid, _ = c.CheckSegmentFSConstraints(segment)
return valid, nil
}