Arduino 3DOF Head Tracker

void A2Ddata(unsigned int n) {
long tempG = 0, tempX = 0, tempY = 0, tempZ = 0;

for(unsigned int k = 1; k <= n; k++){
tempG += analogRead(gPin);
}
gRaw = tempG*10/n; ????
}

???? what the value you get ? is it radians/sec or deg/sec ? is tempG given you as raw value of the sensor ?
especially i didnt understand the temG*10/n ? what this calc. do?

Uhm... A2Ddata(n) simply averages n readings from the A2D converter. If you take the average of many readings from a noisy sensor, the average will have more resolution than the A2D converter readings, so by using temG*10/n I keep some of the increased resolution. So A2Ddata(8) takes the average of 8 readings. These are just raw A2D readings.

rate = (gRaw*Vin/100-gOffset)*0.0150;

gRaw is the raw gyro value10, Vin/100 is the A2D full scale voltage (322/100) or 3.22V. This voltage is feed to the VRef pin of arduino. gOffset is the voltage from the gyro when it's stationary10, and 0.0150 finally converts everything into degree/s. I'm assuming my gyro has a response of 150dgr/s/V output, it's pretty close to that.

angle += (rateold+rate)*0.010;  //trapz intergration
rateold = rate;

This does a second order trapezoidal integration, the formula is:

y_n = y_n-1 + ( x_n + x_n-1 )/2 *dt

dt/2 in my case is approximately 0.010

Did you use this in your code?

That bit of code does work to reduce drift when the head tracker is stationary, but it's not good when it's moving.

My code is very basic. I'm working on a better drift removal method by using a digital high-pass filter.

Hope this helps,
-Z-