title: Combine Gyro and Accelerometer Readings in Rotation-Independent Way?
body: <p>Ok, so I have my Arduino hooked up to an MPU-6050 6-axis gyro+accelerometer GY-521 breakout board. I've got it sending serial data to a program on my computer, all working perfectly. I'll pose this question in C++ even though my program is written in <a href="http://en.wikipedia.org/wiki/DarkBASIC_Professional" rel="nofollow">DBPro</a>.</p>
  
  <p>I'm currently using a complementary filter to combine the gyro rates of angular rotation with the accelerometer gravity vector for stable yet responsive rotations. Here's my current code:</p>
  
  <pre><code>angleX=0.98*(angleX+(gyroX/16.4)*timeMultiplier)+0.02*atanfull(accelY,accelZ)
  angleY=0.98*(angleY+(gyroY/16.4)*timeMultiplier)+0.02*atanfull(accelZ,accelX)
  angleZ=0.98*(angleZ+(gyroZ/16.4)*timeMultiplier)+0.02*atanfull(accelX,accelZ)
  </code></pre>
  
  <p><code>angleX/Y/Z</code> and <code>timeMultiplier</code> are floats. <code>timeMultiplier</code> is basically <code>(millis()-lastMillis)/1000.0</code>. <code>atanfull</code> is basically <code>atan2</code> but outputs in degrees (0-360), not radians (I think that's the only difference).</p>
  
  <p>The issue is that when the board is sitting flat on the table, the accelerometer obviously can't measure rotations about the Y axis (up), but the gyro can. When I twist the board around Y, the 3D cube representation I'm rendering twists for a second then is pulled back to 0. That's because when the board is upright (with the Y axis pointing upwards) the atanfull results in 0.</p>
  
  <p>What I've been scouring the internet for (but really don't know what to search) is a way to reduce the affect of the accelerometer data the less use it becomes. So as the X axis, say, is rotated so it's perpendicular to the ground (and becomes useless for measurements) the gyro is allowed more and more control, until when the X axis becomes fully perpendicular the gyro is taking no notice of the accelerometer for that axis and relies fully on the gyro.</p>
  
  <p><strong>TL;DR</strong>: I'm trying to find a way to combine gyro and accelerometer readings in a rotation-independent way. That is, all the implementations of gyro+accel complementary filters I can find handle pitch/roll differently to yaw, and I don't want to do that. I want to be able to turn my board in any direction and still get good readings.</p>
  
  <p>I really have no idea what I should be searching for to find out how to do this, and would appreciate any help :)</p>