-
Notifications
You must be signed in to change notification settings - Fork 110
/
framesystem.go
293 lines (257 loc) · 9.54 KB
/
framesystem.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
// Package framesystem defines the frame system service which is responsible for managing a stateful frame system
package framesystem
import (
"context"
"fmt"
"sync"
"github.com/edaniels/golog"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/pkg/errors"
"go.opencensus.io/trace"
"go.viam.com/rdk/pointcloud"
"go.viam.com/rdk/referenceframe"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/spatialmath"
"go.viam.com/rdk/utils"
)
// LocalFrameSystemName is the default name of the frame system created by the service.
const LocalFrameSystemName = "robot"
// SubtypeName is a constant that identifies the internal frame system resource subtype string.
const SubtypeName = "frame_system"
// API is the fully qualified API for the internal frame system service.
var API = resource.APINamespaceRDKInternal.WithServiceType(SubtypeName)
// InternalServiceName is used to refer to/depend on this service internally.
var InternalServiceName = resource.NewName(API, "builtin")
// A Service that returns the frame system for a robot.
type Service interface {
resource.Resource
TransformPose(
ctx context.Context,
pose *referenceframe.PoseInFrame,
dst string,
additionalTransforms []*referenceframe.LinkInFrame,
) (*referenceframe.PoseInFrame, error)
TransformPointCloud(ctx context.Context, srcpc pointcloud.PointCloud, srcName, dstName string) (pointcloud.PointCloud, error)
CurrentInputs(ctx context.Context) (map[string][]referenceframe.Input, map[string]referenceframe.InputEnabled, error)
FrameSystem(ctx context.Context, additionalTransforms []*referenceframe.LinkInFrame) (referenceframe.FrameSystem, error)
}
// FromDependencies is a helper for getting the framesystem from a collection of dependencies.
func FromDependencies(deps resource.Dependencies) (Service, error) {
return resource.FromDependencies[Service](deps, InternalServiceName)
}
// New returns a new frame system service for the given robot.
func New(ctx context.Context, deps resource.Dependencies, logger golog.Logger) (Service, error) {
fs := &frameSystemService{
Named: InternalServiceName.AsNamed(),
components: make(map[string]resource.Resource),
logger: logger,
}
if err := fs.Reconfigure(ctx, deps, resource.Config{ConvertedAttributes: &Config{}}); err != nil {
return nil, err
}
return fs, nil
}
var internalFrameSystemServiceName = resource.NewName(
resource.APINamespaceRDKInternal.WithServiceType("framesystem"),
"builtin",
)
func (svc *frameSystemService) Name() resource.Name {
return internalFrameSystemServiceName
}
// Config is a slice of *config.FrameSystemPart.
type Config struct {
resource.TriviallyValidateConfig
Parts []*referenceframe.FrameSystemPart
AdditionalTransforms []*referenceframe.LinkInFrame
}
// String prints out a table of each frame in the system, with columns of name, parent, translation and orientation.
func (cfg Config) String() string {
t := table.NewWriter()
t.AppendHeader(table.Row{"#", "Name", "Parent", "Translation", "Orientation", "Geometry"})
t.AppendRow([]interface{}{"0", referenceframe.World, "", "", "", ""})
for i, part := range cfg.Parts {
pose := part.FrameConfig.Pose()
tra := pose.Point()
ori := pose.Orientation().EulerAngles()
geomString := ""
if gc := part.FrameConfig.Geometry(); gc != nil {
geomString = gc.String()
}
t.AppendRow([]interface{}{
fmt.Sprintf("%d", i+1),
part.FrameConfig.Name(),
part.FrameConfig.Parent(),
fmt.Sprintf("X:%.0f, Y:%.0f, Z:%.0f", tra.X, tra.Y, tra.Z),
fmt.Sprintf(
"Roll:%.2f, Pitch:%.2f, Yaw:%.2f",
utils.RadToDeg(ori.Roll),
utils.RadToDeg(ori.Pitch),
utils.RadToDeg(ori.Yaw),
),
geomString,
})
}
return t.Render()
}
// the frame system service collects all the relevant parts that make up the frame system from the robot
// configs, and the remote robot configs.
type frameSystemService struct {
resource.Named
resource.TriviallyCloseable
components map[string]resource.Resource
logger golog.Logger
parts []*referenceframe.FrameSystemPart
partsMu sync.RWMutex
}
// Reconfigure will rebuild the frame system from the newly updated robot.
func (svc *frameSystemService) Reconfigure(ctx context.Context, deps resource.Dependencies, conf resource.Config) error {
svc.partsMu.Lock()
defer svc.partsMu.Unlock()
_, span := trace.StartSpan(ctx, "services::framesystem::Reconfigure")
defer span.End()
components := make(map[string]resource.Resource)
for name, r := range deps {
short := name.ShortName()
// is this only for InputEnabled components or everything?
if _, present := components[short]; present {
return DuplicateResourceShortNameError(short)
}
components[short] = r
}
svc.components = components
fsCfg, err := resource.NativeConfig[*Config](conf)
if err != nil {
return err
}
sortedParts, err := referenceframe.TopologicallySortParts(fsCfg.Parts)
if err != nil {
return err
}
svc.parts = sortedParts
svc.logger.Debugf("updated robot frame system:\n%v", (&Config{Parts: sortedParts}).String())
return nil
}
// TransformPose will transform the pose of the requested poseInFrame to the desired frame in the robot's frame system.
func (svc *frameSystemService) TransformPose(
ctx context.Context,
pose *referenceframe.PoseInFrame,
dst string,
additionalTransforms []*referenceframe.LinkInFrame,
) (*referenceframe.PoseInFrame, error) {
ctx, span := trace.StartSpan(ctx, "services::framesystem::TransformPose")
defer span.End()
fs, err := svc.FrameSystem(ctx, additionalTransforms)
if err != nil {
return nil, err
}
input := referenceframe.StartPositions(fs)
svc.partsMu.RLock()
defer svc.partsMu.RUnlock()
// build maps of relevant components and inputs from initial inputs
for name, inputs := range input {
// skip frame if it does not have input
if len(inputs) == 0 {
continue
}
// add component to map
component, ok := svc.components[name]
if !ok {
return nil, DependencyNotFoundError(name)
}
inputEnabled, ok := component.(referenceframe.InputEnabled)
if !ok {
return nil, NotInputEnabledError(component)
}
// add input to map
pos, err := inputEnabled.CurrentInputs(ctx)
if err != nil {
return nil, err
}
input[name] = pos
}
tf, err := fs.Transform(input, pose, dst)
if err != nil {
return nil, err
}
pose, _ = tf.(*referenceframe.PoseInFrame)
return pose, nil
}
// CurrentInputs will get present inputs for a framesystem from a robot and return a map of those inputs, as well as a map of the
// InputEnabled resources that those inputs came from.
func (svc *frameSystemService) CurrentInputs(
ctx context.Context,
) (map[string][]referenceframe.Input, map[string]referenceframe.InputEnabled, error) {
fs, err := svc.FrameSystem(ctx, []*referenceframe.LinkInFrame{})
if err != nil {
return nil, nil, err
}
input := referenceframe.StartPositions(fs)
// build maps of relevant components and inputs from initial inputs
resources := map[string]referenceframe.InputEnabled{}
for name, original := range input {
// skip frames with no input
if len(original) == 0 {
continue
}
// add component to map
component, ok := svc.components[name]
if !ok {
return nil, nil, DependencyNotFoundError(name)
}
inputEnabled, ok := component.(referenceframe.InputEnabled)
if !ok {
return nil, nil, NotInputEnabledError(component)
}
resources[name] = inputEnabled
// add input to map
pos, err := inputEnabled.CurrentInputs(ctx)
if err != nil {
return nil, nil, err
}
input[name] = pos
}
return input, resources, nil
}
// FrameSystem returns the frame system of the robot.
func (svc *frameSystemService) FrameSystem(
ctx context.Context,
additionalTransforms []*referenceframe.LinkInFrame,
) (referenceframe.FrameSystem, error) {
_, span := trace.StartSpan(ctx, "services::framesystem::FrameSystem")
defer span.End()
return referenceframe.NewFrameSystem(LocalFrameSystemName, svc.parts, additionalTransforms)
}
// TransformPointCloud applies the same pose offset to each point in a single pointcloud and returns the transformed point cloud.
// if destination string is empty, defaults to transforming to the world frame.
// Do not move the robot between the generation of the initial pointcloud and the receipt
// of the transformed pointcloud because that will make the transformations inaccurate.
func (svc *frameSystemService) TransformPointCloud(ctx context.Context, srcpc pointcloud.PointCloud, srcName, dstName string,
) (pointcloud.PointCloud, error) {
if dstName == "" {
dstName = referenceframe.World
}
if srcName == "" {
return nil, errors.New("srcName cannot be empty, must provide name of point cloud origin")
}
// get transform pose needed to get to destination frame
sourceFrameZero := referenceframe.NewPoseInFrame(srcName, spatialmath.NewZeroPose())
theTransform, err := svc.TransformPose(ctx, sourceFrameZero, dstName, nil)
if err != nil {
return nil, err
}
// returned the transformed pointcloud where the transform was applied to each point
return pointcloud.ApplyOffset(ctx, srcpc, theTransform.Pose(), svc.logger)
}
// PrefixRemoteParts applies prefixes to a list of FrameSystemParts appropriate to the remote they originate from.
func PrefixRemoteParts(parts []*referenceframe.FrameSystemPart, remoteName, remoteParent string) {
for _, part := range parts {
if part.FrameConfig.Parent() == referenceframe.World { // rename World of remote parts
part.FrameConfig.SetParent(remoteParent)
}
// rename each non-world part with prefix
part.FrameConfig.SetName(remoteName + ":" + part.FrameConfig.Name())
if part.FrameConfig.Parent() != remoteParent {
part.FrameConfig.SetParent(remoteName + ":" + part.FrameConfig.Parent())
}
}
}