-
Notifications
You must be signed in to change notification settings - Fork 109
/
gantry.go
319 lines (276 loc) · 9.29 KB
/
gantry.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
package gantry
import (
"context"
"sync"
"github.com/edaniels/golog"
pb "go.viam.com/api/component/gantry/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/referenceframe"
"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,
Status: func(ctx context.Context, resource interface{}) (interface{}, error) {
return CreateStatus(ctx, resource)
},
RegisterRPCService: func(ctx context.Context, rpcServer rpc.Server, subtypeSvc subtype.Service) error {
return rpcServer.RegisterServiceServer(
ctx,
&pb.GantryService_ServiceDesc,
NewServer(subtypeSvc),
pb.RegisterGantryServiceHandlerFromEndpoint,
)
},
RPCServiceDesc: &pb.GantryService_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: position.String(),
}, newPositionCollector)
data.RegisterCollector(data.MethodMetadata{
Subtype: Subtype,
MethodName: lengths.String(),
}, newLengthsCollector)
}
// SubtypeName is a constant that identifies the component resource subtype string "gantry".
const SubtypeName = resource.SubtypeName("gantry")
// Subtype is a constant that identifies the component resource subtype.
var Subtype = resource.NewSubtype(
resource.ResourceNamespaceRDK,
resource.ResourceTypeComponent,
SubtypeName,
)
// Named is a helper for getting the named Gantry's typed resource name.
func Named(name string) resource.Name {
return resource.NameFromSubtype(Subtype, name)
}
// Gantry is used for controlling gantries of N axis.
type Gantry interface {
// Position returns the position in meters
Position(ctx context.Context, extra map[string]interface{}) ([]float64, error)
// MoveToPosition is in meters
// The worldState argument should be treated as optional by all implementing drivers
// This will block until done or a new operation cancels this one
MoveToPosition(ctx context.Context, positionsMm []float64, worldState *referenceframe.WorldState, extra map[string]interface{}) error
// Lengths is the length of gantries in meters
Lengths(ctx context.Context, extra map[string]interface{}) ([]float64, error)
// Stop stops the gantry. It is assumed the gantry stops immediately.
Stop(ctx context.Context, extra map[string]interface{}) error
generic.Generic
referenceframe.ModelFramer
referenceframe.InputEnabled
resource.MovingCheckable
}
// FromDependencies is a helper for getting the named gantry from a collection of
// dependencies.
func FromDependencies(deps registry.Dependencies, name string) (Gantry, error) {
res, ok := deps[Named(name)]
if !ok {
return nil, utils.DependencyNotFoundError(name)
}
part, ok := res.(Gantry)
if !ok {
return nil, DependencyTypeError(name, res)
}
return part, nil
}
// A LocalGantry represents a Gantry that can report whether it is moving or not.
type LocalGantry interface {
Gantry
}
// NewUnimplementedInterfaceError is used when there is a failed interface check.
func NewUnimplementedInterfaceError(actual interface{}) error {
return utils.NewUnimplementedInterfaceError((*Gantry)(nil), actual)
}
// NewUnimplementedLocalInterfaceError is used when there is a failed interface check.
func NewUnimplementedLocalInterfaceError(actual interface{}) error {
return utils.NewUnimplementedInterfaceError((*Gantry)(nil), actual)
}
// DependencyTypeError is used when a resource doesn't implement the expected interface.
func DependencyTypeError(name string, actual interface{}) error {
return utils.DependencyTypeError(name, (*Gantry)(nil), actual)
}
// FromRobot is a helper for getting the named gantry from the given Robot.
func FromRobot(r robot.Robot, name string) (Gantry, error) {
res, err := r.ResourceByName(Named(name))
if err != nil {
return nil, err
}
part, ok := res.(Gantry)
if !ok {
return nil, NewUnimplementedInterfaceError(res)
}
return part, nil
}
// NamesFromRobot is a helper for getting all gantry names from the given Robot.
func NamesFromRobot(r robot.Robot) []string {
return robot.NamesBySubtype(r, Subtype)
}
// CreateStatus creates a status from the gantry.
func CreateStatus(ctx context.Context, resource interface{}) (*pb.Status, error) {
gantry, ok := resource.(Gantry)
if !ok {
return nil, NewUnimplementedLocalInterfaceError(resource)
}
positions, err := gantry.Position(ctx, nil)
if err != nil {
return nil, err
}
lengths, err := gantry.Lengths(ctx, nil)
if err != nil {
return nil, err
}
isMoving, err := gantry.IsMoving(ctx)
if err != nil {
return nil, err
}
return &pb.Status{PositionsMm: positions, LengthsMm: lengths, IsMoving: isMoving}, nil
}
// WrapWithReconfigurable wraps a gantry or localGantry with a reconfigurable
// and locking interface.
func WrapWithReconfigurable(r interface{}, name resource.Name) (resource.Reconfigurable, error) {
g, ok := r.(Gantry)
if !ok {
return nil, NewUnimplementedInterfaceError(r)
}
if reconfigurable, ok := g.(*reconfigurableGantry); ok {
return reconfigurable, nil
}
rGantry := &reconfigurableGantry{name: name, actual: g}
gLocal, ok := r.(LocalGantry)
if !ok {
return rGantry, nil
}
if reconfigurable, ok := gLocal.(*reconfigurableLocalGantry); ok {
return reconfigurable, nil
}
return &reconfigurableLocalGantry{actual: gLocal, reconfigurableGantry: rGantry}, nil
}
var (
_ = Gantry(&reconfigurableGantry{})
_ = LocalGantry(&reconfigurableLocalGantry{})
_ = resource.Reconfigurable(&reconfigurableGantry{})
_ = resource.Reconfigurable(&reconfigurableLocalGantry{})
_ = viamutils.ContextCloser(&reconfigurableLocalGantry{})
)
type reconfigurableGantry struct {
mu sync.RWMutex
name resource.Name
actual Gantry
}
func (g *reconfigurableGantry) Name() resource.Name {
return g.name
}
func (g *reconfigurableGantry) ProxyFor() interface{} {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual
}
func (g *reconfigurableGantry) DoCommand(ctx context.Context, cmd map[string]interface{}) (map[string]interface{}, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.DoCommand(ctx, cmd)
}
// Position returns the position in meters.
func (g *reconfigurableGantry) Position(ctx context.Context, extra map[string]interface{}) ([]float64, error) {
g.mu.Lock()
defer g.mu.Unlock()
return g.actual.Position(ctx, extra)
}
// Lengths returns the position in meters.
func (g *reconfigurableGantry) Lengths(ctx context.Context, extra map[string]interface{}) ([]float64, error) {
g.mu.Lock()
defer g.mu.Unlock()
return g.actual.Lengths(ctx, extra)
}
// position is in meters.
func (g *reconfigurableGantry) MoveToPosition(
ctx context.Context,
positionsMm []float64,
worldState *referenceframe.WorldState,
extra map[string]interface{},
) error {
g.mu.Lock()
defer g.mu.Unlock()
return g.actual.MoveToPosition(ctx, positionsMm, worldState, extra)
}
func (g *reconfigurableGantry) Stop(ctx context.Context, extra map[string]interface{}) error {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.Stop(ctx, extra)
}
func (g *reconfigurableGantry) Close(ctx context.Context) error {
g.mu.RLock()
defer g.mu.RUnlock()
return viamutils.TryClose(ctx, g.actual)
}
// Reconfigure reconfigures the resource.
func (g *reconfigurableGantry) Reconfigure(ctx context.Context, newGantry resource.Reconfigurable) error {
g.mu.Lock()
defer g.mu.Unlock()
return g.reconfigure(ctx, newGantry)
}
func (g *reconfigurableGantry) reconfigure(ctx context.Context, newGantry resource.Reconfigurable) error {
actual, ok := newGantry.(*reconfigurableGantry)
if !ok {
return utils.NewUnexpectedTypeError(g, newGantry)
}
if err := viamutils.TryClose(ctx, g.actual); err != nil {
golog.Global().Errorw("error closing old", "error", err)
}
g.actual = actual.actual
return nil
}
func (g *reconfigurableGantry) ModelFrame() referenceframe.Model {
g.mu.Lock()
defer g.mu.Unlock()
return g.actual.ModelFrame()
}
func (g *reconfigurableGantry) CurrentInputs(ctx context.Context) ([]referenceframe.Input, error) {
g.mu.Lock()
defer g.mu.Unlock()
return g.actual.CurrentInputs(ctx)
}
func (g *reconfigurableGantry) GoToInputs(ctx context.Context, goal []referenceframe.Input) error {
g.mu.Lock()
defer g.mu.Unlock()
return g.actual.GoToInputs(ctx, goal)
}
func (g *reconfigurableGantry) IsMoving(ctx context.Context) (bool, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.IsMoving(ctx)
}
type reconfigurableLocalGantry struct {
*reconfigurableGantry
actual LocalGantry
}
func (g *reconfigurableLocalGantry) IsMoving(ctx context.Context) (bool, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.actual.IsMoving(ctx)
}
func (g *reconfigurableLocalGantry) Reconfigure(ctx context.Context, newGantry resource.Reconfigurable) error {
g.mu.Lock()
defer g.mu.Unlock()
gantry, ok := newGantry.(*reconfigurableLocalGantry)
if !ok {
return utils.NewUnexpectedTypeError(g, newGantry)
}
if err := viamutils.TryClose(ctx, g.actual); err != nil {
golog.Global().Errorw("error closing old", "error", err)
}
g.actual = gantry.actual
return g.reconfigurableGantry.reconfigure(ctx, gantry.reconfigurableGantry)
}