Pages: [1]   Go Down
Author Topic: Logging data at >50Hz  (Read 1270 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!

I am having a bit of trouble lately logging accelerometer data on a microSD (2GB, FAT16).
The reason is, to capture vibrations/impacts/movements, the accelerometer has to run at least at 50Hz in order to obtain a nice resolution.
However, data is being logged at approximately 5-10Hz. I would appreciate any help or ideas on how to log data at higher rates.

Thank you.
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

code? Libraries used?

the SDFAT lib can get in the clock speed area of around 8Mhz, getting data logged at 5-10 Hz sounds like something else is up
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the SDFAT lib can get in the clock speed area of around 8Mhz, getting data logged at 5-10 Hz sounds like something else is up

I'm using SD library, maybe there is something in the code...
Code:
#include <SD.h>
#include <Wire.h>
#include <ADXL345.h>

const int chipSelect = 8;
ADXL345 accel;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  accel = ADXL345();
  pinMode(10, OUTPUT);  

  // Set the range of the accelerometer.
  accel.SetRange(16, true);
  accel.EnableMeasurements();
   if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("Card initialized.");
  //Write Log File Header
  File logFile = SD.open("ADXL345.txt", FILE_WRITE);
  if (logFile)
  {
    String header = "ADXL345 Initialized";
    logFile.println(header);
    logFile.close();
    Serial.println(header);
  }
  else
  {
    Serial.println("Couldn't open log file");
  }
  
}

void loop()
{
  if(accel.IsConnected) // If we are connected to the accelerometer.
  {
//    AccelerometerRaw raw = accel.ReadRawAxis();
       AccelerometerScaled scaled = accel.ReadScaledAxis();

 File dataFile = SD.open("ADXL345.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print(scaled.XAxis);
    dataFile.print(",");
    dataFile.print(scaled.YAxis);
    dataFile.print(",");
    dataFile.println(scaled.ZAxis);

    dataFile.close();
    // print to the serial port too:
    Serial.print(scaled.XAxis);
    Serial.print(",");
    Serial.print(scaled.YAxis);
    Serial.print(",");
    Serial.println(scaled.ZAxis);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("Error opening file");
       }
  }
  delay(10);
}
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 503
Posts: 19086
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.begin(9600);
...
    // print to the serial port too:
    Serial.print(scaled.XAxis);
    Serial.print(",");
    Serial.print(scaled.YAxis);
    Serial.print(",");
    Serial.println(scaled.ZAxis);

Say you are printing 10 characters, eg. 

Code:
12,34,56\r\n

At 9600 baud that will take 0.0104 seconds so you can only do 96 a second.

Comment out the serial prints, or bump up your baud rate to 115200.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  Serial.begin(9600);
...
    // print to the serial port too:
    Serial.print(scaled.XAxis);
    Serial.print(",");
    Serial.print(scaled.YAxis);
    Serial.print(",");
    Serial.println(scaled.ZAxis);

Say you are printing 10 characters, eg. 

Code:
12,34,56\r\n

At 9600 baud that will take 0.0104 seconds so you can only do 96 a second.

Comment out the serial prints, or bump up your baud rate to 115200.

I tried doing that but it didn't work. Actually just a few moments ago I realized that it works better if I send a single string; therefore I have to send the data using a buffer. I also just tested this method and it gave much much better results (almost 100Hz).

The only downside is that my raw accelerometer data is setup as floating point numbers, which cannot be send as strings (being read as "?").
I am trying to convert float to char at the moment, and hopefully that should solve the problem.

Thanks for your tip!
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 503
Posts: 19086
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
it gave much much better results (almost 100Hz)
96 Hz?

Do what you did, and get rid of the serial prints.
Logged


Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

Its a while since I used the SD Library, but do you need to open the file everytime, can't you open it in setup and then just use it in loop ?

i.e move this to setup -

 File dataFile = SD.open("ADXL345.txt", FILE_WRITE);

Duane B

rcarduino.blogspot.com
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 25
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How would you close the file before stopping/resetting the Arduino.

Brown out feature maybe ...

-Enjoy
fh  : )_~
Logged

0
Offline Offline
Edison Member
*
Karma: 67
Posts: 1658
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

See this topic http://arduino.cc/forum/index.php/topic,107429.msg807043.html#msg807043.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I solved the problem today, here are some factors which helped in speeding up the data logging process:

1. Wrote a single string at a time to SD card (most important)
2. Removed all "Serial.print" which were used for debugging
3. Removed any unnecessary variables and code (lowered sketch size)

Now I am logging data on the SD card at about ~10ms (~100Hz) which provides really nice resolution.

I also tried moving the file open command in the setup(), but that does not help as the file is being appended and has to be opened and closed within the loop.

Thank you everyone for your suggestions!
Logged

Pages: [1]   Go Up
Jump to: