Improve SD logger
Posted: Sun Oct 23, 2022 9:51 am
I'm working on a project to read data over CAN, display it on LCD and log data to SD card. LCD is on HSPI and SD card on VSPI. CAN and LCD code is working on core 1 and SD on core 0, there's no wifi. It's all working pretty well but now I'm looking for further improvements. I set the log to 20 Hz and took a sample of 117024 lines. Below is the "uniq -c" output of the seconds that weren't logged at 20 Hz (left column is the actual rate and right column is a specific second in the log). So only 21 out of 117024 lines. But why does this happen and how can I make sure that every line is logged at 20 Hz?
- 19 1632
18 3195
17 5393
16 5401
16 1622
14 5397
14 1628
14 1625
14 1624
13 5400
13 5398
13 5396
13 5395
13 5394
13 1626
12 1631
12 1630
12 1627
12 1623
8 5399
8 1629
- float timeStamp;
- char filenameBuffer[23];
- String filename;
- bool logStarted = 0;
- uint32_t lastSyncTime = 0, logStartTime = 0;
- File file;
- void initSD()
- {
- if (SD.begin(5, SPI, 80000000))
- {
- Serial.println("Card Mount OK");
- updateFromFS(SD);
- generateFileName();
- sdInitialised = true;
- }
- else
- {
- Serial.println("Card Mount Failed");
- return;
- }
- }
- void logToSD()
- {
- if (!sdInitialised) return;
- if (!EMUfresh) return;
- if (!logStarted)
- {
- file = SD.open(filenameBuffer, FILE_APPEND);
- if (file)
- {
- file.print("Time,RPM,MAP,TPS,IAT,CLT,Lambda,PulseWidth,EGT1,EGT2,Vbatt,Baro,IgnAngle,LambdaCorr,Gear,Pwm1,Vss,EmuTemp,CEL,FLAGS1,LambdaTarget,OilTemp,OilPress,CanState\r\n");
- logStartTime = millis();
- logStarted = true;
- }
- }
- uint32_t duration = millis() - logStartTime;
- uint32_t seconds = duration / 1000;
- uint32_t milliseconds = duration % 1000;
- String dataString = "";
- dataString += seconds;
- dataString += ".";
- if (milliseconds < 100) { dataString += "0"; }
- if (milliseconds < 10) { dataString += "0"; }
- dataString += milliseconds;
- dataString += ",";
- dataString += String(emu_data.RPM);
- dataString += ",";
- dataString += String(emu_data.MAP);
- dataString += ",";
- dataString += String(emu_data.TPS);
- dataString += ",";
- dataString += String(emu_data.IAT);
- dataString += ",";
- dataString += String(emu_data.CLT);
- dataString += ",";
- dataString += String(emu_data.lambda);
- dataString += ",";
- dataString += String(emu_data.pulseWidth);
- dataString += ",";
- dataString += String(emu_data.EGT1);
- dataString += ",";
- dataString += String(emu_data.EGT2);
- dataString += ",";
- dataString += String(emu_data.vBat);
- dataString += ",";
- dataString += String(emu_data.baro);
- dataString += ",";
- dataString += String(emu_data.ignAngle, 0);
- dataString += ",";
- dataString += String(emu_data.lambdaCorr, 0);
- dataString += ",";
- dataString += String(emu_data.gear);
- dataString += ",";
- dataString += String(emu_data.pwm1);
- dataString += ",";
- dataString += String(emu_data.vssSpeed);
- dataString += ",";
- dataString += String(emu_data.emuTemp);
- dataString += ",";
- dataString += String(emu_data.cel);
- dataString += ",";
- dataString += String(emu_data.flags1);
- dataString += ",";
- dataString += String(emu_data.lambdaTarget);
- dataString += ",";
- dataString += String(emu_data.oilTemperature);
- dataString += ",";
- dataString += String(emu_data.oilPressure);
- //dataString += ",";
- //dataString += String(can_error_data.state[1]); //Only first character
- dataString += "\r\n";
- file.print(dataString.c_str());
- if (millis() - lastSyncTime > 1000)
- {
- lastSyncTime = millis();
- file.flush();
- }
- }
- void generateFileName()
- {
- char intBuffer[5];
- strcpy(filenameBuffer, "/");
- itoa(random(1000, 9999), intBuffer, 10);
- strcat(filenameBuffer, intBuffer);
- strcat(filenameBuffer, "-");
- itoa(random(10), intBuffer, 10);
- strcat(filenameBuffer, intBuffer);
- strcat(filenameBuffer, "-");
- itoa(random(10), intBuffer, 10);
- strcat(filenameBuffer, intBuffer);
- strcat(filenameBuffer, "_");
- itoa(random(10), intBuffer, 10);
- strcat(filenameBuffer, intBuffer);
- strcat(filenameBuffer, ".");
- itoa(random(10), intBuffer, 10);
- strcat(filenameBuffer, intBuffer);
- strcat(filenameBuffer, ".");
- itoa(random(10), intBuffer, 10);
- strcat(filenameBuffer, intBuffer);
- strcat(filenameBuffer, ".csv");
- Serial.println(filenameBuffer);
- }