I have a robot running control code with real time priority on a PREEMPT-RT patched Linux OS on a Beaglebone Black. All code is written in C and is running at 500Hz.
I've noticed latency in the range of a few hundred milliseconds every so often when running the code and I've tracked it down to the data logging function I wrote. This latency causes my robot's control to fail as I have a lot depending on the real-time functionality.
The relevant portion of code is below. I've cut a lot of code for clarity, but I'll edit this post if anything is needed.
FILE *file;
int main(int argc, char** argv) {
file = fopen(logname, "w");
while (1) {
/* Control code stuff*/
logData();
time_msec = time_msec + controlLoopTime;
}
}
void logData() {
if (time_msec - logTimer_msec >= LOG_TIMER) {
logTimer_msec = time_msec;
if (!bLogCreated) {
fprintf(file,
"SensorData1 SensorData2 SensorDataN"
);
bLogCreated = TRUE;
}
// log data to file
fprintf(file,
"%.2f %.2f\n",
sensorData1, sensorData2, sensorDataN
);
}
}
I need to log data from multiple variables (probably 20-50) at a good rate, maybe 100-125Hz. Data doesn't need to be logged at the control rate (every 2ms), but I've decreased it to 12ms and I still see latency spikes every few minutes.
The latency may be an issue with the fprintf
call. Is this a limitation of the BeagleBone Black, my code, or just the nature of data logging?
A similar question was asked here but didn't seem to address my issue: Finding latency issues (stalls) in embedded Linux systems
fprintf
ing 4096 bytes the buffer will be flushed to disk which may cause the latency. Can't you outsource the actual logging to a separate thread? Queue logmessages and dequeue them in your logthread.