10 years, 8 months ago.

LocalFileSystem speed/timing issue?

Hi everyone,

I have been using the LocalFileSystem to log data from an analogIn, but I run into issues at frequencies of 250Hz or higher when logging the data. "Bumps" start to appear in the data and I don't know why.


In the example file below, I have the Timer t.read() being logged at 500Hz and the bumps can be seen in the excel file and printscreen that I have posted here. The values are all different, but the differences between the t.read() values changes at regular intervals, making the bumps appear.

The bumps appear when I use either Timer or analogIn, but NOT when I am just logging a float point number. Using Ticker doesn't do anything to remove the bumps either.

Is this a buffer issue or something else?


#include "mbed.h"

Ticker logticker;

LocalFileSystem local("local");               // Create the local filesystem under the name "local"
FILE *fp = fopen("/local/out.csv", "w");  // Open "out.txt" on the local file system for writing

Timer t;
float f = 0.0;
int i = 0;

void logger(void){

    fprintf(fp, "%F\n", t.read()); // gets bumps
//    fprintf(fp, "%F\n", f); // no bumps
//    f++;

int main() {

    logticker.attach(&logger, 0.004); // 500 Hz, starts getting obvious at 250Hz or 4ms.
//    while(i<500){
//        fprintf(fp, "%F\n", t.read()); // gets bump
//        i++;
//        wait_ms(2);
//    }

Just an update, I got a microSD card adapter from Adafruit (http://www.adafruit.com/products/254) and I am still seeing the same issue. Looking at the SD File System (http://mbed.org/cookbook/SD-Card-File-System), I see that I "read/write data in multiples of the block size (usually 512-bytes)". It seems to me that this corresponds to the 'bumps' I see around every 50 floating point numbers stored. Is there a way to increase this block size???

posted by Michael Chuah 26 Sep 2013

Increasing the block size is effectively the same as storing more in your RAM before writing it to a filesystem. Doing that will mean fewer bumps, but they will be larger. On the LPC1768 you could make it non-blocking by using DMA, but the SD library doesn't support it currently and it isn't really trivial to make. (It is actually on my very long term todo list).

posted by Erik - 26 Sep 2013

1 Answer

10 years, 8 months ago.

It looks like your logger function is taking so long to execute the timer isn't being updated consistently. LocalFileSystem uses semihosting to access data through the interface chip, so it's inherently slow. And as far as I know, ticker functions execute in interrupt context, and will block everything else while they're executing by default. Incrementing a float value is unaffected by this, since it's guaranteed to be incremented before the next write takes place. As a general rule, you should avoid doing any real work in an interrupt handler, since it will block everything else while it's executing (unless a higher priority interrupt comes in, but that's not the default behaviour on mbeds).

Hi Neil, thanks for clarifying the issue. I recently tried using a micro SD card instead of the LocalFileSystem, but still encounter the same issue. It seems that the data is written in a 512 byte block size, which seems to me to correspond to the 'bumps' I see around every 50 data points. Is there a way to increase this block size or some other way to get rid of these 'bumps'??? The intended application is to record encoder values from a DC motor, and so we would like to do it at high frequencies (>200Hz).

posted by Michael Chuah 26 Sep 2013