I would like to use FIFO mode for the LSM303 accelerometer. The idea is to collect data at a high data rate and read multiple samples back in a polled loop. I’ll want to do the same trick with the LG30 gyroscope.
I am using a baby Orangutan for the controller, and can read/write from the device one sample at a time.
Does anyone know the magic spell for this ?
#include <Wire.h>
#include <L3G.h>
#include <LSM303.h>
#include <Servo.h>
L3G gyro;
LSM303 compass;
void setup() {
Serial.begin (115200);
Wire.begin();
//compass.writeAccReg(LSM303::CTRL_REG5_A, 0xc0); // Reboot.
compass.init(); // Figure out which device.
compass.enableDefault();
compass.writeAccReg(LSM303::CTRL_REG1_A, 0x97 ); // Enable X,y,Z registers. (1.3 khz, x,z channels enabled)
compass.writeAccReg(LSM303::CTRL_REG4_A, 0x88); // 2G full scale, High resolution mode.
compass.writeAccReg(LSM303::CTRL_REG5_A, 0x40);
compass.writeAccReg(LSM303::FIFO_CTRL_REG_A,0x48);// Fifo mode. No triggers. 32 entries.
// compass.writeAccReg(LSM303::REFERENCE_A,0x5f);
Serial.println ("Setup Done");
}
float getCompassAngle () {
int statusA = compass.readAccReg(LSM303::STATUS_A);
int refA = compass.readAccReg(LSM303::FIFO_SRC_REG_A); // FSS --- bottom 5 bits.
int count = refA & 0x1f;
Serial.print ("STatus A : "); Serial.print ( statusA); Serial.print (" RefA " ) ; Serial.print(refA);
Serial.print (" Count : " ) ; Serial.println ( count );
long x=0 , y=0 , z =0; // Accumulators for X, Z values.
for ( byte i = 0 ; i < count ; i ++ ) {
//Serial.println ( "Start Read");
compass.readAcc( );
//Serial.println("Done Read");
if ( compass.timeoutOccurred() ) {
Serial.println("Timeout on Compass Set");
}
x += compass.a.x ;
y += compass.a.y ;
z += compass.a.z;
}
float xx = (float)(x >> 4 );
float yy = (float)(y >> 4 );
float zz = (float)(z >> 4 );
float angle = 180.0/M_PI*asin(-xx/sqrt(xx*xx+yy*yy+zz*zz));
return angle;
}
void loop () {
// Get the number of FIFO entries to read.
static unsigned long lastTick = 0;
unsigned long thisTick = millis () ;
if ( thisTick >= lastTick + 10 ) {
lastTick = thisTick;
int dt = lastTick - thisTick;
float angle = getCompassAngle ();
static int ctr = 0;
if ( ctr ++ >= 100 ){
ctr = 0;
// Serial.print ("One Second " );
// Serial.println ( angle);
}
}
}
Example output form the code :
STatus A : 255 RefA 32 Count : 0
STatus A : 255 RefA 32 Count : 0
STatus A : 255 RefA 32 Count : 0
Thanks !