comparison 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
comparison
equal deleted inserted replaced
1:c623f8832fd7 2:d6d9fba5464d
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import time, struct 3 import time, struct, os
4 4
5 # SiRF at 9600 baud 5 # SiRF at 9600 baud
6 nmea2sirf = '$PSRF100,0,9600,8,1,0*0C\r\n' 6 nmea2sirf = '$PSRF100,0,9600,8,1,0*0C\r\n'
7 7
8 # NMEA at 4800 baud 8 # NMEA at 4800 baud
96 def fmtlatlong(x): 96 def fmtlatlong(x):
97 deg = int(x) 97 deg = int(x)
98 min = abs((x - deg) * 60) 98 min = abs((x - deg) * 60)
99 sec = abs((min - int(min)) * 60) 99 sec = abs((min - int(min)) * 60)
100 100
101 return "%d d %d m %.3f s" % (deg, int(min), sec) 101 return "%dd%dm%.3f" % (deg, int(min), sec)
102 102
103 class Parser(object): 103 class Parser(object):
104 def __init__(self, s = None): 104 def __init__(self, s = None):
105 self.buffer = [] 105 self.buffer = []
106 self.state = 'init1' 106 self.state = 'init1'
247 (xpos, ypos, zpos, xvel, yvel, zvel, mode1, hdop, mode2, gpsweek, gpstow, nsats) = \ 247 (xpos, ypos, zpos, xvel, yvel, zvel, mode1, hdop, mode2, gpsweek, gpstow, nsats) = \
248 struct.unpack(fmt, datastr) 248 struct.unpack(fmt, datastr)
249 249
250 satprn = [] 250 satprn = []
251 for i in xrange(nsats): 251 for i in xrange(nsats):
252 satprn.append(data[struct.calcsize(fmt) + i]) 252 satprn.append(data[struct.calcsize(fmt) - 1 + i])
253 253
254 xvel = float(xvel) / 8 254 xvel = float(xvel) / 8
255 yvel = float(yvel) / 8 255 yvel = float(yvel) / 8
256 zvel = float(zvel) / 8 256 zvel = float(zvel) / 8
257 print " Position: X: %d, Y: %d, Z: %d" % (xpos, ypos, zpos) 257 hdop = float(hdop) / 5
258 print " Velocity: X: %.2f, Y: %.2f, Z: %.2f" % (xvel, yvel, zvel) 258 gpstow = float(gpstow) / 100
259
260 print " Position: X: %d m, Y: %d m, Z: %d m" % (xpos, ypos, zpos)
261 print " Velocity: X: %.2f m/s, Y: %.2f m/s, Z: %.2f m/s" % (xvel, yvel, zvel)
262 print " HDOP: %.1f, Week: %d, TOW: %.2f seconds" % (hdop, gpsweek, gpstow)
259 elif data[0] == 0x06: 263 elif data[0] == 0x06:
260 nulidx = data.index(0) 264 nulidx = data.index(0)
261 print " SW Ver : %s" % (reduce(lambda x, y: x + y, map(chr, data[1:nulidx]))) 265 print " SW Ver : %s" % (reduce(lambda x, y: x + y, map(chr, data[1:nulidx])))
262 elif data[0] == 0x0a: 266 elif data[0] == 0x0a:
263 errid = data[1] << 8 | data[2] 267 errid = data[1] << 8 | data[2]
287 latitude, longitude, alt_elip, alt_msl, datum, sog, cog, magvar, climbrate, 291 latitude, longitude, alt_elip, alt_msl, datum, sog, cog, magvar, climbrate,
288 headrate, estHPE, estVPE, estTE, estHVE, clockbias, CBerr, clockdrift, 292 headrate, estHPE, estVPE, estTE, estHVE, clockbias, CBerr, clockdrift,
289 CDerr, distance, distanceErr, headErr, numsvs, hdop, addmodeinfo) = \ 293 CDerr, distance, distanceErr, headErr, numsvs, hdop, addmodeinfo) = \
290 struct.unpack(fmt, datastr) 294 struct.unpack(fmt, datastr)
291 tow = float(tow) / 1e3 295 tow = float(tow) / 1e3
296 second = float(second) / 1e3
292 latitude = float(latitude) / 1e7 297 latitude = float(latitude) / 1e7
293 longitude = float(longitude) / 1e7 298 longitude = float(longitude) / 1e7
294 alt_elip = float(alt_elip) / 1e2 299 alt_elip = float(alt_elip) / 1e2
295 alt_msl = float(alt_msl) / 1e2 300 alt_msl = float(alt_msl) / 1e2
296 sog = float(sog) / 1e2 301 sog = float(sog) / 1e2
306 clockdrift = float(clockdrift) / 1e2 311 clockdrift = float(clockdrift) / 1e2
307 CDerr = float(CDerr) / 1e2 312 CDerr = float(CDerr) / 1e2
308 headErr = float(headErr) / 1e2 313 headErr = float(headErr) / 1e2
309 hdop = float(hdop) / 5 314 hdop = float(hdop) / 5
310 315
311 print " Fix : %s, Sats : %d, Lat: %s, Long: %s, Alt: %.2fm" % \ 316 utctime = timegm((year, month, day, hour, minute, int(second)))
312 (fixtype[navtype & 0x7], numsvs, fmtlatlong(latitude), fmtlatlong(longitude), \ 317 utctime = utctime + (second - int(second))
313 alt_msl) 318 print " Fix : %s, Sats : %d, Lat: %.06f, Long: %.06f, Alt: %.02fm" % \
319 (fixtype[navtype & 0x7], numsvs, latitude, longitude, alt_msl)
320 print " Date : %04d/%02d/%02d %02d:%02d:%02d.%03d" % \
321 (year, month, day, hour, month, int(second), int((second - int(second)) * 1000))
322 print " Epoch time : %.3f, Delta %.3f" % (utctime, abs(time.time() - utctime))
323 elif data[0] == 0x34:
324 fmt = '>BBBBBHHIB'
325 datastr = reduce(lambda x, y: x + y, map(chr, data[1:struct.calcsize(fmt) + 1]))
326 (hour, minute, second, day, month, year, offsetint, offsetfrac, status) = \
327 struct.unpack(fmt, datastr)
328 offset = offsetint + float(offsetfrac) / 1e9
329 print " PPS : %04d/%02d/%02d %02d:%02d:%02d, Offset : %.9f, Status : 0x%02x" % \
330 (year, month, day, hour, minute, second, offset, status)
314 elif data[0] == 0xa6: 331 elif data[0] == 0xa6:
315 print " Message rate : MID 0x%02x, rate 0x%02x" % (data[2], data[3]) 332 print " Message rate : MID 0x%02x, rate 0x%02x" % (data[2], data[3])
316 333
317 def enablemsgs(s): 334 def enablemsgs(s):
318 s.write(Parser.OrdLsttoStr(Parser.Encap([0xa6, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00]))) 335 s.write(Parser.OrdLsttoStr(Parser.Encap([0xa6, 0x00, 0x02, 0x01, 0x00, 0x00, 0x00, 0x00])))
335 s.write(Parser.OrdLsttoStr(Parser.Encap([0xa6, 0x00, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00]))) 352 s.write(Parser.OrdLsttoStr(Parser.Encap([0xa6, 0x00, 0x32, 0x00, 0x00, 0x00, 0x00, 0x00])))
336 s.write(Parser.OrdLsttoStr(Parser.Encap([0xa6, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00]))) 353 s.write(Parser.OrdLsttoStr(Parser.Encap([0xa6, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00])))
337 s.write(Parser.OrdLsttoStr(Parser.Encap([0xa6, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00]))) 354 s.write(Parser.OrdLsttoStr(Parser.Encap([0xa6, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x00])))
338 355
339 356
357 def timegm(tuple):
358 oldtz = None
359 if 'TZ' in os.environ:
360 oldtz = os.environ['TZ']
361 os.environ['TZ'] = 'UTC'
362
363 t = time.mktime(tuple + (0, 0, 0))
364 if oldtz == None:
365 del os.environ['TZ']
366 else:
367 os.environ['TZ'] = oldtz
368
369 return t