-
Notifications
You must be signed in to change notification settings - Fork 110
/
cameramono.go
321 lines (278 loc) · 9.33 KB
/
cameramono.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
// Package cameramono implements a visual odemetry movement sensor based ona single camera stream
// This is an Experimental package
package cameramono
import (
"context"
"errors"
"sync"
"time"
"github.com/edaniels/golog"
"github.com/edaniels/gostream"
"github.com/golang/geo/r3"
geo "github.com/kellydunn/golang-geo"
"go.viam.com/utils"
"gonum.org/v1/gonum/mat"
"go.viam.com/rdk/components/camera"
"go.viam.com/rdk/components/generic"
"go.viam.com/rdk/components/movementsensor"
"go.viam.com/rdk/config"
"go.viam.com/rdk/registry"
"go.viam.com/rdk/rimage"
"go.viam.com/rdk/spatialmath"
rdkutils "go.viam.com/rdk/utils"
"go.viam.com/rdk/vision/odometry"
)
const modelname = "camera_mono"
// AttrConfig is used for converting config attributes of a cameramono movement sensor.
type AttrConfig struct {
Camera string `json:"camera"`
MotionConfig *odometry.MotionEstimationConfig `json:"motion_estimation_config"`
}
// Validate ensures all parts of the config are valid.
func (cfg *AttrConfig) Validate(path string) error {
if cfg.Camera == "" {
return utils.NewConfigValidationError(path,
errors.New("single camera missing for visual odometry"))
}
if cfg.MotionConfig == nil {
return utils.NewConfigValidationError(path,
errors.New("no motion_estimation_config for visual odometry algorithm"))
}
if cfg.MotionConfig.KeyPointCfg == nil {
return utils.NewConfigValidationError(path,
errors.New("no kps config found in motion_estimation_config"))
}
if cfg.MotionConfig.MatchingCfg == nil {
return utils.NewConfigValidationError(path,
errors.New("no matching config found in motion_estimation_config"))
}
if cfg.MotionConfig.CamIntrinsics == nil {
return utils.NewConfigValidationError(path,
errors.New("no camera_instrinsics config found in motion_estimation_config"))
}
if cfg.MotionConfig.ScaleEstimatorCfg == nil {
return utils.NewConfigValidationError(path,
errors.New("no scale_estimator config found in motion_estimation_config"))
}
if cfg.MotionConfig.CamHeightGround == 0 {
return utils.NewConfigValidationError(path,
errors.New("set camera_height from ground to 0 by default"))
}
if cfg.MotionConfig.KeyPointCfg.BRIEFConf == nil {
return utils.NewConfigValidationError(path,
errors.New("no BRIEF Config found in motion_estimation_config"))
}
if cfg.MotionConfig.KeyPointCfg.DownscaleFactor <= 1 {
return utils.NewConfigValidationError(path,
errors.New("downscale_factor in motion_estimation_config should be greater than 1"))
}
return nil
}
func init() {
registry.RegisterComponent(
movementsensor.Subtype,
modelname,
registry.Component{
Constructor: func(
ctx context.Context,
deps registry.Dependencies,
config config.Component,
logger golog.Logger,
) (interface{}, error) {
return newCameraMono(deps, config, logger)
},
})
config.RegisterComponentAttributeMapConverter(
movementsensor.SubtypeName,
modelname,
func(attributes config.AttributeMap) (interface{}, error) {
var conf AttrConfig
return config.TransformAttributeMapToStruct(&conf, attributes)
},
&AttrConfig{})
}
type cameramono struct {
generic.Unimplemented
activeBackgroundWorkers sync.WaitGroup
cancelCtx context.Context
cancelFunc func()
motion *odometry.Motion3D
logger golog.Logger
result result
stream gostream.VideoStream
lastErr error
}
type result struct {
dt float64
trackedPos r3.Vector
trackedOrient spatialmath.Orientation
angVel spatialmath.AngularVelocity
linVel r3.Vector
}
func newCameraMono(
deps registry.Dependencies,
config config.Component,
logger golog.Logger,
) (movementsensor.MovementSensor, error) {
logger.Info(
"visual odometry using one camera implements GetPosition, GetOrientation, GetLinearVelocity and GetAngularVelocity",
)
conf, ok := config.ConvertedAttributes.(*AttrConfig)
if !ok {
return nil, rdkutils.NewUnexpectedTypeError(conf, config.ConvertedAttributes)
}
cam, err := camera.FromDependencies(deps, conf.Camera)
if err != nil {
return nil, err
}
ctx := context.Background()
cancelCtx, cancelFunc := context.WithCancel(ctx)
co := &cameramono{
cancelCtx: cancelCtx,
cancelFunc: cancelFunc,
logger: logger,
motion: &odometry.Motion3D{
Rotation: mat.NewDense(3, 3, []float64{1, 0, 0, 0, 1, 0, 0, 0, 1}),
Translation: mat.NewDense(3, 1, []float64{0, 0, 0}),
},
result: result{
trackedPos: r3.Vector{X: 0, Y: 0, Z: 0},
trackedOrient: spatialmath.NewOrientationVector(),
},
}
co.stream = gostream.NewEmbeddedVideoStream(cam)
co.lastErr = co.backgroundWorker(co.stream, conf.MotionConfig)
return co, co.lastErr
}
func (co *cameramono) backgroundWorker(stream gostream.VideoStream, cfg *odometry.MotionEstimationConfig) error {
defer func() { utils.UncheckedError(stream.Close(context.Background())) }()
co.activeBackgroundWorkers.Add(1)
utils.ManagedGo(func() {
sT := time.Now()
sImg, _, err := stream.Next(co.cancelCtx)
if err != nil && errors.Is(err, context.Canceled) {
co.lastErr = err
return
}
for {
eT := time.Now()
eImg, _, err := stream.Next(co.cancelCtx)
if err != nil {
co.lastErr = err
return
}
dt, moreThanZero := co.getDt(sT, eT)
co.result.dt = dt
if moreThanZero {
co.motion, err = co.extractMovementFromOdometer(rimage.ConvertImage(sImg), rimage.ConvertImage(eImg), dt, cfg)
if err != nil {
co.lastErr = err
co.logger.Error(err)
continue
}
select {
case <-co.cancelCtx.Done():
return
default:
}
}
sImg = eImg
sT = eT
}
}, co.activeBackgroundWorkers.Done)
return co.lastErr
}
func (co *cameramono) extractMovementFromOdometer(
start, end *rimage.Image,
dt float64,
cfg *odometry.MotionEstimationConfig,
) (*odometry.Motion3D, error) {
motion, _, err := odometry.EstimateMotionFrom2Frames(start, end, cfg, co.logger)
if err != nil {
motion = co.motion
return motion, err
}
rAng, cAng := motion.Rotation.Dims()
if rAng != 3 || cAng != 3 {
return nil, errors.New("rotation dims are not 3,3")
}
rLin, cLin := motion.Translation.Dims()
if rLin != 3 || cLin != 1 {
return nil, errors.New("lin dims are not 3,1")
}
rotMat, err := spatialmath.NewRotationMatrix(co.motion.Rotation.RawMatrix().Data)
if err != nil {
return nil, err
}
co.result.trackedOrient = co.result.trackedOrient.RotationMatrix().LeftMatMul(*rotMat)
co.result.trackedPos = co.result.trackedPos.Add(translationToR3(co.motion))
co.result.linVel = calculateLinVel(motion, dt)
co.result.angVel = spatialmath.OrientationToAngularVel(rotMat.EulerAngles(), dt)
return motion, err
}
func (co *cameramono) getDt(startTime, endTime time.Time) (float64, bool) {
duration := endTime.Sub(startTime)
dt := float64(duration/time.Millisecond) / 1000
moreThanZero := dt > 0
return dt, moreThanZero
}
// Close closes all the channels and threads.
func (co *cameramono) Close() {
co.cancelFunc()
co.activeBackgroundWorkers.Wait()
co.lastErr = co.stream.Close(co.cancelCtx)
}
// Position gets the position of the moving object calculated by visual odometry.
func (co *cameramono) Position(ctx context.Context) (*geo.Point, float64, error) {
return geo.NewPoint(co.result.trackedPos.X, co.result.trackedPos.Y), co.result.trackedPos.Z, nil
}
// Oritentation gets the position of the moving object calculated by visual odometry.
func (co *cameramono) Orientation(ctx context.Context) (spatialmath.Orientation, error) {
return co.result.trackedOrient, nil
}
// Readings gets the position of the moving object calculated by visual odometry.
func (co *cameramono) Readings(ctx context.Context) (map[string]interface{}, error) {
return movementsensor.Readings(ctx, co)
}
// LinearVelocity gets the position of the moving object calculated by visual odometry.
func (co *cameramono) LinearVelocity(ctx context.Context) (r3.Vector, error) {
return co.result.linVel, nil
}
// AngularVelocity gets the position of the moving object calculated by visual odometry.
func (co *cameramono) AngularVelocity(ctx context.Context) (spatialmath.AngularVelocity, error) {
return co.result.angVel, nil
}
// Properties gets the position of the moving object calculated by visual odometry.
func (co *cameramono) Properties(ctx context.Context) (*movementsensor.Properties, error) {
return &movementsensor.Properties{
PositionSupported: true,
OrientationSupported: true,
AngularVelocitySupported: true,
LinearVelocitySupported: true,
}, nil
}
// helpers.
func translationToR3(motion *odometry.Motion3D) r3.Vector {
return r3.Vector{
X: motion.Translation.At(0, 0),
Y: motion.Translation.At(1, 0),
Z: motion.Translation.At(2, 0),
}
}
func calculateLinVel(motion *odometry.Motion3D, dt float64) r3.Vector {
tVec := translationToR3(motion)
return r3.Vector{
X: tVec.X / dt,
Y: tVec.Y / dt,
Z: tVec.Z / dt,
}
}
// unimplemented methods.
// Accuracy gets the position of the moving object calculated by visual odometry.
func (co *cameramono) Accuracy(ctx context.Context) (map[string]float32, error) {
return map[string]float32{}, movementsensor.ErrMethodUnimplementedAccuracy
}
// COmpassHeadings gets the position of the moving object calculated by visual odometry.
func (co *cameramono) CompassHeading(ctx context.Context) (float64, error) {
return 0, movementsensor.ErrMethodUnimplementedCompassHeading
}