Hi-
Im currently building and programming a quadcopter but im having some trouble with my accel/gyro.
Im using the GY-521. The interface code is taken from the arduino playground Arduino Playground - MPU-6050
I can read the raw values fine. I print them out and they make sense as to how i move the sensor. But the readings are a little noisy and that is where my problem is coming from. I wrote a simple for loop to average 5 readings but they output jibberish. The values make no sense.
void updateImu(){
int sax, say, saz, sgx, sgy, sgz = 0;
for(int x = 0; x<imuSamplesPerLoop; x++){
int error;
double dT;
accel_t_gyro_union accel_t_gyro;
// Read the raw values.
// Read 14 bytes at once,
// containing acceleration, temperature and gyro.
// With the default settings of the MPU-6050,
// there is no filter enabled, and the values
// are not very stable.
error = MPU6050_read (MPU6050_ACCEL_XOUT_H, (uint8_t *) &accel_t_gyro, sizeof(accel_t_gyro));
// Serial.print(F("Read accel, temp and gyro, error = "));
// Serial.println(error,DEC);
// Swap all high and low bytes.
// After this, the registers values are swapped,
// so the structure name like x_accel_l does no
// longer contain the lower byte.
uint8_t swap;
#define SWAP(x,y) swap = x; x = y; y = swap
SWAP (accel_t_gyro.reg.x_accel_h, accel_t_gyro.reg.x_accel_l);
SWAP (accel_t_gyro.reg.y_accel_h, accel_t_gyro.reg.y_accel_l);
SWAP (accel_t_gyro.reg.z_accel_h, accel_t_gyro.reg.z_accel_l);
SWAP (accel_t_gyro.reg.t_h, accel_t_gyro.reg.t_l);
SWAP (accel_t_gyro.reg.x_gyro_h, accel_t_gyro.reg.x_gyro_l);
SWAP (accel_t_gyro.reg.y_gyro_h, accel_t_gyro.reg.y_gyro_l);
SWAP (accel_t_gyro.reg.z_gyro_h, accel_t_gyro.reg.z_gyro_l);
dT = ( (double) accel_t_gyro.value.temperature + 12412.0) / 340.0;
sax += accel_t_gyro.value.x_accel;
say += accel_t_gyro.value.y_accel;
saz += accel_t_gyro.value.z_accel;
sgx += accel_t_gyro.value.x_gyro;
sgy += accel_t_gyro.value.y_gyro;
sgz += accel_t_gyro.value.z_gyro;
}
fax = sax/imuSamplesPerLoop;
fay = say/imuSamplesPerLoop;
faz = saz/imuSamplesPerLoop;
fgx = sgx/imuSamplesPerLoop;
fgy = sgy/imuSamplesPerLoop;
fgz = sgz/imuSamplesPerLoop;
}
imuSamplesPer loop = 5
sax = sum accel X-axis
fax = final accel X-axis
so on and so forth...
If there is some other information needed please ask.
-Nick