mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-03 06:27:47 +00:00
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:
@@ -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")
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user