Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func tiltCompass (AxR: Float, AyR: Float, AzR: Float, MxR: Float, MyR: Float, MzR: Float) -> (Float, pitch: Float, roll: Float, accelration: Float, magnitude: Float){
- //AxR, AyR, AzR = Raw accelerometer readings in milli G
- //MxR, MyR, MzR = Raw magnotometer readings in milli guass
- let pitch = atan2(AxR, sqrtf(powf(AyR, 2)+powf(AzR, 2))) //p .. asin(-AxR)
- let roll = atan2(AyR, sqrtf(powf(AxR, 2)+powf(AzR, 2))) //γ .. asin(AyR/acos(pitch))
- let acceleration = sqrt(powf(AxR, 2) + powf(AyR,2) + powf(AzR, 2)) //if not equal to 1 then linear or angular Acceleration detectec
- let magnitude = sqrt(powf(MxR, 2) + powf(MyR,2) + powf(MzR, 2)) //if not equal to 1 then mag interefence or pitch/roll present
- //MxC, MyC, MzC = Tilt compensated magnetic sensor measurements
- let MxC = (MxR * acos(pitch)) + (MzR * asin(pitch))
- let MyC = (MxR * asin(roll) * asin(pitch)) + (MyR * acos(roll)) - (MzR * asin(roll) * acos(pitch))
- let MzC = (-MxR * acos(roll) * asin(pitch)) + (MyR * asin(roll)) + (MzR * acos(roll) * acos(pitch))
- //Heading
- var heading : Float = 0
- if MxC > 0 && MyC >= 0 {heading = atan2f(MyC,MxC)}
- if MxC < 0 {heading = 180 + atan2f(MyC,MxC)}
- if MxC > 0 && MyC <= 0 {heading = 360 + atan2f(MyC,MxC)}
- if MxC == 0 && MyC < 0 {heading = 90}
- if MxC == 0 && MyC > 0 {heading = 270}
- return (heading, pitch, roll, acceleration, magnitude)
- }
- print ("Tilt Heading :: (titlheading)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement