-
Notifications
You must be signed in to change notification settings - Fork 110
/
collectors.go
98 lines (86 loc) · 2.73 KB
/
collectors.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
//go:build !no_cgo
package arm
import (
"context"
"errors"
v1 "go.viam.com/api/common/v1"
pb "go.viam.com/api/component/arm/v1"
"google.golang.org/protobuf/types/known/anypb"
"go.viam.com/rdk/data"
)
type method int64
const (
endPosition method = iota
jointPositions
)
func (m method) String() string {
switch m {
case endPosition:
return "EndPosition"
case jointPositions:
return "JointPositions"
}
return "Unknown"
}
// newEndPositionCollector returns a collector to register an end position method. If one is already registered
// with the same MethodMetadata it will panic.
func newEndPositionCollector(resource interface{}, params data.CollectorParams) (data.Collector, error) {
arm, err := assertArm(resource)
if err != nil {
return nil, err
}
cFunc := data.CaptureFunc(func(ctx context.Context, _ map[string]*anypb.Any) (interface{}, error) {
v, err := arm.EndPosition(ctx, data.FromDMExtraMap)
if err != nil {
// A modular filter component can be created to filter the readings from a component. The error ErrNoCaptureToStore
// is used in the datamanager to exclude readings from being captured and stored.
if errors.Is(err, data.ErrNoCaptureToStore) {
return nil, err
}
return nil, data.FailedToReadErr(params.ComponentName, endPosition.String(), err)
}
o := v.Orientation().OrientationVectorDegrees()
return pb.GetEndPositionResponse{
Pose: &v1.Pose{
X: v.Point().X,
Y: v.Point().Y,
Z: v.Point().Z,
OX: o.OX,
OY: o.OY,
OZ: o.OZ,
Theta: o.Theta,
},
}, nil
})
return data.NewCollector(cFunc, params)
}
// newJointPositionsCollector returns a collector to register a joint positions method. If one is already registered
// with the same MethodMetadata it will panic.
func newJointPositionsCollector(resource interface{}, params data.CollectorParams) (data.Collector, error) {
arm, err := assertArm(resource)
if err != nil {
return nil, err
}
cFunc := data.CaptureFunc(func(ctx context.Context, _ map[string]*anypb.Any) (interface{}, error) {
v, err := arm.JointPositions(ctx, data.FromDMExtraMap)
if err != nil {
// A modular filter component can be created to filter the readings from a component. The error ErrNoCaptureToStore
// is used in the datamanager to exclude readings from being captured and stored.
if errors.Is(err, data.ErrNoCaptureToStore) {
return nil, err
}
return nil, data.FailedToReadErr(params.ComponentName, jointPositions.String(), err)
}
return pb.GetJointPositionsResponse{
Positions: v,
}, nil
})
return data.NewCollector(cFunc, params)
}
func assertArm(resource interface{}) (Arm, error) {
arm, ok := resource.(Arm)
if !ok {
return nil, data.InvalidInterfaceErr(API)
}
return arm, nil
}