Buenos dias muchachos. Estoy trabajando en un projecto el cual quisiera grabar los datos a una carta SD a alta velocidad (Sample Rate: 200Hz). Los datos provienen de un MPU9150 y un GPS.Estoy utilizando un Arudino Mega o un Teensy 3.2. Intente con la libreria <SD.h> y es muy lenta al grabar los datos en formato csv.
Viendo el foro me encontre que fatLib publico una nueva libreria fatSD.h en el cual se guardan los datos a una velocidad asombrosa(libreria aqui). Es un poco complicada para mi nivel de programacion ya que soy Nuevo. Alguien sabe como puedo grabar estos datos en la SD con esta velocidad? He leido que se puede creando bloques de 512 bytesy luego transferirlos de tipo binario (bin) pero no me queda muy claro.
Alguien me puede dar alguna idea o sugerencia para este proyecto?
Muchas gracias por su ayuda!
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
#include <Adafruit_GPS.h>
#include <SD.h>
#include <SoftwareSerial.h>
#define LED_PIN 13 //Blink state writing
#define TIMER 500 //1000ms = 1sec
/* Accelerometer */
MPU6050 accelgyro; //MPU9250 device
int16_t ax, ay, az;
int16_t gx, gy, gz;
int16_t mx, my, mz;
/* Micro-SD */
File dataLogger;
const int CS_mega = 53; //CS for Mega2560
/* Blink state */
bool blinkState = false;
/* GPS */
SoftwareSerial mySerial(10, 2); //GPS software serial TX:D10 RX:D2
Adafruit_GPS GPS(&mySerial); //Varialbe for GPS
#define GPSECHO false //false : turn off echoing the GPS data. True : Debug and listen the raw GPS sentences
boolean usingInterrupt = false; //Keeps track if we're using the interrupt (off by defult)
void useInterrupt(boolean); // Func prototype for interrupt
uint32_t timer = millis();
/************************************************************/
/* SETUP */
/************************************************************/
void setup() {
Wire.begin(); //Join I2C bus
Serial.begin(115200); //Serial communication
// Initialize MPU-9150
Serial.println(F("Initializing MPU-9150..."));
accelgyro.initialize(); //Initialize MPU9250
Serial.println(F("Testing MPU device connections...")); //Verify connection
Serial.println(accelgyro.testConnection() ? "MPU9150 connection successful" : "MPU9150 connection failed");
// Initialize MPU-9150
Serial.println(F("Initializing GPS"));
GPS.begin(9600); //MTK GPS baud rate
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); //Initialize RMC and fix data for MTK GPS
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //Set up date to 1 Hz (recommended)
GPS.sendCommand(PGCMD_ANTENNA); //Request updates on antena status
// Initialize Micro-SD
Serial.println(F("Initializing SD card..."));
pinMode(CS_mega, OUTPUT);
if (!SD.begin(CS_mega)) {
Serial.println(F("initialization SD failed! Insert SD and restart..."));
return;
}
Serial.println("initialization Micro-SD done.");
// Frist row title in Micro-SD
dataLogger = SD.open("data1.csv", FILE_WRITE);
dataLogger.println("Date,Hour,Satellites,Longitude,Latitude,Altitude,Angle,Speed,aX,aY,aZ,gX,gY,gZ,mX,mY,mZ");
dataLogger.close();
pinMode(LED_PIN, OUTPUT); //Blink state LED
useInterrupt(true); //Interrup go off every 1ms and reads data (to avoid delay fnc)
}
// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
char c = GPS.read();
// if you want to debug, this is a good time to do it!
#ifdef UDR0
if (GPSECHO)
if (c) UDR0 = c;
// writing direct to UDR0 is much much faster than Serial.print
// but only one character can be written at a time.
#endif
}
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
usingInterrupt = true;
} else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
usingInterrupt = false;
}
}
/************************************************************/
/* LOOP */
/************************************************************/
void loop() {
// in case you are not using the interrupt above, you'll
// need to 'hand query' the GPS, not suggested :(
if (! usingInterrupt) {
// read data from the GPS in the 'main loop'
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if (GPSECHO)
if (c) Serial.print(c);
}
// if a sentence is received, check the checksum and parse it
if (GPS.newNMEAreceived()) {
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// if millis() or timer wraps around, we'll just reset it
if (timer > millis()) timer = millis();
if (millis() - timer > TIMER) {
timer = millis(); // reset the timer
dataLogger = SD.open("data1.csv", FILE_WRITE);
gpsData (dataLogger);
getMPU(dataLogger);
blinkState = !blinkState; // blink LED to indicate activity
digitalWrite(LED_PIN, blinkState);
}
dataLogger.close();
}
void getMPU(File data)
{
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);
accelgyro.getMotion9(&ax, &ay, &az, &gx, &gy, &gz, &mx, &my, &mz); //read raw accel/gyro measurements from device
if (data) {
int sensAx = (analogRead(A0));
float voltageAx = (sensAx*(4.0/1023.0));
//data.print((map(vax,0,1023,0,4)/100.000)) ; data.print(","); // aX
data.print(voltageAx,2);
data.print((analogRead(A1))) ; data.print(","); // aY
data.print((analogRead(A2))) ; data.print(","); // aZ
data.print(gx); data.print(",");
data.print(gx); data.print(",");
data.print(gx); data.print(",");
data.print(mx); data.print(",");
data.print(mx); data.print(",");
data.print(mx); data.println(",");
Serial.println(F("Writing Data..."));
}
else {
// if the file didn't open, print an error:
Serial.println(F("error opening file on SD"));
}
}
void gpsData (File data)
{
int sat = GPS.satellites;
data.print(GPS.day, DEC); data.print("-");
data.print(GPS.month, DEC); data.print("/");
data.print(GPS.year, DEC); data.print(",");
data.print(GPS.hour, DEC); data.print(":");
data.print(GPS.minute, DEC); data.print(":");
data.print(GPS.seconds, DEC); data.print(",");
data.print(sat); data.print(",");
data.print(GPS.longitudeDegrees, 4); data.print(",");
data.print(GPS.latitudeDegrees, 4); data.print(",");
data.print(GPS.altitude); data.print(",");
data.print(GPS.angle); data.print(",");
data.print(GPS.speed); data.print(",");
}