views:

322

answers:

3

hi,

has anybody already programmed a iphone compass heading tilt compensation?

i have got some approaches, but some help or a better solution would be cool!

FIRST i define a vector Ev, calculated out of the cross product of Gv and Hv. Gv is a gravity vector i build out of the accelerometer values and Hv is an heading vector built out the magnetometer values. Ev stands perpendicular on Gv and Hv, so it is heading to horizonatl East.

SECOND i define a vector Rv, calculated out of the cross product Bv and Gv. Bv is my looking vector and it is defined as [0,0,-1]. Rv is perpendicular to Gv and Bv and shows always to the right.

THIRD the angle between these two vectors, Ev and Rv, should be my corrected heading. to calculate the angle i build the dot product and thereof the arcos.

phi = arcos ( Ev * Rv / |Ev| * |Rv| )

Theoretically it should work, but maybe i have to normalize the vectors?!

Has anybody got a solution for this?

Thanks, m01d

A: 

I'm facing a similar problem, and i wonder if there's a solution. Have you found one?

Raphael Pinto
A: 

yes, i did it like described above. but the result is not very accurate. i think with smoother accelerometer values it should work that way. because of this i have choosen to do the tilt compensation by adding/subtracting the accelermoter values of the corresponding axis to/from the compass values.

Here iss my code for the solution above, but its not a final working solution:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
if (newHeading != nil) {
float Ax = accelerationValueX;
float Ay = accelerationValueY;
float Az = accelerationValueZ;
float filterFactor = 0.2;
Mx = [newHeading x] * filterFactor + (Mx * (1.0 - filterFactor));
My = [newHeading y] * filterFactor + (My * (1.0 - filterFactor));
Mz = [newHeading z] * filterFactor + (Mz * (1.0 - filterFactor));

float counter = (  -pow(Ax, 2)*Mz + Ax*Az*Mx - pow(Ay, 2)*Mz + Ay*Az*My );
float denominator = ( sqrt( pow((My*Az-Mz*Ay), 2) + pow((Mz*Ax-Mx*Az), 2) + pow((Mx*Ay-My*Ax), 2) ) * sqrt(pow(Ay, 2)+pow(-Ax, 2)) );
headingCorrected = (acos(counter/denominator)* (180.0 / M_PI)) * filterFactor + (headingCorrected * (1.0 - filterFactor));
}
...
}
+1  A: 

Yep. You DEFINITELY have to normalize. This is from my code that I use to extract the orientation of the device. Gravity is obtained as the x,y,z of the accelerometer and compass is obtained from the x,y,z of the heading function

gravity.normalize(); compass.normalize(); compassEast=gravity.cross(compass);
compassEast.normalize();
compassNorth=compassEast.cross(gravity); compassNorth.normalize();

Let me know if you need the full code. Also, for those who havnt yet seen the iphone 4s gyroscope in action: its amazing! I swapped the above input to gravity and compass for the equivalents from the gyro and the result is stable and smooth and awesome :) Go Apple.

twerdster