forked from viam-modules/viam-cartographer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
viam-cartographer.go
449 lines (391 loc) · 14 KB
/
viam-cartographer.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
// Package viamcartographer implements simultaneous localization and mapping.
// This is an Experimental package.
package viamcartographer
import (
"bufio"
"context"
"io"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/edaniels/golog"
"github.com/pkg/errors"
"go.opencensus.io/trace"
pb "go.viam.com/api/service/slam/v1"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/services/slam"
"go.viam.com/rdk/services/slam/grpchelper"
"go.viam.com/rdk/spatialmath"
slamConfig "go.viam.com/slam/config"
"go.viam.com/slam/sensors/lidar"
slamUtils "go.viam.com/slam/utils"
goutils "go.viam.com/utils"
"go.viam.com/utils/pexec"
dim2d "github.com/viamrobotics/viam-cartographer/internal/dim-2d"
)
// Model is the model name of cartographer.
var Model = resource.NewModel("viam", "slam", "cartographer")
const (
// DefaultExecutableName is what this program expects to call to start the cartographer grpc server.
DefaultExecutableName = "carto_grpc_server"
defaultDataRateMsec = 200
defaultMapRateSec = 60
defaultDialMaxTimeoutSec = 30
defaultSensorValidationMaxTimeoutSec = 30
defaultSensorValidationIntervalSec = 1
parsePortMaxTimeoutSec = 60
localhost0 = "localhost:0"
)
// SubAlgo defines the cartographer specific sub-algorithms that we support.
type SubAlgo string
// Dim2d runs cartographer with a 2D LIDAR only.
const Dim2d SubAlgo = "2d"
func init() {
resource.RegisterService(slam.API, Model, resource.Registration[slam.Service, *slamConfig.Config]{
Constructor: func(
ctx context.Context,
deps resource.Dependencies,
c resource.Config,
logger golog.Logger,
) (slam.Service, error) {
return New(
ctx,
deps,
c,
logger,
false,
DefaultExecutableName,
defaultSensorValidationMaxTimeoutSec,
defaultSensorValidationIntervalSec,
defaultDialMaxTimeoutSec,
)
},
})
}
// New returns a new slam service for the given robot.
func New(
ctx context.Context,
deps resource.Dependencies,
c resource.Config,
logger golog.Logger,
bufferSLAMProcessLogs bool,
executableName string,
sensorValidationMaxTimeoutSec int,
sensorValidationIntervalSec int,
dialMaxTimeoutSec int,
) (slam.Service, error) {
ctx, span := trace.StartSpan(ctx, "viamcartographer::slamService::New")
defer span.End()
svcConfig, err := resource.NativeConfig[*slamConfig.Config](c)
if err != nil {
return nil, err
}
subAlgo := SubAlgo(svcConfig.ConfigParams["mode"])
if subAlgo != Dim2d {
return nil, errors.Errorf("%v does not have a 'mode: %v'",
c.Model.Name, svcConfig.ConfigParams["mode"])
}
// Set up the data directories
if err := slamConfig.SetupDirectories(svcConfig.DataDirectory, logger); err != nil {
return nil, err
}
port, dataRateMsec, mapRateSec, useLiveData, deleteProcessedData, err := slamConfig.GetOptionalParameters(
svcConfig,
localhost0,
defaultDataRateMsec,
defaultMapRateSec,
logger,
)
if err != nil {
return nil, err
}
// Get the lidar for the Dim2D cartographer sub algorithm
lidar, err := dim2d.NewLidar(ctx, deps, svcConfig.Sensors, logger)
if err != nil {
return nil, err
}
// Need to pass in a long-lived context because ctx is short-lived
cancelCtx, cancelFunc := context.WithCancel(context.Background())
// Cartographer SLAM Service Object
cartoSvc := &cartographerService{
Named: c.ResourceName().AsNamed(),
primarySensorName: lidar.Name,
executableName: executableName,
subAlgo: subAlgo,
slamProcess: pexec.NewProcessManager(logger),
configParams: svcConfig.ConfigParams,
dataDirectory: svcConfig.DataDirectory,
useLiveData: useLiveData,
deleteProcessedData: deleteProcessedData,
port: port,
dataRateMs: dataRateMsec,
mapRateSec: mapRateSec,
cancelFunc: cancelFunc,
logger: logger,
bufferSLAMProcessLogs: bufferSLAMProcessLogs,
}
success := false
defer func() {
if !success {
if err := cartoSvc.Close(ctx); err != nil {
logger.Errorw("error closing out after error", "error", err)
}
}
}()
if cartoSvc.useLiveData {
if err := dim2d.ValidateGetAndSaveData(cancelCtx, cartoSvc.dataDirectory, lidar,
sensorValidationMaxTimeoutSec, sensorValidationIntervalSec, cartoSvc.logger); err != nil {
return nil, errors.Wrap(err, "getting and saving data failed")
}
cartoSvc.StartDataProcess(cancelCtx, lidar, nil)
logger.Debug("Running in live mode")
} else {
logger.Debug("Running in offline mode")
}
if err := cartoSvc.StartSLAMProcess(ctx); err != nil {
return nil, errors.Wrap(err, "error with slam service slam process")
}
client, clientClose, err := slamConfig.SetupGRPCConnection(ctx, cartoSvc.port, dialMaxTimeoutSec, logger)
if err != nil {
return nil, errors.Wrap(err, "error with initial grpc client to slam algorithm")
}
cartoSvc.clientAlgo = client
cartoSvc.clientAlgoClose = clientClose
success = true
return cartoSvc, nil
}
// cartographerService is the structure of the slam service.
type cartographerService struct {
resource.Named
resource.AlwaysRebuild
primarySensorName string
executableName string
subAlgo SubAlgo
slamProcess pexec.ProcessManager
clientAlgo pb.SLAMServiceClient
clientAlgoClose func() error
configParams map[string]string
dataDirectory string
deleteProcessedData bool
useLiveData bool
port string
dataRateMs int
mapRateSec int
cancelFunc func()
logger golog.Logger
activeBackgroundWorkers sync.WaitGroup
bufferSLAMProcessLogs bool
slamProcessLogReader io.ReadCloser
slamProcessLogWriter io.WriteCloser
slamProcessBufferedLogReader bufio.Reader
}
// GetPosition forwards the request for positional data to the slam library's gRPC service. Once a response is received,
// it is unpacked into a Pose and a component reference string.
func (cartoSvc *cartographerService) GetPosition(ctx context.Context) (spatialmath.Pose, string, error) {
ctx, span := trace.StartSpan(ctx, "viamcartographer::cartographerService::GetPosition")
defer span.End()
req := &pb.GetPositionRequest{Name: cartoSvc.Name().ShortName()}
resp, err := cartoSvc.clientAlgo.GetPosition(ctx, req)
if err != nil {
return nil, "", errors.Wrap(err, "error getting SLAM position")
}
pose := spatialmath.NewPoseFromProtobuf(resp.GetPose())
componentReference := resp.GetComponentReference()
returnedExt := resp.Extra.AsMap()
return slamUtils.CheckQuaternionFromClientAlgo(pose, componentReference, returnedExt)
}
// GetPointCloudMap creates a request, calls the slam algorithms GetPointCloudMap endpoint and returns a callback
// function which will return the next chunk of the current pointcloud map.
func (cartoSvc *cartographerService) GetPointCloudMap(ctx context.Context) (func() ([]byte, error), error) {
ctx, span := trace.StartSpan(ctx, "viamcartographer::cartographerService::GetPointCloudMap")
defer span.End()
return grpchelper.GetPointCloudMapCallback(ctx, cartoSvc.Name().ShortName(), cartoSvc.clientAlgo)
}
// GetInternalState creates a request, calls the slam algorithms GetInternalState endpoint and returns a callback
// function which will return the next chunk of the current internal state of the slam algo.
func (cartoSvc *cartographerService) GetInternalState(ctx context.Context) (func() ([]byte, error), error) {
ctx, span := trace.StartSpan(ctx, "viamcartographer::cartographerService::GetInternalState")
defer span.End()
return grpchelper.GetInternalStateCallback(ctx, cartoSvc.Name().ShortName(), cartoSvc.clientAlgo)
}
// StartDataProcess starts a go routine that saves data from the lidar to the user-defined data directory.
func (cartoSvc *cartographerService) StartDataProcess(
cancelCtx context.Context,
lidar lidar.Lidar,
c chan int,
) {
cartoSvc.activeBackgroundWorkers.Add(1)
if err := cancelCtx.Err(); err != nil {
if !errors.Is(err, context.Canceled) {
cartoSvc.logger.Errorw("unexpected error in SLAM service", "error", err)
}
cartoSvc.activeBackgroundWorkers.Done()
return
}
goutils.PanicCapturingGo(func() {
ticker := time.NewTicker(time.Millisecond * time.Duration(cartoSvc.dataRateMs))
defer ticker.Stop()
defer cartoSvc.activeBackgroundWorkers.Done()
for {
if err := cancelCtx.Err(); err != nil {
if !errors.Is(err, context.Canceled) {
cartoSvc.logger.Errorw("unexpected error in SLAM data process", "error", err)
}
return
}
select {
case <-cancelCtx.Done():
return
case <-ticker.C:
cartoSvc.activeBackgroundWorkers.Add(1)
if err := cancelCtx.Err(); err != nil {
if !errors.Is(err, context.Canceled) {
cartoSvc.logger.Errorw("unexpected error in SLAM data process", "error", err)
}
cartoSvc.activeBackgroundWorkers.Done()
return
}
goutils.PanicCapturingGo(func() {
defer cartoSvc.activeBackgroundWorkers.Done()
if _, err := dim2d.GetAndSaveData(cancelCtx, cartoSvc.dataDirectory, lidar, cartoSvc.logger); err != nil {
cartoSvc.logger.Warn(err)
}
if c != nil {
c <- 1
}
})
}
}
})
}
// Close out of all slam related processes.
func (cartoSvc *cartographerService) Close(ctx context.Context) error {
defer func() {
if cartoSvc.clientAlgoClose != nil {
goutils.UncheckedErrorFunc(cartoSvc.clientAlgoClose)
}
}()
cartoSvc.cancelFunc()
if cartoSvc.bufferSLAMProcessLogs {
if cartoSvc.slamProcessLogReader != nil {
if err := cartoSvc.slamProcessLogReader.Close(); err != nil {
return errors.Wrap(err, "error occurred during closeout of slam log reader")
}
}
if cartoSvc.slamProcessLogWriter != nil {
if err := cartoSvc.slamProcessLogWriter.Close(); err != nil {
return errors.Wrap(err, "error occurred during closeout of slam log writer")
}
}
}
if err := cartoSvc.StopSLAMProcess(); err != nil {
return errors.Wrap(err, "error occurred during closeout of process")
}
cartoSvc.activeBackgroundWorkers.Wait()
return nil
}
// GetSLAMProcessConfig returns the process config for the SLAM process.
func (cartoSvc *cartographerService) GetSLAMProcessConfig() pexec.ProcessConfig {
var args []string
args = append(args, "-sensors="+cartoSvc.primarySensorName)
args = append(args, "-config_param="+slamUtils.DictToString(cartoSvc.configParams))
args = append(args, "-data_rate_ms="+strconv.Itoa(cartoSvc.dataRateMs))
args = append(args, "-map_rate_sec="+strconv.Itoa(cartoSvc.mapRateSec))
args = append(args, "-data_dir="+cartoSvc.dataDirectory)
args = append(args, "-delete_processed_data="+strconv.FormatBool(cartoSvc.deleteProcessedData))
args = append(args, "-use_live_data="+strconv.FormatBool(cartoSvc.useLiveData))
args = append(args, "-port="+cartoSvc.port)
args = append(args, "--aix-auto-update")
target := cartoSvc.executableName
appDir := os.Getenv("APPDIR")
if appDir != "" {
// The carto grpc server is expected to be in
// /usr/bin/ if we are running in an AppImage
target = appDir + "/usr/bin/" + strings.TrimPrefix(target, "/")
}
return pexec.ProcessConfig{
ID: "slam_cartographer",
Name: target,
Args: args,
Log: true,
// In appimage this is set to the appimage
// squashfs mount location (/tmp/.mountXXXXX)
// Otherwise, it is an empty string
CWD: os.Getenv("APPRUN_RUNTIME"),
OneShot: false,
}
}
func (cartoSvc *cartographerService) GetSLAMProcessBufferedLogReader() bufio.Reader {
return cartoSvc.slamProcessBufferedLogReader
}
// startSLAMProcess starts up the SLAM library process by calling the executable binary and giving it the necessary arguments.
func (cartoSvc *cartographerService) StartSLAMProcess(ctx context.Context) error {
ctx, span := trace.StartSpan(ctx, "viamcartographer::slamService::StartSLAMProcess")
defer span.End()
processConfig := cartoSvc.GetSLAMProcessConfig()
var logReader io.ReadCloser
var logWriter io.WriteCloser
var bufferedLogReader bufio.Reader
if cartoSvc.port == localhost0 || cartoSvc.bufferSLAMProcessLogs {
logReader, logWriter = io.Pipe()
bufferedLogReader = *bufio.NewReader(logReader)
processConfig.LogWriter = logWriter
}
_, err := cartoSvc.slamProcess.AddProcessFromConfig(ctx, processConfig)
if err != nil {
return errors.Wrap(err, "problem adding slam process")
}
cartoSvc.logger.Debug("starting slam process")
if err = cartoSvc.slamProcess.Start(ctx); err != nil {
return errors.Wrap(err, "problem starting slam process")
}
if cartoSvc.port == localhost0 {
timeoutCtx, timeoutCancel := context.WithTimeout(ctx, parsePortMaxTimeoutSec*time.Second)
defer timeoutCancel()
if !cartoSvc.bufferSLAMProcessLogs {
defer func(logger golog.Logger) {
if err := logReader.Close(); err != nil {
logger.Debugw("Closing logReader returned an error", "error", err)
}
}(cartoSvc.logger)
defer func(logger golog.Logger) {
if err := logWriter.Close(); err != nil {
logger.Debugw("Closing logReader returned an error", "error", err)
}
}(cartoSvc.logger)
}
for {
if err := timeoutCtx.Err(); err != nil {
return errors.Wrapf(err, "error getting port from slam process")
}
line, err := bufferedLogReader.ReadString('\n')
if err != nil {
return errors.Wrapf(err, "error getting port from slam process")
}
portLogLinePrefix := "Server listening on "
if strings.Contains(line, portLogLinePrefix) {
linePieces := strings.Split(line, portLogLinePrefix)
if len(linePieces) != 2 {
return errors.Errorf("failed to parse port from slam process log line: %v", line)
}
cartoSvc.port = "localhost:" + strings.TrimRight(linePieces[1], "\n")
break
}
}
}
if cartoSvc.bufferSLAMProcessLogs {
cartoSvc.slamProcessLogReader = logReader
cartoSvc.slamProcessLogWriter = logWriter
cartoSvc.slamProcessBufferedLogReader = bufferedLogReader
}
return nil
}
// stopSLAMProcess uses the process manager to stop the created slam process from running.
func (cartoSvc *cartographerService) StopSLAMProcess() error {
if err := cartoSvc.slamProcess.Stop(); err != nil {
return errors.Wrap(err, "problem stopping slam process")
}
return nil
}