0

I am using the SensorEvent API in order to get data for my app from different sensors (more specifically: TYPE_ROTATION_VECTOR, TYPE_GRAVITY, TYPE_GYROSCOPE, TYPE_LINEAR_ACCELERATION). Now, I know that in iOS there is the so called CMAttitudeReferenceFrameXTrueNorthZVertical, which gives all the sensors values with respect to True North, whereas z axis will always be vertical.

I couldn't find anything similar in Android, so I am thinking to manually translate the coordinate system. I am also thinking of using the remapCoordinateSystem method. However, I still don't know how to get the data with respect to True North. Did anyone have to deal with something similar before?

approxiblue
  • 6,982
  • 16
  • 51
  • 59
pixie
  • 507
  • 9
  • 21

1 Answers1

0

This answer is inspired by the class used here:

Yout should have the onSensorChanged method in the SensorEventListener

@Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
               //get the rotation matrix from the sensor (this will be using the magnetic north)
                SensorManager.getRotationMatrixFromVector(mRotationMatrix, event.values);
                //change the coordinate system (your new matrix is in the variable mChangedRotationMatrix)
                SensorManager.remapCoordinateSystem(mRotationMatrix, SensorManager.AXIS_Y,
                        SensorManager.AXIS_MINUS_X, mChangedRotationMatrix);
                //get the orientationMatrix of your new coordinate system
                SensorManager.getOrientation(mChangedRotationMatrix, mOrientation);
               //get the magnetic heading 
               float magneticHeading = (float) Math.toDegrees(mOrientation[0]); 
              //adjust accordingly by calculating the true north with either the "computeTrueNorth" method available the above link or the method used in the link below

            }
        }

To get the true north in degrees you may use this answer

Community
  • 1
  • 1