-
Notifications
You must be signed in to change notification settings - Fork 110
/
motion.go
182 lines (161 loc) · 5.22 KB
/
motion.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
// Package motion implements an motion service.
package motion
import (
"context"
"sync"
"github.com/edaniels/golog"
servicepb "go.viam.com/api/service/motion/v1"
goutils "go.viam.com/utils"
"go.viam.com/utils/rpc"
"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{
RegisterRPCService: func(ctx context.Context, rpcServer rpc.Server, subtypeSvc subtype.Service) error {
return rpcServer.RegisterServiceServer(
ctx,
&servicepb.MotionService_ServiceDesc,
NewServer(subtypeSvc),
servicepb.RegisterMotionServiceHandlerFromEndpoint,
)
},
RPCServiceDesc: &servicepb.MotionService_ServiceDesc,
RPCClient: func(ctx context.Context, conn rpc.ClientConn, name string, logger golog.Logger) interface{} {
return NewClientFromConn(ctx, conn, name, logger)
},
Reconfigurable: WrapWithReconfigurable,
})
}
// A Service controls the flow of moving components.
type Service interface {
Move(
ctx context.Context,
componentName resource.Name,
destination *referenceframe.PoseInFrame,
worldState *referenceframe.WorldState,
extra map[string]interface{},
) (bool, error)
MoveSingleComponent(
ctx context.Context,
componentName resource.Name,
destination *referenceframe.PoseInFrame,
worldState *referenceframe.WorldState,
extra map[string]interface{},
) (bool, error)
GetPose(
ctx context.Context,
componentName resource.Name,
destinationFrame string,
supplementalTransforms []*referenceframe.LinkInFrame,
extra map[string]interface{},
) (*referenceframe.PoseInFrame, error)
}
var (
_ = Service(&reconfigurableMotionService{})
_ = resource.Reconfigurable(&reconfigurableMotionService{})
_ = goutils.ContextCloser(&reconfigurableMotionService{})
)
// SubtypeName is the name of the type of service.
const SubtypeName = resource.SubtypeName("motion")
// Subtype is a constant that identifies the motion service resource subtype.
var Subtype = resource.NewSubtype(
resource.ResourceNamespaceRDK,
resource.ResourceTypeService,
SubtypeName,
)
// Named is a helper for getting the named motion service's typed resource name.
func Named(name string) resource.Name {
return resource.NameFromSubtype(Subtype, name)
}
// NewUnimplementedInterfaceError is used when there is a failed interface check.
func NewUnimplementedInterfaceError(actual interface{}) error {
return utils.NewUnimplementedInterfaceError((*Service)(nil), actual)
}
// FromRobot is a helper for getting the named motion service from the given Robot.
func FromRobot(r robot.Robot, name string) (Service, error) {
resource, err := r.ResourceByName(Named(name))
if err != nil {
return nil, err
}
svc, ok := resource.(Service)
if !ok {
return nil, NewUnimplementedInterfaceError(resource)
}
return svc, nil
}
type reconfigurableMotionService struct {
mu sync.RWMutex
name resource.Name
actual Service
}
func (svc *reconfigurableMotionService) Name() resource.Name {
return svc.name
}
func (svc *reconfigurableMotionService) Move(
ctx context.Context,
componentName resource.Name,
destination *referenceframe.PoseInFrame,
worldState *referenceframe.WorldState,
extra map[string]interface{},
) (bool, error) {
svc.mu.RLock()
defer svc.mu.RUnlock()
return svc.actual.Move(ctx, componentName, destination, worldState, extra)
}
func (svc *reconfigurableMotionService) MoveSingleComponent(
ctx context.Context,
componentName resource.Name,
destination *referenceframe.PoseInFrame,
worldState *referenceframe.WorldState,
extra map[string]interface{},
) (bool, error) {
svc.mu.RLock()
defer svc.mu.RUnlock()
return svc.actual.MoveSingleComponent(ctx, componentName, destination, worldState, extra)
}
func (svc *reconfigurableMotionService) GetPose(
ctx context.Context,
componentName resource.Name,
destinationFrame string,
supplementalTransforms []*referenceframe.LinkInFrame,
extra map[string]interface{},
) (*referenceframe.PoseInFrame, error) {
svc.mu.RLock()
defer svc.mu.RUnlock()
return svc.actual.GetPose(ctx, componentName, destinationFrame, supplementalTransforms, extra)
}
func (svc *reconfigurableMotionService) Close(ctx context.Context) error {
svc.mu.RLock()
defer svc.mu.RUnlock()
return goutils.TryClose(ctx, svc.actual)
}
// Reconfigure replaces the old Motion Service with a new Motion Service.
func (svc *reconfigurableMotionService) Reconfigure(ctx context.Context, newSvc resource.Reconfigurable) error {
svc.mu.Lock()
defer svc.mu.Unlock()
rSvc, ok := newSvc.(*reconfigurableMotionService)
if !ok {
return utils.NewUnexpectedTypeError(svc, newSvc)
}
if err := goutils.TryClose(ctx, svc.actual); err != nil {
golog.Global().Errorw("error closing old", "error", err)
}
svc.actual = rSvc.actual
return nil
}
// WrapWithReconfigurable wraps a Motion Service as a Reconfigurable.
func WrapWithReconfigurable(s interface{}, name resource.Name) (resource.Reconfigurable, error) {
svc, ok := s.(Service)
if !ok {
return nil, NewUnimplementedInterfaceError(s)
}
if reconfigurable, ok := s.(*reconfigurableMotionService); ok {
return reconfigurable, nil
}
return &reconfigurableMotionService{name: name, actual: svc}, nil
}