为什么SD卡会在没有错误的情况下停止记录?

时间:2017-02-27 19:56:00

标签: arduino

以下草图适用于Arduino Nano克隆。它等待START命令然后从I2C从设备收集数据,组装它以登录SD卡,将其写入卡,将其打印到串行监视器并重复。我已经过测试和重新测试。记录标题和30行数据中的3行后,SD卡日志文件始终停止,但串行监视器显示所有预期的数据。在我的任何测试中都没有生成SD写入错误。

我很感激SD为什么停止记录以及如何修复它的想法。

Arduino Sketch

#include <Wire.h>
#include <Servo.h>
#include <SD.h>
#include <SPI.h>

// Uncomment the #define below to enable internal polling of data.
#define POLLING_ENABLED

//define slave i2c address
#define I2C_SLAVE_ADDRESS 9 

/* ===================================
        Arduino Nano Connections

  ESC (PWM) Signal    - Pin 9 (1000ms min, 2000ms max)
  S.Port Signal - Pin 10

  SPI Connections 
    MOSI = Pin 11
    MISO = Pin 12
    SCLK = PIN 13

   I2C Connections
     SDA = Pin A4
     SCL = Pin A5   

    Start/Stop Switches
    Start = Pin 2 => INT0
    Stop  = Pin 3 => INT1
  ===================================*/  

 Servo esc;    // Servo object for the ESC - PIN 9

 const unsigned long pause = 800; // Number of ms between readings
 const unsigned long testDelay = 30000; // Number of ms between tests

 const int CS_pin = 10; // Pin to use for CS (SS) on your board
 const int Startpin = 2;
 const int Stoppin = 3;
 const int readings = 3; // Number of readings to take at every step
 const int steps = 5; // Number of steps to stop the ESC and take readings
 const byte HALT = 0;
 int ESC = 0;
 int throttle = 0; 
 int increment;
 volatile bool STOP = 0; 
 volatile bool START = 0;
 const String header = "% Thr,Thrust,Curr,Volts,RPM,Cell1,Cell2,Cell3,Cell4,Cell5,Cell6";
 char buffer0[33]; // Buffer for I2C received data
 char buffer1[33]; // Buffer for I2C received data
 String logEntry = "         GOT  NO  DATA                            "; //52 bytes

void setup() {
  Wire.begin(); 
  Serial.begin(115200);
  pinMode(Startpin, INPUT_PULLUP);  
  pinMode(Stoppin, INPUT_PULLUP);  
  // Attach an interrupt to the ISR vector
  attachInterrupt(digitalPinToInterrupt(Startpin), start_ISR, LOW);
  attachInterrupt(digitalPinToInterrupt(Stoppin), stop_ISR, LOW);
  esc.attach(9, 1000, 2000);
  // attaches the ESC on pin 9 to the servo object and sets min and max pulse width
  esc.write(HALT); // Shut down Motor NOW!
  increment = 180 / (steps - 1);
  // Number of degrees to move servo (ESC) per step (servo travel is 0-180 degrees so 180 = 100% throttle)
  delay(500);
  Serial.println("         Thrust Meter I2C Master");
  //Print program name
  //Initialize SD Card
  if (!SD.begin(CS_pin)) {
   Serial.println("Card Failure");
  }
  Serial.println("Card Ready");
  //Write Log File Header to SD Card
  writeSD(header);
  Serial.println(header);
}

void loop() {
  if (START) {
    Serial.println("Start Pressed");
    while (!STOP) {
      for (throttle = 0; throttle <= 180; throttle += increment) {
        for (int x = 0; x < readings; x++) {
          if (STOP) {
            esc.write(HALT); //  Shut down Motor NOW!
            Serial.println("Halting Motor");
          } else {
            wait (pause);
            esc.write(throttle); // increment the ESC
            wait (200);
            ESC = throttle * 100 / 180;
            getData(buffer0);
            wait (100);
            getData(buffer1);
            String logEntry = String(ESC) + "," + String(buffer1) + "," + String(buffer0);
            writeSD(logEntry);
            Serial.println(logEntry);
          }
        }
      }
      for (throttle = 180; throttle >= 0; throttle -= increment) {
        for (int x = 0; x < readings; x++) {
          if (STOP) {
            esc.write(HALT); //  Shut down Motor NOW!
            Serial.println("Halting Motor");
          } else {
            wait (pause);
            esc.write(throttle); // increment the ESC
            wait (200);
            ESC = throttle * 100 / 180;
            getData(buffer0);
            wait (100);
            getData(buffer1);
            String logEntry = String(ESC) + "," + String(buffer1) + "," + String(buffer0);
            writeSD(logEntry);
            Serial.println(logEntry);
          }
        }
      }
      Serial.println("End of Test Pass");
      wait (testDelay);
    }
    esc.write(HALT); //  Shut down Motor NOW!
  }
}

void writeSD(String logdata) {
  File logFile = SD.open("NANO_LOG.csv", FILE_WRITE);
  if (logFile) {
    logFile.println(logdata);
    logFile.close();
  } else {
    Serial.println("Error writing log data");
  }
}

void wait(unsigned long i) {
  unsigned long time = millis() + i;
  while(millis()<time) { }
}

void start_ISR() {
  START = 1;
  STOP = 0;
}

void stop_ISR() {
  STOP = 1;
  START = 0;
}

void getData(char* buff) {
  Wire.requestFrom(9, 32);
  for (byte i = 0; i < 32 && Wire.available(); ++i) {
    buff[i] = Wire.read();
    if (buff[i] == '#') {
      buff[i] = '\0';
      break;
    }
  }
}

这是SD卡内容:

% Thr,Thrust,Curr,Volts,RPM,Cell1,Cell2,Cell3,Cell4,Cell5,Cell6
0,-12,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
0,-12,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
0,128,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00

这是串行监视器的输出:

         Thrust Meter I2C Master
Card Ready
% Thr,Thrust,Curr,Volts,RPM,Cell1,Cell2,Cell3,Cell4,Cell5,Cell6
Start Pressed
0,-12,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
0,-12,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
0,128,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
25,2062,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
25,2520,0.00,15.75,0,3.10,4.20,3.96,3.96,0.00,0.00
25,2710,0.00,15.75,0,3.10,4.20,3.96,3.96,0.00,0.00
50,519,0.00,15.75,0,3.10,4.20,3.96,3.96,0.00,0.00
50,216,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
50,2288,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
75,890,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
75,891,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
75,1386,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
100,2621,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
100,2424,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
100,692,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
100,3409,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
100,227,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
100,3349,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
75,2220,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
75,2249,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
75,509,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
50,1977,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
50,2986,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
50,546,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
25,3746,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
25,3337,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
25,3015,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
0,96,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
0,-12,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
0,-14,0.00,15.76,0,3.10,4.20,3.96,3.96,0.00,0.00
End of Test Pass

1 个答案:

答案 0 :(得分:1)

问题的解决方案是用更快的SD卡替换SD卡。一旦我这样做,数据记录应该。感谢Patrick的建议。