diff --git a/examples/gps/i2c/main.go b/examples/gps/i2c/main.go index b767f0c..2aab5d9 100644 --- a/examples/gps/i2c/main.go +++ b/examples/gps/i2c/main.go @@ -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") diff --git a/examples/gps/uart/main.go b/examples/gps/uart/main.go index 82328e8..1affb2b 100644 --- a/examples/gps/uart/main.go +++ b/examples/gps/uart/main.go @@ -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") diff --git a/gps/gpsparser.go b/gps/gpsparser.go index 54587d7..befc141 100644 --- a/gps/gpsparser.go +++ b/gps/gpsparser.go @@ -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 +}