0

I recently tried out the Compass inside Androids 4.0 API-Demos. It works as long as you hold your phone in portrait-mode, but as soon as you put it into landscape, the "North" is pointing in another direction than before. I thought that it was clearly stated here that

The coordinate-system is defined relative to the screen of the phone in its default orientation. The axes are not swapped when the device's screen orientation changes.

But thats exactly what happens!

Why the Compass is pointing to a wrong direction? Is the above statement untrue, or is it just a bug inside the computation of the data by the API-samples?

Rafael T
  • 15,401
  • 15
  • 83
  • 144

1 Answers1

3

the compass demo is indeed incomplete. You need to take the device orientation into account when drawing a compass on the screen. Have a look at this question for more details.

This is also a nice discussion of the subject.

I also have a working example on my github you can use. (its too much code to paste in here) The relevant classes are CompassSensorListener.java and CompassHelper.java.

Cheers

Community
  • 1
  • 1
Renard
  • 6,909
  • 2
  • 27
  • 23
  • Hey, I checked out your project on github. So while debugging it I see some mysteries, which I hope you can enlight me. You have a `onDirectionChanged(bearing)` which has values from 1.XX to 3.XX??? You correct these values using `Location.getDeclination()` inside your RadaView. All I wanna have is the `azimuth` which was provided by the Orientation_Sensor before we have to deal with RotationMatrices. See http://stackoverflow.com/questions/10026575/how-to-handle-sensoreventvalues-to-get-correct-azimuth for clearification. feel free to send me a mail to raffn1 AT gmail DOT com – Rafael T Apr 05 '12 at 12:12
  • 1
    Yes onDirectionChanged gives you the azimuth. simply do Math.toDegrees(mAzimuth) to get degrees (0-359). Declination is a very small value which i use to get the real north as opposed to the magnetic north. You can safely ignore that part of the code. – Renard Apr 05 '12 at 12:27
  • This is not actually true. I have logged `onDirectionChanged` with `Math.toDegrees(bearing)` and your `rotateAngle` inside `RadarView.draw()` with http://pastebin.com/NYrTXwdR results. So I'm assuming that I have to add 180??? Where the hell is that documentated? Also How to get correct `roll` and `pitch` values is totally unclear – Rafael T Apr 05 '12 at 12:40
  • 1
    sorry. you are correct. iam also adding 180 later when drawing the compass. – Renard Apr 05 '12 at 13:03
  • Really No Problem Sir. I just wanna know, where this is documented, and how to calculate `pitch` and `roll` correctly – Rafael T Apr 05 '12 at 13:12
  • Since i wrote this code as a hobby project for myself you wont find much documenation :-) Actually if you are just interested in a correct azimuth. Only use CompassHelper.CalculateHeading() in your onSensorChanged method. It takes the float arrays you get from the android sensors plus WindowManager.getDefaultDisplay().getOrientation(). I found another [example](http://kurser.iha.dk/eit/jem1/Basic/Android/Professional%20Android%202%20Code%20Example%20Projects/Chapter%2014%20Compass/) which also uses pitch and roll. – Renard Apr 05 '12 at 13:33
  • I meant where is that documented in Android? – Rafael T Apr 05 '12 at 14:19
  • I added an extra link to the original answer with more infos. Official docs are [here](http://developer.android.com/reference/android/hardware/SensorManager.html) . Look for getOrientation(). But honestly. if found that to complicated to use.. – Renard Apr 05 '12 at 14:41