-
Notifications
You must be signed in to change notification settings - Fork 109
/
gpsrtkserial.go
707 lines (590 loc) · 17.3 KB
/
gpsrtkserial.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// Package gpsrtkserial implements a gps using serial connection
package gpsrtkserial
/*
This package supports GPS RTK (Real Time Kinematics), which takes in the normal signals
from the GNSS (Global Navigation Satellite Systems) along with a correction stream to achieve
positional accuracy (accuracy tbd), over Serial.
Example GPS RTK chip datasheet:
https://content.u-blox.com/sites/default/files/ZED-F9P-04B_DataSheet_UBX-21044850.pdf
Ntrip Documentation:
https://gssc.esa.int/wp-content/uploads/2018/07/NtripDocumentation.pdf
Example configuration:
{
"type": "movement_sensor",
"model": "gps-nmea-rtk-serial",
"name": "my-gps-rtk"
"attributes": {
"ntrip_url": "url",
"ntrip_username": "usr",
"ntrip_connect_attempts": 10,
"ntrip_mountpoint": "MTPT",
"ntrip_password": "pwd",
"serial_baud_rate": 115200,
"serial_path": "serial-path"
},
"depends_on": [],
}
*/
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"math"
"strings"
"sync"
"github.com/go-gnss/rtcm/rtcm3"
"github.com/golang/geo/r3"
slib "github.com/jacobsa/go-serial/serial"
geo "github.com/kellydunn/golang-geo"
"go.viam.com/utils"
"go.viam.com/rdk/components/movementsensor"
"go.viam.com/rdk/components/movementsensor/gpsutils"
"go.viam.com/rdk/logging"
"go.viam.com/rdk/resource"
"go.viam.com/rdk/spatialmath"
)
var rtkmodel = resource.DefaultModelFamily.WithModel("gps-nmea-rtk-serial")
const (
serialStr = "serial"
ntripStr = "ntrip"
)
// Config is used for converting NMEA MovementSensor with RTK capabilities config attributes.
type Config struct {
SerialPath string `json:"serial_path"`
SerialBaudRate int `json:"serial_baud_rate,omitempty"`
NtripURL string `json:"ntrip_url"`
NtripConnectAttempts int `json:"ntrip_connect_attempts,omitempty"`
NtripMountpoint string `json:"ntrip_mountpoint,omitempty"`
NtripPass string `json:"ntrip_password,omitempty"`
NtripUser string `json:"ntrip_username,omitempty"`
}
// Validate ensures all parts of the config are valid.
func (cfg *Config) Validate(path string) ([]string, error) {
if cfg.SerialPath == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "serial_path")
}
if cfg.NtripURL == "" {
return nil, resource.NewConfigValidationFieldRequiredError(path, "ntrip_url")
}
return nil, nil
}
func init() {
resource.RegisterComponent(
movementsensor.API,
rtkmodel,
resource.Registration[movementsensor.MovementSensor, *Config]{
Constructor: newRTKSerial,
})
}
// rtkSerial is an nmea movementsensor model that can intake RTK correction data.
type rtkSerial struct {
resource.Named
resource.AlwaysRebuild
logger logging.Logger
cancelCtx context.Context
cancelFunc func()
activeBackgroundWorkers sync.WaitGroup
err movementsensor.LastError
lastposition movementsensor.LastPosition
lastcompassheading movementsensor.LastCompassHeading
InputProtocol string
isClosed bool
mu sync.Mutex
// everything below this comment is protected by mu
isConnectedToNtrip bool
ntripClient *gpsutils.NtripInfo
cachedData *gpsutils.CachedData
correctionWriter io.ReadWriteCloser
writePath string
wbaud int
isVirtualBase bool
readerWriter *bufio.ReadWriter
writer io.Writer
reader io.Reader
}
// Reconfigure reconfigures attributes.
func (g *rtkSerial) Reconfigure(ctx context.Context, deps resource.Dependencies, conf resource.Config) error {
g.mu.Lock()
defer g.mu.Unlock()
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return err
}
if newConf.SerialPath != "" {
g.writePath = newConf.SerialPath
g.logger.CInfof(ctx, "updated serial_path to #%v", newConf.SerialPath)
}
if newConf.SerialBaudRate != 0 {
g.wbaud = newConf.SerialBaudRate
g.logger.CInfof(ctx, "updated serial_baud_rate to %v", newConf.SerialBaudRate)
} else {
g.wbaud = 38400
g.logger.CInfo(ctx, "serial_baud_rate using default baud rate 38400")
}
ntripConfig := &gpsutils.NtripConfig{
NtripURL: newConf.NtripURL,
NtripUser: newConf.NtripUser,
NtripPass: newConf.NtripPass,
NtripMountpoint: newConf.NtripMountpoint,
NtripConnectAttempts: newConf.NtripConnectAttempts,
}
// Init ntripInfo from attributes
tempNtripClient, err := gpsutils.NewNtripInfo(ntripConfig, g.logger)
if err != nil {
return err
}
if g.ntripClient != nil { // Copy over the old state
tempNtripClient.Client = g.ntripClient.Client
tempNtripClient.Stream = g.ntripClient.Stream
}
g.ntripClient = tempNtripClient
g.logger.Debug("done reconfiguring")
return nil
}
func newRTKSerial(
ctx context.Context,
deps resource.Dependencies,
conf resource.Config,
logger logging.Logger,
) (movementsensor.MovementSensor, error) {
newConf, err := resource.NativeConfig[*Config](conf)
if err != nil {
return nil, err
}
cancelCtx, cancelFunc := context.WithCancel(context.Background())
g := &rtkSerial{
Named: conf.ResourceName().AsNamed(),
cancelCtx: cancelCtx,
cancelFunc: cancelFunc,
logger: logger,
err: movementsensor.NewLastError(1, 1),
lastposition: movementsensor.NewLastPosition(),
lastcompassheading: movementsensor.NewLastCompassHeading(),
}
if err := g.Reconfigure(ctx, deps, conf); err != nil {
return nil, err
}
g.InputProtocol = serialStr
serialConfig := &gpsutils.SerialConfig{
SerialPath: newConf.SerialPath,
SerialBaudRate: newConf.SerialBaudRate,
}
dev, err := gpsutils.NewSerialDataReader(serialConfig, logger)
if err != nil {
return nil, err
}
g.cachedData = gpsutils.NewCachedData(dev, logger)
if err := g.start(); err != nil {
return nil, err
}
return g, g.err.Get()
}
func (g *rtkSerial) start() error {
err := g.connectToNTRIP()
if err != nil {
return err
}
g.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(g.receiveAndWriteSerial)
return g.err.Get()
}
// getStream attempts to connect to ntrip stream. We give up after maxAttempts unsuccessful tries.
func (g *rtkSerial) getStream(mountPoint string, maxAttempts int) error {
success := false
attempts := 0
var rc io.ReadCloser
var err error
g.logger.Debug("Getting NTRIP stream")
for !success && attempts < maxAttempts {
select {
case <-g.cancelCtx.Done():
return errors.New("Canceled")
default:
}
rc, err = func() (io.ReadCloser, error) {
return g.ntripClient.Client.GetStream(mountPoint)
}()
if err == nil {
success = true
}
attempts++
}
if err != nil {
g.logger.Errorf("Can't connect to NTRIP stream: %s", err)
return err
}
g.logger.Debug("Connected to stream")
g.mu.Lock()
defer g.mu.Unlock()
g.ntripClient.Stream = rc
return g.err.Get()
}
// openPort opens the serial port for writing.
func (g *rtkSerial) openPort() error {
options := slib.OpenOptions{
PortName: g.writePath,
BaudRate: uint(g.wbaud),
DataBits: 8,
StopBits: 1,
MinimumReadSize: 1,
}
if err := g.cancelCtx.Err(); err != nil {
return err
}
var err error
g.correctionWriter, err = slib.Open(options)
if err != nil {
g.logger.Errorf("serial.Open: %v", err)
return err
}
return nil
}
// closePort closes the serial port.
func (g *rtkSerial) closePort() {
g.mu.Lock()
defer g.mu.Unlock()
if g.correctionWriter != nil {
err := g.correctionWriter.Close()
if err != nil {
g.logger.Errorf("Error closing port: %v", err)
}
}
}
// connectAndParseSourceTable connects to the NTRIP caster, gets and parses source table
// from the caster.
func (g *rtkSerial) connectAndParseSourceTable() error {
if err := g.cancelCtx.Err(); err != nil {
return g.err.Get()
}
err := g.ntripClient.Connect(g.cancelCtx, g.logger)
if err != nil {
g.err.Set(err)
return g.err.Get()
}
if !g.ntripClient.Client.IsCasterAlive() {
g.logger.Infof("caster %s seems to be down, retrying", g.ntripClient.URL)
attempts := 0
// we will try to connect to the caster five times if it's down.
for attempts < 5 {
if !g.ntripClient.Client.IsCasterAlive() {
attempts++
g.logger.Debugf("attempt(s) to connect to caster: %v ", attempts)
} else {
break
}
}
if attempts == 5 {
return fmt.Errorf("caster %s is down", g.ntripClient.URL)
}
}
g.logger.Debug("getting source table")
srcTable, err := g.ntripClient.ParseSourcetable(g.logger)
if err != nil {
g.logger.Errorf("failed to get source table: %v", err)
return err
}
g.logger.Debugf("sourceTable is: %v\n", srcTable)
g.logger.Debug("got sourcetable, parsing it...")
g.isVirtualBase, err = gpsutils.HasVRSStream(srcTable, g.ntripClient.MountPoint)
if err != nil {
g.logger.Errorf("can't find mountpoint in source table, found err %v\n", err)
return err
}
return nil
}
// connectToNTRIP connects to NTRIP stream.
func (g *rtkSerial) connectToNTRIP() error {
select {
case <-g.cancelCtx.Done():
return errors.New("context canceled")
default:
}
err := g.connectAndParseSourceTable()
if err != nil {
return err
}
err = g.openPort()
if err != nil {
return err
}
if g.isVirtualBase {
g.logger.Debug("connecting to a Virtual Reference Station")
err = g.getNtripFromVRS()
if err != nil {
return err
}
} else {
g.logger.Debug("connecting to NTRIP stream........")
g.writer = bufio.NewWriter(g.correctionWriter)
err = g.getStream(g.ntripClient.MountPoint, g.ntripClient.MaxConnectAttempts)
if err != nil {
return err
}
g.reader = io.TeeReader(g.ntripClient.Stream, g.writer)
}
return nil
}
// receiveAndWriteSerial connects to NTRIP receiver and sends correction stream to the MovementSensor through serial.
func (g *rtkSerial) receiveAndWriteSerial() {
defer g.activeBackgroundWorkers.Done()
defer g.closePort()
var scanner rtcm3.Scanner
if g.isVirtualBase {
scanner = rtcm3.NewScanner(g.readerWriter)
} else {
scanner = rtcm3.NewScanner(g.reader)
}
g.mu.Lock()
g.isConnectedToNtrip = true
g.mu.Unlock()
// It's okay to skip the mutex on this next line: g.isConnectedToNtrip can only be mutated by this
// goroutine itself
for g.isConnectedToNtrip && !g.isClosed {
select {
case <-g.cancelCtx.Done():
return
default:
}
msg, err := scanner.NextMessage()
if err != nil {
g.mu.Lock()
g.isConnectedToNtrip = false
g.mu.Unlock()
if msg == nil {
if g.isClosed {
return
}
if g.isVirtualBase {
g.logger.Debug("reconnecting to the Virtual Reference Station")
err = g.getNtripFromVRS()
if err != nil && !errors.Is(err, io.EOF) {
g.err.Set(err)
return
}
scanner = rtcm3.NewScanner(g.readerWriter)
} else {
g.logger.Debug("No message... reconnecting to stream...")
err = g.getStream(g.ntripClient.MountPoint, g.ntripClient.MaxConnectAttempts)
if err != nil {
g.err.Set(err)
return
}
g.reader = io.TeeReader(g.ntripClient.Stream, g.writer)
scanner = rtcm3.NewScanner(g.reader)
}
g.mu.Lock()
g.isConnectedToNtrip = true
g.mu.Unlock()
continue
}
}
}
}
// Most of the movementsensor functions here don't have mutex locks since g.cachedData is protected by
// it's own mutex and not having mutex around g.err is alright.
// Position returns the current geographic location of the MOVEMENTSENSOR.
func (g *rtkSerial) Position(ctx context.Context, extra map[string]interface{}) (*geo.Point, float64, error) {
lastError := g.err.Get()
if lastError != nil {
lastPosition := g.lastposition.GetLastPosition()
if lastPosition != nil {
return lastPosition, 0, nil
}
return geo.NewPoint(math.NaN(), math.NaN()), math.NaN(), lastError
}
position, alt, err := g.cachedData.Position(ctx, extra)
if err != nil {
// Use the last known valid position if current position is (0,0)/ NaN.
if position != nil && (movementsensor.IsZeroPosition(position) || movementsensor.IsPositionNaN(position)) {
lastPosition := g.lastposition.GetLastPosition()
if lastPosition != nil {
return lastPosition, alt, nil
}
}
return geo.NewPoint(math.NaN(), math.NaN()), math.NaN(), err
}
if movementsensor.IsPositionNaN(position) {
position = g.lastposition.GetLastPosition()
}
return position, alt, nil
}
// LinearVelocity passthrough.
func (g *rtkSerial) LinearVelocity(ctx context.Context, extra map[string]interface{}) (r3.Vector, error) {
lastError := g.err.Get()
if lastError != nil {
return r3.Vector{}, lastError
}
return g.cachedData.LinearVelocity(ctx, extra)
}
// LinearAcceleration passthrough.
func (g *rtkSerial) LinearAcceleration(ctx context.Context, extra map[string]interface{}) (r3.Vector, error) {
lastError := g.err.Get()
if lastError != nil {
return r3.Vector{}, lastError
}
return g.cachedData.LinearAcceleration(ctx, extra)
}
// AngularVelocity passthrough.
func (g *rtkSerial) AngularVelocity(ctx context.Context, extra map[string]interface{}) (spatialmath.AngularVelocity, error) {
lastError := g.err.Get()
if lastError != nil {
return spatialmath.AngularVelocity{}, lastError
}
return g.cachedData.AngularVelocity(ctx, extra)
}
// CompassHeading passthrough.
func (g *rtkSerial) CompassHeading(ctx context.Context, extra map[string]interface{}) (float64, error) {
lastError := g.err.Get()
if lastError != nil {
return 0, lastError
}
return g.cachedData.CompassHeading(ctx, extra)
}
// Orientation passthrough.
func (g *rtkSerial) Orientation(ctx context.Context, extra map[string]interface{}) (spatialmath.Orientation, error) {
lastError := g.err.Get()
if lastError != nil {
return spatialmath.NewZeroOrientation(), lastError
}
return g.cachedData.Orientation(ctx, extra)
}
// readFix passthrough.
func (g *rtkSerial) readFix(ctx context.Context) (int, error) {
lastError := g.err.Get()
if lastError != nil {
return 0, lastError
}
return g.cachedData.ReadFix(ctx)
}
// readSatsInView returns the number of satellites in view.
func (g *rtkSerial) readSatsInView(ctx context.Context) (int, error) {
lastError := g.err.Get()
if lastError != nil {
return 0, lastError
}
return g.cachedData.ReadSatsInView(ctx)
}
// Properties passthrough.
func (g *rtkSerial) Properties(ctx context.Context, extra map[string]interface{}) (*movementsensor.Properties, error) {
lastError := g.err.Get()
if lastError != nil {
return &movementsensor.Properties{}, lastError
}
return g.cachedData.Properties(ctx, extra)
}
// Accuracy passthrough.
func (g *rtkSerial) Accuracy(ctx context.Context, extra map[string]interface{}) (*movementsensor.Accuracy, error,
) {
lastError := g.err.Get()
if lastError != nil {
return nil, lastError
}
return g.cachedData.Accuracy(ctx, extra)
}
// Readings will use the default MovementSensor Readings if not provided.
func (g *rtkSerial) Readings(ctx context.Context, extra map[string]interface{}) (map[string]interface{}, error) {
readings, err := movementsensor.DefaultAPIReadings(ctx, g, extra)
if err != nil {
return nil, err
}
fix, err := g.readFix(ctx)
if err != nil {
return nil, err
}
satsInView, err := g.readSatsInView(ctx)
if err != nil {
return nil, err
}
readings["fix"] = fix
readings["satellites_in_view"] = satsInView
return readings, nil
}
// Close shuts down the rtkSerial.
func (g *rtkSerial) Close(ctx context.Context) error {
g.mu.Lock()
g.cancelFunc()
g.logger.Debug("Closing GPS RTK Serial")
if err := g.cachedData.Close(ctx); err != nil {
g.mu.Unlock()
return err
}
// close ntrip writer
if g.correctionWriter != nil {
if err := g.correctionWriter.Close(); err != nil {
g.isClosed = true
g.mu.Unlock()
return err
}
g.correctionWriter = nil
}
// close ntrip client and stream
if g.ntripClient.Client != nil {
g.ntripClient.Client.CloseIdleConnections()
g.ntripClient.Client = nil
}
if g.ntripClient.Stream != nil {
if err := g.ntripClient.Stream.Close(); err != nil {
g.mu.Unlock()
return err
}
g.ntripClient.Stream = nil
}
g.mu.Unlock()
g.activeBackgroundWorkers.Wait()
if err := g.err.Get(); err != nil && !errors.Is(err, context.Canceled) {
return err
}
g.logger.Debug("GPS RTK Serial is closed")
return nil
}
// getNtripFromVRS sends GGA messages to the NTRIP Caster over a TCP connection
// to get the NTRIP steam when the mount point is a Virtual Reference Station.
func (g *rtkSerial) getNtripFromVRS() error {
g.mu.Lock()
defer g.mu.Unlock()
var err error
g.readerWriter, err = gpsutils.ConnectToVirtualBase(g.ntripClient, g.logger)
if err != nil {
return err
}
// read from the socket until we know if a successful connection has been
// established.
for {
line, _, err := g.readerWriter.ReadLine()
if err != nil {
if errors.Is(err, io.EOF) {
g.readerWriter = nil
return err
}
g.logger.Error("Failed to read server response:", err)
return err
}
if strings.HasPrefix(string(line), "HTTP/1.1 ") {
if strings.Contains(string(line), "200 OK") {
break
}
g.logger.Errorf("Bad HTTP response: %v", string(line))
return err
}
}
ggaMessage, err := gpsutils.GetGGAMessage(g.correctionWriter, g.logger)
if err != nil {
g.logger.Error("Failed to get GGA message")
return err
}
g.logger.Debugf("Writing GGA message: %v\n", string(ggaMessage))
_, err = g.readerWriter.WriteString(string(ggaMessage))
if err != nil {
g.logger.Error("Failed to send NMEA data:", err)
return err
}
err = g.readerWriter.Flush()
if err != nil {
g.logger.Error("failed to write to buffer: ", err)
return err
}
g.logger.Debug("GGA message sent successfully.")
return nil
}