-
Notifications
You must be signed in to change notification settings - Fork 10
/
integration_test.go
492 lines (430 loc) · 16.2 KB
/
integration_test.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package viamorbslam3_test
import (
"bytes"
"context"
"io"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
"time"
"github.com/edaniels/golog"
"github.com/golang/geo/r3"
"go.viam.com/rdk/pointcloud"
"go.viam.com/rdk/services/slam"
"go.viam.com/rdk/spatialmath"
"go.viam.com/test"
"go.viam.com/utils/artifact"
viamorbslam3 "github.com/viamrobotics/viam-orb-slam3"
orbSlamConfig "github.com/viamrobotics/viam-orb-slam3/config"
dataprocess "github.com/viamrobotics/viam-orb-slam3/dataprocess"
"github.com/viamrobotics/viam-orb-slam3/internal/testhelper"
)
const (
dataInsertionMaxTimeoutMin = 3
orbSleepMsec = 100
)
// createVocabularyFile creates the vocabulary file required by the orbslam binary.
func createVocabularyFile(name string) error {
source, err := os.Open(artifact.MustPath("slam/ORBvoc.txt"))
if err != nil {
return err
}
defer source.Close()
destination, err := os.Create(name + "/config/ORBvoc.txt")
if err != nil {
return err
}
defer destination.Close()
_, err = io.Copy(destination, source)
return err
}
// releaseImages releases an image or image pair to be served by the mock camera(s). If a pair of images is
// released, it is released under a mutex so that the images will be consumed in the same call
// to getSimultaneousColorAndDepth().
func releaseImages(t *testing.T, subAlgo viamorbslam3.SubAlgo) {
switch subAlgo {
case viamorbslam3.Mono:
orbslamIntWebcamReleaseImageChan <- 1
case viamorbslam3.Rgbd:
for {
orbslamIntCameraMutex.Lock()
if len(orbslamIntCameraReleaseImagesChan) == cap(orbslamIntCameraReleaseImagesChan) {
orbslamIntCameraMutex.Unlock()
time.Sleep(10 * time.Millisecond)
} else {
orbslamIntCameraReleaseImagesChan <- 1
orbslamIntCameraReleaseImagesChan <- 1
orbslamIntCameraMutex.Unlock()
return
}
}
default:
t.FailNow()
}
}
// testOrbslamMap checks the orbslam map and confirms there are more than zero map points.
func testOrbslamMap(t *testing.T, svc slam.Service) {
pcd, err := slam.GetPointCloudMapFull(context.Background(), svc)
test.That(t, err, test.ShouldBeNil)
test.That(t, pcd, test.ShouldNotBeNil)
pointcloud, err := pointcloud.ReadPCD(bytes.NewReader(pcd))
test.That(t, err, test.ShouldBeNil)
t.Logf("Pointcloud points: %v", pointcloud.Size())
test.That(t, pointcloud.Size(), test.ShouldBeGreaterThanOrEqualTo, 100)
}
// testOrbslamPosition checks the orbslam position within a defined tolerance.
func testOrbslamPosition(t *testing.T, svc slam.Service, subAlgo viamorbslam3.SubAlgo, actionMode, expectedComponentRef string) {
var expectedPos r3.Vector
expectedOri := &spatialmath.R4AA{}
tolerancePos := 0.5
toleranceOri := 0.5
switch {
case subAlgo == viamorbslam3.Mono && actionMode == "mapping":
expectedPos = r3.Vector{X: 0.020, Y: -0.032, Z: -0.053}
expectedOri = &spatialmath.R4AA{Theta: 0.104, RX: 0.144, RY: 0.980, RZ: -0.137}
case subAlgo == viamorbslam3.Mono && actionMode == "updating":
expectedPos = r3.Vector{X: 0.023, Y: -0.036, Z: -0.040}
expectedOri = &spatialmath.R4AA{Theta: 0.099, RX: 0.092, RY: 0.993, RZ: -0.068}
case subAlgo == viamorbslam3.Rgbd:
expectedPos = r3.Vector{X: -0.001, Y: -0.004, Z: -0.008}
expectedOri = &spatialmath.R4AA{Theta: 0.002, RX: 0.602, RY: -0.772, RZ: -0.202}
}
position, componentRef, err := svc.GetPosition(context.Background())
test.That(t, err, test.ShouldBeNil)
test.That(t, componentRef, test.ShouldEqual, expectedComponentRef)
actualPos := position.Point()
t.Logf("Position point: (%v, %v, %v)", actualPos.X, actualPos.Y, actualPos.Z)
test.That(t, actualPos.X, test.ShouldBeBetween, expectedPos.X-tolerancePos, expectedPos.X+tolerancePos)
test.That(t, actualPos.Y, test.ShouldBeBetween, expectedPos.Y-tolerancePos, expectedPos.Y+tolerancePos)
test.That(t, actualPos.Z, test.ShouldBeBetween, expectedPos.Z-tolerancePos, expectedPos.Z+tolerancePos)
actualOri := position.Orientation().AxisAngles()
t.Logf("Position orientation: RX: %v, RY: %v, RZ: %v, Theta: %v", actualOri.RX, actualOri.RY, actualOri.RZ, actualOri.Theta)
test.That(t, actualOri.RX, test.ShouldBeBetween, expectedOri.RX-toleranceOri, expectedOri.RX+toleranceOri)
test.That(t, actualOri.RY, test.ShouldBeBetween, expectedOri.RY-toleranceOri, expectedOri.RY+toleranceOri)
test.That(t, actualOri.RZ, test.ShouldBeBetween, expectedOri.RZ-toleranceOri, expectedOri.RZ+toleranceOri)
test.That(t, actualOri.Theta, test.ShouldBeBetween, expectedOri.Theta-toleranceOri, expectedOri.Theta+toleranceOri)
}
// testOrbslamInternalState checks the orbslam internal state.
func testOrbslamInternalState(t *testing.T, svc slam.Service, dataDir string) {
internalState, err := slam.GetInternalStateFull(context.Background(), svc)
test.That(t, err, test.ShouldBeNil)
// Save the data from the call to GetInternalState for use in next test.
timeStamp := time.Now()
filename := filepath.Join(dataDir, "map", "orbslam_int_color_camera_data_"+timeStamp.UTC().Format(dataprocess.SlamTimeFormat)+".osa")
err = os.WriteFile(filename, internalState, 0o644)
test.That(t, err, test.ShouldBeNil)
}
func integrationTestHelperOrbslam(t *testing.T, subAlgo viamorbslam3.SubAlgo) {
_, err := exec.LookPath("orb_grpc_server")
if err != nil {
t.Skip("Skipping test because orb_grpc_server binary was not found")
}
logger := golog.NewTestLogger(t)
name, err := testhelper.CreateTempFolderArchitecture(logger)
test.That(t, err, test.ShouldBeNil)
err = createVocabularyFile(name)
test.That(t, err, test.ShouldBeNil)
prevNumFiles := 0
t.Log("\n=== Testing online mode ===\n")
var sensors []string
var expectedMapsOnline, expectedMapsOffline, expectedMapsApriori int
switch subAlgo {
case viamorbslam3.Mono:
sensors = []string{"orbslam_int_webcam"}
expectedMapsOnline = 0
expectedMapsOffline = 1
expectedMapsApriori = expectedMapsOnline + 1
case viamorbslam3.Rgbd:
sensors = []string{"orbslam_int_color_camera", "orbslam_int_depth_camera"}
expectedMapsOnline = 5
expectedMapsOffline = 1
expectedMapsApriori = expectedMapsOnline + 1
default:
t.FailNow()
}
mapRate := 1
deleteProcessedData := false
useLiveData := true
attrCfg := &orbSlamConfig.Config{
Sensors: sensors,
ConfigParams: map[string]string{
"mode": reflect.ValueOf(subAlgo).String(),
"orb_n_features": "1250",
"orb_scale_factor": "1.2",
"orb_n_levels": "8",
"orb_n_ini_th_fast": "20",
"orb_n_min_th_fast": "7",
"debug": "true",
},
DataDirectory: name,
// Even though we don't use the maps saved in this run, indicate in the config that
// we want to save maps because the same yaml config gets used for the next run.
MapRateSec: &mapRate,
DeleteProcessedData: &deleteProcessedData,
UseLiveData: &useLiveData,
}
// Release camera image(s) for service validation
releaseImages(t, subAlgo)
// Create slam service using a real orbslam binary
svc, err := createSLAMService(t, attrCfg, logger, true, true, viamorbslam3.DefaultExecutableName)
test.That(t, err, test.ShouldBeNil)
// Release camera image(s), since orbslam looks for the second most recent image(s)
releaseImages(t, subAlgo)
// Check if orbslam hangs and needs to be shut down
orbslamHangs := false
// Wait for orbslam to finish processing images
logReader := svc.(testhelper.Service).GetSLAMProcessBufferedLogReader()
for i := 0; i < getNumOrbslamImages(subAlgo)-2; i++ {
startTimeSentImage := time.Now()
t.Logf("Find log line for image %v", i)
releaseImages(t, subAlgo)
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Passed image to SLAM") {
prevNumFiles = testhelper.CheckDeleteProcessedData(
t,
subAlgo,
name,
prevNumFiles,
deleteProcessedData,
useLiveData)
break
}
test.That(t, strings.Contains(line, "Fail to track local map!"), test.ShouldBeFalse)
if time.Since(startTimeSentImage) > time.Duration(dataInsertionMaxTimeoutMin)*time.Minute {
orbslamHangs = true
t.Log("orbslam hangs: exiting the data loop")
break
}
}
if orbslamHangs {
break
}
}
testOrbslamPosition(t, svc, subAlgo, "mapping", attrCfg.Sensors[0])
testOrbslamMap(t, svc)
// Close out slam service
err = svc.Close(context.Background())
if !orbslamHangs {
test.That(t, err, test.ShouldBeNil)
} else if err != nil {
t.Skip("Skipping test because orbslam hangs and failed to shut down")
}
// Don't clear out the directory, since we will re-use the config and data for the next run.
closeOutSLAMService(t, "")
// Added sleep to ensure orbslam stops.
time.Sleep(time.Millisecond * orbSleepMsec)
// Test orbslam directory, should have two configs.
testOrbslamDir(t, name, expectedMapsOnline, 2)
// Delete the last image (or image pair) in the data directory so that offline mode runs on
// the same data as online mode. Note: Online mode will not read the last image (or image pair),
// since it always processes the second-most-recent image (or image pair), in case the
// most recent image (or image pair) is still being written.
var directories []string
switch subAlgo {
case viamorbslam3.Mono:
directories = []string{"rgb/"}
case viamorbslam3.Rgbd:
directories = []string{"rgb/", "depth/"}
default:
t.FailNow()
}
for _, directoryName := range directories {
files, err := os.ReadDir(name + "/data/" + directoryName)
test.That(t, err, test.ShouldBeNil)
lastFileName := files[len(files)-1].Name()
test.That(t, os.Remove(name+"/data/"+directoryName+lastFileName), test.ShouldBeNil)
}
prevNumFiles--
// Remove any maps
test.That(t, testhelper.ResetFolder(name+"/map"), test.ShouldBeNil)
// Test offline mode using the config and data generated in the online test
t.Log("\n=== Testing offline mode ===\n")
mapRate = 1
deleteProcessedData = false
useLiveData = false
attrCfg = &orbSlamConfig.Config{
Sensors: []string{},
ConfigParams: map[string]string{
"mode": reflect.ValueOf(subAlgo).String(),
"orb_n_features": "1250",
"orb_scale_factor": "1.2",
"orb_n_levels": "8",
"orb_n_ini_th_fast": "20",
"orb_n_min_th_fast": "7",
"debug": "true",
},
DataDirectory: name,
MapRateSec: &mapRate,
DeleteProcessedData: &deleteProcessedData,
UseLiveData: &useLiveData,
}
// Create slam service using a real orbslam binary
svc, err = createSLAMService(t, attrCfg, golog.NewTestLogger(t), true, true, viamorbslam3.DefaultExecutableName)
test.That(t, err, test.ShouldBeNil)
// Check if orbslam hangs and needs to be shut down
orbslamHangs = false
startTimeSentImage := time.Now()
// Wait for orbslam to finish processing images
logReader = svc.(testhelper.Service).GetSLAMProcessBufferedLogReader()
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Passed image to SLAM") {
prevNumFiles = testhelper.CheckDeleteProcessedData(
t,
subAlgo,
name,
prevNumFiles,
deleteProcessedData,
useLiveData)
startTimeSentImage = time.Now()
}
if strings.Contains(line, "Finished processing offline images") {
break
}
test.That(t, strings.Contains(line, "Fail to track local map!"), test.ShouldBeFalse)
if time.Since(startTimeSentImage) > time.Duration(dataInsertionMaxTimeoutMin)*time.Minute {
orbslamHangs = true
t.Log("orbslam hangs: exiting the data loop")
break
}
}
// Setting to sensors[0] because orbslam interprets the component reference in offline mode
testOrbslamPosition(t, svc, subAlgo, "mapping", sensors[0])
testOrbslamMap(t, svc)
if !orbslamHangs {
// Wait for the final map to be saved
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Finished saving final map") {
break
}
}
}
// Remove maps so that testing is done on the map generated by the internal map
test.That(t, testhelper.ResetFolder(name+"/map"), test.ShouldBeNil)
testOrbslamInternalState(t, svc, name)
// Close out slam service
err = svc.Close(context.Background())
if !orbslamHangs {
test.That(t, err, test.ShouldBeNil)
} else if err != nil {
t.Skip("Skipping test because orbslam hangs and failed to shut down")
}
// Don't clear out the directory, since we will re-use the maps for the next run
closeOutSLAMService(t, "")
// Added sleep to ensure orbslam stops
time.Sleep(time.Millisecond * orbSleepMsec)
// Test orbslam directory, should have two configs
testOrbslamDir(t, name, expectedMapsOffline, 2)
// Remove existing images, but leave maps and config (so we keep the vocabulary file).
// Orbslam will use the most recent config.
test.That(t, testhelper.ResetFolder(name+"/data"), test.ShouldBeNil)
prevNumFiles = 0
// Test online mode using the map generated in the offline test
t.Log("\n=== Testing online mode with saved map ===\n")
mapRate = 1
deleteProcessedData = true
useLiveData = true
attrCfg = &orbSlamConfig.Config{
Sensors: sensors,
ConfigParams: map[string]string{
"mode": reflect.ValueOf(subAlgo).String(),
"orb_n_features": "1250",
"orb_scale_factor": "1.2",
"orb_n_levels": "8",
"orb_n_ini_th_fast": "20",
"orb_n_min_th_fast": "7",
"debug": "true",
},
DataDirectory: name,
MapRateSec: &mapRate,
DeleteProcessedData: &deleteProcessedData,
UseLiveData: &useLiveData,
}
// Release camera image(s) for service validation
releaseImages(t, subAlgo)
// Create slam service using a real orbslam binary
svc, err = createSLAMService(t, attrCfg, golog.NewTestLogger(t), true, true, viamorbslam3.DefaultExecutableName)
test.That(t, err, test.ShouldBeNil)
// Make sure we initialize from a saved map
logReader = svc.(testhelper.Service).GetSLAMProcessBufferedLogReader()
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Initialization of Atlas from file") {
break
}
test.That(t, strings.Contains(line, "Initialization of Atlas from scratch"), test.ShouldBeFalse)
}
// Release camera image(s), since orbslam looks for the second most recent image(s)
releaseImages(t, subAlgo)
// Check if orbslam hangs and needs to be shut down
orbslamHangs = false
// Wait for orbslam to finish processing images
for i := 0; i < getNumOrbslamImages(subAlgo)-2; i++ {
startTimeSentImage = time.Now()
t.Logf("Find log line for image %v", i)
releaseImages(t, subAlgo)
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Passed image to SLAM") {
prevNumFiles = testhelper.CheckDeleteProcessedData(
t,
subAlgo,
name,
prevNumFiles,
deleteProcessedData,
useLiveData)
break
}
test.That(t, strings.Contains(line, "Fail to track local map!"), test.ShouldBeFalse)
if time.Since(startTimeSentImage) > time.Duration(dataInsertionMaxTimeoutMin)*time.Minute {
orbslamHangs = true
t.Log("orbslam hangs: exiting the data loop")
break
}
}
if orbslamHangs {
break
}
}
testOrbslamPosition(t, svc, subAlgo, "updating", attrCfg.Sensors[0])
testOrbslamMap(t, svc)
// Close out slam service
err = svc.Close(context.Background())
if !orbslamHangs {
test.That(t, err, test.ShouldBeNil)
} else if err != nil {
t.Skip("Skipping test because orbslam hangs and failed to shut down")
}
// Added sleep to ensure orbslam stops
time.Sleep(time.Millisecond * orbSleepMsec)
// Test orbslam directory, should have three configs
testOrbslamDir(t, name, expectedMapsApriori, 3)
// Clear out directory
closeOutSLAMService(t, name)
}
// Checks the current slam directory to see if the number of files is around the expected amount
// Because how orbslam runs, the number of maps is not the same between integration tests.
func testOrbslamDir(t *testing.T, path string, expectedMaps, expectedConfigs int) {
mapsInDir, err := os.ReadDir(path + "/map/")
test.That(t, err, test.ShouldBeNil)
test.That(t, len(mapsInDir), test.ShouldBeGreaterThanOrEqualTo, expectedMaps)
configsInDir, err := os.ReadDir(path + "/config/")
test.That(t, err, test.ShouldBeNil)
test.That(t, len(configsInDir), test.ShouldEqual, expectedConfigs)
}
func TestOrbslamIntegrationRGBD(t *testing.T) {
integrationTestHelperOrbslam(t, viamorbslam3.Rgbd)
}
func TestOrbslamIntegrationMono(t *testing.T) {
integrationTestHelperOrbslam(t, viamorbslam3.Mono)
}