-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix azimuth of rotation vector sensors
- Loading branch information
1 parent
0838cee
commit 745715f
Showing
7 changed files
with
123 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
sense/src/main/java/com/kylecorry/andromeda/sense/orientation/BaseRotationSensor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.kylecorry.andromeda.sense.orientation | ||
|
||
import android.content.Context | ||
import android.hardware.SensorEvent | ||
import android.hardware.SensorManager | ||
import com.kylecorry.andromeda.sense.BaseSensor | ||
import com.kylecorry.sol.math.Quaternion | ||
import com.kylecorry.sol.math.QuaternionMath | ||
|
||
abstract class BaseRotationSensor(context: Context, type: Int) : | ||
BaseSensor(context, type, SensorManager.SENSOR_DELAY_FASTEST), | ||
IOrientationSensor { | ||
|
||
private val lock = Object() | ||
|
||
override val hasValidReading: Boolean | ||
get() = _hasReading | ||
|
||
override val orientation: Quaternion | ||
get() = Quaternion.from(rawOrientation) | ||
|
||
override val rawOrientation: FloatArray | ||
get() { | ||
return synchronized(lock) { | ||
_quaternion | ||
} | ||
} | ||
|
||
private val _quaternion = Quaternion.zero.toFloatArray() | ||
|
||
private var _hasReading = false | ||
|
||
override fun handleSensorEvent(event: SensorEvent) { | ||
synchronized(lock) { | ||
onHandleSensorEvent(event) | ||
|
||
// Calculate the quaternion | ||
SensorManager.getQuaternionFromVector(_quaternion, event.values) | ||
val w = _quaternion[0] | ||
val x = _quaternion[1] | ||
val y = _quaternion[2] | ||
val z = _quaternion[3] | ||
_quaternion[0] = x | ||
_quaternion[1] = y | ||
_quaternion[2] = z | ||
_quaternion[3] = w | ||
QuaternionMath.inverse(_quaternion, _quaternion) | ||
} | ||
_hasReading = true | ||
} | ||
|
||
protected open fun onHandleSensorEvent(event: SensorEvent) {} | ||
} |
36 changes: 36 additions & 0 deletions
36
sense/src/main/java/com/kylecorry/andromeda/sense/orientation/BaseWorldRotationSensor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.kylecorry.andromeda.sense.orientation | ||
|
||
import android.content.Context | ||
import android.hardware.SensorEvent | ||
import com.kylecorry.andromeda.sense.compass.ICompass | ||
import com.kylecorry.sol.units.Bearing | ||
|
||
abstract class BaseWorldRotationSensor( | ||
context: Context, | ||
private val useTrueNorth: Boolean, | ||
type: Int | ||
) : | ||
BaseRotationSensor(context, type), ICompass { | ||
|
||
override val bearing: Bearing | ||
get() = Bearing(rawBearing) | ||
|
||
override var declination: Float = 0.0f | ||
|
||
override val rawBearing: Float | ||
get() { | ||
return if (useTrueNorth) { | ||
Bearing.getBearing(Bearing.getBearing(_bearing) + declination) | ||
} else { | ||
Bearing.getBearing(_bearing) | ||
} | ||
} | ||
|
||
private val _orientation = OrientationCalculator() | ||
private var _bearing = 0f | ||
|
||
override fun onHandleSensorEvent(event: SensorEvent) { | ||
super.onHandleSensorEvent(event) | ||
_bearing = _orientation.getAzimuth(event.values) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
sense/src/main/java/com/kylecorry/andromeda/sense/orientation/OrientationCalculator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.kylecorry.andromeda.sense.orientation | ||
|
||
import android.hardware.SensorManager | ||
import com.kylecorry.sol.math.SolMath.toDegrees | ||
|
||
internal class OrientationCalculator { | ||
|
||
private val rotationMatrix = FloatArray(9) | ||
private val remappedRotationMatrix = FloatArray(9) | ||
private val orientation = FloatArray(3) | ||
|
||
fun getAzimuth(rotation: FloatArray): Float = synchronized(this) { | ||
if (rotation.size != 9) { | ||
// It is a rotation vector | ||
SensorManager.getRotationMatrixFromVector(rotationMatrix, rotation) | ||
return getAzimuth(rotationMatrix) | ||
} | ||
|
||
SensorManager.remapCoordinateSystem( | ||
rotation, | ||
SensorManager.AXIS_Y, | ||
SensorManager.AXIS_MINUS_X, | ||
remappedRotationMatrix | ||
) | ||
SensorManager.getOrientation(remappedRotationMatrix, orientation) | ||
return orientation[0].toDegrees() - 90f | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters