-
Notifications
You must be signed in to change notification settings - Fork 110
/
frame.go
447 lines (389 loc) · 14.6 KB
/
frame.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Package referenceframe defines the api and does the math of translating between reference frames
// Useful for if you have a camera, connected to a gripper, connected to an arm,
// and need to translate the camera reference frame to the arm reference frame,
// if you've found something in the camera, and want to move the gripper + arm to get it.
package referenceframe
import (
"encoding/json"
"fmt"
"math"
"math/rand"
"strings"
"github.com/golang/geo/r3"
"github.com/pkg/errors"
"go.uber.org/multierr"
pb "go.viam.com/api/component/arm/v1"
spatial "go.viam.com/rdk/spatialmath"
"go.viam.com/rdk/utils"
)
// OOBErrString is a string that all OOB errors should contain, so that they can be checked for distinct from other Transform errors.
const OOBErrString = "input out of bounds"
// Limit represents the limits of motion for a referenceframe.
type Limit struct {
Min float64
Max float64
}
// RestrictedRandomFrameInputs will produce a list of valid, in-bounds inputs for the frame.
// The range of selection is restricted to `restrictionPercent` percent of the limits, and the
// selection frame is centered at reference.
func RestrictedRandomFrameInputs(m Frame, rSeed *rand.Rand, restrictionPercent float64, reference []Input) ([]Input, error) {
if rSeed == nil {
//nolint:gosec
rSeed = rand.New(rand.NewSource(1))
}
dof := m.DoF()
if len(reference) != len(dof) {
return nil, NewIncorrectInputLengthError(len(reference), len(dof))
}
pos := make([]Input, 0, len(dof))
for i, limit := range dof {
l, u := limit.Min, limit.Max
// Default to [-999,999] as range if limits are infinite
if l == math.Inf(-1) {
l = -999
}
if u == math.Inf(1) {
u = 999
}
frameSpan := u - l
minVal := math.Max(l, reference[i].Value-restrictionPercent*frameSpan/2)
maxVal := math.Min(u, reference[i].Value+restrictionPercent*frameSpan/2)
samplingSpan := maxVal - minVal
pos = append(pos, Input{samplingSpan*rSeed.Float64() + minVal})
}
return pos, nil
}
// RandomFrameInputs will produce a list of valid, in-bounds inputs for the referenceframe.
func RandomFrameInputs(m Frame, rSeed *rand.Rand) []Input {
if rSeed == nil {
//nolint:gosec
rSeed = rand.New(rand.NewSource(1))
}
dof := m.DoF()
pos := make([]Input, 0, len(dof))
for _, lim := range dof {
l, u := lim.Min, lim.Max
// Default to [-999,999] as range if limits are infinite
if l == math.Inf(-1) {
l = -999
}
if u == math.Inf(1) {
u = 999
}
pos = append(pos, Input{rSeed.Float64()*(u-l) + l})
}
return pos
}
// Limited represents anything that has Limits.
type Limited interface {
// DoF will return a slice with length equal to the number of degrees of freedom.
// Each element describes the min and max movement limit of that degree of freedom.
// For robot parts that don't move, it returns an empty slice.
DoF() []Limit
}
// Frame represents a reference frame, e.g. an arm, a joint, a gripper, a board, etc.
type Frame interface {
Limited
// Name returns the name of the referenceframe.
Name() string
// Transform is the pose (rotation and translation) that goes FROM current frame TO parent's referenceframe.
Transform([]Input) (spatial.Pose, error)
// Interpolate interpolates the given amount between the two sets of inputs.
Interpolate([]Input, []Input, float64) ([]Input, error)
// Geometries returns a map between names and geometries for the reference frame and any intermediate frames that
// may be defined for it, e.g. links in an arm. If a frame does not have a geometry it will not be added into the map
Geometries([]Input) (*GeometriesInFrame, error)
// InputFromProtobuf does there correct thing for this frame to convert protobuf units (degrees/mm) to input units (radians/mm)
InputFromProtobuf(*pb.JointPositions) []Input
// ProtobufFromInput does there correct thing for this frame to convert input units (radians/mm) to protobuf units (degrees/mm)
ProtobufFromInput([]Input) *pb.JointPositions
json.Marshaler
}
// baseFrame contains all the data and methods common to all frames, notably it does not implement the Frame interface itself.
type baseFrame struct {
name string
limits []Limit
}
// Name returns the name of the referenceframe.
func (bf *baseFrame) Name() string {
return bf.name
}
// DoF will return a slice with length equal to the number of joints/degrees of freedom.
func (bf *baseFrame) DoF() []Limit {
return bf.limits
}
// Interpolate interpolates the given amount between the two sets of inputs.
func (bf *baseFrame) Interpolate(from, to []Input, by float64) ([]Input, error) {
err := bf.validInputs(from)
if err != nil {
return nil, err
}
err = bf.validInputs(to)
if err != nil {
return nil, err
}
return interpolateInputs(from, to, by), nil
}
// validInputs checks whether the given array of joint positions violates any joint limits.
func (bf *baseFrame) validInputs(inputs []Input) error {
var errAll error
if len(inputs) != len(bf.limits) {
return NewIncorrectInputLengthError(len(inputs), len(bf.limits))
}
for i := 0; i < len(bf.limits); i++ {
if inputs[i].Value < bf.limits[i].Min || inputs[i].Value > bf.limits[i].Max {
lim := []float64{bf.limits[i].Max, bf.limits[i].Min}
multierr.AppendInto(&errAll, fmt.Errorf("%s %s %s, %s %.5f %s %.5f", "joint", fmt.Sprint(i),
OOBErrString, "input", inputs[i].Value, "needs to be within range", lim))
}
}
return errAll
}
// a static Frame is a simple corrdinate system that encodes a fixed translation and rotation
// from the current Frame to the parent referenceframe.
type staticFrame struct {
*baseFrame
transform spatial.Pose
geometry spatial.Geometry
}
// a tailGeometryStaticFrame is a static frame whose geometry is placed at the end of the frame's transform, rather than at the beginning.
type tailGeometryStaticFrame struct {
*staticFrame
}
func (sf *tailGeometryStaticFrame) Geometries(input []Input) (*GeometriesInFrame, error) {
if sf.geometry == nil {
return NewGeometriesInFrame(sf.Name(), nil), nil
}
if len(input) != 0 {
return nil, NewIncorrectInputLengthError(len(input), 0)
}
newGeom := sf.geometry.Transform(sf.transform)
if newGeom.Label() == "" {
newGeom.SetLabel(sf.name)
}
// Create the new geometry at a pose of `transform` from the frame
return NewGeometriesInFrame(sf.name, []spatial.Geometry{newGeom}), nil
}
// namedFrame is used to change the name of a frame.
type namedFrame struct {
Frame
name string
}
// Name returns the name of the namedFrame.
func (nf *namedFrame) Name() string {
return nf.name
}
func (nf *namedFrame) Geometries(inputs []Input) (*GeometriesInFrame, error) {
gif, err := nf.Frame.Geometries(inputs)
if err != nil {
return nil, err
}
return NewGeometriesInFrame(nf.name, gif.geometries), nil
}
// NewNamedFrame will return a frame which has a new name but otherwise passes through all functions of the original frame.
func NewNamedFrame(frame Frame, name string) Frame {
return &namedFrame{Frame: frame, name: name}
}
// NewStaticFrame creates a frame given a pose relative to its parent. The pose is fixed for all time.
// Pose is not allowed to be nil.
func NewStaticFrame(name string, pose spatial.Pose) (Frame, error) {
if pose == nil {
return nil, errors.New("pose is not allowed to be nil")
}
return &staticFrame{&baseFrame{name, []Limit{}}, pose, nil}, nil
}
// NewZeroStaticFrame creates a frame with no translation or orientation changes.
func NewZeroStaticFrame(name string) Frame {
return &staticFrame{&baseFrame{name, []Limit{}}, spatial.NewZeroPose(), nil}
}
// NewStaticFrameWithGeometry creates a frame given a pose relative to its parent. The pose is fixed for all time.
// It also has an associated geometry representing the space that it occupies in 3D space. Pose is not allowed to be nil.
func NewStaticFrameWithGeometry(name string, pose spatial.Pose, geometry spatial.Geometry) (Frame, error) {
if pose == nil {
return nil, errors.New("pose is not allowed to be nil")
}
return &staticFrame{&baseFrame{name, []Limit{}}, pose, geometry}, nil
}
// Transform returns the pose associated with this static referenceframe.
func (sf *staticFrame) Transform(input []Input) (spatial.Pose, error) {
if len(input) != 0 {
return nil, NewIncorrectInputLengthError(len(input), 0)
}
return sf.transform, nil
}
// InputFromProtobuf converts pb.JointPosition to inputs.
func (sf *staticFrame) InputFromProtobuf(jp *pb.JointPositions) []Input {
return []Input{}
}
// ProtobufFromInput converts inputs to pb.JointPosition.
func (sf *staticFrame) ProtobufFromInput(input []Input) *pb.JointPositions {
return &pb.JointPositions{}
}
// Geometries returns an object representing the 3D space associeted with the staticFrame.
func (sf *staticFrame) Geometries(input []Input) (*GeometriesInFrame, error) {
if sf.geometry == nil {
return NewGeometriesInFrame(sf.Name(), nil), nil
}
if len(input) != 0 {
return nil, NewIncorrectInputLengthError(len(input), 0)
}
newGeom := sf.geometry.Transform(spatial.NewZeroPose())
if newGeom.Label() == "" {
newGeom.SetLabel(sf.name)
}
return NewGeometriesInFrame(sf.name, []spatial.Geometry{newGeom}), nil
}
func (sf staticFrame) MarshalJSON() ([]byte, error) {
temp := LinkConfig{
ID: sf.name,
Translation: sf.transform.Point(),
}
orientationConfig, err := spatial.NewOrientationConfig(sf.transform.Orientation())
if err != nil {
return nil, err
}
temp.Orientation = orientationConfig
if sf.geometry != nil {
temp.Geometry, err = spatial.NewGeometryConfig(sf.geometry)
if err != nil {
return nil, err
}
}
return json.Marshal(temp)
}
// a prismatic Frame is a frame that can translate without rotation in any/all of the X, Y, and Z directions.
type translationalFrame struct {
*baseFrame
transAxis r3.Vector
geometry spatial.Geometry
}
// NewTranslationalFrame creates a frame given a name and the axis in which to translate.
func NewTranslationalFrame(name string, axis r3.Vector, limit Limit) (Frame, error) {
return NewTranslationalFrameWithGeometry(name, axis, limit, nil)
}
// NewTranslationalFrameWithGeometry creates a frame given a given a name and the axis in which to translate.
// It also has an associated geometry representing the space that it occupies in 3D space. Pose is not allowed to be nil.
func NewTranslationalFrameWithGeometry(name string, axis r3.Vector, limit Limit, geometry spatial.Geometry) (Frame, error) {
if spatial.R3VectorAlmostEqual(r3.Vector{}, axis, 1e-8) {
return nil, errors.New("cannot use zero vector as translation axis")
}
return &translationalFrame{
baseFrame: &baseFrame{name: name, limits: []Limit{limit}},
transAxis: axis.Normalize(),
geometry: geometry,
}, nil
}
// Transform returns a pose translated by the amount specified in the inputs.
func (pf *translationalFrame) Transform(input []Input) (spatial.Pose, error) {
err := pf.validInputs(input)
// We allow out-of-bounds calculations, but will return a non-nil error
if err != nil && !strings.Contains(err.Error(), OOBErrString) {
return nil, err
}
return spatial.NewPoseFromPoint(pf.transAxis.Mul(input[0].Value)), err
}
// InputFromProtobuf converts pb.JointPosition to inputs.
func (pf *translationalFrame) InputFromProtobuf(jp *pb.JointPositions) []Input {
n := make([]Input, len(jp.Values))
for idx, d := range jp.Values {
n[idx] = Input{d}
}
return n
}
// ProtobufFromInput converts inputs to pb.JointPosition.
func (pf *translationalFrame) ProtobufFromInput(input []Input) *pb.JointPositions {
n := make([]float64, len(input))
for idx, a := range input {
n[idx] = a.Value
}
return &pb.JointPositions{Values: n}
}
// Geometries returns an object representing the 3D space associeted with the translationalFrame.
func (pf *translationalFrame) Geometries(input []Input) (*GeometriesInFrame, error) {
if pf.geometry == nil {
return NewGeometriesInFrame(pf.Name(), nil), nil
}
pose, err := pf.Transform(input)
if pose == nil || (err != nil && !strings.Contains(err.Error(), OOBErrString)) {
return nil, err
}
return NewGeometriesInFrame(pf.name, []spatial.Geometry{pf.geometry.Transform(pose)}), err
}
func (pf translationalFrame) MarshalJSON() ([]byte, error) {
if len(pf.limits) > 1 {
return nil, ErrMarshalingHighDOFFrame
}
temp := JointConfig{
ID: pf.name,
Type: PrismaticJoint,
Axis: spatial.AxisConfig{pf.transAxis.X, pf.transAxis.Y, pf.transAxis.Z},
Max: pf.limits[0].Max,
Min: pf.limits[0].Min,
}
if pf.geometry != nil {
var err error
temp.Geometry, err = spatial.NewGeometryConfig(pf.geometry)
if err != nil {
return nil, err
}
}
return json.Marshal(temp)
}
type rotationalFrame struct {
*baseFrame
rotAxis r3.Vector
}
// NewRotationalFrame creates a new rotationalFrame struct.
// A standard revolute joint will have 1 DoF.
func NewRotationalFrame(name string, axis spatial.R4AA, limit Limit) (Frame, error) {
axis.Normalize()
return &rotationalFrame{
baseFrame: &baseFrame{name: name, limits: []Limit{limit}},
rotAxis: r3.Vector{axis.RX, axis.RY, axis.RZ},
}, nil
}
// Transform returns the Pose representing the frame's 6DoF motion in space. Requires a slice
// of inputs that has length equal to the degrees of freedom of the referenceframe.
func (rf *rotationalFrame) Transform(input []Input) (spatial.Pose, error) {
err := rf.validInputs(input)
// We allow out-of-bounds calculations, but will return a non-nil error
if err != nil && !strings.Contains(err.Error(), OOBErrString) {
return nil, err
}
// Create a copy of the r4aa for thread safety
return spatial.NewPoseFromOrientation(&spatial.R4AA{input[0].Value, rf.rotAxis.X, rf.rotAxis.Y, rf.rotAxis.Z}), err
}
// InputFromProtobuf converts pb.JointPosition to inputs.
func (rf *rotationalFrame) InputFromProtobuf(jp *pb.JointPositions) []Input {
n := make([]Input, len(jp.Values))
for idx, d := range jp.Values {
n[idx] = Input{utils.DegToRad(d)}
}
return n
}
// ProtobufFromInput converts inputs to pb.JointPosition.
func (rf *rotationalFrame) ProtobufFromInput(input []Input) *pb.JointPositions {
n := make([]float64, len(input))
for idx, a := range input {
n[idx] = utils.RadToDeg(a.Value)
}
return &pb.JointPositions{Values: n}
}
// Geometries will always return (nil, nil) for rotationalFrames, as not allowing rotationalFrames to occupy geometries is a
// design choice made for simplicity. staticFrame and translationalFrame should be used instead.
func (rf *rotationalFrame) Geometries(input []Input) (*GeometriesInFrame, error) {
return NewGeometriesInFrame(rf.Name(), nil), nil
}
func (rf rotationalFrame) MarshalJSON() ([]byte, error) {
if len(rf.limits) > 1 {
return nil, ErrMarshalingHighDOFFrame
}
temp := JointConfig{
ID: rf.name,
Type: RevoluteJoint,
Axis: spatial.AxisConfig{rf.rotAxis.X, rf.rotAxis.Y, rf.rotAxis.Z},
Max: utils.RadToDeg(rf.limits[0].Max),
Min: utils.RadToDeg(rf.limits[0].Min),
}
return json.Marshal(temp)
}