Skip to content

Commit

Permalink
Merge pull request #178 from AvSquirrel/ahrsFusion
Browse files Browse the repository at this point in the history
Check for valid GPS date. Fix time string formatting.
  • Loading branch information
cyoung committed Jan 1, 2016
2 parents bb23bd5 + 0f61156 commit d682bb5
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions main/ry835ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,18 @@ func processNMEALine(l string) bool {
*/

} else if x[1] == "04" { // clock message
// field 5 is UTC week (epoch = 1980-JAN-06). If this is invalid, do not parse date / time
utcWeek, err0 := strconv.Atoi(x[5])
if err0 != nil {
// log.Printf("Error reading GPS week\n")
return false
}
if utcWeek < 1877 || utcWeek >= 32767 { // unless we're in a flying Delorean, UTC dates before 2016-JAN-01 are not valid. Check underflow condition as well.
log.Printf("GPS week # %v out of scope; not setting time and date\n", utcWeek)
return false
} /* else {
log.Printf("GPS week # %v valid; evaluate time and date\n", utcWeek) //debug option
} */

// field 2 is UTC time
if len(x[2]) < 9 {
Expand All @@ -541,10 +553,11 @@ func processNMEALine(l string) bool {

if len(x[3]) == 6 {
// Date of Fix, i.e 191115 = 19 November 2015 UTC field 9
gpsTimeStr := fmt.Sprintf("%s %d:%d:%d", x[3], hr, min, sec)
gpsTimeStr := fmt.Sprintf("%s %02d:%02d:%02d", x[3], hr, min, sec)
gpsTime, err := time.Parse("020106 15:04:05", gpsTimeStr)
if err == nil {
if time.Since(gpsTime) > 10*time.Minute {
// log.Printf("GPS time is: %s\n", gpsTime) //debug
if time.Since(gpsTime) > 3*time.Second || time.Since(gpsTime) < -3*time.Second {
log.Printf("setting system time to: %s\n", gpsTime)
setStr := gpsTime.Format("20060102 15:04:05")
if err := exec.Command("date", "-s", setStr).Run(); err != nil {
Expand Down Expand Up @@ -736,10 +749,10 @@ func processNMEALine(l string) bool {

if len(x[9]) == 6 {
// Date of Fix, i.e 191115 = 19 November 2015 UTC field 9
gpsTimeStr := fmt.Sprintf("%s %d:%d:%d", x[9], hr, min, sec)
gpsTimeStr := fmt.Sprintf("%s %02d:%02d:%02d", x[9], hr, min, sec)
gpsTime, err := time.Parse("020106 15:04:05", gpsTimeStr)
if err == nil {
if time.Since(gpsTime) > 10*time.Minute {
if time.Since(gpsTime) > 3*time.Second || time.Since(gpsTime) < -3*time.Second {
log.Printf("setting system time to: %s\n", gpsTime)
setStr := gpsTime.Format("20060102 15:04:05")
if err := exec.Command("date", "-s", setStr).Run(); err != nil {
Expand Down

0 comments on commit d682bb5

Please sign in to comment.