Fix the use of GPS as time source in the new cameteo module

This commit is contained in:
Pierrick C 2018-09-10 10:01:36 +02:00
parent aafc5f2c77
commit 1cce6281b5
2 changed files with 29 additions and 16 deletions

View File

@ -328,7 +328,7 @@ class GPSData(Data):
def __init__(self, name="GPS", def __init__(self, name="GPS",
update_interval=0, write_interval=60, send_interval=60, update_interval=0, write_interval=60, send_interval=60,
enable=True, enable_pin=DigitalInOut(board.A5), enable=True, enable_pin=DigitalInOut(board.A5),
rtc=None, debug=False): debug=False):
Data.__init__(self, name=name, Data.__init__(self, name=name,
update_interval=update_interval, update_interval=update_interval,
@ -342,7 +342,6 @@ class GPSData(Data):
'alt': float(), 'alt': float(),
'qual': int(), 'qual': int(),
'age': int()} 'age': int()}
self._rtc = rtc
self._enable = enable self._enable = enable
self._gps_last_fix = int() self._gps_last_fix = int()
self._gps_current_fix = int() self._gps_current_fix = int()
@ -370,8 +369,6 @@ class GPSData(Data):
self._gps.timestamp_utc.tm_hour, self._gps.timestamp_utc.tm_hour,
self._gps.timestamp_utc.tm_min, self._gps.timestamp_utc.tm_min,
self._gps.timestamp_utc.tm_sec) self._gps.timestamp_utc.tm_sec)
if self._rtc is not None:
set_clock_from_localtime(self._rtc)
if self._gps.has_fix: if self._gps.has_fix:
self._gps_last_fix = self._gps_current_fix self._gps_last_fix = self._gps_current_fix
self.data['lat'] = self._gps.latitude self.data['lat'] = self._gps.latitude
@ -397,25 +394,23 @@ class GPSData(Data):
self._gps.send_command('PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0') self._gps.send_command('PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
self._gps.send_command('PMTK220,1000') # 1000 ms refresh rate self._gps.send_command('PMTK220,1000') # 1000 ms refresh rate
if self._rtc is not None:
rtc.set_time_source(self._rtc)
if self.debug:
print("GPS is the time source!")
def disable(self): def disable(self):
"""Disable GPS module""" """Disable GPS module"""
if self._enable: if self._enable:
self._enable = False self._enable = False
self._gps_en_pin.value = not self._enable # Set enable pin high to disable GPS module self._gps_en_pin.value = not self._enable # Set enable pin high to disable GPS module
# Switch backto internal RTC
rtc.set_time_source(rtc.RTC()) @property
def datetime(self):
"""Wrapper to feed rtc.set_time_source()"""
return self._gps.timestamp_utc
############# #############
# Functions # # Functions #
############# #############
def set_clock_from_localtime(clock, treshold=2.0): def set_clock_from_localtime(clock, threshold=2.0, debug=False):
""" """
Compare internal RTC and date-time from time.localtime() and set Compare internal RTC and date-time from time.localtime() and set
the RTC date/time to this value if the difference is more or equal to the RTC date/time to this value if the difference is more or equal to
@ -423,9 +418,24 @@ def set_clock_from_localtime(clock, treshold=2.0):
The source of time.localtime() can be set with rtc.set_time_source() The source of time.localtime() can be set with rtc.set_time_source()
""" """
if debug:
print("Clock date/time : " + TIME_FORMAT.format(*clock.datetime[0:6]))
print("Local date/time : " + TIME_FORMAT.format(*time.localtime()[0:6]))
# Check if time.localtime() is valid and can be converted in seconds
try:
local_seconds = time.mktime(time.localtime())
except OverflowError as err:
print(err)
print("Local time source : " + TIME_FORMAT.format(*time.localtime()[0:6]))
print("Wait some time to have proper time from time source...")
return 1
# Max difference between GPS and internal RTC (in seconds): # Max difference between GPS and internal RTC (in seconds):
if abs(time.mktime(time.localtime()) - time.mktime(clock.datetime)) >= treshold: if abs(local_seconds - time.mktime(clock.datetime)) >= threshold:
# print("Clock difference with GPS!") print("Clock difference with GPS!")
# print("Previous date/time : " + TIME_FORMAT.format(*clock.datetime[0:6])) print("Previous date/time : " + TIME_FORMAT.format(*clock.datetime[0:6]))
clock.datetime = time.localtime() # Trust localtime if there is a bias clock.datetime = time.localtime() # Trust localtime if there is a bias
print("Clocks synced !") print("Clocks synced !")
return 0

View File

@ -91,10 +91,12 @@ else:
sys_data = cameteo.SysData(debug=False) sys_data = cameteo.SysData(debug=False)
bme_data = cameteo.BME280Data() bme_data = cameteo.BME280Data()
gps_data = cameteo.GPSData(rtc=CLOCK) gps_data = cameteo.GPSData()
data = [gps_data, sys_data, bme_data] data = [gps_data, sys_data, bme_data]
rtc.set_time_source(gps_data, debug=True)
# Init timers # Init timers
last_update = last_sent_packet = last_written_data = time.monotonic() last_update = last_sent_packet = last_written_data = time.monotonic()
@ -108,6 +110,7 @@ while True:
for src in data: for src in data:
src.update(current_time, verbose=PRINT_DATA) src.update(current_time, verbose=PRINT_DATA)
cameteo.set_clock_from_localtime(CLOCK, threshold=2.0)
for src in data: for src in data:
src.write_on_flash(current_time) src.write_on_flash(current_time)