forked from viam-modules/viam-cartographer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration_test.go
299 lines (251 loc) · 11.6 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
// 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"
"errors"
"os"
"path"
"path/filepath"
"reflect"
"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"
viamcartographer "github.com/viamrobotics/viam-cartographer"
"github.com/viamrobotics/viam-cartographer/cartofacade"
vcConfig "github.com/viamrobotics/viam-cartographer/config"
"github.com/viamrobotics/viam-cartographer/testhelper"
)
// Checks the cartographer map and confirms there at least 100 map points.
func testCartographerMap(t *testing.T, svc slam.Service, localizationMode bool) {
timestamp1, err := svc.GetLatestMapInfo(context.Background())
test.That(t, err, test.ShouldBeNil)
pcd, err := slam.GetPointCloudMapFull(context.Background(), svc)
test.That(t, err, test.ShouldBeNil)
test.That(t, pcd, test.ShouldNotBeNil)
timestamp2, err := svc.GetLatestMapInfo(context.Background())
test.That(t, err, test.ShouldBeNil)
if localizationMode == true {
test.That(t, timestamp1, test.ShouldResemble, timestamp2)
} else {
test.That(t, timestamp2.After(timestamp1), test.ShouldBeTrue)
}
pointcloud, _ := pointcloud.ReadPCD(bytes.NewReader(pcd))
t.Logf("Pointcloud points: %v", pointcloud.Size())
test.That(t, pointcloud.Size(), test.ShouldBeGreaterThanOrEqualTo, 100)
}
func testCartographerPosition(t *testing.T, svc slam.Service, expectedComponentRef string) {
expectedPosOSX := r3.Vector{X: 155.7488316264227, Y: -90.25868252233964, Z: 0}
expectedPosLinux := r3.Vector{X: 158.79903385710674, Y: -77.01514065531592, Z: 0}
tolerancePos := 0.001
expectedOriOSX := &spatialmath.R4AA{Theta: 1.5465081272043815, RX: 0, RY: 0, RZ: 1}
expectedOriLinux := &spatialmath.R4AA{Theta: 0.3331667853231311, RX: 0, RY: 0, RZ: 1}
toleranceOri := 0.001
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)
// https://viam.atlassian.net/browse/RSDK-3866
// mac
if actualPos.X > expectedPosOSX.X-tolerancePos && actualPos.X < expectedPosOSX.X+tolerancePos {
test.That(t, actualPos.Y, test.ShouldBeBetween, expectedPosOSX.Y-tolerancePos, expectedPosOSX.Y+tolerancePos)
test.That(t, actualPos.Z, test.ShouldBeBetween, expectedPosOSX.Z-tolerancePos, expectedPosOSX.Z+tolerancePos)
// linux
} else if actualPos.X > expectedPosLinux.X-tolerancePos && actualPos.X < expectedPosLinux.X+tolerancePos {
test.That(t, actualPos.Y, test.ShouldBeBetween, expectedPosLinux.Y-tolerancePos, expectedPosLinux.Y+tolerancePos)
test.That(t, actualPos.Z, test.ShouldBeBetween, expectedPosLinux.Z-tolerancePos, expectedPosLinux.Z+tolerancePos)
} else {
t.Error("Position is outside of expected platform range")
}
actualOri := position.Orientation().AxisAngles()
t.Logf("Position orientation: RX: %v, RY: %v, RZ: %v, Theta: %v", actualOri.RX, actualOri.RY, actualOri.RZ, actualOri.Theta)
if actualOri.Theta > expectedOriOSX.Theta-toleranceOri && actualOri.Theta < expectedOriOSX.Theta+toleranceOri {
test.That(t, actualOri.RX, test.ShouldBeBetween, expectedOriOSX.RX-toleranceOri, expectedOriOSX.RX+toleranceOri)
test.That(t, actualOri.RY, test.ShouldBeBetween, expectedOriOSX.RY-toleranceOri, expectedOriOSX.RY+toleranceOri)
test.That(t, actualOri.Theta, test.ShouldBeBetween, expectedOriOSX.Theta-toleranceOri, expectedOriOSX.Theta+toleranceOri)
} else if actualOri.Theta > expectedOriLinux.Theta-toleranceOri && actualOri.Theta < expectedOriLinux.Theta+toleranceOri {
test.That(t, actualOri.RX, test.ShouldBeBetween, expectedOriLinux.RX-toleranceOri, expectedOriLinux.RX+toleranceOri)
test.That(t, actualOri.RY, test.ShouldBeBetween, expectedOriLinux.RY-toleranceOri, expectedOriLinux.RY+toleranceOri)
test.That(t, actualOri.RZ, test.ShouldBeBetween, expectedOriLinux.RZ-toleranceOri, expectedOriLinux.RZ+toleranceOri)
} else {
t.Error("Orientation is outside of expected platform range")
}
}
func saveInternalState(t *testing.T, internalState []byte, dataDir string) {
timeStamp := time.Now()
internalStateDir := filepath.Join(dataDir, "internal_state")
if err := os.Mkdir(internalStateDir, 0o755); err != nil {
t.Error("failed to create test internal state directory")
}
filename := filepath.Join(internalStateDir, "map_data_"+timeStamp.UTC().Format(testhelper.SlamTimeFormat)+".pbstream")
if err := os.WriteFile(filename, internalState, 0o644); err != nil {
t.Error("failed to write test internal state")
}
}
func testHelperCartographer(
t *testing.T,
dataDirectory string,
subAlgo viamcartographer.SubAlgo,
logger golog.Logger,
replaySensor bool,
mapRateSec int,
expectedMode cartofacade.SlamMode,
) []byte {
termFunc := testhelper.InitTestCL(t, logger)
defer termFunc()
attrCfg := &vcConfig.Config{
Sensors: []string{"stub_lidar"},
ConfigParams: map[string]string{
"mode": reflect.ValueOf(subAlgo).String(),
},
MapRateSec: &mapRateSec,
DataDirectory: dataDirectory,
}
done := make(chan struct{})
sensorReadingInterval := time.Millisecond * 200
timedSensor, err := testhelper.IntegrationLidarTimedSensor(t, attrCfg.Sensors[0], replaySensor, sensorReadingInterval, done)
test.That(t, err, test.ShouldBeNil)
svc, err := testhelper.CreateIntegrationSLAMService(t, attrCfg, timedSensor, logger)
test.That(t, err, test.ShouldBeNil)
start := time.Now()
cSvc, ok := svc.(*viamcartographer.CartographerService)
test.That(t, ok, test.ShouldBeTrue)
test.That(t, cSvc.SlamMode, test.ShouldEqual, expectedMode)
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFunc()
// wait till all lidar readings have been read
if !utils.SelectContextOrWaitChan(ctx, done) {
test.That(t, errors.New("test timeout"), test.ShouldBeNil)
}
testCartographerPosition(t, svc, attrCfg.Sensors[0])
testCartographerMap(t, svc, cSvc.SlamMode == cartofacade.LocalizingMode)
internalState, err := slam.GetInternalStateFull(context.Background(), svc)
test.That(t, err, test.ShouldBeNil)
// Close out slam service
test.That(t, svc.Close(context.Background()), test.ShouldBeNil)
stop := time.Now()
testDuration := stop.Sub(start)
t.Logf("test duration %d", testDuration)
// Check that a map was generated as the test has been running for more than the map rate msec
mapsInDir, err := os.ReadDir(path.Join(dataDirectory, "internal_state"))
test.That(t, err, test.ShouldBeNil)
test.That(t, testDuration.Seconds(), test.ShouldBeGreaterThanOrEqualTo, time.Duration(*attrCfg.MapRateSec).Seconds())
test.That(t, len(mapsInDir), test.ShouldBeGreaterThan, 0)
// return the internal state so updating mode can be tested
return internalState
}
func integrationtestHelperCartographer(t *testing.T, subAlgo viamcartographer.SubAlgo) {
logger := golog.NewTestLogger(t)
t.Run("live sensor mapping mode", func(t *testing.T) {
dataDirectory, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer func() {
err := os.RemoveAll(dataDirectory)
test.That(t, err, test.ShouldBeNil)
}()
testHelperCartographer(t, dataDirectory, subAlgo, logger, false, 1, cartofacade.MappingMode)
})
t.Run("replay sensor mapping mode", func(t *testing.T) {
dataDirectory, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer func() {
err := os.RemoveAll(dataDirectory)
test.That(t, err, test.ShouldBeNil)
}()
testHelperCartographer(t, dataDirectory, subAlgo, logger, true, 1, cartofacade.MappingMode)
})
t.Run("live sensor localizing mode", func(t *testing.T) {
dataDirectoryMapping, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer func() {
err := os.RemoveAll(dataDirectoryMapping)
test.That(t, err, test.ShouldBeNil)
}()
// do a mapping run with replay sensor
internalState := testHelperCartographer(t, dataDirectoryMapping, subAlgo, logger, true, 1, cartofacade.MappingMode)
dataDirectoryLocalizing, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer func() {
err := os.RemoveAll(dataDirectoryLocalizing)
test.That(t, err, test.ShouldBeNil)
}()
// save the internal state of the mapping run to a new datadir
saveInternalState(t, internalState, dataDirectoryLocalizing)
// localize on that internal state
testHelperCartographer(t, dataDirectoryLocalizing, subAlgo, logger, false, 0, cartofacade.LocalizingMode)
})
t.Run("replay sensor localizing mode", func(t *testing.T) {
dataDirectoryMapping, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer func() {
err := os.RemoveAll(dataDirectoryMapping)
test.That(t, err, test.ShouldBeNil)
}()
// do a mapping run with replay sensor
internalState := testHelperCartographer(t, dataDirectoryMapping, subAlgo, logger, true, 1, cartofacade.MappingMode)
dataDirectoryLocalizing, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer func() {
err := os.RemoveAll(dataDirectoryLocalizing)
test.That(t, err, test.ShouldBeNil)
}()
// save the internal state of the mapping run to a new datadir
saveInternalState(t, internalState, dataDirectoryLocalizing)
// localize on that internal state
testHelperCartographer(t, dataDirectoryLocalizing, subAlgo, logger, true, 0, cartofacade.LocalizingMode)
})
t.Run("live sensor updating mode", func(t *testing.T) {
dataDirectoryMapping, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer func() {
err := os.RemoveAll(dataDirectoryMapping)
test.That(t, err, test.ShouldBeNil)
}()
// do a mapping run
internalState := testHelperCartographer(t, dataDirectoryMapping, subAlgo, logger, true, 1, cartofacade.MappingMode)
dataDirectoryUpdating, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer func() {
err := os.RemoveAll(dataDirectoryUpdating)
test.That(t, err, test.ShouldBeNil)
}()
// save the internal state of the mapping run to a new datadir
saveInternalState(t, internalState, dataDirectoryUpdating)
// update fromthat internal state
testHelperCartographer(t, dataDirectoryUpdating, subAlgo, logger, false, 1, cartofacade.UpdatingMode)
})
t.Run("replay sensor updating mode", func(t *testing.T) {
dataDirectoryMapping, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer func() {
err := os.RemoveAll(dataDirectoryMapping)
test.That(t, err, test.ShouldBeNil)
}()
// do a mapping run
internalState := testHelperCartographer(t, dataDirectoryMapping, subAlgo, logger, true, 1, cartofacade.MappingMode)
dataDirectoryUpdating, err := os.MkdirTemp("", "*")
test.That(t, err, test.ShouldBeNil)
defer func() {
err := os.RemoveAll(dataDirectoryUpdating)
test.That(t, err, test.ShouldBeNil)
}()
// save the internal state of the mapping run to a new datadir
saveInternalState(t, internalState, dataDirectoryUpdating)
// update fromthat internal state
testHelperCartographer(t, dataDirectoryUpdating, subAlgo, logger, true, 1, cartofacade.UpdatingMode)
})
}
func TestCartographerIntegration2D(t *testing.T) {
integrationtestHelperCartographer(t, viamcartographer.Dim2d)
}