forked from viam-modules/viam-cartographer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
410 lines (345 loc) · 14.3 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
// Package viamcartographer_test tests the functions that require injected components (such as robot and camera)
// in order to be run. It utilizes the internal package located in testhelper.go to access
// certain exported functions which we do not want to make available to the user. It also runs integration tests
// that test the interaction with the core C++ viam-cartographer code and the Golang implementation of the
// cartographer slam service.
package viamcartographer_test
import (
"bytes"
"context"
"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"
slamConfig "go.viam.com/slam/config"
"go.viam.com/slam/dataprocess"
slamTesthelper "go.viam.com/slam/testhelper"
"go.viam.com/test"
viamcartographer "github.com/viamrobotics/viam-cartographer"
"github.com/viamrobotics/viam-cartographer/internal/testhelper"
)
const (
cartoSleepMsec = 100
)
// Checks the cartographer map and confirms there at least 100 map points.
func testCartographerMap(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, _ := pointcloud.ReadPCD(bytes.NewReader(pcd))
t.Logf("Pointcloud points: %v", pointcloud.Size())
test.That(t, pointcloud.Size(), test.ShouldBeGreaterThanOrEqualTo, 100)
}
// Checks the cartographer position within a defined tolerance.
func testCartographerPosition(t *testing.T, svc slam.Service, expectedComponentRef string) {
expectedPos := r3.Vector{X: -0.004, Y: -0.004, Z: 0}
tolerancePos := 0.04
expectedOri := &spatialmath.R4AA{Theta: 0, RX: 0, RY: 0, RZ: -1}
toleranceOri := 0.5
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)
}
// Checks the cartographer internal state.
func testCartographerInternalState(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", "map_data_"+timeStamp.UTC().Format(dataprocess.SlamTimeFormat)+".pbstream")
err = os.WriteFile(filename, internalState, 0o644)
test.That(t, err, test.ShouldBeNil)
}
func integrationtestHelperCartographer(t *testing.T, subAlgo viamcartographer.SubAlgo) {
logger := golog.NewTestLogger(t)
_, err := exec.LookPath("carto_grpc_server")
if err != nil {
t.Log("Skipping test because carto_grpc_server binary was not found")
t.Skip()
}
dataDir, err := slamTesthelper.CreateTempFolderArchitecture(logger)
test.That(t, err, test.ShouldBeNil)
prevNumFiles := 0
t.Log("\n=== Testing online mode ===\n")
mapRateSec := 9999
deleteProcessedData := false
useLiveData := true
attrCfg := &slamConfig.Config{
Sensors: []string{"cartographer_int_lidar"},
ConfigParams: map[string]string{
"mode": reflect.ValueOf(subAlgo).String(),
"v": "1",
"debug": "true",
},
MapRateSec: &mapRateSec,
DataDirectory: dataDir,
DeleteProcessedData: &deleteProcessedData,
UseLiveData: &useLiveData,
}
// Release point cloud for service validation
testhelper.IntegrationLidarReleasePointCloudChan <- 1
// Create slam service using a real cartographer binary
svc, err := testhelper.CreateSLAMService(t, attrCfg, logger, true, viamcartographer.DefaultExecutableName)
test.That(t, err, test.ShouldBeNil)
// Release point cloud, since cartographer looks for the second most recent point cloud
testhelper.IntegrationLidarReleasePointCloudChan <- 1
// Make sure we initialize in mapping mode
logReader := svc.(testhelper.Service).GetSLAMProcessBufferedLogReader()
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Running in mapping mode") {
break
}
test.That(t, strings.Contains(line, "Running in updating mode"), test.ShouldBeFalse)
test.That(t, strings.Contains(line, "Running in localization only mode"), test.ShouldBeFalse)
}
// Wait for cartographer to finish processing data
for i := 0; i < testhelper.NumPointClouds-2; i++ {
t.Logf("Find log line for point cloud %v", i)
testhelper.IntegrationLidarReleasePointCloudChan <- 1
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Passed sensor data to SLAM") {
prevNumFiles = testhelper.CheckDeleteProcessedData(
t,
subAlgo,
dataDir,
prevNumFiles,
deleteProcessedData,
useLiveData)
break
}
}
}
testCartographerPosition(t, svc, attrCfg.Sensors[0])
testCartographerMap(t, svc)
// Close out slam service
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
// Sleep to ensure cartographer stops
time.Sleep(time.Millisecond * cartoSleepMsec)
// Delete the last .pcd file in the data directory, so that offline mode runs on the
// same data as online mode. (Online mode will not read the last .pcd file, since it
// always processes the second-most-recent .pcd file, in case the most-recent .pcd
// file is currently being written.)
files, err := os.ReadDir(dataDir + "/data/")
test.That(t, err, test.ShouldBeNil)
lastFileName := files[len(files)-1].Name()
test.That(t, os.Remove(dataDir+"/data/"+lastFileName), test.ShouldBeNil)
prevNumFiles--
// Check that no maps were generated during previous test
testCartographerDir(t, dataDir, 0)
// Test offline mode using the data generated in the online test
t.Log("\n=== Testing offline mode ===\n")
useLiveData = false
mapRateSec = 1
attrCfg = &slamConfig.Config{
Sensors: []string{},
ConfigParams: map[string]string{
"mode": reflect.ValueOf(subAlgo).String(),
"v": "1",
},
MapRateSec: &mapRateSec,
DataDirectory: dataDir,
DeleteProcessedData: &deleteProcessedData,
UseLiveData: &useLiveData,
}
// Create slam service using a real cartographer binary
svc, err = testhelper.CreateSLAMService(t, attrCfg, golog.NewTestLogger(t), true, viamcartographer.DefaultExecutableName)
test.That(t, err, test.ShouldBeNil)
// Make sure we initialize in mapping mode
logReader = svc.(testhelper.Service).GetSLAMProcessBufferedLogReader()
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Running in mapping mode") {
break
}
test.That(t, strings.Contains(line, "Running in updating mode"), test.ShouldBeFalse)
test.That(t, strings.Contains(line, "Running in localization only mode"), test.ShouldBeFalse)
}
// Wait for cartographer to finish processing data
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Passed sensor data to SLAM") {
prevNumFiles = testhelper.CheckDeleteProcessedData(
t,
subAlgo,
dataDir,
prevNumFiles,
deleteProcessedData,
useLiveData)
}
if strings.Contains(line, "Finished optimizing final map") {
break
}
}
testCartographerPosition(t, svc, "") // leaving this empty because cartographer does not interpret the component reference in offline mode
testCartographerMap(t, svc)
// Sleep to ensure cartographer saves at least one map
time.Sleep(time.Second * time.Duration(*attrCfg.MapRateSec))
// Close out slam service
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
// Sleep to ensure cartographer stops
time.Sleep(time.Millisecond * cartoSleepMsec)
// Remove existing pointclouds, but leave maps and config (so we keep the lua files).
test.That(t, slamTesthelper.ResetFolder(dataDir+"/data"), test.ShouldBeNil)
prevNumFiles = 0
// Count the initial number of maps in the map directory (should equal 1)
testCartographerDir(t, dataDir, 1)
// Test online mode using the map generated in the offline test
t.Log("\n=== Testing online localization mode ===\n")
mapRateSec = 0
deleteProcessedData = true
useLiveData = true
attrCfg = &slamConfig.Config{
Sensors: []string{"cartographer_int_lidar"},
ConfigParams: map[string]string{
"mode": reflect.ValueOf(subAlgo).String(),
"v": "1",
},
MapRateSec: &mapRateSec,
DataDirectory: dataDir,
DeleteProcessedData: &deleteProcessedData,
UseLiveData: &useLiveData,
}
// Release point cloud for service validation
testhelper.IntegrationLidarReleasePointCloudChan <- 1
// Create slam service using a real cartographer binary
svc, err = testhelper.CreateSLAMService(t, attrCfg, golog.NewTestLogger(t), true, viamcartographer.DefaultExecutableName)
test.That(t, err, test.ShouldBeNil)
// Make sure we initialize in localization mode
logReader = svc.(testhelper.Service).GetSLAMProcessBufferedLogReader()
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Running in localization only mode") {
break
}
test.That(t, strings.Contains(line, "Running in updating mode"), test.ShouldBeFalse)
test.That(t, strings.Contains(line, "Running in mapping mode"), test.ShouldBeFalse)
}
// Release point cloud, since cartographer looks for the second most recent point cloud
testhelper.IntegrationLidarReleasePointCloudChan <- 1
for i := 0; i < testhelper.NumPointClouds-2; i++ {
t.Logf("Find log line for point cloud %v", i)
testhelper.IntegrationLidarReleasePointCloudChan <- 1
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Passed sensor data to SLAM") {
prevNumFiles = testhelper.CheckDeleteProcessedData(
t,
subAlgo,
dataDir,
prevNumFiles,
deleteProcessedData,
useLiveData)
break
}
}
}
testCartographerPosition(t, svc, attrCfg.Sensors[0])
testCartographerMap(t, svc)
// Remove maps so that testing is done on the map generated by the internal map
test.That(t, slamTesthelper.ResetFolder(dataDir+"/map"), test.ShouldBeNil)
testCartographerInternalState(t, svc, dataDir)
// Close out slam service
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
// Test that only the map present is the one generated by the GetInternalState call
testCartographerDir(t, dataDir, 1)
// Sleep to ensure cartographer stops
time.Sleep(time.Millisecond * cartoSleepMsec)
// Remove existing pointclouds, but leave maps and config (so we keep the lua files).
test.That(t, slamTesthelper.ResetFolder(dataDir+"/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")
mapRateSec = 1
attrCfg = &slamConfig.Config{
Sensors: []string{"cartographer_int_lidar"},
ConfigParams: map[string]string{
"mode": reflect.ValueOf(subAlgo).String(),
"v": "1",
},
MapRateSec: &mapRateSec,
DataDirectory: dataDir,
UseLiveData: &useLiveData,
}
// Release point cloud for service validation
testhelper.IntegrationLidarReleasePointCloudChan <- 1
// Create slam service using a real cartographer binary
svc, err = testhelper.CreateSLAMService(t, attrCfg, golog.NewTestLogger(t), true, viamcartographer.DefaultExecutableName)
test.That(t, err, test.ShouldBeNil)
// Make sure we initialize in updating mode
logReader = svc.(testhelper.Service).GetSLAMProcessBufferedLogReader()
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Running in updating mode") {
break
}
test.That(t, strings.Contains(line, "Running in localization only mode"), test.ShouldBeFalse)
test.That(t, strings.Contains(line, "Running in mapping mode"), test.ShouldBeFalse)
}
// Release point cloud, since cartographer looks for the second most recent point cloud
testhelper.IntegrationLidarReleasePointCloudChan <- 1
for i := 0; i < testhelper.NumPointClouds-2; i++ {
t.Logf("Find log line for point cloud %v", i)
testhelper.IntegrationLidarReleasePointCloudChan <- 1
for {
line, err := logReader.ReadString('\n')
test.That(t, err, test.ShouldBeNil)
if strings.Contains(line, "Passed sensor data to SLAM") {
prevNumFiles = testhelper.CheckDeleteProcessedData(
t,
subAlgo,
dataDir,
prevNumFiles,
deleteProcessedData,
useLiveData)
break
}
test.That(t, strings.Contains(line, "Failed to open proto stream"), test.ShouldBeFalse)
test.That(t, strings.Contains(line, "Failed to read SerializationHeader"), test.ShouldBeFalse)
}
}
testCartographerPosition(t, svc, attrCfg.Sensors[0])
testCartographerMap(t, svc)
// Close out slam service
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
// Test that a new map was generated
testCartographerDir(t, dataDir, 2)
// Clear out directory
testhelper.ClearDirectory(t, dataDir)
}
// Checks the current slam directory to see if the number of files matches the expected amount.
func testCartographerDir(t *testing.T, path string, expectedMaps int) {
mapsInDir, err := os.ReadDir(path + "/map/")
test.That(t, err, test.ShouldBeNil)
test.That(t, len(mapsInDir), test.ShouldBeGreaterThanOrEqualTo, expectedMaps)
}
func TestCartographerIntegration2D(t *testing.T) {
integrationtestHelperCartographer(t, viamcartographer.Dim2d)
}