Skip to content

Commit b1b598e

Browse files
committed
Move file data/time stamps to RTC.
1 parent a6115f6 commit b1b598e

File tree

3 files changed

+122
-137
lines changed

3 files changed

+122
-137
lines changed

Firmware/RTK_Surveyor/NVM.ino

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ void loadSettings()
4545
//Record these new settings to EEPROM and config file to be sure they are the same
4646
//(do this even if loadSystemSettingsFromFile returned false)
4747
recordSystemSettings();
48+
49+
log_d("Settings loaded");
4850
}
4951

5052
//Load settings without recording
@@ -84,6 +86,7 @@ void recordSystemSettings()
8486
EEPROM.put(0, settings);
8587
EEPROM.commit();
8688
delay(1); //Give CPU time to pet WDT
89+
log_d("System settings recorded");
8790
}
8891
else
8992
{
@@ -115,8 +118,8 @@ void recordSystemSettingsToFile()
115118
Serial.println(F("Failed to create settings file"));
116119
return;
117120
}
118-
if (online.gnss)
119-
updateDataFileCreate(&settingsFile); // Update the file to create time & date
121+
122+
updateDataFileCreate(&settingsFile); // Update the file to create time & date
120123

121124
settingsFile.println("sizeOfSettings=" + (String)settings.sizeOfSettings);
122125
settingsFile.println("rtkIdentifier=" + (String)settings.rtkIdentifier);
@@ -194,11 +197,12 @@ void recordSystemSettingsToFile()
194197
settingsFile.println(tempString);
195198
}
196199

197-
if (online.gnss)
198-
updateDataFileAccess(&settingsFile); // Update the file access time & date
200+
updateDataFileAccess(&settingsFile); // Update the file access time & date
199201

200202
settingsFile.close();
201203

204+
log_d("System settings recorded to file");
205+
202206
xSemaphoreGive(xFATSemaphore);
203207
}
204208
}
@@ -337,6 +341,9 @@ bool parseLine(char* str) {
337341
}
338342
}
339343

344+
// log_d("settingName: %s", settingName);
345+
// log_d("settingValue: %s", settingValue);
346+
// log_d("d: %0.3f", d);
340347

341348
// Get setting name
342349
if (strcmp(settingName, "sizeOfSettings") == 0)

Firmware/RTK_Surveyor/RTK_Surveyor.ino

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ long lastStackReport = 0; //Controls the report rate of stack highwater mark wit
344344
uint32_t lastHeapReport = 0; //Report heap every 1s if option enabled
345345
uint32_t lastTaskHeapReport = 0; //Report task heap every 1s if option enabled
346346
uint32_t lastCasterLEDupdate = 0; //Controls the cycling of position LEDs during casting
347-
uint32_t lastBeginLoggingAttempt = 0; //Wait 1000ms between newLog creation for ZED to get date/time
347+
uint32_t lastRTCAttempt = 0; //Wait 1000ms between checking GNSS for current date/time
348348

