-
Notifications
You must be signed in to change notification settings - Fork 110
/
encoder.go
231 lines (199 loc) · 7.27 KB
/
encoder.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
// Package encoder implements the encoder component
package encoder
import (
"context"
"sync"
"github.com/edaniels/golog"
pb "go.viam.com/api/component/encoder/v1"
viamutils "go.viam.com/utils"
"go.viam.com/utils/rpc"
"go.viam.com/rdk/components/generic"
"go.viam.com/rdk/data"
"go.viam.com/rdk/registry"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/robot"
"go.viam.com/rdk/subtype"
"go.viam.com/rdk/utils"
)
func init() {
registry.RegisterResourceSubtype(Subtype, registry.ResourceSubtype{
Reconfigurable: WrapWithReconfigurable,
RegisterRPCService: func(ctx context.Context, rpcServer rpc.Server, subtypeSvc subtype.Service) error {
return rpcServer.RegisterServiceServer(
ctx,
&pb.EncoderService_ServiceDesc,
NewServer(subtypeSvc),
pb.RegisterEncoderServiceHandlerFromEndpoint,
)
},
RPCServiceDesc: &pb.EncoderService_ServiceDesc,
RPCClient: func(ctx context.Context, conn rpc.ClientConn, name string, logger golog.Logger) interface{} {
return NewClientFromConn(ctx, conn, name, logger)
},
})
data.RegisterCollector(data.MethodMetadata{
Subtype: Subtype,
MethodName: ticksCount.String(),
}, newTicksCountCollector)
}
// SubtypeName is a constant that identifies the component resource subtype string "encoder".
const SubtypeName = resource.SubtypeName("encoder")
// Subtype is a constant that identifies the component resource subtype.
var Subtype = resource.NewSubtype(
resource.ResourceNamespaceRDK,
resource.ResourceTypeComponent,
SubtypeName,
)
// PositionType is an enum representing the encoders position type.
type PositionType int32
const (
// PositionTypeUNSPECIFIED is the return type for
// when the user has not specified a PositionType.
PositionTypeUNSPECIFIED PositionType = 0
// PositionTypeTICKS is the return type for relative encoders
// that report how far they've gone from a start position.
PositionTypeTICKS PositionType = 1
// PositionTypeDEGREES is the return type for absolute encoders
// that report their position in degrees along the radial axis.
PositionTypeDEGREES PositionType = 2
)
// Enum reveals the value of PositionType.
func (x PositionType) Enum() *PositionType {
p := new(PositionType)
*p = x
return p
}
// A Encoder turns a position into a signal.
type Encoder interface {
// GetPosition returns the current position in terms of ticks or degrees, and whether it is a relative or absolute position.
GetPosition(ctx context.Context, positionType *PositionType, extra map[string]interface{}) (float64, PositionType, error)
// ResetPosition sets the current position of the motor to be its new zero position.
ResetPosition(ctx context.Context, extra map[string]interface{}) error
// GetProperties returns a list of all the position types that are supported by a given encoder
GetProperties(ctx context.Context, extra map[string]interface{}) (map[Feature]bool, error)
generic.Generic
}
// Named is a helper for getting the named Encoder's typed resource name.
func Named(name string) resource.Name {
return resource.NameFromSubtype(Subtype, name)
}
var (
_ = Encoder(&reconfigurableEncoder{})
_ = resource.Reconfigurable(&reconfigurableEncoder{})
_ = viamutils.ContextCloser(&reconfigurableEncoder{})
)
// FromDependencies is a helper for getting the named encoder from a collection of
// dependencies.
func FromDependencies(deps registry.Dependencies, name string) (Encoder, error) {
return registry.ResourceFromDependencies[Encoder](deps, Named(name))
}
// NewUnimplementedInterfaceError is used when there is a failed interface check.
func NewUnimplementedInterfaceError(actual interface{}) error {
return utils.NewUnimplementedInterfaceError((*Encoder)(nil), actual)
}
// FromRobot is a helper for getting the named encoder from the given Robot.
func FromRobot(r robot.Robot, name string) (Encoder, error) {
return robot.ResourceFromRobot[Encoder](r, Named(name))
}
// NamesFromRobot is a helper for getting all encoder names from the given Robot.
func NamesFromRobot(r robot.Robot) []string {
return robot.NamesBySubtype(r, Subtype)
}
type reconfigurableEncoder struct {
mu sync.RWMutex
name resource.Name
actual Encoder
}
func (r *reconfigurableEncoder) Name() resource.Name {
return r.name
}
func (r *reconfigurableEncoder) ProxyFor() interface{} {
r.mu.RLock()
defer r.mu.RUnlock()
return r.actual
}
func (r *reconfigurableEncoder) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
r.mu.RLock()
defer r.mu.RUnlock()
return r.actual.DoCommand(ctx, cmd)
}
func (r *reconfigurableEncoder) GetPosition(
ctx context.Context,
positionType *PositionType,
extra map[string]interface{},
) (float64, PositionType, error) {
r.mu.RLock()
defer r.mu.RUnlock()
return r.actual.GetPosition(ctx, positionType, extra)
}
func (r *reconfigurableEncoder) ResetPosition(ctx context.Context, extra map[string]interface{}) error {
r.mu.RLock()
defer r.mu.RUnlock()
return r.actual.ResetPosition(ctx, extra)
}
func (r *reconfigurableEncoder) GetProperties(ctx context.Context, extra map[string]interface{}) (map[Feature]bool, error) {
r.mu.RLock()
defer r.mu.RUnlock()
return r.actual.GetProperties(ctx, extra)
}
func (r *reconfigurableEncoder) Close(ctx context.Context) error {
r.mu.RLock()
defer r.mu.RUnlock()
return viamutils.TryClose(ctx, r.actual)
}
func (r *reconfigurableEncoder) Reconfigure(ctx context.Context, newEncoder resource.Reconfigurable) error {
r.mu.RLock()
defer r.mu.RUnlock()
return r.reconfigure(ctx, newEncoder)
}
func (r *reconfigurableEncoder) reconfigure(ctx context.Context, newEncoder resource.Reconfigurable) error {
actual, ok := newEncoder.(*reconfigurableEncoder)
if !ok {
return utils.NewUnexpectedTypeError(r, newEncoder)
}
if err := viamutils.TryClose(ctx, r.actual); err != nil {
golog.Global().Errorw("error closing old", "error", err)
}
r.actual = actual.actual
return nil
}
// WrapWithReconfigurable converts a regular Encoder implementation to a reconfigurableEncoder.
// If encoder is already a reconfigurableEncoder, then nothing is done.
func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) {
m, ok := r.(Encoder)
if !ok {
return nil, NewUnimplementedInterfaceError(r)
}
if reconfigurable, ok := m.(*reconfigurableEncoder); ok {
return reconfigurable, nil
}
return &reconfigurableEncoder{name: name, actual: m}, nil
}
// ToEncoderPositionType takes a GetPositionResponse and returns
// an equivalent PositionType-to-int map.
func ToEncoderPositionType(positionType *pb.PositionType) PositionType {
if positionType == nil {
return PositionTypeUNSPECIFIED
}
if *positionType == pb.PositionType_POSITION_TYPE_ANGLE_DEGREES {
return PositionTypeDEGREES
}
if *positionType == pb.PositionType_POSITION_TYPE_TICKS_COUNT {
return PositionTypeTICKS
}
return PositionTypeUNSPECIFIED
}
// ToProtoPositionType takes a map of PositionType-to-int (indicating
// the PositionType) and converts it to a GetPositionResponse.
func ToProtoPositionType(positionType *PositionType) pb.PositionType {
if positionType == nil {
return pb.PositionType_POSITION_TYPE_UNSPECIFIED
}
if *positionType == PositionTypeDEGREES {
return pb.PositionType_POSITION_TYPE_ANGLE_DEGREES
}
if *positionType == PositionTypeTICKS {
return pb.PositionType_POSITION_TYPE_TICKS_COUNT
}
return pb.PositionType_POSITION_TYPE_UNSPECIFIED
}