diff --git a/server/core/src/main/java/dev/slimevr/tracker/behaviours/calibration-refresh.kt b/server/core/src/main/java/dev/slimevr/tracker/behaviours/calibration-refresh.kt index 845e95de4..6da07e3b1 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/behaviours/calibration-refresh.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/behaviours/calibration-refresh.kt @@ -17,7 +17,7 @@ class TrackerCalibrationRefreshBehaviour : TrackerBehaviour { } .onEach { // Make sure to send the raw data to have calibration re-apply - receiver.context.dispatch(TrackerActions.SetRotation(it.rawRotation, it.rawAcceleration, it.rawMagnetometer, newData = false)) + receiver.context.dispatch(TrackerActions.SetRotation(it.rawRotation, it.rawAcceleration, it.rawMagnetometer, refresh = true)) }.launchIn(receiver.context.scope) } } diff --git a/server/core/src/main/java/dev/slimevr/tracker/module.kt b/server/core/src/main/java/dev/slimevr/tracker/module.kt index 28a49f360..ca609711e 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/module.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/module.kt @@ -68,6 +68,7 @@ data class TrackerState( val lastMountingMethod: MountingMethod, val mountingOrientation: HeadingAlignment, val restOrientation: RestOrientation, + val lastReference: Quaternion, val sessionCalibration: SessionCalibration, val rawRotation: RawRotation, val rotation: CalibratedRotation, @@ -95,7 +96,7 @@ sealed interface TrackerActions { data class SetMagStatus(val status: MagnetometerStatus) : TrackerActions data class SetStatus(val status: TrackerStatus) : TrackerActions data class SetDriverName(val driverName: String?) : TrackerActions - data class SetRotation(val rotation: Quaternion? = null, val acceleration: Vector3? = null, val magnetometer: Vector3? = null, val position: Vector3? = null, val newData: Boolean = true) : TrackerActions + data class SetRotation(val rotation: Quaternion? = null, val acceleration: Vector3? = null, val magnetometer: Vector3? = null, val position: Vector3? = null, val refresh: Boolean = false) : TrackerActions data class SetMountingOrientation(val mountingOrientation: HeadingAlignment) : TrackerActions data class SetRestOrientation(val restOrientation: Quaternion) : TrackerActions data class FullReset(val referenceRotation: Quaternion, val resetPositionalHeadAttitude: Boolean = false) : TrackerActions @@ -193,6 +194,7 @@ class Tracker( lastMountingMethod = MountingMethod.MANUAL, mountingOrientation = Quaternion.IDENTITY, restOrientation = Quaternion.IDENTITY, + lastReference = Quaternion.IDENTITY, sessionCalibration = SessionCalibration(), rawRotation = Quaternion.IDENTITY, rotation = Quaternion.IDENTITY, diff --git a/server/core/src/main/java/dev/slimevr/tracker/reducer.kt b/server/core/src/main/java/dev/slimevr/tracker/reducer.kt index 3a069b334..4449a2f0a 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/reducer.kt @@ -18,7 +18,7 @@ fun reduce( is TrackerActions.SetDriverName -> state.copy(driverName = action.driverName) is TrackerActions.SetRotation -> { - val accumulatedTicks = if (action.newData && action.rotation != null) (state.accumulatedTicks + 1u).toUShort() else state.accumulatedTicks + val accumulatedTicks = if (!action.refresh && action.rotation != null) (state.accumulatedTicks + 1u).toUShort() else state.accumulatedTicks // Rotation val rawRotation: RawRotation = action.rotation ?: state.rawRotation @@ -28,6 +28,12 @@ fun reduce( } else { rawRotation } + val polarityAlign = if (action.refresh) { + // Reset polarity according to last reference rotation. + state.lastReference + } else { + state.rotation + } // Other inputs val rawAcceleration: RawAcceleration = action.acceleration ?: state.rawAcceleration @@ -40,7 +46,7 @@ fun reduce( val rotation: CalibratedRotation = if (action.rotation != null) { applyCalibration(correctedRawRotation, cal.headingCorrection, cal.attitudeAlignment, cal.headingAlignment, state.restOrientation) - .twinNearest(state.rotation) + .twinNearest(polarityAlign) } else { state.rotation } @@ -117,9 +123,9 @@ fun reduce( attitudeAlignment = attitudeAlignment, headingAlignment = headingAlignment, ), + lastReference = action.referenceRotation, // Full reset snaps: cancel any in-progress yaw smoothing. yawResetSmoothing = null, - rotation = state.rotation.twinNearest(action.referenceRotation), ) } @@ -136,24 +142,23 @@ fun reduce( // TrackerYawResetSmoothingBehaviour eases sessionCalibration.headingCorrection // to newHeading over smoothTime. A reset mid-ease just replaces the seed. state.copy( + lastReference = action.referenceRotation, yawResetSmoothing = YawResetSmoothing( from = cal.headingCorrection, to = newHeading, duration = action.smoothTime, ), - rotation = state.rotation.twinNearest(state.rotation), ) } else { // Snap: apply the new heading immediately (default, no smoothing configured). state.copy( sessionCalibration = cal.copy(headingCorrection = newHeading), + lastReference = action.referenceRotation, yawResetSmoothing = null, - rotation = state.rotation.twinNearest(action.referenceRotation), ) } } - // TODO: fix centaur on PoseMountingReset is TrackerActions.PoseMountingReset -> { val cal = state.sessionCalibration @@ -169,8 +174,8 @@ fun reduce( state.copy( sessionCalibration = state.sessionCalibration.copy(headingAlignment = headingAlignment), + lastReference = action.referenceRotation, lastMountingMethod = MountingMethod.POSE, - rotation = state.rotation.twinNearest(action.referenceRotation), ) } diff --git a/server/core/src/main/java/dev/slimevr/tracker/session-calibration.kt b/server/core/src/main/java/dev/slimevr/tracker/session-calibration.kt index 084e84f7d..fca80216d 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/session-calibration.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/session-calibration.kt @@ -1,6 +1,5 @@ package dev.slimevr.tracker -import io.github.axisangles.ktmath.EulerOrder import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 import kotlin.math.atan2 @@ -74,7 +73,7 @@ fun estimateHeadingCorrect( rotation: Quaternion, referenceRotation: Quaternion, ): HeadingCorrection = (inverseYProjection(referenceRotation) * rotation).eulerHeading().inv() - .twinNearest(Quaternion.IDENTITY) + .twinNearest(referenceRotation) fun estimateAttitudeAlign( rotation: Quaternion, @@ -90,12 +89,7 @@ fun estimateHeadingAlign( headingAlign: HeadingAlignment = Quaternion.IDENTITY, yawOffset: Float = 0.0f, ): HeadingAlignment { - val rotation = applyCalibration( - rotation, - headingCorrect, - attitudeAlign, - headingAlign, - ) + val rotation = applyCalibration(rotation, headingCorrect, attitudeAlign, headingAlign) val pitchRoll = (inverseYProjection(referenceRotation) * rotation).sandwichUnitY() val yawAngle = atan2(pitchRoll.x, pitchRoll.z) + yawOffset return Quaternion.rotationAroundYAxis(yawAngle) diff --git a/server/core/src/test/java/dev/slimevr/tracker/PolarityTrackingTest.kt b/server/core/src/test/java/dev/slimevr/tracker/PolarityTrackingTest.kt index 995bd4d6b..557701bd9 100644 --- a/server/core/src/test/java/dev/slimevr/tracker/PolarityTrackingTest.kt +++ b/server/core/src/test/java/dev/slimevr/tracker/PolarityTrackingTest.kt @@ -1,83 +1,50 @@ package dev.slimevr.tracker import dev.slimevr.degreeToRadian -import io.github.axisangles.ktmath.EulerAngles -import io.github.axisangles.ktmath.EulerOrder import io.github.axisangles.ktmath.Quaternion import solarxr_protocol.rpc.ResetType -import kotlin.math.abs -import kotlin.math.withSign import kotlin.test.Test import kotlin.test.assertEquals class PolarityTrackingTest { + private fun getResetAction(resetType: ResetType, referenceRot: Quaternion) = when (resetType) { + ResetType.FULL -> TrackerActions.FullReset(referenceRot) + ResetType.YAW -> TrackerActions.YawReset(referenceRot) + ResetType.POSE_MOUNTING -> TrackerActions.PoseMountingReset(referenceRot, 0f) + } + @Test - fun `Reducer polarity tracking mega test`() = ResetType.entries.forEach { resetType -> - untrackedHeading.forEach { trackerHeading -> - // rawRotation in state - trackedHeading.forEach { stateHeading -> - // rotation in state - untrackedHeading.forEach { referenceHeading -> - untrackedAttitude.forEach { referenceAttitude -> - val debugText = "resetType $resetType, trackerHeading $trackerHeading, stateHeading $stateHeading, referenceHeading $referenceHeading, referenceAttitude $referenceAttitude" - val referenceRotation = referenceHeading * referenceAttitude + fun `Reducer Resets align polarity with reference rotation`() = ResetType.entries.forEach { resetType -> + // Tracker rawRotation (untracked polarity) + untrackedHeading.forEach { rawRot -> + // Tracker calibrated rotation (tracked polarity) + trackedHeading.forEach { stateRot -> + // Reference rotation (assumes it is using the shortest rotation) + untrackedHeading.forEach { referenceRot -> + // Reset + val initialState = Tracker.DEFAULT_STATE.copy(rawRotation = rawRot, rotation = stateRot) + val resetState = reduce(initialState, getResetAction(resetType, referenceRot)) - // Reset - val initialState = Tracker.DEFAULT_STATE.copy(rawRotation = trackerHeading, rotation = stateHeading) - val trackerAction = when (resetType) { - ResetType.FULL -> TrackerActions.FullReset(referenceRotation) - ResetType.YAW -> TrackerActions.YawReset(referenceRotation) - ResetType.POSE_MOUNTING -> TrackerActions.PoseMountingReset(referenceRotation, 0f) - } - val resetState = reduce(initialState, trackerAction) - assertEquals(resetState.rotation, resetState.rotation.twinNearest(referenceRotation), message = "Reset didn't reset polarity, $debugText") - - // Set same rotation, polarity should match IDENTITY - val rotatedState = reduce(resetState, TrackerActions.SetRotation(initialState.rawRotation)) - assertEquals(rotatedState.rotation, rotatedState.rotation.twinNearest(referenceRotation), message = "Polarity didn't reset after SetRotation, $debugText") - - // Rotate - trackedStep.forEach { degreesChange -> - var rotatedState = resetState - for (change in 0..abs(degreesChange) step STEP_INCREMENT) { // So we don't go over 180d in one SetRotation - val headingChange = Quaternion.rotationAroundYAxis(degreeToRadian(change.toFloat().withSign(degreesChange))) - val newRotation = trackerHeading * headingChange - rotatedState = reduce(rotatedState, TrackerActions.SetRotation(newRotation)) - } - - // If within 180 degrees, it's still IDENTITY polarity, if >180 degrees, it's not - val expectedRotation = if (abs(degreesChange) < 180) { - rotatedState.rotation.twinNearest(referenceRotation) - } else { - rotatedState.rotation.twinFurthest(referenceRotation) - } - assertEquals(rotatedState.rotation, expectedRotation, "Rotating after reset yielded wrong polarity, $debugText. degreesChange = $degreesChange") - } - } + // Calibration refresh, polarity should match reference + val refreshedState = reduce(resetState, TrackerActions.SetRotation(initialState.rawRotation, refresh = true)) + assertEquals( + refreshedState.rotation, + refreshedState.rotation.twinNearest(referenceRot), + message = "resetType $resetType, rawRot $rawRot, stateRot $stateRot, referenceRot $referenceRot", + ) } } } } companion object { - const val STEP_INCREMENT = 72 - val untrackedStep = (-180..180 step STEP_INCREMENT) - val trackedStep = (-360..360 step STEP_INCREMENT) + val untrackedStep = (-180..180 step 72) + val trackedStep = (-360..360 step 72) val trackedHeading = trackedStep.map { y -> Quaternion.rotationAroundYAxis(degreeToRadian(y.toFloat())) } val untrackedHeading = untrackedStep.map { y -> Quaternion.rotationAroundYAxis(degreeToRadian(y.toFloat())) } - val untrackedAttitude = untrackedStep.flatMap { x -> - untrackedStep.map { z -> - EulerAngles( - EulerOrder.YZX, - degreeToRadian(x.toFloat()), - 0f, - degreeToRadian(z.toFloat()), - ).toQuaternion() - } - } } } diff --git a/server/core/src/test/java/dev/slimevr/tracker/SessionCalibrationTest.kt b/server/core/src/test/java/dev/slimevr/tracker/SessionCalibrationTest.kt index 97c900e4b..1484eeed7 100644 --- a/server/core/src/test/java/dev/slimevr/tracker/SessionCalibrationTest.kt +++ b/server/core/src/test/java/dev/slimevr/tracker/SessionCalibrationTest.kt @@ -224,23 +224,19 @@ class SessionCalibrationTest { val rawRotation = undoCalibration(rR * Quaternion.IDENTITY, rR, aA) - // TODO: Can we avoid needing to use twinNearest? It would be best if it was - // just inherently in the right quaternion space (for both heading & attitude). - // This might also just not be a problem, I'm not sure. val estimatedHeadingCorrect = estimateHeadingCorrect(rawRotation, rR) quaternionAssertEquals( rR, - estimatedHeadingCorrect.twinNearest(rR), + estimatedHeadingCorrect, message = "Estimated heading correction is wrong ( hC: $hC, aA: $aA, rR: $rR )", ) - // TODO: See if we can avoid using twinNearest val estimatedAttitudeAlign = estimateAttitudeAlign(rawRotation, estimatedHeadingCorrect, rR) quaternionAssertEquals( aA, - estimatedAttitudeAlign.twinNearest(aA), + estimatedAttitudeAlign, message = "Estimated attitude alignment is wrong ( hC: $hC, aA: $aA, rR: $rR )", ) } @@ -264,12 +260,12 @@ class SessionCalibrationTest { headingAlign = hA * yawOffsetRotation.inv(), ) - // TODO: See if we can avoid using twinNearest val estimateHeadingAlign = estimateHeadingAlign( rawRotation, ref, yawOffset = yawOffset, ) + // twinNearest is equivalent since this doesn't care about polarity quaternionAssertEquals( hA, estimateHeadingAlign.twinNearest(hA),