349349
uint32_t lastSatelliteDishIconUpdate = 0;
350350
bool satelliteDishIconDisplayed = false; //Toggles as lastSatelliteDishIconUpdate goes above 1000ms
@@ -562,8 +562,8 @@ void updateLogs()
562562
//Calculate generation and write speeds every 5 seconds
563563
uint32_t fileSizeDelta = fileSize - lastLogSize;
564564
Serial.printf(" - Generation rate: %0.1fkB/s", fileSizeDelta / 5.0 / 1000.0);
565-
566-
if(totalWriteTime > 0)
565+
566+
if (totalWriteTime > 0)
567567
Serial.printf(" - Write speed: %0.1fkB/s", fileSizeDelta / (totalWriteTime / 1000000.0) / 1000.0);
568568
else
569569
Serial.printf(" - Write speed: 0.0kB/s");
@@ -600,19 +600,39 @@ void updateRTC()
600600
{
601601
if (online.gnss == true)
602602
{
603-
if (i2cGNSS.getConfirmedDate() == true && i2cGNSS.getConfirmedTime() == true)
603+
if (millis() - lastRTCAttempt > 1000)
604604
{
605-
//Set the internal system time
606-
//This is normally set with WiFi NTP but we will rarely have WiFi
607-
rtc.setTime(i2cGNSS.getSecond(), i2cGNSS.getMinute(), i2cGNSS.getHour(), i2cGNSS.getDay(), i2cGNSS.getMonth(), i2cGNSS.getYear()); // 17th Jan 2021 15:24:30
605+
lastRTCAttempt = millis();
608606

609-
online.rtc = true;
607+
i2cGNSS.checkUblox();
610608

611-
Serial.print(F("System time set to: "));
612-
Serial.println(rtc.getTime("%B %d %Y %H:%M:%S")); //From ESP32Time library example
609+
bool timeValid = false;
610+
if (i2cGNSS.getTimeValid() == true && i2cGNSS.getDateValid() == true) //Will pass if ZED's RTC is reporting (regardless of GNSS fix)
611+
timeValid = true;
612+
if (i2cGNSS.getConfirmedTime() == true && i2cGNSS.getConfirmedDate() == true) //Requires GNSS fix
613+
timeValid = true;
613614

614-
recordSystemSettingsToFile(); //This will re-record the setting file with current date/time.
615-
}
616-
}
617-
}
615+
if (timeValid == true)
616+
{
617+
//Set the internal system time
618+
//This is normally set with WiFi NTP but we will rarely have WiFi
619+
rtc.setTime(i2cGNSS.getSecond(), i2cGNSS.getMinute(), i2cGNSS.getHour(), i2cGNSS.getDay(), i2cGNSS.getMonth(), i2cGNSS.getYear()); // 17th Jan 2021 15:24:30
620+
621+
online.rtc = true;
622+
623+
Serial.print(F("System time set to: "));
624+
Serial.println(rtc.getTime("%B %d %Y %H:%M:%S")); //From ESP32Time library example
625+
626+
recordSystemSettingsToFile(); //This will re-record the setting file with current date/time.
627+
}
628+
else
629+
{
630+
Serial.println("No GNSS date/time available for system RTC.");
631+
} //End timeValid
632+
} //End lastRTCAttempt
633+
} //End online.gnss
634+
} //End online.rtc
635+
}
636+
637+
void printElapsedTime(const char* title)
618638
}

Firmware/RTK_Surveyor/menuMessages.ino

