Read entire MPU6050 buffer

By superwurm , 7 January 2014

So i recently started to learn programming, and I wanted to make a fast datalogger with the mpu6050 and an arduino Uno. So far I've gotten it to about ~1.3 to 6 ms between sensor reads, which then the sd card is written to. But it takes minimum 20 ms. so my thought process was;
mpu6050 has 1024 bytes of ram.
accel/gyro has each axis 2 bytes, so total is 12 bytes, but in the motionappv2 driver, there is a listing saying the default packet structure is 42bytes.
simply looking at the sample rate, at ~12/42 bytes @1khz, it takes ~85/24 ms before it ideally fills, should be enough time to read and write to an sdcard.

So my main issue, is;
1. I do not know how to read the entire MPU6050 buffer in 1 shot.
2. I do not know how to parse the read buffer in a way to write/read it.
3. Is this even possible?

I've attached the code that I have so far, but I really am not sure if I'm approaching this the right way. Any help would be appreciated.
Note: Most of what I used was sourced from the drivers, and examples available online of the mpu6050.


#include
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"


MPU6050 mpu;
int16_t ax, ay, az, gx, gy, gz;
int16_t ax1, ay1, az1, gx1, gy1, gz1;
int16_t s;
char buff[7];

uint32_t m;

bool dmpReady = false; // set true if DMP init was successful
uint8_t mpuIntStatus; // holds actual interrupt status byte from MPU
uint8_t devStatus; // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize; // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount; // count of all bytes currently in FIFO
uint8_t fifoBuffer[48];
uint8_t fifoBuffer1[48];

volatile bool mpuInterrupt = false; // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
mpuInterrupt = true;
}

void setup() {
// put your setup code here, to run once:
Wire.begin();// join I2C bus
TWBR = 12; // this makes i2c bus faster (fast mode?) but there maybe a faster option
// also dont understand how it works.
Serial.begin(115200); //data output rate
while (!Serial);
// set MPU6050 chip parameters
mpu.initialize(); // Starts mpu chip

// verify connection
Serial.println(F("Testing device connections..."));
Serial.println(mpu.testConnection() ? F("MPU6050 connection successful") : F("MPU6050 connection failed"));
delay(1000); // starts sensor poll rather then wait for input.
// // wait for ready
// Serial.println(F("nSend any character to begin DMP programming and demo: "));
// while (Serial.available() && Serial.read()); // empty buffer
// while (!Serial.available()); // wait for data
// while (Serial.available() && Serial.read()); // empty buffer again

// // load and configure the DMP
// Serial.println(F("Initializing DMP..."));
// devStatus = mpu.dmpInitialize();


// // make sure it worked (returns 0 if so)
// if (devStatus == 0) {
// // turn on the DMP, now that it's ready
// Serial.println(F("Enabling DMP..."));
// mpu.setDMPEnabled(true);
//
// // enable Arduino interrupt detection
// Serial.println(F("Enabling interrupt detection (Arduino external interrupt 0)..."));
// attachInterrupt(0, dmpDataReady, RISING);
// mpuIntStatus = mpu.getIntStatus();
//
// // set our DMP Ready flag so the main loop() function knows it's okay to use it
// Serial.println(F("DMP ready! Waiting for first interrupt..."));
// dmpReady = true;
//
// // get expected DMP packet size for later comparison
// packetSize = mpu.dmpGetFIFOPacketSize();
// } else {
// // ERROR!
// // 1 = initial memory load failed
// // 2 = DMP configuration updates failed
// // (if it's going to break, usually the code will be 1)
// Serial.print(F("DMP Initialization failed (code "));
// Serial.print(devStatus);
// Serial.println(F(")"));
// }


mpu.setFIFOEnabled(1); // enables the MPU FIFO buffer
mpu.setFullScaleGyroRange(3); // sets gyro to +/- 2000degrees/sec
mpu.setFullScaleAccelRange(3); // sets accel to +/- 16g
mpu.setAccelFIFOEnabled(1); // enables accel writing to FIFO buffer
mpu.setXGyroFIFOEnabled(1); // enables gyroX writing to FIFO buffer
mpu.setYGyroFIFOEnabled(1); // enables gyroY writing to FIFO buffer
mpu.setZGyroFIFOEnabled(1); // enables gyroZ writing to FIFO buffer
mpu.setIntFIFOBufferOverflowEnabled(1); // enables interrupt from FIFO buffer
mpu.setMasterClockSpeed(9); // this sets I2C master clock speed to 500khz.
mpu.setDLPFMode(7); // sets accel/gyro polling rate (0 is 1Mhz)

}

void loop() {
fifoCount = mpu.getFIFOCount();

//if (mpu.getFIFOCount() >= 196)
//{
//mpu.getFIFOBytes(fifoBuffer, 48);
//
//mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // this pulls sensor dat
////mpu.resetFIFO();
//}
//mpu.getAcceleration(&ax, &ay, &az);// alternate way of gettting raw Accel/Gyro data
//mpu.getRotation(&gx, &gy, &gz);

m = millis();
mpu.getFIFOBytes(fifoBuffer, packetSize);// fifoBuffer is the arduinos buffer
// // I think packetSize is packet size, what if i set it larger?
//mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // this pulls sensor data

//sprintf(buff, "%dl, %d, %d, %d, %d, %dn", ax, ay, az, gx, gy, gz); assigns for easy serial read.


//Serial.print(ax); Serial.print('t');
//Serial.print(ay); Serial.print('t');
//Serial.print(az); Serial.print('t');
//Serial.print(gx); Serial.print('t');
//Serial.print(gy); Serial.print('t');
//Serial.print(gz); Serial.print('t');
//Serial.println(m);

//Serial.print("Buffer 1:"); Serial.print(buff);

}

phpbb Topic ID
15840