From f391b558c91b41b658fbd992828935cf661d77d2 Mon Sep 17 00:00:00 2001 From: Erimel Date: Thu, 28 Mar 2024 02:17:08 -0400 Subject: [PATCH] Tpose down fix (#967) --- .../tracking/trackers/TrackerResetsHandler.kt | 72 ++++++++++--------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/server/core/src/main/java/dev/slimevr/tracking/trackers/TrackerResetsHandler.kt b/server/core/src/main/java/dev/slimevr/tracking/trackers/TrackerResetsHandler.kt index 5ff0364e0..7803f84a1 100644 --- a/server/core/src/main/java/dev/slimevr/tracking/trackers/TrackerResetsHandler.kt +++ b/server/core/src/main/java/dev/slimevr/tracking/trackers/TrackerResetsHandler.kt @@ -20,6 +20,12 @@ private const val DRIFT_COOLDOWN_MS = 50000L */ class TrackerResetsHandler(val tracker: Tracker) { + private val HalfHorizontal = EulerAngles( + EulerOrder.YZX, + 0f, + Math.PI.toFloat(), + 0f, + ).toQuaternion() private var driftAmount = 0f private var averagedDriftQuat = Quaternion.IDENTITY private var rotationSinceReset = Quaternion.IDENTITY @@ -36,12 +42,7 @@ class TrackerResetsHandler(val tracker: Tracker) { var lastResetQuaternion: Quaternion? = null // Manual mounting orientation - var mountingOrientation: Quaternion = EulerAngles( - EulerOrder.YZX, - 0f, - Math.PI.toFloat(), - 0f, - ).toQuaternion() + var mountingOrientation = HalfHorizontal set(value) { field = value // Clear the mounting reset now that it's been set manually @@ -59,7 +60,7 @@ class TrackerResetsHandler(val tracker: Tracker) { private var gyroFixNoMounting = Quaternion.IDENTITY private var attachmentFixNoMounting = Quaternion.IDENTITY private var yawFixZeroReference = Quaternion.IDENTITY - private var tposeFix = Quaternion.IDENTITY + private var tposeDownFix = Quaternion.IDENTITY /** * Reads/loads drift compensation settings from given config @@ -137,8 +138,9 @@ class TrackerResetsHandler(val tracker: Tracker) { rot = gyroFix * rot rot *= attachmentFix rot = mountRotFix.inv() * (rot * mountRotFix) + rot *= tposeDownFix rot = yawFix * rot - return rot * tposeFix + return rot } /** @@ -146,7 +148,8 @@ class TrackerResetsHandler(val tracker: Tracker) { * applying quaternions produced after full reset and yaw reset only */ private fun adjustToIdentity(rotation: Quaternion): Quaternion { - var rot = gyroFixNoMounting * rotation + var rot = rotation + rot = gyroFixNoMounting * rot rot *= attachmentFixNoMounting rot = yawFixZeroReference * rot return rot @@ -172,36 +175,37 @@ class TrackerResetsHandler(val tracker: Tracker) { * 0). This allows the tracker to be strapped to body at any pitch and roll. */ fun resetFull(reference: Quaternion) { - // Adjust for T-Pose - if ((isLeftArmTracker() && armsResetMode == ArmsResetModes.TPOSE_DOWN)) { - tposeFix = EulerAngles( - EulerOrder.YZX, - 0f, - 0f, - -FastMath.HALF_PI, - ).toQuaternion() + // Adjust for T-Pose (down) + tposeDownFix = if ((isLeftArmTracker() && armsResetMode == ArmsResetModes.TPOSE_DOWN)) { + EulerAngles(EulerOrder.YZX, 0f, 0f, -FastMath.HALF_PI).toQuaternion() } else if ((isRightArmTracker() && armsResetMode == ArmsResetModes.TPOSE_DOWN)) { - tposeFix = EulerAngles(EulerOrder.YZX, 0f, 0f, FastMath.HALF_PI).toQuaternion() + EulerAngles(EulerOrder.YZX, 0f, 0f, FastMath.HALF_PI).toQuaternion() } else { - tposeFix = Quaternion.IDENTITY + Quaternion.IDENTITY } - lastResetQuaternion = adjustToReference(tracker.getRawRotation()) - val oldRot = adjustToReference(tracker.getRawRotation()) + lastResetQuaternion = oldRot + + val mountingAdjustedRotation = tracker.getRawRotation() * mountingOrientation if (tracker.needsMounting) { - gyroFix = fixGyroscope(tposeFix * tracker.getRawRotation() * mountingOrientation) + gyroFix = fixGyroscope(mountingAdjustedRotation * tposeDownFix) } else { // Set mounting to the HMD's yaw so that the non-mounting-adjusted // tracker goes forward. mountRotFix = getYawQuaternion(reference) } - attachmentFix = fixAttachment(tracker.getRawRotation() * mountingOrientation) + attachmentFix = fixAttachment(mountingAdjustedRotation) + + // Rotate attachmentFix by 180 degrees as a workaround for tpose (down) + if (tposeDownFix != Quaternion.IDENTITY && tracker.needsMounting) { + attachmentFix *= HalfHorizontal + } makeIdentityAdjustmentQuatsFull() - yawFix = fixYaw(tracker.getRawRotation() * mountingOrientation, reference) + yawFix = fixYaw(mountingAdjustedRotation, reference) calculateDrift(oldRot) @@ -218,15 +222,14 @@ class TrackerResetsHandler(val tracker: Tracker) { * position should be corrected in the source. */ fun resetYaw(reference: Quaternion) { - lastResetQuaternion = adjustToReference(tracker.getRawRotation()) - - val rot: Quaternion = adjustToReference(tracker.getRawRotation()) + val oldRot = adjustToReference(tracker.getRawRotation()) + lastResetQuaternion = oldRot yawFix = fixYaw(tracker.getRawRotation() * mountingOrientation, reference) makeIdentityAdjustmentQuatsYaw() - calculateDrift(rot) + calculateDrift(oldRot) // Remove the status if yaw reset was performed after the tracker // was disconnected and connected. @@ -247,17 +250,16 @@ class TrackerResetsHandler(val tracker: Tracker) { } // Get the current calibrated rotation - var buffer = adjustToDrift(tracker.getRawRotation() * mountingOrientation) - buffer *= tposeFix - buffer = gyroFix * buffer - buffer *= attachmentFix - buffer = yawFix * buffer + var rotBuf = adjustToDrift(tracker.getRawRotation() * mountingOrientation) + rotBuf = gyroFix * rotBuf + rotBuf *= attachmentFix + rotBuf = yawFix * rotBuf // Adjust buffer to reference - buffer = reference.project(Vector3.POS_Y).inv().unit() * buffer + rotBuf = reference.project(Vector3.POS_Y).inv().unit() * rotBuf // Rotate a vector pointing up by the quat - val rotVector = buffer.sandwich(Vector3.POS_Y) + val rotVector = rotBuf.sandwich(Vector3.POS_Y) // Calculate the yaw angle using tan var yawAngle = atan2(rotVector.x, rotVector.z)