Fix centaur using unit test

This commit is contained in:
Erimel committed 2026-09-15 17:58:59 -04:00
1 parent d69357b53b
commit 62ed5d0031
6 files changed
+46 -82

No files matched your search

@@ -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)
}
}
@@ -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,
@@ -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),
)
}
@@ -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)