title: Combine Gyroscope and Accelerometer Data
body: <p>I am building a balancing robot using the Lego Mindstorm's NXT system. I am using two sensors from HiTechnic, the first being an Accelerometer and the second being a Gyroscope. I've successfully filtered out noise from both sensors and derived angles for both in a range between -90 and 90 degrees, with 0 degrees being perfectly balanced.</p>
  
  <p>My next challenge is to combine both of the sensor values to correct for the Gyroscope's drift over time. Below is an example graph I created from actual data to demonstrate the drift from the gyroscope:</p>
  
  <p><img src="https://i.stack.imgur.com/AHx4r.png" alt="enter image description here"></p>
  
  <p>The most commonly used approach I've seen to make combining these sensors rock solid is by using a Kalman filter. However, I'm not an expert in calculus and I really don't understand mathematical symbols, I do understand math in source code though.</p>
  
  <p>I'm using RobotC (which is like any other C derivative) and would really appreciate if someone can give me examples of how to accomplish this in C.</p>
  
  <p>Thank you for your help!</p>
  
  <p><strong>SOLUTION RESULTS:</strong></p>
  
  <p>Alright, kersny solved my problem by introducing me to complementary filters. This is a graph illustrating my results:</p>
  
  <p><em>Result #1</em></p>
  
  <p><img src="https://i.stack.imgur.com/MqRid.png" alt="enter image description here"></p>
  
  <p><em>Result #2</em></p>
  
  <p><img src="https://i.stack.imgur.com/mePBt.png" alt="enter image description here"></p>
  
  <p>As you can see, the filter corrects for gyroscopic drift and combines both signals into a single smooth signal.</p>
  
  <p><strong>Edit:</strong> Since I was fixing the broken images anyways, I thought it would be fun to show the rig I used to generate this data:</p>
  
  <p><img src="https://i.stack.imgur.com/hWbBh.jpg" alt="enter image description here"></p>