From 37186668901750153e64214c0a7d185cf6ee53e7 Mon Sep 17 00:00:00 2001 From: Pierrick C Date: Sat, 4 Aug 2018 16:20:51 +0200 Subject: [PATCH] Add age (in seconds) of the last GPS fix --- circuitpython/code/main.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/circuitpython/code/main.py b/circuitpython/code/main.py index 3d0c429..391f20f 100644 --- a/circuitpython/code/main.py +++ b/circuitpython/code/main.py @@ -74,8 +74,11 @@ class Data: 'lat': float(), 'lon': float(), 'alt': float(), - 'qual': int()} + 'qual': int(), + 'age': int() } } + self._gps_last_fix = int() + self._gps_current_fix = int() def update(self): """Read the data from various sensors and update the data dict variable""" @@ -90,7 +93,9 @@ class Data: self.data['BME']['press'] = round(bme280.pressure, 2) if gps_enable: + self._gps_current_fix = int(time.monotonic()) if gps.has_fix: + self._gps_last_fix = self._gps_current_fix self.data['GPS']['time'] = datetime_format.format(gps.timestamp_utc.tm_year, gps.timestamp_utc.tm_mon, gps.timestamp_utc.tm_mday, @@ -101,10 +106,9 @@ class Data: self.data['GPS']['lon'] = gps.longitude self.data['GPS']['alt'] = gps.altitude_m self.data['GPS']['qual'] = gps.fix_quality + self.data['GPS']['age'] = 0 else: - self.data['GPS']['lat'] = None - self.data['GPS']['lon'] = None - self.data['GPS']['alt'] = None + self.data['GPS']['age'] = int(self._gps_current_fix - self._gps_last_fix) else: self.data['GPS'] = None @@ -220,10 +224,10 @@ def set_clock_from_GPS(treshold=5.0): gps.timestamp_utc.tm_sec, 0, 0, 0)) #Max difference between GPS and internal RTC (in seconds): if abs(time.mktime(gps_datetime) - time.mktime(clock.datetime)) >= treshold: - print("Clock difference with GPS!") - print("Previous date/time : " + datetime_format.format(*clock.datetime[0:6])) + # print("Clock difference with GPS!") + # print("Previous date/time : " + datetime_format.format(*clock.datetime[0:6])) clock.datetime = gps_datetime #Trust GPS if there is a bias - print("New date/time : " + datetime_format.format(*clock.datetime[0:6])) + print("Clocks synced !") def measure_vbat(samples=10, timestep=0.01): """Measure Vbattery as the mean of n samples with timestep second between