gps: add speed and heading to fix, as parsed from RMC NMEA sentence

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2020-08-21 07:24:41 +02:00
parent af4efceac1
commit 4f82c06df9
3 changed files with 42 additions and 0 deletions
+8
View File
@@ -33,6 +33,14 @@ func main() {
print(fix.Longitude)
print(", altitude=", fix.Altitude)
print(", satellites=", fix.Satellites)
if fix.Speed != 0 {
print(", speed=")
print(fix.Speed)
}
if fix.Heading != 0 {
print(", heading=")
print(fix.Heading)
}
println()
} else {
println("No fix")
+8
View File
@@ -33,6 +33,14 @@ func main() {
print(fix.Longitude)
print(", altitude=", fix.Altitude)
print(", satellites=", fix.Satellites)
if fix.Speed != 0 {
print(", speed=")
print(fix.Speed)
}
if fix.Heading != 0 {
print(", heading=")
print(fix.Heading)
}
println()
} else {
println("No fix")
+26
View File
@@ -37,6 +37,12 @@ type Fix struct {
// Satellites is the number of visible satellites, but is only returned for GGA sentences.
Satellites int16
// Speed based on reported movement. Only returned for RMC sentences.
Speed float32
// Heading based on reported movement. Only returned for RMC sentences.
Heading float32
}
// NewParser returns a GPS NMEA Parser.
@@ -75,6 +81,8 @@ func (parser *Parser) Parse(sentence string) (fix Fix, err error) {
fix.Longitude = findLongitude(fields[5], fields[6])
fix.Latitude = findLatitude(fields[3], fields[4])
fix.Time = findTime(fields[1])
fix.Speed = findSpeed(fields[7])
fix.Heading = findHeading(fields[8])
fix.Valid = (len(fields[2]) > 0 && fields[2][0:1] == "A")
default:
err = errUnknownNMEASentence
@@ -153,3 +161,21 @@ func findSatellites(val string) (n int16) {
}
return 0
}
// findSpeed returns the speed from an RMC NMEA sentence.
func findSpeed(val string) float32 {
if len(val) > 0 {
var v, _ = strconv.ParseFloat(val, 32)
return float32(v)
}
return 0
}
// findHeading returns the speed from an RMC NMEA sentence.
func findHeading(val string) float32 {
if len(val) > 0 {
var v, _ = strconv.ParseFloat(val, 32)
return float32(v)
}
return 0
}