-
Notifications
You must be signed in to change notification settings - Fork 110
/
frame.go
498 lines (431 loc) · 16.8 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// 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/rdk/proto/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
}
func limitsAlmostEqual(a, b []Limit) bool {
if len(a) != len(b) {
return false
}
const epsilon = 1e-5
for idx, x := range a {
if !utils.Float64AlmostEqual(x.Min, b[idx].Min, epsilon) ||
!utils.Float64AlmostEqual(x.Max, b[idx].Max, epsilon) {
return false
}
}
return true
}
// RestrictedRandomFrameInputs will produce a list of valid, in-bounds inputs for the frame, restricting the range to
// `lim` percent of the limits.
func RestrictedRandomFrameInputs(m Frame, rSeed *rand.Rand, lim float64) []Input {
if rSeed == nil {
//nolint:gosec
rSeed = rand.New(rand.NewSource(1))
}
dof := m.DoF()
pos := make([]Input, 0, len(dof))
for _, 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
}
span := u - l
pos = append(pos, Input{lim*span*rSeed.Float64() + l + (span * (1 - lim) / 2)})
}
return pos
}
// 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
}
// Frame represents a reference frame, e.g. an arm, a joint, a gripper, a board, etc.
type Frame interface {
// 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)
// 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 geometryCreator it will not be added into the map
Geometries([]Input) (*GeometriesInFrame, error)
// DoF will return a slice with length equal to the number of joints/degrees of freedom.
// Each element describes the min and max movement limit of that joint/degree of freedom.
// For robot parts that don't move, it returns an empty slice.
DoF() []Limit
// AlmostEquals returns if the otherFrame is close to the referenceframe.
// differences should just be things like floating point inprecision
AlmostEquals(otherFrame Frame) bool
// 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
}
// 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 {
name string
transform spatial.Pose
geometryCreator spatial.GeometryCreator
}
// 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{name, pose, nil}, nil
}
// NewZeroStaticFrame creates a frame with no translation or orientation changes.
func NewZeroStaticFrame(name string) Frame {
return &staticFrame{name, 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 geometryCreator representing the space that it occupies in 3D space. Pose is not allowed to be nil.
func NewStaticFrameWithGeometry(name string, pose spatial.Pose, geometryCreator spatial.GeometryCreator) (Frame, error) {
if pose == nil {
return nil, errors.New("pose is not allowed to be nil")
}
return &staticFrame{name, pose, geometryCreator}, nil
}
// NewStaticFrameFromFrame creates a frame given a pose relative to its parent. The pose is fixed for all time.
// It inherits its name and geometryCreator properties from the specified Frame. Pose is not allowed to be nil.
func NewStaticFrameFromFrame(frame Frame, pose spatial.Pose) (Frame, error) {
if pose == nil {
return nil, errors.New("pose is not allowed to be nil")
}
if tf, ok := frame.(*translationalFrame); ok {
return &staticFrame{tf.Name(), pose, tf.geometryCreator}, nil
}
if tf, ok := frame.(*staticFrame); ok {
return &staticFrame{tf.Name(), pose, tf.geometryCreator}, nil
}
if tf, ok := frame.(*mobile2DFrame); ok {
return &staticFrame{tf.Name(), pose, tf.geometryCreator}, nil
}
return &staticFrame{frame.Name(), pose, nil}, nil
}
// FrameFromPoint creates a new Frame from a 3D point.
func FrameFromPoint(name string, point r3.Vector) (Frame, error) {
return NewStaticFrame(name, spatial.NewPoseFromPoint(point))
}
// Name is the name of the referenceframe.
func (sf *staticFrame) Name() string {
return sf.name
}
// Transform returns the pose associated with this static referenceframe.
func (sf *staticFrame) Transform(input []Input) (spatial.Pose, error) {
if len(input) != 0 {
return nil, fmt.Errorf("given input length %q does not match frame DoF 0", len(input))
}
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.geometryCreator == nil {
return nil, fmt.Errorf("frame of type %T has nil geometryCreator", sf)
}
if len(input) != 0 {
return nil, fmt.Errorf("given input length %q does not match frame DoF 0", len(input))
}
m := make(map[string]spatial.Geometry)
m[sf.Name()] = sf.geometryCreator.NewGeometry(spatial.NewZeroPose())
return NewGeometriesInFrame(sf.name, m), nil
}
// DoF are the degrees of freedom of the transform. In the staticFrame, it is always 0.
func (sf *staticFrame) DoF() []Limit {
return []Limit{}
}
func (sf *staticFrame) MarshalJSON() ([]byte, error) {
transform, err := spatial.PoseMap(sf.transform)
if err != nil {
return nil, err
}
m := FrameMapConfig{
"type": "static",
"name": sf.name,
"transform": transform,
}
return json.Marshal(m)
}
func (sf *staticFrame) AlmostEquals(otherFrame Frame) bool {
other, ok := otherFrame.(*staticFrame)
return ok && sf.name == other.name && spatial.PoseAlmostEqual(sf.transform, other.transform)
}
// a prismatic Frame is a frame that can translate without rotation in any/all of the X, Y, and Z directions.
type translationalFrame struct {
name string
transAxis r3.Vector
limit []Limit
geometryCreator spatial.GeometryCreator
}
// 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 geometryCreator 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, geometryCreator spatial.GeometryCreator) (Frame, error) {
if spatial.R3VectorAlmostEqual(r3.Vector{}, axis, 1e-8) {
return nil, errors.New("cannot use zero vector as translation axis")
}
return &translationalFrame{name: name, transAxis: axis.Normalize(), limit: []Limit{limit}, geometryCreator: geometryCreator}, nil
}
// Name is the name of the frame.
func (pf *translationalFrame) Name() string {
return pf.name
}
// Transform returns a pose translated by the amount specified in the inputs.
func (pf *translationalFrame) Transform(input []Input) (spatial.Pose, error) {
var err error
if len(input) != 1 {
return nil, fmt.Errorf("given input length %d does not match frame DoF %d", len(input), 1)
}
// We allow out-of-bounds calculations, but will return a non-nil error
if input[0].Value < pf.limit[0].Min || input[0].Value > pf.limit[0].Max {
err = fmt.Errorf("%.5f %s %v", input[0].Value, OOBErrString, pf.limit[0])
}
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.geometryCreator == nil {
return nil, fmt.Errorf("frame of type %T has nil geometryCreator", pf)
}
pose, err := pf.Transform(input)
if pose == nil || (err != nil && !strings.Contains(err.Error(), OOBErrString)) {
return nil, err
}
m := make(map[string]spatial.Geometry)
m[pf.Name()] = pf.geometryCreator.NewGeometry(pose)
return NewGeometriesInFrame(pf.name, m), err
}
// DoF are the degrees of freedom of the transform.
func (pf *translationalFrame) DoF() []Limit {
return pf.limit
}
func (pf *translationalFrame) MarshalJSON() ([]byte, error) {
m := FrameMapConfig{
"type": "translational",
"name": pf.name,
"transAxis": pf.transAxis,
"limit": pf.limit,
}
return json.Marshal(m)
}
func (pf *translationalFrame) AlmostEquals(otherFrame Frame) bool {
other, ok := otherFrame.(*translationalFrame)
return ok && pf.name == other.name &&
spatial.R3VectorAlmostEqual(pf.transAxis, other.transAxis, 1e-8) &&
limitsAlmostEqual(pf.DoF(), other.DoF())
}
type rotationalFrame struct {
name string
rotAxis r3.Vector
limit []Limit
}
// 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{
name: name,
rotAxis: r3.Vector{axis.RX, axis.RY, axis.RZ},
limit: []Limit{limit},
}, 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) {
var err error
if len(input) != 1 {
return nil, fmt.Errorf("given input length %d does not match frame DoF 1", len(input))
}
// We allow out-of-bounds calculations, but will return a non-nil error
if input[0].Value < rf.limit[0].Min || input[0].Value > rf.limit[0].Max {
err = fmt.Errorf("%.5f %s %.5f", input[0].Value, OOBErrString, rf.limit[0])
}
// Create a copy of the r4aa for thread safety
return spatial.NewPoseFromOrientation(r3.Vector{0, 0, 0}, &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 nil, fmt.Errorf("s not implemented for type %T", rf)
}
// DoF returns the number of degrees of freedom that a joint has. This would be 1 for a standard revolute joint.
func (rf *rotationalFrame) DoF() []Limit {
return rf.limit
}
// Name returns the name of the referenceframe.
func (rf *rotationalFrame) Name() string {
return rf.name
}
func (rf *rotationalFrame) MarshalJSON() ([]byte, error) {
m := FrameMapConfig{
"type": "rotational",
"name": rf.name,
"rotAxis": rf.rotAxis,
"limit": rf.limit,
}
return json.Marshal(m)
}
func (rf *rotationalFrame) AlmostEquals(otherFrame Frame) bool {
other, ok := otherFrame.(*rotationalFrame)
return ok && rf.name == other.name &&
spatial.R3VectorAlmostEqual(rf.rotAxis, other.rotAxis, 1e-8) &&
limitsAlmostEqual(rf.DoF(), other.DoF())
}
type mobile2DFrame struct {
name string
limit []Limit
geometryCreator spatial.GeometryCreator
}
// NewMobile2DFrame instantiates a frame that can translate in the x and y dimensions and will always remain on the plane Z=0
// This frame will have a name, limits (representing the bounds the frame is allowed to translate within) and a geometryCreator
// defined by the arguments passed into this function.
func NewMobile2DFrame(name string, limit []Limit, geometryCreator spatial.GeometryCreator) (Frame, error) {
if len(limit) != 2 {
return nil, fmt.Errorf("cannot create a %d dof mobile frame, only support 2 dimensions currently", len(limit))
}
return &mobile2DFrame{name: name, limit: limit, geometryCreator: geometryCreator}, nil
}
func (mf *mobile2DFrame) Name() string {
return mf.name
}
func (mf *mobile2DFrame) Transform(input []Input) (spatial.Pose, error) {
var errAll error
if len(input) != len(mf.limit) {
return nil, fmt.Errorf("given input length %d does not match frame DoF %d", len(input), len(mf.limit))
}
// We allow out-of-bounds calculations, but will return a non-nil error
for i, lim := range mf.limit {
if input[i].Value < lim.Min || input[i].Value > lim.Max {
multierr.AppendInto(&errAll, fmt.Errorf("%.5f %s %.5f", input[i].Value, OOBErrString, mf.limit[i]))
}
}
return spatial.NewPoseFromPoint(r3.Vector{input[0].Value, input[1].Value, 0}), errAll
}
// InputFromProtobuf converts pb.JointPosition to inputs.
func (mf *mobile2DFrame) 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 (mf *mobile2DFrame) ProtobufFromInput(input []Input) *pb.JointPositions {
n := make([]float64, len(input))
for idx, a := range input {
n[idx] = a.Value
}
return &pb.JointPositions{Values: n}
}
func (mf *mobile2DFrame) Geometries(input []Input) (*GeometriesInFrame, error) {
if mf.geometryCreator == nil {
return nil, fmt.Errorf("frame of type %T has nil geometryCreator", mf)
}
pose, err := mf.Transform(input)
if pose == nil || (err != nil && !strings.Contains(err.Error(), OOBErrString)) {
return nil, err
}
m := make(map[string]spatial.Geometry)
m[mf.Name()] = mf.geometryCreator.NewGeometry(pose)
return NewGeometriesInFrame(mf.name, m), err
}
func (mf *mobile2DFrame) DoF() []Limit {
return mf.limit
}
func (mf *mobile2DFrame) MarshalJSON() ([]byte, error) {
return json.Marshal(FrameMapConfig{
"type": "rotational",
"name": mf.name,
"limit": mf.limit,
})
}
func (mf *mobile2DFrame) AlmostEquals(otherFrame Frame) bool {
other, ok := otherFrame.(*rotationalFrame)
return ok && mf.name == other.name && limitsAlmostEqual(mf.DoF(), other.DoF())
}