Skip to content

Commit

Permalink
Better RTC sync and printConditions
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulZC committed Sep 24, 2024
1 parent ac0a63b commit 9dbcbdc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
46 changes: 29 additions & 17 deletions Firmware/RTK_mosaic-T_Firmware/RTK_mosaic-T_Firmware.ino
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ unsigned long syncRTCInterval = 1000; // To begin, sync RTC every second. Interv

// ReceiverTime 5914
unsigned long gnssTimeArrivalMillis = 0;
bool gnssTimeUpdated[2] = { false, false }; // RTC, TCXO
bool gnssTimeUpdated[3] = { false, false, false }; // RTC, TCXO, printConditions
uint32_t gnssTOW_ms = 0;
uint8_t gnssDay = 0;
uint8_t gnssMonth = 0;
Expand Down Expand Up @@ -400,27 +400,39 @@ void loop()
// Once we have a fix, sync system clock to GNSS
void updateRTC()
{
static bool firstTime = true;
if (firstTime)
{
gnssTimeUpdated[0] = false; // This ensures gnssTimeUpdated[0] isn't stale
firstTime = false;
}

if (online.rtc == false) // Only do this if the rtc has not been sync'd previously
{
if (online.gnss == true) // Only do this if the GNSS is online
{
if (gnssWNSet && gnssToWSet && gnssFineTime && gnssTimeUpdated[0])
if (gnssTimeUpdated[0])
{
// To perform the time zone adjustment correctly, it's easiest if we convert the GNSS time and date
// into Unix epoch first and then correct for the arrival time
uint32_t epochSecs;
uint32_t epochMillis;
convertGnssTimeToEpoch(&epochSecs, &epochMillis);

epochMillis += millis() - gnssTimeArrivalMillis; // Remove the lag

// Set the internal system time
rtc.setTime(epochSecs, epochMillis * 1000);

online.rtc = true;

systemPrint("System time set to: ");
systemPrintln(rtc.getDateTime(true));
gnssTimeUpdated[0] = false;

if (gnssWNSet && gnssToWSet && gnssFineTime)
{
// To perform the time zone adjustment correctly, it's easiest if we convert the GNSS time and date
// into Unix epoch first and then correct for the arrival time
uint32_t epochSecs;
uint32_t epochMillis;
convertGnssTimeToEpoch(&epochSecs, &epochMillis);

epochMillis += millis() - gnssTimeArrivalMillis; // Remove the lag

// Set the internal system time
rtc.setTime(epochSecs, epochMillis * 1000);

online.rtc = true;

systemPrint("System time set to: ");
systemPrintln(rtc.getDateTime(true));
}
}
} // End online.gnss
} // End online.rtc
Expand Down
13 changes: 10 additions & 3 deletions Firmware/RTK_mosaic-T_Firmware/System.ino
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ void settingsToDefaults()
// Periodically print conditions if enabled
void printConditions()
{
// Periodically print the position etc.
if (settings.enablePrintConditions && ((millis() - lastPrintConditions) > settings.periodicPrintInterval_ms))
// Periodically print the position etc. each time a ReceiverTime message is received
if (settings.enablePrintConditions && gnssTimeUpdated[2])
{
gnssTimeUpdated[2] = false;
printCurrentConditions(settings.enablePrintConditions == 2);
lastPrintConditions = millis();
}

// Periodically print the position etc.
//if (settings.enablePrintConditions && ((millis() - lastPrintConditions) > settings.periodicPrintInterval_ms))
//{
// printCurrentConditions(settings.enablePrintConditions == 2);
// lastPrintConditions = millis();
//}
}

// Print the error message every 15 seconds
Expand Down
2 changes: 1 addition & 1 deletion Firmware/RTK_mosaic-T_Firmware/Tasks.ino
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ void processConsumerMessage(PARSE_STATE *parse, uint8_t type)
{
if ((parse->message & 0x1FFF) == 5914) // ReceiverTime
{
gnssTimeUpdated[0] = gnssTimeUpdated[1] = true;
gnssTimeUpdated[0] = gnssTimeUpdated[1] = gnssTimeUpdated[2] = true;
gnssTimeArrivalMillis = millis();

gnssTOW_ms = ((uint32_t)parse->buffer[8]) << 0;
Expand Down
1 change: 0 additions & 1 deletion Firmware/RTK_mosaic-T_Firmware/menuSystem.ino
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ void menuDebugSoftware()
break;
}
}
systemPrintf("%s\r\n", settings.enablePrintConditions ? "Enabled" : "Disabled");

systemPrint("9) Print consumers: ");
systemPrintf("%s\r\n", settings.enablePrintConsumers ? "Enabled" : "Disabled");
Expand Down

0 comments on commit 9dbcbdc

Please sign in to comment.