2017-02-27 5 views
1

次のスケッチはArduino Nanoクローンのスケッチです。 STARTコマンドを待ち、I2Cスレーブからデータを収集し、SDカードに記録するために組み立て、カードに書き込み、シリアルモニタに出力して繰り返します。私はテストし、再テストしました。 SDカードのログファイルは、ヘッダと30行のデータのうち3行をロギングした後も常に停止しますが、シリアルモニタにはすべてのデータが表示されます。私のテストで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'はwrtと何を言いますか?ファイルの長さ? 'cat -A file_name'は何を表示しますか?毎回別のファイルに書き込むとどうなりますか? 'sd card'を別のものに置き換えてみましたか? –

答えて

1

問題を解決するには、することでしたSDカードをより速いものに交換してください。一度私はデータを記録しなければなりませんでした。パトリックが提案してくれてありがとう。

関連する問題