Home > other >  How to convert radians into full 360 degrees format?
How to convert radians into full 360 degrees format?

Time:11-16

I'm trying to draw azimuth line on my map using accelerometer magnetometer values. So i'm using google example. Here is what i get:

private val sensorManager by lazy { context.getSystemService(Context.SENSOR_SERVICE) as SensorManager }

    private val lastAccelerometerReading = FloatArray(3)
    private val latMagnetometerReading = FloatArray(3)

    private val rotationMatrix = FloatArray(9)
    private val orientationAngles = FloatArray(3)

    private var lastAccelerometerSet = false
    private var lastMagnetometerSet = false

    override fun onSensorChanged(event: SensorEvent) {
        if (event.sensor.type == Sensor.TYPE_ACCELEROMETER) {
            System.arraycopy(event.values, 0, lastAccelerometerReading, 0, lastAccelerometerReading.size)
            lastAccelerometerSet = true
        } else if (event.sensor.type == Sensor.TYPE_MAGNETIC_FIELD) {
            System.arraycopy(event.values, 0, latMagnetometerReading, 0, latMagnetometerReading.size)
            lastMagnetometerSet = true
        }
        if (lastAccelerometerSet && lastMagnetometerSet) updateOrientationAngles()



        Timber.d("!!! orientation = ${
            orientationAngles[0] * (180 / Math.PI)
        }")
    }

    override fun onAccuracyChanged(sensor: Sensor, accuracy: Int) = Unit

    fun registerDeviceOrientationListener() {
        sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)?.also { accelerometer ->
            sensorManager.registerListener(
                this,
                accelerometer,
                SensorManager.SENSOR_DELAY_NORMAL,
                SensorManager.SENSOR_DELAY_UI
            )
        }
        sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD)?.also { magneticField ->
            sensorManager.registerListener(
                this,
                magneticField,
                SensorManager.SENSOR_DELAY_NORMAL,
                SensorManager.SENSOR_DELAY_UI
            )
        }
    }

    fun unregisterDeviceOrientationListener() {
        sensorManager.unregisterListener(this)
    }

    private fun updateOrientationAngles() {
        SensorManager.getRotationMatrix(
            rotationMatrix,
            null,
            lastAccelerometerReading,
            latMagnetometerReading
        )
        SensorManager.getOrientation(rotationMatrix, orientationAngles)
    }

It's working fine but i have one problem. When i'm converting orientation into degrees format from radians i'm getting values in (-180; 180) format. But i need it to be (0; 360) format for my map to work correct. So how can I bring it to (0; 360) format? It's 0 on north right now and going to 180 in north-east-south direction and 0 to -180 from north-west-south direction. I need it to be 0 on north, 180 on nort-west-south direction and 180 to 360 from south-east-north direction just like bearing in Google's Fused location

CodePudding user response:

To normalize (-180; 180) to (0; 360):

(degrees   360) % 360

where % is the modulo operator.

CodePudding user response:

The formula is: Degrees = Radians X 180/Pi

  • Related