Accelerometer inverts when flipped upside down.

I’m having some trouble with orientation calculation, it’s easiest explained by looking at the video below. I’m using a gy-80, so the accelerometer is an L3G4200D and gyro is a ADXL345. It seems to track movements pretty well, until it’s tilted over + or - 90 degrees, at which point the opposite direction flips 180 degrees (so if Pitch is over 90 then roll flips 180).

The trouble seems to be the accelerometer values, is it possible? I’m calculating pitch and roll as follows, as from Lazurus’s guide - also using his kalman filter:

  accPitch = (atan2(accValX,accValZ)+PI)*RAD_TO_DEG;
  accRoll = (atan2(accValY,accValZ)+PI)*RAD_TO_DEG;

  if (accPitch <= 360 & accPitch >= 180){
    accPitch = accPitch - 360;
  }

  if (accRoll <= 360 & accRoll >= 180){
    accRoll = accRoll - 360;
  }

Thanks

I'm having the same problem and I cant find a solution to this.

The left column are my roll values, the left column are my pitch values. They are the result of this calculation: roll = (atan2(ay, az)+PI)*RAD_TO_DEG; pitch = (atan2(ax, az)+PI)*RAD_TO_DEG;

(ax, ay, az are accelerometer measurements)

I rotated my device around the X-axis (should only change my roll value). I did not rotate around the y-axis, so my pitch should theoretically stay at ca. 180 degrees all the time… but it doesn't! Red is the part where it gets strange….

110.037712 177.94928; 108.725166 177.268387; 107.690384 178.456924; 105.609833 179.55899; 103.349213 181.768768; 102.630997 178.129913; 100.206619 180.742752; 97.836052 181.004349; 95.612274 180.824249; 94.362167 181.906158; 92.943687 180.99469; 91.328262 165.373825; 89.467445 57.259464; 88.114891 22.104139; 86.327652 15.940088; 85.284119 15.84662; 84.20356 12.40211; 82.488235 13.507222; 81.106842 7.860759; 80.041031 5.403023; 78.477669 7.589334; 77.705246 7.589335; 75.788643 7.558187; 75.097031 6.980302; 73.839981 5.246093; 73.038017 4.990366;

Can anybody help me?

You may be experiencing "gimbal lock" which happens in any angular coordinate system. If that term is unfamiliar, here is a clear overview of orientation systems and explanation for how to avoid gimbal lock. http://www.chrobotics.com/library Edit: start here http://www.chrobotics.com/library/understanding-euler-angles