-
Notifications
You must be signed in to change notification settings - Fork 110
/
motion_service.go
169 lines (156 loc) · 5.13 KB
/
motion_service.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
package inject
import (
"context"
geo "github.com/kellydunn/golang-geo"
servicepb "go.viam.com/api/service/motion/v1"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/services/motion"
"go.viam.com/rdk/spatialmath"
)
// MotionService represents a fake instance of an motion
// service.
type MotionService struct {
motion.Service
name resource.Name
MoveFunc func(
ctx context.Context,
componentName resource.Name,
grabPose *referenceframe.PoseInFrame,
worldState *referenceframe.WorldState,
constraints *servicepb.Constraints,
extra map[string]interface{},
) (bool, error)
MoveOnMapFunc func(
ctx context.Context,
componentName resource.Name,
destination spatialmath.Pose,
slamName resource.Name,
extra map[string]interface{},
) (bool, error)
MoveOnGlobeFunc func(
ctx context.Context,
componentName resource.Name,
destination *geo.Point,
heading float64,
movementSensorName resource.Name,
obstacles []*spatialmath.GeoObstacle,
linearVelocity float64,
angularVelocity float64,
extra map[string]interface{},
) (bool, error)
MoveSingleComponentFunc func(
ctx context.Context,
componentName resource.Name,
grabPose *referenceframe.PoseInFrame,
worldState *referenceframe.WorldState,
extra map[string]interface{},
) (bool, error)
GetPoseFunc func(
ctx context.Context,
componentName resource.Name,
destinationFrame string,
supplementalTransforms []*referenceframe.LinkInFrame,
extra map[string]interface{},
) (*referenceframe.PoseInFrame, error)
DoCommandFunc func(ctx context.Context,
cmd map[string]interface{}) (map[string]interface{}, error)
CloseFunc func(ctx context.Context) error
}
// NewMotionService returns a new injected motion service.
func NewMotionService(name string) *MotionService {
return &MotionService{name: motion.Named(name)}
}
// Name returns the name of the resource.
func (mgs *MotionService) Name() resource.Name {
return mgs.name
}
// Move calls the injected Move or the real variant.
func (mgs *MotionService) Move(
ctx context.Context,
componentName resource.Name,
destination *referenceframe.PoseInFrame,
worldState *referenceframe.WorldState,
constraints *servicepb.Constraints,
extra map[string]interface{},
) (bool, error) {
if mgs.MoveFunc == nil {
return mgs.Service.Move(ctx, componentName, destination, worldState, constraints, extra)
}
return mgs.MoveFunc(ctx, componentName, destination, worldState, constraints, extra)
}
// MoveOnMap calls the injected MoveOnMap or the real variant.
func (mgs *MotionService) MoveOnMap(
ctx context.Context,
componentName resource.Name,
destination spatialmath.Pose,
slamName resource.Name,
extra map[string]interface{},
) (bool, error) {
if mgs.MoveOnMapFunc == nil {
return mgs.Service.MoveOnMap(ctx, componentName, destination, slamName, extra)
}
return mgs.MoveOnMapFunc(ctx, componentName, destination, slamName, extra)
}
// MoveOnGlobe calls the injected MoveOnGlobe or the real variant.
func (mgs *MotionService) MoveOnGlobe(
ctx context.Context,
componentName resource.Name,
destination *geo.Point,
heading float64,
movementSensorName resource.Name,
obstacles []*spatialmath.GeoObstacle,
linearVel float64,
angularVel float64,
extra map[string]interface{},
) (bool, error) {
if mgs.MoveOnGlobeFunc == nil {
return mgs.Service.MoveOnGlobe(ctx, componentName, destination, heading, movementSensorName, obstacles, linearVel, angularVel, extra)
}
return mgs.MoveOnGlobeFunc(ctx, componentName, destination, heading, movementSensorName, obstacles, linearVel, angularVel, extra)
}
// MoveSingleComponent calls the injected MoveSingleComponent or the real variant. It uses the same function as Move.
func (mgs *MotionService) MoveSingleComponent(
ctx context.Context,
componentName resource.Name,
destination *referenceframe.PoseInFrame,
worldState *referenceframe.WorldState,
extra map[string]interface{},
) (bool, error) {
if mgs.MoveFunc == nil {
return mgs.Service.MoveSingleComponent(ctx, componentName, destination, worldState, extra)
}
return mgs.MoveSingleComponentFunc(ctx, componentName, destination, worldState, extra)
}
// GetPose calls the injected GetPose or the real variant.
func (mgs *MotionService) GetPose(
ctx context.Context,
componentName resource.Name,
destinationFrame string,
supplementalTransforms []*referenceframe.LinkInFrame,
extra map[string]interface{},
) (*referenceframe.PoseInFrame, error) {
if mgs.GetPoseFunc == nil {
return mgs.Service.GetPose(ctx, componentName, destinationFrame, supplementalTransforms, extra)
}
return mgs.GetPoseFunc(ctx, componentName, destinationFrame, supplementalTransforms, extra)
}
// DoCommand calls the injected DoCommand or the real variant.
func (mgs *MotionService) DoCommand(ctx context.Context,
cmd map[string]interface{},
) (map[string]interface{}, error) {
if mgs.DoCommandFunc == nil {
return mgs.Service.DoCommand(ctx, cmd)
}
return mgs.DoCommandFunc(ctx, cmd)
}
// Close calls the injected Close or the real version.
func (mgs *MotionService) Close(ctx context.Context) error {
if mgs.CloseFunc == nil {
if mgs.Service == nil {
return nil
}
return mgs.Service.Close(ctx)
}
return mgs.CloseFunc(ctx)
}