The next task was to take the noisy raw acceleration and drifting angular rate outputs, and combine them into a noiseless non-drifting signal, ideally with no phase shift. The first approach was to implement a kalman filter, a complex cycle eating process. However despite hours of tuning, I struggled to fully eliminate the accelerometer noise whilst still keeping the phase shift to a minimum. This may just be down to me misunderstanding the filters operation, but given how much cpu time it demanded a simpler solution was required. After a brief moment in thought I came up with the following equation: ANGLE=(PREVIOUS_ANGLE+GYRO_RATE*(dt*57.295))*0.99 + ACCEL_ANGLE*0.01. It simply functions by giving the filters a 1:99 weighting, which appears to be just enough for the accelerometer to cancel out the gyroscopes drift:
The system had been allowed to run for a few minutes before this screenshot to allow the gyro drift to build up (red). The blue trace shows the noisy accelerometer reading, and the green is the filtered value.
I have no idea if this type of filter has been used before, but to me it very elegantly utilizes the gyroscopes accuracy and the accelerometers lack of drift, without introducing any negative effects.