Averaging IMU Sensor Readings

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

Does sgz work? That is the only one initialized to zero.

I initialized sax say... etc with 0; Now all the values are generally right, but when I get a raw reading of like 17000ish, the average comes out as 4000ish.

-Nick

An int goes to about +/- 32000. Five readings at 17000 is "85,000ish". You may need long ints.

Shoot. I remember running into that problem and i switched the sum numbers to doubles, I guess I must have forgotten to save after I changed it or somthing.

-Nick

i switched the sum numbers to doubles

Why? Are the values being summed floats? If not, an integer type would have been more appropriate.