diff sirf.py @ 2:d6d9fba5464d

Log more info and in a better fashion.
author darius@Inchoate
date Wed, 02 Sep 2009 13:27:13 +0930
parents 6503256a3fc4
children 387e6e928567
line wrap: on
line diff
--- a/sirf.py	Wed Sep 02 13:24:44 2009 +0930
+++ b/sirf.py	Wed Sep 02 13:27:13 2009 +0930
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 
-import time, struct
+import time, struct, os
 
 # SiRF at 9600 baud
 nmea2sirf = '$PSRF100,0,9600,8,1,0*0C\r\n'
@@ -98,7 +98,7 @@
     min = abs((x - deg) * 60)
     sec = abs((min - int(min)) * 60)
 
-    return "%d d %d m %.3f s" % (deg, int(min), sec)
+    return "%dd%dm%.3f" % (deg, int(min), sec)
 
 class Parser(object):
     def __init__(self, s = None):
@@ -249,13 +249,17 @@
             
             satprn = []
             for i in xrange(nsats):
-                satprn.append(data[struct.calcsize(fmt) + i])
+                satprn.append(data[struct.calcsize(fmt) - 1 + i])
 
             xvel = float(xvel) / 8
             yvel = float(yvel) / 8
             zvel = float(zvel) / 8
-            print "                          Position: X: %d, Y: %d, Z: %d" % (xpos, ypos, zpos)
-            print "                          Velocity: X: %.2f, Y: %.2f, Z: %.2f" % (xvel, yvel, zvel)
+            hdop = float(hdop) / 5
+            gpstow = float(gpstow) / 100
+            
+            print "                          Position: X: %d m, Y: %d m, Z: %d m" % (xpos, ypos, zpos)
+            print "                          Velocity: X: %.2f m/s, Y: %.2f m/s, Z: %.2f m/s" % (xvel, yvel, zvel)
+            print "                          HDOP: %.1f, Week: %d, TOW: %.2f seconds" % (hdop, gpsweek, gpstow)
         elif data[0] == 0x06:
             nulidx = data.index(0)
             print "                          SW Ver : %s" % (reduce(lambda x, y: x + y, map(chr, data[1:nulidx])))
@@ -289,6 +293,7 @@
              CDerr, distance, distanceErr, headErr, numsvs, hdop, addmodeinfo) = \
              struct.unpack(fmt, datastr)
             tow = float(tow) / 1e3
+            second = float(second) / 1e3
             latitude = float(latitude) / 1e7
             longitude = float(longitude) / 1e7
             alt_elip = float(alt_elip) / 1e2
@@ -308,9 +313,21 @@
             headErr = float(headErr) / 1e2
             hdop = float(hdop) / 5
 
-            print "                          Fix : %s, Sats : %d, Lat: %s, Long: %s, Alt: %.2fm" % \
-                  (fixtype[navtype & 0x7], numsvs, fmtlatlong(latitude), fmtlatlong(longitude), \
-                   alt_msl)
+            utctime = timegm((year, month, day, hour, minute, int(second)))
+            utctime = utctime + (second - int(second))
+            print "                          Fix : %s, Sats : %d, Lat: %.06f, Long: %.06f, Alt: %.02fm" % \
+                  (fixtype[navtype & 0x7], numsvs, latitude, longitude, alt_msl)
+            print "                          Date : %04d/%02d/%02d %02d:%02d:%02d.%03d" % \
+                  (year, month, day, hour, month, int(second), int((second - int(second)) * 1000))
+            print "                          Epoch time : %.3f, Delta %.3f" % (utctime, abs(time.time() - utctime))
+        elif data[0] == 0x34:
+            fmt = '>BBBBBHHIB'
+            datastr = reduce(lambda x, y: x + y, map(chr, data[1:struct.calcsize(fmt) + 1]))
+            (hour, minute, second, day, month, year, offsetint, offsetfrac, status) = \
+                   struct.unpack(fmt, datastr)
+            offset = offsetint + float(offsetfrac) / 1e9
+            print "                          PPS : %04d/%02d/%02d %02d:%02d:%02d, Offset : %.9f, Status : 0x%02x" % \
+                  (year, month, day, hour, minute, second, offset, status)
         elif data[0] == 0xa6:
             print "                          Message rate : MID 0x%02x, rate 0x%02x" % (data[2], data[3])
 
@@ -337,3 +354,16 @@
     s.write(Parser.OrdLsttoStr(Parser.Encap([0xa6, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00])))
 
     
+def timegm(tuple):
+    oldtz = None
+    if 'TZ' in os.environ:
+        oldtz = os.environ['TZ']
+    os.environ['TZ'] = 'UTC'
+    
+    t = time.mktime(tuple + (0, 0, 0))
+    if oldtz == None:
+        del os.environ['TZ']
+    else:
+        os.environ['TZ'] = oldtz
+
+    return t