#!/usr/bin/python3 # thanks again to Adafruit for all their wonderful boards, libs and docs. # # docs: # https://learn.adafruit.com/adafruit-mini-gps-pa1010d-module?view=all # # datasheet: # https://cdn-learn.adafruit.com/assets/assets/000/084/295/original/CD_PA1010D_Datasheet_v.03.pdf import time, board, busio, adafruit_gps, sys, argparse sys.path.append('/import/home/ghz/repos/wxlib') import wxlib as wx i2c = board.I2C() gps = adafruit_gps.GPS_GtopI2C(i2c) # note this isn't /import/home, this is on the SD card wx_dir = "/home/ghz/alt" gps.send_command(b'PMTK314,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0') gps.send_command(b'PMTK220,100') dat_fname = 'alt.dat' time1 = time0 = time.monotonic() alt_c = sat_c = vdop_c = hdop_c = pdop_c = cnt = 0 parser = argparse.ArgumentParser(description=u'Collect and average elevation data from a gps reciever, and then upload it somewhere.') parser.add_argument('--debug', dest = 'debug', action = 'store_true', help = u'print intermittent data collection steps to console.') args = parser.parse_args() while True: time1 = time.monotonic() try: # apparently gps.update() can fail on us, from sth going on 4 lib functions down. gps_up_stat = gps.update() except KeyboardInterrupt: # this is not very likely, but whatever. print("keyboard interrupt. bailing.") exit() except: # warn. wait. bail. print("gps.update() error. prly from deep in the bowels of a lib somewere.") time.sleep(1.0) # wait a tic, mostly so we don't spam the console with infinite error messages when the data cable gets cut. continue # we haven't done anything yet, so just try again, time1 will take care of itself. if gps_up_stat: to = gps.timestamp_utc if gps.has_fix and to and to.tm_year and ((time1 - time0) > .1): time0 = time.monotonic() TS = "{}-{:02}-{:02}T{:02}:{:02}:{:02}Z".format( to.tm_year, to.tm_mon, to.tm_mday, to.tm_hour, to.tm_min, to.tm_sec) if args.debug: print(TS, end = "\t") alt = gps.altitude_m if alt is not None and args.debug: print(alt, "m", end = "\t") vdop = gps.vdop hdop = gps.hdop pdop = gps.pdop if (vdop and hdop and pdop) is not None and args.debug: print("{:.2f} vdop".format(vdop), end = "\t") print("{:.2f} hdop".format(hdop), end = "\t") print("{:.2f} pdop".format(pdop), end = "\t") # this has so far always returned 47.9 m if args.debug: geo_h = gps.height_geoid if geo_h is not None: print("{} m geoid height".format(geo_h), end = "\t") sats = gps.satellites if sats is not None and args.debug: print(sats, "sats") if (alt and sats and vdop and hdop and pdop) is not None: # experience (graphs) hath shewn that our worst data comes below these conditions. lose them. if (sats > 8.00 and pdop < 2.90 and hdop < 2.70 and vdop < 1.00): alt_c += alt sat_c += sats vdop_c += vdop hdop_c += hdop pdop_c += pdop cnt += 1 if cnt >= 60: cts = time.strftime("%FT%TZ", time.gmtime()) dat_s = "{}\t{:.2f} m\t{:.2f} vdop\t{:.2f} hdop \t{:.2f} pdop\t{:.2f} sats\n".format( cts, alt_c / cnt, vdop_c / cnt, hdop_c / cnt, pdop_c / cnt, sat_c / cnt) wx.write_out_dat_stamp_iso(cts, dat_fname, dat_s, wx_dir) if args.debug: print(dat_s) alt_c = sat_c = vdop_c = hdop_c = pdop_c = cnt = 0 time.sleep(0.05)