-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgps.go
More file actions
161 lines (130 loc) · 3.08 KB
/
gps.go
File metadata and controls
161 lines (130 loc) · 3.08 KB
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
//go:build tinygo
package main
import (
"machine"
"sync"
"runtime"
"time"
"tinygo.org/x/drivers/gps"
)
var (
ublox *gps.Device
currentFix gps.Fix
)
var (
gpsReset = machine.GPIO6
gpsLoadSwitch = machine.GPIO2
)
var (
gpsStarted bool
gpsStopChan chan struct{}
gpsFixAcquired bool
mu sync.Mutex
gpsTimeAdjust bool
)
func gpsIsStarted() bool {
return gpsStarted
}
func gpsHasFix() bool {
return gpsFixAcquired
}
func gpsTimeAdjusted() bool {
mu.Lock()
defer mu.Unlock()
return gpsTimeAdjust
}
// initialize GPS (called once at startup)
func initGPS() {
// used to reset GPS module
gpsReset.Configure(machine.PinConfig{Mode: machine.PinOutput})
gpsReset.Low()
// used to control GPS power. high means off. can turn off when not in use.
gpsLoadSwitch.Configure(machine.PinConfig{Mode: machine.PinOutput})
gpsLoadSwitch.High()
}
// start GPS reading goroutine
func startGPS() {
updateWatchdog()
gpsStarted = true
gpsFixAcquired = false
// Power on GPS
gpsLoadSwitch.Low()
gpsReset.High()
time.Sleep(500 * time.Millisecond)
machine.UART1.Configure(machine.UARTConfig{BaudRate: 9600, RX: machine.UART1_RX_PIN, TX: machine.UART1_TX_PIN})
u := gps.NewUART(machine.UART1)
ublox = &u
gps.SetMessageRatesMinimal(ublox)
parser := gps.NewParser()
gpsStopChan = make(chan struct{})
for {
select {
case <-gpsStopChan:
return
default:
}
s, err := ublox.NextSentence()
if err != nil {
switch err {
case gps.ErrUnknownNMEASentence, gps.ErrInvalidNMEASentence, gps.ErrInvalidNMEASentenceLength:
continue
default:
log("sentence error:", err)
continue
}
}
newfix, err := parser.Parse(s)
if err != nil {
switch err {
case gps.ErrUnknownNMEASentence, gps.ErrInvalidNMEASentence, gps.ErrInvalidNMEASentenceLength:
continue
default:
log("parse error:", err)
continue
}
}
if newfix.Valid {
currentFix = newfix
if newfix.Type == gps.GGA || newfix.Type == gps.RMC {
currentLatitude = newfix.Latitude
currentLongitude = newfix.Longitude
currentAltitude = newfix.Altitude
}
// adjust time based on GPS time?
adjustTimeFromGPS(newfix)
gpsFixAcquired = true
}
if !gpsFixAcquired {
if newfix.Type == gps.GSV {
log("Satellites in view:", newfix.Satellites)
}
}
time.Sleep(100 * time.Millisecond)
}
}
// stop GPS reading and power down GPS
func stopGPS() {
gpsFixAcquired = false
updateWatchdog()
close(gpsStopChan)
time.Sleep(100 * time.Millisecond)
// Power off GPS
machine.UART1.Close()
machine.UART1_TX_PIN.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.UART1_TX_PIN.Low()
machine.UART1_RX_PIN.Configure(machine.PinConfig{Mode: machine.PinOutput})
machine.UART1_RX_PIN.Low()
gpsReset.Low()
gpsLoadSwitch.High()
gpsStarted = false
}
func adjustTimeFromGPS(fix gps.Fix) {
if !gpsTimeAdjust {
mu.Lock()
defer mu.Unlock()
now := time.Now()
runtime.AdjustTimeOffset(int64(fix.Time.Sub(now)))
log("Adjusting system time based on GPS fix from", now.Format("15:04:05"), "to", fix.Time.Format("15:04:05"))
gpsTimeAdjust = true
}
}