Lines changed: 77 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -306,153 +306,111 @@ void beginLogging()
306306
{
307307
if (online.logging == false)
308308
{
309-
if (online.microSD == true && settings.enableLogging == true)
309+
if (online.microSD == true && settings.enableLogging == true && online.rtc == true) //We can't create a file until we have date/time
310310
{
311-
//Wait 1000ms between newLog creation for ZED to get date/time
312-
if (millis() - lastBeginLoggingAttempt > 1000)
313-
{
314-
lastBeginLoggingAttempt = millis();
311+
char fileName[40] = "";
315312

316-
char fileName[40] = "";
313+
if (reuseLastLog == true) //attempt to use previous log
314+
{
315+
if (findLastLog(fileName) == false)
316+
log_d("Failed to find last log. Making new one.");
317+
}
317318

318-
i2cGNSS.checkUblox();
319+
if (strlen(fileName) == 0)
320+
{
321+
sprintf(fileName, "%s_%02d%02d%02d_%02d%02d%02d.ubx", //SdFat library
322+
platformFilePrefix,
323+
rtc.getYear() - 2000, rtc.getMonth(), rtc.getDay(),
324+
rtc.getHour(), rtc.getMinute(), rtc.getSecond()
325+
);
326+
}
319327

320-
if (reuseLastLog == true) //attempt to use previous log
328+
//Attempt to write to file system. This avoids collisions with file writing in F9PSerialReadTask()
329+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
330+
{
331+
// O_CREAT - create the file if it does not exist
332+
// O_APPEND - seek to the end of the file prior to each write
333+
// O_WRITE - open for write
334+
if (ubxFile.open(fileName, O_CREAT | O_APPEND | O_WRITE) == false)
321335
{
322-
if (findLastLog(fileName) == false)
323-
{
324-
Serial.println(F("Failed to find last log. Making new one."));
325-
}
336+
Serial.printf("Failed to create GNSS UBX data file: %s\n\r", fileName);
337+
online.logging = false;
338+
xSemaphoreGive(xFATSemaphore);
339+
return;
326340
}
327341

328-
if (strlen(fileName) == 0)
329-
{
330-
//Based on GPS data/time, create a log file in the format SFE_Surveyor_YYMMDD_HHMMSS.ubx
331-
bool timeValid = false;
332-
// if (i2cGNSS.getTimeValid() == true && i2cGNSS.getDateValid() == true) //Will pass if ZED's RTC is reporting (regardless of GNSS fix)
333-
// timeValid = true;
334-
if (i2cGNSS.getConfirmedTime() == true && i2cGNSS.getConfirmedDate() == true) //Requires GNSS fix
335-
timeValid = true;
336-
337-
if (timeValid == false)
338-
{
339-
Serial.println(F("beginLoggingUBX: No date/time available. No file created."));
340-
online.logging = false;
341-
return;
342-
}
342+
updateDataFileCreate(&ubxFile); // Update the file to create time & date
343343

344-
sprintf(fileName, "%s_%02d%02d%02d_%02d%02d%02d.ubx", //SdFat library
345-
platformFilePrefix,
346-
i2cGNSS.getYear() - 2000, i2cGNSS.getMonth(), i2cGNSS.getDay(),
347-
i2cGNSS.getHour(), i2cGNSS.getMinute(), i2cGNSS.getSecond()
348-
);
349-
}
344+
startLogTime_minutes = millis() / 1000L / 60; //Mark now as start of logging
350345

351-
//Attempt to write to file system. This avoids collisions with file writing in F9PSerialReadTask()
352-
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_longWait_ms) == pdPASS)
346+
//Add NMEA txt message with restart reason
347+
char rstReason[30];
348+
switch (esp_reset_reason())
353349
{
354-
// O_CREAT - create the file if it does not exist
355-
// O_APPEND - seek to the end of the file prior to each write
356-
// O_WRITE - open for write
357-
if (ubxFile.open(fileName, O_CREAT | O_APPEND | O_WRITE) == false)
358-
{
359-
Serial.printf("Failed to create GNSS UBX data file: %s\n\r", fileName);
360-
online.logging = false;
361-
xSemaphoreGive(xFATSemaphore);
362-
return;
363-
}
364-
365-
updateDataFileCreate(&ubxFile); // Update the file to create time & date
366-
367-
startLogTime_minutes = millis() / 1000L / 60; //Mark now as start of logging
368-
369-
//Add NMEA txt message with restart reason
370-
char rstReason[30];
371-
switch (esp_reset_reason())
372-
{
373-
case ESP_RST_UNKNOWN: strcpy(rstReason, "ESP_RST_UNKNOWN"); break;
374-
case ESP_RST_POWERON : strcpy(rstReason, "ESP_RST_POWERON"); break;
375-
case ESP_RST_SW : strcpy(rstReason, "ESP_RST_SW"); break;
376-
case ESP_RST_PANIC : strcpy(rstReason, "ESP_RST_PANIC"); break;
377-
case ESP_RST_INT_WDT : strcpy(rstReason, "ESP_RST_INT_WDT"); break;
378-
case ESP_RST_TASK_WDT : strcpy(rstReason, "ESP_RST_TASK_WDT"); break;
379-
case ESP_RST_WDT : strcpy(rstReason, "ESP_RST_WDT"); break;
380-
case ESP_RST_DEEPSLEEP : strcpy(rstReason, "ESP_RST_DEEPSLEEP"); break;
381-
case ESP_RST_BROWNOUT : strcpy(rstReason, "ESP_RST_BROWNOUT"); break;
382-
case ESP_RST_SDIO : strcpy(rstReason, "ESP_RST_SDIO"); break;
383-
default : strcpy(rstReason, "Unknown");
384-
}
350+
case ESP_RST_UNKNOWN: strcpy(rstReason, "ESP_RST_UNKNOWN"); break;
351+
case ESP_RST_POWERON : strcpy(rstReason, "ESP_RST_POWERON"); break;
352+
case ESP_RST_SW : strcpy(rstReason, "ESP_RST_SW"); break;
353+
case ESP_RST_PANIC : strcpy(rstReason, "ESP_RST_PANIC"); break;
354+
case ESP_RST_INT_WDT : strcpy(rstReason, "ESP_RST_INT_WDT"); break;
355+
case ESP_RST_TASK_WDT : strcpy(rstReason, "ESP_RST_TASK_WDT"); break;
356+
case ESP_RST_WDT : strcpy(rstReason, "ESP_RST_WDT"); break;
357+
case ESP_RST_DEEPSLEEP : strcpy(rstReason, "ESP_RST_DEEPSLEEP"); break;
358+
case ESP_RST_BROWNOUT : strcpy(rstReason, "ESP_RST_BROWNOUT"); break;
359+
case ESP_RST_SDIO : strcpy(rstReason, "ESP_RST_SDIO"); break;
360+
default : strcpy(rstReason, "Unknown");
361+
}
385362

386-
char nmeaMessage[82]; //Max NMEA sentence length is 82
387-
createNMEASentence(CUSTOM_NMEA_TYPE_RESET_REASON, nmeaMessage, rstReason); //textID, buffer, text
388-
ubxFile.println(nmeaMessage);
363+
char nmeaMessage[82]; //Max NMEA sentence length is 82
364+
createNMEASentence(CUSTOM_NMEA_TYPE_RESET_REASON, nmeaMessage, rstReason); //textID, buffer, text
365+
ubxFile.println(nmeaMessage);
389366

390-
//Record system firmware versions and info to log
391-
392-
//SparkFun RTK Express v1.10-Feb 11 2022
393-
char firmwareVersion[30]; //v1.3 December 31 2021
394-
sprintf(firmwareVersion, "v%d.%d-%s", FIRMWARE_VERSION_MAJOR, FIRMWARE_VERSION_MINOR, __DATE__);
395-
createNMEASentence(CUSTOM_NMEA_TYPE_SYSTEM_VERSION, nmeaMessage, firmwareVersion); //textID, buffer, text
396-
ubxFile.println(nmeaMessage);
367+
//Record system firmware versions and info to log
397368

398-
//ZED-F9P firmware: HPG 1.30
399-
createNMEASentence(CUSTOM_NMEA_TYPE_ZED_VERSION, nmeaMessage, zedFirmwareVersion); //textID, buffer, text
400-
ubxFile.println(nmeaMessage);
369+
//SparkFun RTK Express v1.10-Feb 11 2022
370+
char firmwareVersion[30]; //v1.3 December 31 2021
371+
sprintf(firmwareVersion, "v%d.%d-%s", FIRMWARE_VERSION_MAJOR, FIRMWARE_VERSION_MINOR, __DATE__);
372+
createNMEASentence(CUSTOM_NMEA_TYPE_SYSTEM_VERSION, nmeaMessage, firmwareVersion); //textID, buffer, text
373+
ubxFile.println(nmeaMessage);
401374

402-
if (reuseLastLog == true)
403-
{
404-
Serial.println(F("Appending last available log"));
405-
}
375+
//ZED-F9P firmware: HPG 1.30
376+
createNMEASentence(CUSTOM_NMEA_TYPE_ZED_VERSION, nmeaMessage, zedFirmwareVersion); //textID, buffer, text
377+
ubxFile.println(nmeaMessage);
406378

407-
xSemaphoreGive(xFATSemaphore);
408-
}
409-
else
379+
if (reuseLastLog == true)
410380
{
411-
Serial.println(F("Failed to get file system lock to create GNSS UBX data file"));
412-
online.logging = false;
413-
return;
381+
Serial.println(F("Appending last available log"));
414382
}
415383

416-
Serial.printf("Log file created: %s\n\r", fileName);
417-
online.logging = true;
418-
} //lastBeginLoggingAttempt > 1000ms
419-
} //sdOnline, settings.logging = true
420-
else
421-
online.logging = false;
422-
} //online.logging = false
384+
xSemaphoreGive(xFATSemaphore);
385+
}
386+
else
387+
{
388+
Serial.println(F("Failed to get file system lock to create GNSS UBX data file"));
389+
online.logging = false;
390+
return;
391+
}
392+
393+
Serial.printf("Log file created: %s\n\r", fileName);
394+
online.logging = true;
395+
} //online.sd, enable.logging, online.rtc
396+
} //online.logging
423397
}
424398

