2017-02-27 66 views
1

以下草圖適用於Arduino Nano克隆。它等待一個START命令,然後從一個I2C從設備收集數據,組裝它以便登錄SD卡,將其寫入卡,將其打印到串行監視器並重復。我已經測試並重新測試。記錄標題和30行數據中的3行後,SD卡日誌文件總是停止,但串行監視器顯示所有預期數據。從未在我的任何測試中產生SD寫入錯誤。爲什麼SD卡沒有錯誤地停止記錄?

我很感激任何想法,爲什麼SD停止記錄和如何解決它。

Arduino的草圖

#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 
+0

如果你讓'logFile'不斷打開,它會起作用嗎? –

+0

好主意。剛剛嘗試過。相同的結果。太糟糕了。我真的認爲你可能會有一些東西。感謝這個想法。 –

+0

這是猜測推斷髮生了什麼。這裏還有一些。你如何閱讀'SD'文件來顯示其內容? 'ls -la file_name'說什麼?文件的長度? 'cat -A file_name'顯示什麼?如果每次寫入不同的文件會發生什麼?你是否嘗試用另一個替換SD卡? –

回答

1

的解決問題的辦法是用更快的一個更換SD卡。一旦我這樣做了數據記錄,因爲它應該。感謝帕特里克的建議。