Fix boneOffset and add rotation offset for driver

This commit is contained in:
Erimel committed 2026-09-30 16:12:38 -04:00
1 parent 7d89c8deaf
commit d791928ece
8 files changed
+72 -43

No files matched your search

@@ -123,7 +123,7 @@ val BONE_SPECS: Map<SkeletonBone, BoneSpec> = mapOf(
SkeletonBone.SHOULDERS_WIDTH to BoneSpec(default = 0.35f, min = 0.01f, max = 0.8f, curve = SHOULDERS_WIDTH_BY_STATURE),
SkeletonBone.UPPER_ARM to BoneSpec(default = 0.26f, min = 0.01f, max = 0.6f, curve = UPPER_ARM_LENGTH_BY_STATURE),
SkeletonBone.LOWER_ARM to BoneSpec(default = 0.26f, min = 0.01f, max = 0.6f, curve = LOWER_ARM_LENGTH_BY_STATURE),
SkeletonBone.HAND to BoneSpec(default = 0.08f, min = 0.01f, max = 0.3f, curve = HAND_LENGTH_BY_STATURE),
SkeletonBone.HAND to BoneSpec(default = 0.073f, min = 0.01f, max = 0.3f, curve = HAND_LENGTH_BY_STATURE),
)
val DEFAULT_PROPORTIONS: Map<SkeletonBone, Float> = BONE_SPECS.mapValues { (_, spec) -> spec.default }
@@ -345,7 +345,7 @@ private val FINGERS = listOf(
BodyPart.LEFT_LITTLE_INTERMEDIATE to BodyPart.RIGHT_LITTLE_INTERMEDIATE,
BodyPart.LEFT_LITTLE_DISTAL to BodyPart.RIGHT_LITTLE_DISTAL,
),
lengthFraction = 0.69f,
lengthFraction = 0.67f,
knuckle = Vector3(0f, 0.1f, 0.31f),
lean = Vector3.ZERO,
),
@@ -10,6 +10,10 @@ import dev.slimevr.solarxr.toQuaternion
import dev.slimevr.solarxr.toVector3
import dev.slimevr.tracker.Tracker
import dev.slimevr.tracker.TrackerActions
import dev.slimevr.util.MonotonicValueTimeMark
import dev.slimevr.util.inFloatingSeconds
import dev.slimevr.util.timeSource
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import solarxr_protocol.datatypes.DeviceOrigin
@@ -21,24 +25,34 @@ import solarxr_protocol.driver_protocol.UpdateTrackerPosition
import solarxr_protocol.driver_protocol.UpdateTrackerStatus
import kotlin.to
// TODO add more devices and retest Knuckles
// HMDs might all have the same offset
private val DISPLAY_NAME_TO_INTENDED_OFFSET = mapOf(
"Beyond" to Vector3(0f, 0f, 0.1f),
"Knuckles Left" to Vector3(0f, 0.11f, 0.08f),
"Knuckles Right" to Vector3(0f, 0.11f, 0.08f),
data class Offset(
/** Default position offset from bone to tracker. Can be changed by the user. */
val boneOffset: Vector3,
/** Rotation offset applied on the tracker's raw rotation before everything else. */
val rotationOffset: Quaternion = Quaternion.IDENTITY,
)
private val indexBoneOffset = Vector3(-0.02f, 0.07f, 0.13f)
private val indexRotX = Quaternion.rotationAroundXAxis(0.4f)
private val indexRotZ = Quaternion.rotationAroundZAxis(0.35f)
// TODO add more devices
private val DISPLAY_NAME_TO_OFFSET = mapOf(
"Knuckles Left" to Offset(indexBoneOffset, indexRotX * indexRotZ),
"Knuckles Right" to Offset(indexBoneOffset.unaryMinusX(), indexRotX * indexRotZ.inv()),
)
// Used as fallback when map above doesn't contain the entry
private val BODY_PART_TO_INTENDED_OFFSET = mapOf(
BodyPart.HEAD to Vector3(0f, 0f, 0.1f),
BodyPart.LEFT_HAND to Vector3(0f, 0.11f, 0.08f),
BodyPart.RIGHT_HAND to Vector3(0f, 0.11f, 0.08f),
private val BODY_PART_TO_OFFSET = mapOf(
BodyPart.HEAD to Offset(Vector3(0f, 0f, 0.1f)),
BodyPart.LEFT_HAND to Offset(indexBoneOffset, indexRotX * indexRotZ),
BodyPart.RIGHT_HAND to Offset(indexBoneOffset.unaryMinusX(), indexRotX * indexRotZ.inv()),
)
class DriverIncomingTrackersBehaviour(
private val appContext: AppContextProvider,
) : SolarXRBridgeBehaviour {
private val rotationOffsets: MutableMap<Int, Quaternion> = mutableMapOf()
private val lastVelocities: MutableMap<Int, Pair<MonotonicValueTimeMark, Vector3>> = mutableMapOf()
override fun observe(receiver: SolarXRBridge) {
val server = appContext.server
@@ -86,6 +100,7 @@ class DriverIncomingTrackersBehaviour(
)
server.context.dispatch(VRServerActions.NewDevice(deviceId, device))
val offset = DISPLAY_NAME_TO_OFFSET[req.displayName] ?: BODY_PART_TO_OFFSET[req.bodyPart]
val trackerId = server.nextHandle()
val tracker = Tracker.create(
scope = scope,
@@ -93,13 +108,14 @@ class DriverIncomingTrackersBehaviour(
name = req.displayName ?: "Tracker #$trackerId",
bodyPart = req.bodyPart,
intendedBodyPart = req.bodyPart,
intendedBoneOffset = DISPLAY_NAME_TO_INTENDED_OFFSET[req.displayName] ?: BODY_PART_TO_INTENDED_OFFSET[req.bodyPart],
intendedBoneOffset = offset?.boneOffset,
deviceId = deviceId,
hardwareId = hardwareId,
origin = DeviceOrigin.DRIVER,
driverName = driverName,
appContext = appContext,
)
rotationOffsets[trackerId] = offset?.rotationOffset ?: Quaternion.IDENTITY
server.context.dispatch(VRServerActions.NewTracker(trackerId, tracker))
receiver.sendDriverMessage(
@@ -135,10 +151,26 @@ class DriverIncomingTrackersBehaviour(
val trackerId = event.trackerId.toInt()
if (trackerId == 0) return@on
// TODO: receive velocity, mapping to accel?
// Map velocity to accel TODO: driver doesn't send velocity 30/09/2026
val acceleration = event.linearVelocity?.let { velocity ->
val now = timeSource.markNow()
val velocity = velocity.toVector3()
val lastVelocity = lastVelocities[trackerId]
lastVelocities[trackerId] = now to velocity
lastVelocity?.let {
val deltaVelocity = (velocity - it.second)
val deltaTime = (now - it.first).inFloatingSeconds
deltaVelocity / deltaTime
}
}
// Rotation offset done here before rotation gets to the tracker
val rotationOffset = rotationOffsets[trackerId] ?: Quaternion.IDENTITY
// Update tracker with new data
server.getTracker(trackerId)?.context?.dispatch(
TrackerActions.SetRotation(
rotation = event.rotation?.toQuaternion(),
rotation = event.rotation?.toQuaternion()?.times(rotationOffset),
acceleration = acceleration,
position = event.position?.toVector3(),
),
)
@@ -11,7 +11,7 @@ import dev.slimevr.solarxr.datafeed.DataFeedInitBehaviour
import dev.slimevr.solarxr.driver.DriverHandshakeBehaviour
import dev.slimevr.solarxr.driver.DriverIncomingTrackersBehaviour
import dev.slimevr.solarxr.driver.DriverOutgoingTrackersBehaviour
import dev.slimevr.solarxr.rpc.AssignTrackerBehaviour
import dev.slimevr.solarxr.rpc.UpdateTrackerBehaviour
import dev.slimevr.solarxr.rpc.BoneRoutingBehaviour
import dev.slimevr.solarxr.rpc.BvhBehaviour
import dev.slimevr.solarxr.rpc.DongleSettingsBehaviour
@@ -198,7 +198,7 @@ class SolarXRBridge(
add(SkeletonSettingsBehaviour(appContext.config.settings))
add(SkeletonProportionsBehaviour(appContext.config.userConfig, appContext.skeleton, appContext.errorReporting))
add(TrackingChecklistBehaviour(appContext.trackingChecklist, appContext.config.settings, appContext.errorReporting))
add(AssignTrackerBehaviour(appContext.server, appContext.errorReporting))
add(UpdateTrackerBehaviour(appContext.server, appContext.errorReporting))
add(DongleSettingsBehaviour(appContext.server))
add(TelemetryBehaviour(appContext.server))
add(DriverHandshakeBehaviour(appContext))
@@ -14,7 +14,7 @@ import solarxr_protocol.datatypes.BodyPart
import solarxr_protocol.rpc.ResetTrackerAssignments
import solarxr_protocol.rpc.UpdateTrackerRequest
class AssignTrackerBehaviour(
class UpdateTrackerBehaviour(
private val server: VRServer,
private val errorReporting: ErrorReportingManager,
) : SolarXRBridgeBehaviour {
@@ -59,7 +59,7 @@ class AssignTrackerBehaviour(
tracker.context.dispatch(
TrackerActions.SetMountingOrientation(mountingOrientation),
)
// Assignment re-sends the current orientation. So we make sure it didnt change
// Assignment re-sends the current orientation, so we make sure it didn't change for Sentry
if (current.angleToR(mountingOrientation) > 0.1f) {
errorReporting.reportUsageOncePerSession("mounting_method_used", mapOf("method" to "manual"))
}
@@ -24,7 +24,7 @@ class TrackerConfigBehaviour(
.distinctUntilChangedBy {
val saveMountingReset = receiver.settings.context.state.value.data.resetsConfig.saveMountingReset
val headingAlignment = if (saveMountingReset) it.sessionCalibration.headingAlignment else null
it.bodyPart to it.customName to it.mountingOrientation to it.magStatus to headingAlignment
it.bodyPart to it.customName to it.mountingOrientation to it.magStatus to headingAlignment to it.boneOffsets
}
.drop(1)
.onEach { state ->
@@ -50,10 +50,20 @@ fun reduce(
}
// Accel calibration: going from raw, rotation-aligned, accel to world-aligned accel
// TODO: double check calibration; doesn't seem right - Erimel
val acceleration: CalibratedAcceleration? =
if (action.acceleration != null) {
applyFullCalibration(action.acceleration, correctedRawRotation, state)
if (state.imuType != null) {
// TODO: double check calibration; doesn't seem right - Erimel
applyCalibration(
action.acceleration,
correctedRawRotation,
state.sessionCalibration.headingCorrection,
state.sessionCalibration.headingAlignment,
)
} else {
// Assume acceleration is already calibrated for non-IMU trackers
action.acceleration
}
} else {
state.acceleration
}
@@ -90,8 +100,8 @@ fun reduce(
is TrackerActions.FullReset -> {
val alignAttitude = !state.isAssignedReliableReference || (action.resetReliableReferenceAttitude && action.referenceRotation == null)
val correctHeading = action.referenceRotation != null
val alignHeading = state.position != null && action.referenceRotation != null
val correctHeading = action.referenceRotation != null && !state.isAssignedReliableReference
val alignHeading = state.position != null && action.referenceRotation != null && !state.isAssignedReliableReference
val referenceRotation = action.referenceRotation ?: state.rawRotation
@@ -171,9 +181,9 @@ fun reduce(
is TrackerActions.PoseMountingReset -> {
// Positional trackers' heading is aligned on full reset, not on mounting reset, except for a reference.
val alignHeading = state.position == null || action.referenceRotation == null
val alignHeading = (state.position == null || action.referenceRotation == null) && !state.isAssignedReliableReference
// A positional reference tracker needs to correct its heading on mounting reset.
val correctHeading = state.position != null && action.referenceRotation == null
val correctHeading = state.position != null && action.referenceRotation == null && !state.isAssignedReliableReference
val referenceRotation = action.referenceRotation ?: state.rotation
@@ -2,7 +2,6 @@ package dev.slimevr.tracker
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import solarxr_protocol.datatypes.BodyPart
import kotlin.math.atan2
typealias RawRotation = Quaternion
@@ -75,22 +74,6 @@ fun applyCalibration(
rawAcceleration,
)
fun applyFullCalibration(
rawAcceleration: RawAcceleration,
rawRotation: RawRotation,
trackerState: TrackerState,
): CalibratedAcceleration {
// Never use heading correction/alignment for an assigned reliable reference
val headingCorrection = if (trackerState.isAssignedReliableReference) Quaternion.IDENTITY else trackerState.sessionCalibration.headingCorrection
val headingAlignment = if (trackerState.isAssignedReliableReference) Quaternion.IDENTITY else trackerState.sessionCalibration.headingAlignment
return applyCalibration(
rawAcceleration,
rawRotation,
headingCorrection,
headingAlignment,
)
}
fun undoCalibration(
calibratedAcceleration: CalibratedAcceleration,
rawRotation: RawRotation,
@@ -23,6 +23,10 @@ class Vector3(val x: Float, val y: Float, val z: Float) {
operator fun unaryMinus() = Vector3(-x, -y, -z)
fun unaryMinusX() = Vector3(-x, y, z)
fun unaryMinusY() = Vector3(x, -y, z)
fun unaryMinusZ() = Vector3(x, y, -z)
operator fun plus(that: Vector3) = Vector3(
this.x + that.x,
this.y + that.y,