iphone compass tilt compensation
Asked Answered
S

3

6

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

Sezen answered 27/3, 2010 at 15:46 Comment(0)
G
3

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.

Glucinum answered 18/9, 2010 at 0:52 Comment(2)
Is it possible to see source code ? It made me curious. Could you send source code to [email protected] please ?Hosea
I am also interested in the code. My email: ellen.sundh[at]gmail.com Thanks!Philoctetes
P
2

I didn't receive the source code but I set up my own example. You can see the project and code here: http://www.sundh.com/blog/2011/09/stabalize-compass-of-iphone-with-gyroscope/

Philoctetes answered 19/9, 2011 at 15:28 Comment(0)
S
0

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));
}
...
}
Sezen answered 19/5, 2010 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.