I have implemented a data logger to test several new features of SdFat and a small RTOS.
This logger produces a CSV file and performs very well for a logger that formats text. I have logged two analog pins at 1000 Hz on a Uno and 16 pins at 100 Hz on a Mega.
It is designed to log any number of analog pins starting with pin zero. This could be modified easily.
It works best with a quality SD card but will provide good results with lower quality cards.
Several features of SdFat and the RTOS enhance performance.
Two threads and a large buffer are used to overcome the occasional large write latency of SD cards.
A special version of analogRead() is used that sleeps during the ADC conversion. This saves CPU time for the SD write thread.
A new SdFat function, printField(), is used to format data. This function is several times faster than the standard Print class.
The logger is the nilSdLogger example in NilRTOS.
NilRTOS20130214.zip Google Code Archive - Long-term storage for Google Code Project Hosting..
Three parameters configure the program. Here is an example to log the 16 Mega analog pins at 100 Hz.
// Time between points in microseconds.
// Maximum value is 4,194,304 (2^22) usec.
const uint32_t PERIOD_USEC = 10000;
// Number of ADC channels to log
const uint8_t NADC = 16;
// FIFO buffer size. Adjust so unused idle thread stack is about 100 bytes.
const size_t FIFO_SIZE_BYTES = 4000;
Here is the beginning of the data file:
PERIOD_USEC,10000
ADC0,ADC1,ADC2,ADC3,ADC4,ADC5,ADC6,ADC7,ADC8,ADC9,ADC10,ADC11,ADC12,ADC13,ADC14,ADC15,Overruns
528,466,429,402,388,372,362,352,370,356,361,350,339,346,341,342,0
407,414,409,398,389,376,365,352,352,347,349,344,335,337,334,337,0
365,380,387,386,383,376,368,354,345,342,343,340,333,333,330,332,0
345,355,364,368,371,369,366,352,343,341,342,340,335,335,333,335,0
329,333,339,343,347,348,348,338,328,326,326,325,321,321,318,322,0
329,330,333,337,341,343,344,335,328,328,328,327,324,324,321,321,0
315,315,316,318,321,324,325,320,315,315,315,315,313,313,313,313,0
311,310,311,311,313,315,316,311,307,307,307,307,305,305,304,303,0
305,304,305,306,307,308,310,305,303,304,305,305,304,305,304,302,0
292,291,291,291,292,292,293,290,288,288,288,288,287,288,288,286,0
Statistics are printed at the end of a run. Here are stats for the Mega.
Max Write Latency: 68780 usec
Unused Stack: 49 3207
FIFO record count: 125
Minimum free count: 117
A very good SD card was used so the max write latency was about 69 ms.
The data read thread had 49 bytes of unused stack.
The SD write thread had 3207 unused bytes so more buffering could have been used. Adjust FIFO_SIZE_BYTES so the write thread has about 100 unused bytes for best buffer performance.
125 buffers were allocated with space for 16 ADC value in each buffer. The total buffer space was 4000 = 125*32 bytes.
The minimum free count was 117 buffers so there was never danger of a data overrun.