1

I am working on an application which uses compass although I successfully implemented it but there is no such brief explanation that how it works. Like how the TYPE_ACCELEROMETER and TYPE_MAGNETIC_FIELD was used to get the orientation. I know we did get it and the zero index of the array we got is the azimuth from which we get the degree and came to know where north is. But how it get to this all. Can anyone explain please?

Secondly what if I place my mobile on the floor suppose it points in the direction of north when it is placed on ground now I take up my mobile and hold I upside down what will happen and why?

According to my understanding the direction shouldn't changed. I might be wrong, can anyone please guide to me on this. Thanks a lot.

Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47

2 Answers2

1

I'm taking a free online Android course, and this is the example code he uses to make a simple compass, you can look at that: https://github.com/aporter/coursera-android/tree/master/Examples/SensorCompass.

The way is works is that the phone senses the strongest "north" magnetic field, and points the arrow north, and then when the accelerometer moves, it changes the arrow according to that.

Also, if you hold the phone vertically above your head, the display will not be pointing north because the phone is not parallel to the ground.

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
ElectronicGeek
  • 3,312
  • 2
  • 23
  • 35
  • dear thanks for your help but the thing that doubts me is that the direction haven't changed, like north stays the north then why it started pointing in different direction that part really confused me and more i read more i get confused. – Syed Raza Mehdi Mar 11 '14 at 13:38
  • 1
    When you hold the phone vertically, the "north" arrow changes, because it can only display correctly when it is parallel to the group. The magnetic sensor is 3D, but you're trying to show it on a 2D screen. – ElectronicGeek Mar 11 '14 at 13:39
  • thanks for helping me will see into the depth now i am less confused if there is any other links you find about the concepts not the code i will appreciate, stay blessed thanks again. – Syed Raza Mehdi Mar 11 '14 at 13:50
1

You need both the Magnectic field and Accelerometer to calculate the compass direction. You can see why if you read the source code of the getRotationMatrix or for a more verbose explanation see Convert magnetic field X, Y, Z values from device into global reference frame

If you stand at the same place, the magnetic field does not change. That is the magnetic field vector returned in onSensorChanged should change very little. It is not the vector that change but the coordinates of the device that change. It is the meaning of what is being calculate and call the "compass direction" that confuses you. In this case the direction to be used as the "compass direction" is the direction of the y coordinate. Thus when you hold the phone vertically, the y coordinate points to the sky or the ground and it does not make sense to talk about "compass direction"

Community
  • 1
  • 1
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54