Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. func tiltCompass (AxR: Float, AyR: Float, AzR: Float, MxR: Float, MyR: Float, MzR: Float) -> (Float, pitch: Float, roll: Float, accelration: Float, magnitude: Float){
  2.  
  3. //AxR, AyR, AzR = Raw accelerometer readings in milli G
  4. //MxR, MyR, MzR = Raw magnotometer readings in milli guass
  5.  
  6. let pitch = atan2(AxR, sqrtf(powf(AyR, 2)+powf(AzR, 2))) //p .. asin(-AxR)
  7. let roll = atan2(AyR, sqrtf(powf(AxR, 2)+powf(AzR, 2))) //γ .. asin(AyR/acos(pitch))
  8.  
  9. let acceleration = sqrt(powf(AxR, 2) + powf(AyR,2) + powf(AzR, 2)) //if not equal to 1 then linear or angular Acceleration detectec
  10. let magnitude = sqrt(powf(MxR, 2) + powf(MyR,2) + powf(MzR, 2)) //if not equal to 1 then mag interefence or pitch/roll present
  11.  
  12. //MxC, MyC, MzC = Tilt compensated magnetic sensor measurements
  13. let MxC = (MxR * acos(pitch)) + (MzR * asin(pitch))
  14. let MyC = (MxR * asin(roll) * asin(pitch)) + (MyR * acos(roll)) - (MzR * asin(roll) * acos(pitch))
  15. let MzC = (-MxR * acos(roll) * asin(pitch)) + (MyR * asin(roll)) + (MzR * acos(roll) * acos(pitch))
  16.  
  17. //Heading
  18. var heading : Float = 0
  19. if MxC > 0 && MyC >= 0 {heading = atan2f(MyC,MxC)}
  20. if MxC < 0 {heading = 180 + atan2f(MyC,MxC)}
  21. if MxC > 0 && MyC <= 0 {heading = 360 + atan2f(MyC,MxC)}
  22. if MxC == 0 && MyC < 0 {heading = 90}
  23. if MxC == 0 && MyC > 0 {heading = 270}
  24.  
  25. return (heading, pitch, roll, acceleration, magnitude)
  26. }
  27.  
  28. print ("Tilt Heading :: (titlheading)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement