Rework rest orientation

This commit is contained in:
Erimel committed 2026-07-23 20:46:40 -04:00
1 parent 05fbf64ac3
commit c850f18403
4 files changed
+74 -55

No files matched your search

@@ -44,9 +44,7 @@ typealias ResetsBehaviour = Behaviour<ResetsState, ResetsActions, ResetsManager>
class ResetsManager(val context: ResetsContext, val server: VRServer, val settings: Settings) {
fun startObserving() = context.observeAll(this)
private val quarterRollLeft = EulerAngles(EulerOrder.YZX, 0f, 0f, -FastMath.HALF_PI).toQuaternion()
private val quarterRollRight = EulerAngles(EulerOrder.YZX, 0f, 0f, FastMath.HALF_PI).toQuaternion()
private var resetJob: Job = Job()
/**
@@ -121,23 +119,13 @@ class ResetsManager(val context: ResetsContext, val server: VRServer, val settin
it.context.dispatch(
when (resetType) {
ResetType.YAW -> TrackerActions.YawReset(referenceRotation)
ResetType.FULL -> TrackerActions.FullReset(referenceRotation, getRestOrientation(it.context.state.value.bodyPart, config.armsResetMode))
ResetType.FULL -> TrackerActions.FullReset(referenceRotation)
ResetType.MOUNTING -> TrackerActions.MountingReset(referenceRotation, getYawOffset(it.context.state.value.bodyPart, config.armsResetMode))
},
)
}
}
private fun getRestOrientation(bodyPart: BodyPart?, armsResetMode: ArmsResetMode) = if (armsResetMode == ArmsResetMode.T_POSE_DOWN) {
when (bodyPart) {
in LEFT_ARM_PARTS, in LEFT_FINGER_PARTS -> quarterRollLeft
in RIGHT_ARM_PARTS, in RIGHT_FINGER_PARTS -> quarterRollRight
else -> Quaternion.IDENTITY
}
} else {
Quaternion.IDENTITY
}
private fun getYawOffset(bodyPart: BodyPart?, armsResetMode: ArmsResetMode) = when (bodyPart) {
// Going forward
in UPPER_LEG_PARTS -> 0f
@@ -1,13 +1,24 @@
package dev.slimevr.tracker
import com.jme3.math.FastMath
import dev.slimevr.AppLogger
import dev.slimevr.config.Settings
import dev.slimevr.config.SettingsActions
import dev.slimevr.resets.LEFT_ARM_PARTS
import dev.slimevr.resets.LEFT_FINGER_PARTS
import dev.slimevr.resets.RIGHT_ARM_PARTS
import dev.slimevr.resets.RIGHT_FINGER_PARTS
import dev.slimevr.skeleton.SkeletonActions
import dev.slimevr.util.safeLaunch
import io.github.axisangles.ktmath.EulerAngles
import io.github.axisangles.ktmath.EulerOrder
import io.github.axisangles.ktmath.Quaternion
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.distinctUntilChangedBy
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
@@ -15,6 +26,7 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.isActive
import solarxr_protocol.datatypes.BodyPart
import solarxr_protocol.datatypes.TrackerStatus
import solarxr_protocol.rpc.ArmsResetMode
import kotlin.concurrent.atomics.AtomicInt
import kotlin.concurrent.atomics.ExperimentalAtomicApi
import kotlin.concurrent.atomics.incrementAndFetch
@@ -54,6 +66,7 @@ class TrackerBasicBehaviour : TrackerBehaviour {
rawRotation,
cal.headingCorrection,
cal.headingAlignment,
state.restOrientation
)
cal != null -> state.acceleration
@@ -74,6 +87,8 @@ class TrackerBasicBehaviour : TrackerBehaviour {
is TrackerActions.SetMountingOrientation -> state.copy(mountingOrientation = action.mountingOrientation)
is TrackerActions.SetRestOrientation -> state.copy(restOrientation = action.restOrientation)
is TrackerActions.FullReset -> {
val headingCorrection = estimateHeadingCorrect(
state.rawRotation,
@@ -92,19 +107,12 @@ class TrackerBasicBehaviour : TrackerBehaviour {
attitudeAlignment = attitudeAlignment,
)
// Apply calibration on rawRotation
val rotation = applyCalibration(
state.rawRotation,
sessionCalibration.headingCorrection,
sessionCalibration.attitudeAlignment,
sessionCalibration.headingAlignment,
action.restOrientation
) // don't track polarity on Full Reset
// Reset polarity tracking
val defaultPolarityRotation = state.rotation.twinNearest(Quaternion.IDENTITY)
state.copy(
sessionCalibration = sessionCalibration,
restOrientation = action.restOrientation,
rotation = rotation,
rotation = defaultPolarityRotation,
)
}
@@ -120,19 +128,7 @@ class TrackerBasicBehaviour : TrackerBehaviour {
headingCorrection = headingCorrection,
)
// Apply calibration on rawRotation
val rotation = applyCalibration(
state.rawRotation,
sessionCalibration.headingCorrection,
sessionCalibration.attitudeAlignment,
sessionCalibration.headingAlignment,
state.restOrientation
).twinNearest(state.rotation)
state.copy(
sessionCalibration = sessionCalibration,
rotation = rotation,
)
state.copy(sessionCalibration = sessionCalibration)
}
is TrackerActions.MountingReset -> {
@@ -153,21 +149,18 @@ class TrackerBasicBehaviour : TrackerBehaviour {
headingAlignment = headingAlignment,
)
// Apply calibration on rawRotation
val rotation = applyCalibration(
state.rawRotation,
sessionCalibration.headingCorrection,
sessionCalibration.attitudeAlignment,
sessionCalibration.headingAlignment,
state.restOrientation
).twinNearest(state.rotation)
state.copy(
sessionCalibration = sessionCalibration,
rotation = rotation,
)
state.copy(sessionCalibration = sessionCalibration)
}
}
override fun observe(receiver: Tracker) {
// Refresh the tracker's rotation whenever the sessionCalibration gets updated
receiver.context.state
.distinctUntilChangedBy { it.sessionCalibration to it.restOrientation }
.onEach {
receiver.context.dispatch(TrackerActions.SetRotation(it.rawRotation, it.rawAcceleration, it.rawMagnetometer))
}.launchIn(receiver.context.scope)
}
}
class TrackerDefaultMountingOrientationBehaviour : TrackerBehaviour {
@@ -208,6 +201,7 @@ class TrackerDefaultMountingOrientationBehaviour : TrackerBehaviour {
receiver.context.state
.map { it.bodyPart }
.distinctUntilChanged()
.drop(1)
.onEach {
receiver.context.dispatch(TrackerActions.SetMountingOrientation(defaultMountingForBodyPart(it)))
}.launchIn(receiver.context.scope)
@@ -272,3 +266,33 @@ class TrackerToSkeletonBehaviour : TrackerBehaviour {
.launchIn(receiver.context.scope)
}
}
class TrackerRestOrientationBehaviour(
private val settings: Settings,
) : TrackerBehaviour {
override fun observe(receiver: Tracker) {
val armsResetModeFlow = settings.context.state.map { it.data.resetsConfig.armsResetMode }
val bodyPartFlow = receiver.context.state.map { it.bodyPart }
combine(armsResetModeFlow, bodyPartFlow) { armsResetMode, bodyPart ->
getRestOrientation(bodyPart, armsResetMode)
}
.distinctUntilChanged()
.onEach { restOrientation ->
receiver.context.dispatch(TrackerActions.SetRestOrientation(restOrientation))
}
.launchIn(receiver.context.scope)
}
private val quarterRollLeft = EulerAngles(EulerOrder.YZX, 0f, 0f, -FastMath.HALF_PI).toQuaternion()
private val quarterRollRight = EulerAngles(EulerOrder.YZX, 0f, 0f, FastMath.HALF_PI).toQuaternion()
private fun getRestOrientation(bodyPart: BodyPart?, armsResetMode: ArmsResetMode) = if (armsResetMode == ArmsResetMode.T_POSE_DOWN) {
when (bodyPart) {
in LEFT_ARM_PARTS, in LEFT_FINGER_PARTS -> quarterRollLeft
in RIGHT_ARM_PARTS, in RIGHT_FINGER_PARTS -> quarterRollRight
else -> Quaternion.IDENTITY
}
} else {
Quaternion.IDENTITY
}
}
@@ -25,7 +25,7 @@ data class TrackerState(
val bodyPart: BodyPart?,
val customName: String?,
val mountingOrientation: HeadingAlignment?,
val restOrientation: Quaternion,
val restOrientation: RestOrientation,
val rawRotation: RawRotation,
val rotation: CalibratedRotation,
val rawAcceleration: RawAcceleration,
@@ -48,7 +48,8 @@ sealed interface TrackerActions {
data class SetStatus(val status: TrackerStatus) : TrackerActions
data class SetRotation(val rotation: Quaternion? = null, val acceleration: Vector3? = null, val magnetometer: Vector3? = null) : TrackerActions
data class SetMountingOrientation(val mountingOrientation: HeadingAlignment?) : TrackerActions
data class FullReset(val referenceRotation: Quaternion, val restOrientation: Quaternion) : TrackerActions
data class SetRestOrientation(val restOrientation: Quaternion) : TrackerActions
data class FullReset(val referenceRotation: Quaternion) : TrackerActions
data class YawReset(val referenceRotation: Quaternion) : TrackerActions
data class MountingReset(val referenceRotation: Quaternion, val yawOffset: Float) : TrackerActions
}
@@ -81,6 +82,7 @@ class Tracker(
id = id,
hardwareId = hardwareId,
name = name,
restOrientation = Quaternion.IDENTITY,
rawRotation = Quaternion.IDENTITY,
rotation = Quaternion.IDENTITY,
rawAcceleration = Vector3.NULL,
@@ -99,7 +101,6 @@ class Tracker(
completedRestCalibration = false,
magStatus = MagnetometerStatus.NOT_SUPPORTED,
sessionCalibration = null,
restOrientation = Quaternion.IDENTITY,
)
val trackerState = if (savedConfig != null) {
restoreFromConfig(baseState, savedConfig, settings.context.state.value.data.resetsConfig.saveMountingReset)
@@ -110,6 +111,7 @@ class Tracker(
val behaviours = listOf(
TrackerBasicBehaviour(),
TrackerDefaultMountingOrientationBehaviour(),
TrackerRestOrientationBehaviour(settings),
TrackerConfigBehaviour(settings, hardwareId),
TrackerTPSBehaviour(),
TrackerToSkeletonBehaviour(),
@@ -11,6 +11,7 @@ typealias RawAcceleration = Vector3
typealias HeadingCorrection = Quaternion
typealias AttitudeAlignment = Quaternion
typealias HeadingAlignment = Quaternion
typealias RestOrientation = Quaternion // TODO verify math and make unit tests
typealias AccelerationRotation = Quaternion
@@ -21,14 +22,14 @@ data class SessionCalibration(
val headingCorrection: HeadingCorrection = Quaternion.IDENTITY,
val attitudeAlignment: AttitudeAlignment = Quaternion.IDENTITY,
val headingAlignment: HeadingAlignment = Quaternion.IDENTITY,
)
)
fun applyCalibration(
rawRotation: RawRotation,
headingCorrect: HeadingCorrection = Quaternion.IDENTITY,
attitudeAlign: AttitudeAlignment = Quaternion.IDENTITY,
headingAlign: HeadingAlignment = Quaternion.IDENTITY,
restOrientation: Quaternion = Quaternion.IDENTITY,
restOrientation: RestOrientation = Quaternion.IDENTITY,
): CalibratedRotation = headingAlign.inv() * headingCorrect * rawRotation * attitudeAlign * headingAlign * restOrientation
// We reverse the order of headingAlign and attitudeAlign here since our
@@ -40,6 +41,7 @@ fun undoCalibration(
headingCorrect: HeadingCorrection = Quaternion.IDENTITY,
attitudeAlign: AttitudeAlignment = Quaternion.IDENTITY,
headingAlign: HeadingAlignment = Quaternion.IDENTITY,
restOrientation: RestOrientation = Quaternion.IDENTITY,
): RawRotation = headingCorrect.inv() * headingAlign * calibratedRotation * headingAlign.inv() * attitudeAlign.inv()
// Acceleration needs to be rotated by raw rotation with heading corrected
@@ -47,6 +49,7 @@ private fun accelerationRotation(
rawRotation: RawRotation,
headingCorrect: HeadingCorrection = Quaternion.IDENTITY,
headingAlign: HeadingAlignment = Quaternion.IDENTITY,
restOrientation: RestOrientation = Quaternion.IDENTITY,
): AccelerationRotation = headingAlign.inv() * headingCorrect * rawRotation
fun applyCalibration(
@@ -54,6 +57,7 @@ fun applyCalibration(
rawRotation: RawRotation,
headingCorrect: HeadingCorrection = Quaternion.IDENTITY,
headingAlign: HeadingAlignment = Quaternion.IDENTITY,
restOrientation: RestOrientation = Quaternion.IDENTITY,
): CalibratedAcceleration = accelerationRotation(rawRotation, headingCorrect, headingAlign).sandwich(
rawAcceleration,
)
@@ -63,6 +67,7 @@ fun undoCalibration(
rawRotation: RawRotation,
headingCorrect: HeadingCorrection = Quaternion.IDENTITY,
headingAlign: HeadingAlignment = Quaternion.IDENTITY,
restOrientation: RestOrientation = Quaternion.IDENTITY,
): RawAcceleration = accelerationRotation(rawRotation, headingCorrect, headingAlign).inv()
.sandwich(calibratedAcceleration)