-
Notifications
You must be signed in to change notification settings - Fork 109
/
serial.go
244 lines (210 loc) · 6.52 KB
/
serial.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
// Package gpsnmea implements an NMEA serial gps.
package gpsnmea
import (
"bufio"
"context"
"fmt"
"io"
"sync"
"github.com/adrianmo/go-nmea"
"github.com/edaniels/golog"
"github.com/golang/geo/r3"
"github.com/jacobsa/go-serial/serial"
geo "github.com/kellydunn/golang-geo"
"go.viam.com/utils"
"go.viam.com/rdk/components/generic"
"go.viam.com/rdk/components/movementsensor"
"go.viam.com/rdk/spatialmath"
)
// SerialNMEAMovementSensor allows the use of any MovementSensor chip that communicates over serial.
type SerialNMEAMovementSensor struct {
generic.Unimplemented
mu sync.RWMutex
cancelCtx context.Context
cancelFunc func()
logger golog.Logger
data gpsData
activeBackgroundWorkers sync.WaitGroup
disableNmea bool
errMu sync.Mutex
lastError error
dev io.ReadWriteCloser
path string
baudRate uint
correctionBaudRate uint
correctionPath string
}
// NewSerialGPSNMEA gps that communicates over serial.
func NewSerialGPSNMEA(ctx context.Context, attr *AttrConfig, logger golog.Logger) (NmeaMovementSensor, error) {
serialPath := attr.SerialAttrConfig.SerialPath
if serialPath == "" {
return nil, fmt.Errorf("SerialNMEAMovementSensor expected non-empty string for %q", attr.SerialAttrConfig.SerialPath)
}
correctionPath := attr.SerialAttrConfig.SerialCorrectionPath
if correctionPath == "" {
correctionPath = serialPath
logger.Infof("SerialNMEAMovementSensor: correction_path using path: %s", correctionPath)
}
baudRate := attr.SerialAttrConfig.SerialBaudRate
if baudRate == 0 {
baudRate = 9600
logger.Info("SerialNMEAMovementSensor: baud_rate using default 9600")
}
correctionBaudRate := attr.SerialAttrConfig.SerialCorrectionBaudRate
if correctionBaudRate == 0 {
correctionBaudRate = baudRate
logger.Infof("SerialNMEAMovementSensor: correction_baud using baud_rate: %d", baudRate)
}
disableNmea := attr.DisableNMEA
if disableNmea {
logger.Info("SerialNMEAMovementSensor: NMEA reading disabled")
}
options := serial.OpenOptions{
PortName: serialPath,
BaudRate: uint(baudRate),
DataBits: 8,
StopBits: 1,
MinimumReadSize: 4,
}
dev, err := serial.Open(options)
if err != nil {
return nil, err
}
cancelCtx, cancelFunc := context.WithCancel(ctx)
g := &SerialNMEAMovementSensor{
dev: dev,
cancelCtx: cancelCtx,
cancelFunc: cancelFunc,
logger: logger,
path: serialPath,
correctionPath: correctionPath,
baudRate: uint(baudRate),
correctionBaudRate: uint(correctionBaudRate),
disableNmea: disableNmea,
data: gpsData{},
}
if err := g.Start(ctx); err != nil {
return nil, err
}
return g, g.lastError
}
func (g *SerialNMEAMovementSensor) setLastError(err error) {
g.errMu.Lock()
defer g.errMu.Unlock()
g.lastError = err
}
// Start begins reading nmea messages from module and updates gps data.
func (g *SerialNMEAMovementSensor) Start(ctx context.Context) error {
g.activeBackgroundWorkers.Add(1)
utils.PanicCapturingGo(func() {
defer g.activeBackgroundWorkers.Done()
r := bufio.NewReader(g.dev)
for {
select {
case <-g.cancelCtx.Done():
return
default:
}
if !g.disableNmea {
line, err := r.ReadString('\n')
if err != nil {
g.logger.Errorf("can't read gps serial %s", err)
g.setLastError(err)
return
}
// Update our struct's gps data in-place
g.mu.Lock()
err = g.data.parseAndUpdate(line)
g.mu.Unlock()
if err != nil {
g.logger.Debugf("can't parse nmea %s : %s", line, err)
}
}
}
})
return g.lastError
}
// GetCorrectionInfo returns the serial path that takes in rtcm corrections and baudrate for reading.
func (g *SerialNMEAMovementSensor) GetCorrectionInfo() (string, uint) {
return g.correctionPath, g.correctionBaudRate
}
// Position position, altitide.
func (g *SerialNMEAMovementSensor) Position(ctx context.Context) (*geo.Point, float64, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.data.location, g.data.alt, g.lastError
}
// Accuracy returns the accuracy, hDOP and vDOP.
func (g *SerialNMEAMovementSensor) Accuracy(ctx context.Context) (map[string]float32, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return map[string]float32{"hDOP": float32(g.data.hDOP), "vDOP": float32(g.data.vDOP)}, g.lastError
}
// LinearVelocity linear velocity.
func (g *SerialNMEAMovementSensor) LinearVelocity(ctx context.Context) (r3.Vector, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return r3.Vector{X: 0, Y: g.data.speed, Z: 0}, g.lastError
}
// AngularVelocity angularvelocity.
func (g *SerialNMEAMovementSensor) AngularVelocity(ctx context.Context) (spatialmath.AngularVelocity, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return spatialmath.AngularVelocity{}, g.lastError
}
// Orientation orientation.
func (g *SerialNMEAMovementSensor) Orientation(ctx context.Context) (spatialmath.Orientation, error) {
return nil, g.lastError
}
// CompassHeading 0->360.
func (g *SerialNMEAMovementSensor) CompassHeading(ctx context.Context) (float64, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return 0, g.lastError
}
// ReadFix returns Fix quality of MovementSensor measurements.
func (g *SerialNMEAMovementSensor) ReadFix(ctx context.Context) (int, error) {
g.mu.RLock()
defer g.mu.RUnlock()
return g.data.fixQuality, g.lastError
}
// Readings will use return all of the MovementSensor Readings.
func (g *SerialNMEAMovementSensor) Readings(ctx context.Context) (map[string]interface{}, error) {
readings, err := movementsensor.Readings(ctx, g)
if err != nil {
return nil, err
}
fix, err := g.ReadFix(ctx)
if err != nil {
return nil, err
}
readings["fix"] = fix
return readings, g.lastError
}
// Properties what do I do!
func (g *SerialNMEAMovementSensor) Properties(ctx context.Context) (*movementsensor.Properties, error) {
return &movementsensor.Properties{
LinearVelocitySupported: true,
PositionSupported: true,
}, g.lastError
}
// Close shuts down the SerialNMEAMovementSensor.
func (g *SerialNMEAMovementSensor) Close() error {
g.logger.Debug("Closing SerialNMEAMovementSensor")
g.cancelFunc()
g.activeBackgroundWorkers.Wait()
g.mu.Lock()
defer g.mu.Unlock()
if g.dev != nil {
if err := g.dev.Close(); err != nil {
return err
}
g.dev = nil
g.logger.Debug("SerialNMEAMovementSensor Closed")
}
return g.lastError
}
// toPoint converts a nmea.GLL to a geo.Point.
func toPoint(a nmea.GLL) *geo.Point {
return geo.NewPoint(a.Latitude, a.Longitude)
}