2

I'm working on an Android device which does not have a Magnetometer but has a Rotation Sensor (Gyro) and Gravity Sensor and GPS. I would like to determine True North / Azimuth from these sensors but can't figure out how. Without a Magnetometer, how do I determine the orientation of this device? Clearly somehow it knows where North is given the Maps app works just fine.

strangetimes
  • 4,953
  • 1
  • 34
  • 62
  • http://stackoverflow.com/questions/38711705/android-device-orientation-without-geomagnetic – Ircover Oct 11 '16 at 11:10
  • @Ircover Thanks, I've updated my question, I guess it's 'true north' that I'm after, not just the orientation of the device (as that would return 0 for it facing straight up). Essentially I am trying to create a compass with the given constraints – strangetimes Oct 11 '16 at 11:14
  • If the device moves (more than just a few meters), then the GPS can provide you the direction of the movement. Theoretically you could use that to "calibrate" the rotation sensor and then track the device's rotation with it. Of course in practice things get complicated, if the device rotates over several axis. – Markus Kauppinen Oct 11 '16 at 11:49

1 Answers1

0

UPDATE:

You can the Rotation Vector Sensor that seems perfect for your situation.

private SensorManager mSensorManager;
private Sensor mSensor;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

The system of coordinates you want is as follows:

  • X is defined as the vector product Y x Z. It is tangential to the ground at the device's current location and points approximately East.
  • Y is tangential to the ground at the device's current location and points toward the geomagnetic North Pole.
  • Z points toward the sky and is perpendicular to the ground plane.

enter image description here


Android makes a lot of sensors available

if you specifically wish to use the gyroscope (Sensor.TYPE_GYROSCOPE) you could do it like this (taken from the Google example):

private SensorManager mSensorManager;
private Sensor mSensor;
...
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

public void onSensorChanged(SensorEvent event) {
      //...
}

but you could try and check if you can use a higher level API that will leverage more than one sensor

You should have a look at this simple example by JavaCodeGeeks where they leverage the default Sensor.TYPE_ORIENTATION to build an android compass

Just a little more info on the Sensor.TYPE_ORIENTATION. According to Google you can still use device orientation, but by calling getOrientation() instead of acting directly over the sensor.

Instead of using raw data from the orientation sensor, we recommend that you use the getRotationMatrix() method in conjunction with the getOrientation() method to compute orientation values, as shown in the following code sample. As part of this process, you can use the remapCoordinateSystem() method to translate the orientation values to your application's frame of reference.

The bad news is that both getOrientation() and the old Sensor.TYPE_ORIENTATION rely at least partially on the magnetometer

Hope this helps!

HenriqueMS
  • 3,864
  • 2
  • 30
  • 39
  • 1
    thanks for this, I know how to use the gyro sensor - I just don't know how to determine 'true north' from its readings as I need to first get an idea of where North is. TYPE_ORIENTATION sadly is deprecated and so not available for use :( – strangetimes Oct 11 '16 at 11:28
  • I will have another look then ;) – HenriqueMS Oct 11 '16 at 11:29
  • thank you, I'll mark that as the correct answer, although the hardware I'm on doesn't give me any readings for TYPE_ROTATION :( But that sounds like the only other choice I may have. – strangetimes Oct 11 '16 at 13:27
  • Thanks ;) Have you noticed that it is actually TYPE_ROTATION_VECTOR and not TYPE_ROTATION? (just making sure) – HenriqueMS Oct 11 '16 at 14:09
  • Yup thanks for double checking, that's what I meant :) It's a car's dashboard I'm working with and apparently it's not willing to play ball – strangetimes Oct 11 '16 at 16:23
  • But `TYPE_ROTATION_VECTOR` uses the magnetometer though? – lolelo Sep 21 '20 at 07:09