-
Notifications
You must be signed in to change notification settings - Fork 110
/
merged.go
422 lines (360 loc) · 11.6 KB
/
merged.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Package merged implements a movementsensor combining movement data from other sensors
package merged
import (
"context"
"fmt"
"math"
"sync"
"github.com/golang/geo/r3"
geo "github.com/kellydunn/golang-geo"
"go.uber.org/multierr"
"golang.org/x/exp/maps"
"go.viam.com/rdk/components/movementsensor"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/spatialmath"
)
const errStrAccuracy = "_accuracy_err"
var model = resource.DefaultModelFamily.WithModel("merged")
// Config is the config of the merged movement_sensor model.
type Config struct {
Position []string `json:"position,omitempty"`
Orientation []string `json:"orientation,omitempty"`
CompassHeading []string `json:"compass_heading,omitempty"`
LinearVelocity []string `json:"linear_velocity,omitempty"`
AngularVelocity []string `json:"angular_velocity,omitempty"`
LinearAcceleration []string `json:"linear_acceleration,omitempty"`
}
// Validate validates the merged model's configuration.
func (cfg *Config) Validate(path string) ([]string, error) {
var deps []string
deps = append(deps, cfg.Position...)
deps = append(deps, cfg.Orientation...)
deps = append(deps, cfg.CompassHeading...)
deps = append(deps, cfg.LinearVelocity...)
deps = append(deps, cfg.AngularVelocity...)
deps = append(deps, cfg.LinearAcceleration...)
return deps, nil
}
type merged struct {
resource.Named
logger logging.Logger
mu sync.Mutex
ori movementsensor.MovementSensor
pos movementsensor.MovementSensor
compass movementsensor.MovementSensor
linVel movementsensor.MovementSensor
angVel movementsensor.MovementSensor
linAcc movementsensor.MovementSensor
}
func init() {
resource.Register(
movementsensor.API, model,
resource.Registration[movementsensor.MovementSensor, *Config]{
Constructor: newMergedModel,
})
}
func newMergedModel(ctx context.Context, deps resource.Dependencies, conf resource.Config, logger logging.Logger) (
movementsensor.MovementSensor, error,
) {
m := merged{
logger: logger,
Named: conf.ResourceName().AsNamed(),
}
if err := m.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
return &m, nil
}
func (m *merged) Reconfigure(ctx context.Context, deps resource.Dependencies, conf resource.Config) error {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return err
}
m.mu.Lock()
defer m.mu.Unlock()
firstGoodSensorWithProperties := func(
deps resource.Dependencies, names []string, logger logging.Logger,
want *movementsensor.Properties, propname string,
) (movementsensor.MovementSensor, error) {
// check if the config names and dependencies have been passed at all
if len(names) == 0 || deps == nil {
return nil, nil
}
for _, name := range names {
ms, err := movementsensor.FromDependencies(deps, name)
msName := ms.Name().ShortName()
if err != nil {
logger.CDebugf(ctx, "error getting sensor %v from dependencies", msName)
continue
}
props, err := ms.Properties(ctx, nil)
if err != nil {
logger.CDebugf(ctx, "error in getting sensor %v properties", msName)
continue
}
// check that the sensor matches the properties passed in
// if it doesn't, skip it and go on to the next sensor in the list
if want.OrientationSupported && !props.OrientationSupported {
continue
}
if want.PositionSupported && !props.PositionSupported {
continue
}
if want.CompassHeadingSupported && !props.CompassHeadingSupported {
continue
}
if want.LinearVelocitySupported && !props.LinearVelocitySupported {
continue
}
if want.AngularVelocitySupported && !props.AngularVelocitySupported {
continue
}
if want.LinearAccelerationSupported && !props.LinearAccelerationSupported {
continue
}
// we've found the sensor that reports everything we want
m.logger.Debugf("using sensor %v as %s sensor", msName, propname)
return ms, nil
}
return nil, fmt.Errorf("%v not supported by any sensor in list %#v", propname, names)
}
m.ori, err = firstGoodSensorWithProperties(
deps, newConf.Orientation, m.logger,
&movementsensor.Properties{OrientationSupported: true}, "orientation")
if err != nil {
return err
}
m.pos, err = firstGoodSensorWithProperties(
deps, newConf.Position, m.logger,
&movementsensor.Properties{PositionSupported: true}, "position")
if err != nil {
return err
}
m.compass, err = firstGoodSensorWithProperties(
deps, newConf.CompassHeading, m.logger,
&movementsensor.Properties{CompassHeadingSupported: true}, "compass_heading")
if err != nil {
return err
}
m.linVel, err = firstGoodSensorWithProperties(
deps, newConf.LinearVelocity, m.logger,
&movementsensor.Properties{LinearVelocitySupported: true}, "linear_velocity")
if err != nil {
return err
}
m.angVel, err = firstGoodSensorWithProperties(
deps, newConf.AngularVelocity, m.logger,
&movementsensor.Properties{AngularVelocitySupported: true}, "angular_velocity")
if err != nil {
return err
}
m.linAcc, err = firstGoodSensorWithProperties(
deps, newConf.LinearAcceleration, m.logger,
&movementsensor.Properties{LinearAccelerationSupported: true}, "linear_acceleration")
if err != nil {
return err
}
return nil
}
func (m *merged) Position(ctx context.Context, extra map[string]interface{}) (*geo.Point, float64, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.pos == nil {
return geo.NewPoint(math.NaN(), math.NaN()), math.NaN(),
movementsensor.ErrMethodUnimplementedPosition
}
return m.pos.Position(ctx, extra)
}
func (m *merged) Orientation(ctx context.Context, extra map[string]interface{}) (spatialmath.Orientation, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.ori == nil {
nanOri := spatialmath.NewOrientationVector()
nanOri.OX = math.NaN()
nanOri.OY = math.NaN()
nanOri.OZ = math.NaN()
nanOri.Theta = math.NaN()
return nanOri,
movementsensor.ErrMethodUnimplementedOrientation
}
return m.ori.Orientation(ctx, extra)
}
func (m *merged) CompassHeading(ctx context.Context, extra map[string]interface{}) (float64, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.compass == nil {
return math.NaN(),
movementsensor.ErrMethodUnimplementedCompassHeading
}
return m.compass.CompassHeading(ctx, extra)
}
func (m *merged) LinearVelocity(ctx context.Context, extra map[string]interface{}) (r3.Vector, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.linVel == nil {
return r3.Vector{X: math.NaN(), Y: math.NaN(), Z: math.NaN()},
movementsensor.ErrMethodUnimplementedLinearVelocity
}
return m.linVel.LinearVelocity(ctx, extra)
}
func (m *merged) AngularVelocity(ctx context.Context, extra map[string]interface{}) (spatialmath.AngularVelocity, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.angVel == nil {
return spatialmath.AngularVelocity{X: math.NaN(), Y: math.NaN(), Z: math.NaN()},
movementsensor.ErrMethodUnimplementedAngularVelocity
}
return m.angVel.AngularVelocity(ctx, extra)
}
func (m *merged) LinearAcceleration(ctx context.Context, extra map[string]interface{}) (r3.Vector, error) {
m.mu.Lock()
defer m.mu.Unlock()
if m.linAcc == nil {
return r3.Vector{X: math.NaN(), Y: math.NaN(), Z: math.NaN()},
movementsensor.ErrMethodUnimplementedLinearAcceleration
}
return m.linAcc.LinearAcceleration(ctx, extra)
}
func mapWithSensorName(name string, accMap map[string]float32) map[string]float32 {
result := map[string]float32{}
for k, v := range accMap {
result[name+"_"+k] = v
}
return result
}
func (m *merged) Accuracy(ctx context.Context, extra map[string]interface{}) (*movementsensor.Accuracy, error) {
m.mu.Lock()
defer m.mu.Unlock()
accMap := make(map[string]float32)
var errs error
if m.ori != nil {
oriAcc, err := m.ori.Accuracy(ctx, extra)
if err != nil {
// replace entire map with a map that shows that it has errors
errorAcc := &movementsensor.Accuracy{
AccuracyMap: map[string]float32{
m.ori.Name().ShortName() + errStrAccuracy: float32(math.NaN()),
},
}
oriAcc = errorAcc
errs = multierr.Combine(errs, err)
}
if oriAcc != nil {
maps.Copy(accMap, mapWithSensorName(m.ori.Name().ShortName(), oriAcc.AccuracyMap))
}
}
hdop := float32(math.NaN())
vdop := float32(math.NaN())
nmeaFix := int32(-1)
if m.pos != nil {
posAcc, err := m.pos.Accuracy(ctx, extra)
if err != nil {
errorAcc := &movementsensor.Accuracy{
AccuracyMap: map[string]float32{
m.pos.Name().ShortName() + errStrAccuracy: float32(math.NaN()),
},
}
posAcc = errorAcc
errs = multierr.Combine(errs, err)
}
if posAcc != nil {
maps.Copy(accMap, mapWithSensorName(m.pos.Name().ShortName(), posAcc.AccuracyMap))
}
hdop = posAcc.Hdop
vdop = posAcc.Vdop
nmeaFix = posAcc.NmeaFix
}
compassDegreeError := float32(math.NaN())
if m.compass != nil {
compassAcc, err := m.compass.Accuracy(ctx, extra)
if err != nil {
errorAcc := &movementsensor.Accuracy{
AccuracyMap: map[string]float32{
m.compass.Name().ShortName() + errStrAccuracy: float32(math.NaN()),
},
}
compassAcc = errorAcc
errs = multierr.Combine(errs, err)
}
if compassAcc != nil {
maps.Copy(accMap, mapWithSensorName(m.compass.Name().ShortName(), compassAcc.AccuracyMap))
}
compassDegreeError = compassAcc.CompassDegreeError
}
if m.linVel != nil {
linvelAcc, err := m.linVel.Accuracy(ctx, extra)
if err != nil {
errorAcc := &movementsensor.Accuracy{
AccuracyMap: map[string]float32{
m.linVel.Name().ShortName() + errStrAccuracy: float32(math.NaN()),
},
}
linvelAcc = errorAcc
errs = multierr.Combine(errs, err)
}
if linvelAcc != nil {
maps.Copy(accMap, mapWithSensorName(m.linVel.Name().ShortName(), linvelAcc.AccuracyMap))
}
}
if m.angVel != nil {
angvelAcc, err := m.angVel.Accuracy(ctx, extra)
if err != nil {
errorAcc := &movementsensor.Accuracy{
AccuracyMap: map[string]float32{
m.angVel.Name().ShortName() + errStrAccuracy: float32(math.NaN()),
},
}
angvelAcc = errorAcc
errs = multierr.Combine(errs, err)
}
if angvelAcc != nil {
maps.Copy(accMap, mapWithSensorName(m.angVel.Name().ShortName(), angvelAcc.AccuracyMap))
}
}
if m.linAcc != nil {
linaccAcc, err := m.linAcc.Accuracy(ctx, extra)
if err != nil {
errorAcc := &movementsensor.Accuracy{
AccuracyMap: map[string]float32{
m.linAcc.Name().ShortName() + errStrAccuracy: float32(math.NaN()),
},
}
linaccAcc = errorAcc
errs = multierr.Combine(errs, err)
}
if linaccAcc != nil {
maps.Copy(accMap, mapWithSensorName(m.linAcc.Name().ShortName(), linaccAcc.AccuracyMap))
}
}
acc := movementsensor.Accuracy{
AccuracyMap: accMap,
Hdop: hdop,
Vdop: vdop,
NmeaFix: nmeaFix,
CompassDegreeError: compassDegreeError,
}
return &acc, errs
}
func (m *merged) Properties(ctx context.Context, extra map[string]interface{}) (*movementsensor.Properties, error) {
m.mu.Lock()
defer m.mu.Unlock()
return &movementsensor.Properties{
PositionSupported: m.pos != nil,
OrientationSupported: m.ori != nil,
CompassHeadingSupported: m.compass != nil,
LinearVelocitySupported: m.linVel != nil,
AngularVelocitySupported: m.angVel != nil,
LinearAccelerationSupported: m.linAcc != nil,
}, nil
}
func (m *merged) Readings(ctx context.Context, extra map[string]interface{}) (map[string]interface{}, error) {
// we're already in lock in this driver
// don't lock the mutex again for the Readings call
return movementsensor.DefaultAPIReadings(ctx, m, extra)
}
func (m *merged) Close(context.Context) error {
// we do not try to Close the movement sensors that this driver depends on
// we let their own drivers and modules close them
return nil
}