我的一些数据记录比其他记录要长

时间:2018-03-01 18:25:38

标签: logging arduino adafruit

我需要使用Adafruit Feather M0 Adalogger并以每秒100个样本的速度制作8个传感器的日志数据。问题是:某些数据需要更长的时间才能写入txt文件。每个样本之间的差距是10ms(左右),但突然(大约30s,i = 2700,37s,i = 3150,启动后),获取数据需要30-50ms,然后,回来到10ms。它对应用程序没有好处,因为我们正在尝试比较这种数据收集的某些方法,这个问题会妨碍同步。我需要知道为什么会发生这种情况,以及如何解决它。

#include <BlockDriver.h>
#include <FreeStack.h>
#include <MinimumSerial.h>
#include <SdFat.h>
#include <SdFatConfig.h>
#include <SysCall.h>

#include <SPI.h>
//#include <SD.h>

SdFat sd;

const int chipSelect = 4;
const int cardDetect = 7;
const int ledPin = 3;
String fileName = "DATA.TXT";
bool alreadyBegan = false;

int sensorPin[] = {A0,A1,A2,A3,A4,A5,0,A7};
uint8_t sensorVal[] = {0,0,0,0,0,0,0,0};
const int sensorNo = 8;

boolean interrupt = false;
boolean displayed = false;
int i = 0;
unsigned long loopCount = 0;
unsigned long beforeTime = 0;
unsigned long afterTime = 0;
unsigned long t1 = 0;
unsigned long t2 = 0;
unsigned long dif = 0;
int sampleSize = 6000;

File dataFile;

void setup(void)
{
  pinMode(ledPin, OUTPUT);
  pinMode(cardDetect, INPUT_PULLUP);
  for (int k = 0; k < sensorNo; k++) {
    pinMode(sensorPin[k], INPUT);
  }
  Serial.begin(9600);
  while(!Serial){
    delay(1000);
  }
  Serial.println("");Serial.println("INITIALIZING.....");
  iniCard();
  Serial.println("");Serial.println("INITIALIZED!!!");
  Serial.println("");Serial.println("========================");
  Serial.println("");Serial.print("File Name: ");Serial.println(fileName);
  Serial.println("");Serial.println("========================");
  delay(2000);
}

void loop(void)
{
    loopCount++;
    t2 = micros();
    dif = t2-t1;
    if (dif >= 9999){
      if (i==0){
        dataFile = sd.open(fileName, FILE_WRITE);
        beforeTime = micros();
        digitalWrite(ledPin, HIGH);
      }
      else if (i == sampleSize){
        afterTime = micros();
        digitalWrite(ledPin, LOW);
      }
      t1 = t2;
      if (i < sampleSize){
        String dataString = "";
        dataString += micros();
        dataString += ",";
        for (int j = 0; j < sensorNo; j++) {
          sensorVal[j] = analogRead(sensorPin[j]);
          dataString += String(sensorVal[j]);
          if (j < sensorNo-1) {
            dataString += ",";
          }
        }
        Serial.println(dataString);
        dataFile.println(dataString);
        i++;
      }
      else{
        digitalWrite(ledPin, LOW);
        if (!displayed){
          dataFile.close();
          displayed = true;
          Serial.println("");Serial.println("========================");
          Serial.print("Frames: ");Serial.println(i);
          Serial.print("Loops: ");Serial.println(loopCount);
          Serial.print("Start Time: ");Serial.println(beforeTime);
          Serial.print("End Time: ");Serial.println(afterTime);
          float period = (afterTime - beforeTime)/i;
          Serial.print("Period: ");Serial.println(period);
          float freq = 1000000 / period;
          Serial.print("Freq: ");Serial.println(freq);
        }
      }
    }
    else{
    }
}

void erro() {
  digitalWrite(ledPin, LOW);
  delay(250);
  digitalWrite(ledPin, HIGH);
  delay(250);
}

void iniCard() {
  digitalWrite(ledPin, HIGH);

  // see if the card is present and can be initialized:
  while (!digitalRead(cardDetect)) {
    erro();
  }

  // Card seems to exist.  begin() returns failure
  // even if it worked if it's not the first call.
  if (!sd.begin(chipSelect) && !alreadyBegan) { // begin uses half-speed...
    iniCard();  // Possible infinite retry loop is as valid as anything
  }
  else {
    alreadyBegan = true;
  }

  // card initialized:
  delay(2000);
  digitalWrite(ledPin, LOW);
}

0 个答案:

没有答案