425-
//Updates the timestemp on a given data file
399+
//Update the file access and write time with date and time obtained from GNSS
426400
void updateDataFileAccess(SdFile *dataFile)
427401
{
428-
bool timeValid = false;
429-
if (i2cGNSS.getTimeValid() == true && i2cGNSS.getDateValid() == true) //Will pass if ZED's RTC is reporting (regardless of GNSS fix)
430-
timeValid = true;
431-
if (i2cGNSS.getConfirmedTime() == true && i2cGNSS.getConfirmedDate() == true) //Requires GNSS fix
432-
timeValid = true;
433-
434-
if (timeValid == true)
402+
if (online.rtc == true)
435403
{
436-
//Update the file access time
437-
dataFile->timestamp(T_ACCESS, i2cGNSS.getYear(), i2cGNSS.getMonth(), i2cGNSS.getDay(), i2cGNSS.getHour(), i2cGNSS.getMinute(), i2cGNSS.getSecond());
438-
//Update the file write time
439-
dataFile->timestamp(T_WRITE, i2cGNSS.getYear(), i2cGNSS.getMonth(), i2cGNSS.getDay(), i2cGNSS.getHour(), i2cGNSS.getMinute(), i2cGNSS.getSecond());
404+
dataFile->timestamp(T_ACCESS, rtc.getYear(), rtc.getMonth(), rtc.getDay(), rtc.getHour(), rtc.getMinute(), rtc.getSecond());
405+
dataFile->timestamp(T_WRITE, rtc.getYear(), rtc.getMonth(), rtc.getDay(), rtc.getHour(), rtc.getMinute(), rtc.getSecond());
440406
}
441407
}
442408

409+
//Update the file create time with date and time obtained from GNSS
443410
void updateDataFileCreate(SdFile *dataFile)
444411
{
445-
bool timeValid = false;
446-
if (i2cGNSS.getTimeValid() == true && i2cGNSS.getDateValid() == true) //Will pass if ZED's RTC is reporting (regardless of GNSS fix)
447-
timeValid = true;
448-
if (i2cGNSS.getConfirmedTime() == true && i2cGNSS.getConfirmedDate() == true) //Requires GNSS fix
449-
timeValid = true;
450-
451-
if (timeValid == true)
452-
{
453-
//Update the file create time
454-
dataFile->timestamp(T_CREATE, i2cGNSS.getYear(), i2cGNSS.getMonth(), i2cGNSS.getDay(), i2cGNSS.getHour(), i2cGNSS.getMinute(), i2cGNSS.getSecond());
455-
}
412+
if (online.rtc == true)
413+
dataFile->timestamp(T_CREATE, rtc.getYear(), rtc.getMonth(), rtc.getDay(), rtc.getHour(), rtc.getMinute(), rtc.getSecond());
456414
}
457415

458416
//Finds last log

0 commit comments

Comments
 (0)