diff --git a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt index 6664405fb..af889a94d 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt @@ -71,8 +71,15 @@ private fun getSourceToFollow(fk: ComputedSkeleton): FollowSource = if (isUserSi FollowSource.COM } -// Feet are always considered even if inactive -private val alwaysActiveBodyParts = arrayOf(BodyPart.LEFT_FOOT, BodyPart.RIGHT_FOOT, BodyPart.LEFT_LOWER_LEG, BodyPart.RIGHT_LOWER_LEG) +// Legs are always used even if inactive +private val alwaysActiveBodyParts = arrayOf( + BodyPart.LEFT_FOOT, + BodyPart.RIGHT_FOOT, + BodyPart.LEFT_LOWER_LEG, + BodyPart.RIGHT_LOWER_LEG, + BodyPart.LEFT_UPPER_LEG, + BodyPart.RIGHT_UPPER_LEG, +) private fun getActiveBodyParts(inputs: InputSkeleton) = inputs.filter { it.value.isRotationActive }.map { it.key } + alwaysActiveBodyParts /** Returns the active bone closest to or furthest inside the ground */ @@ -92,8 +99,8 @@ object FootLocalizer { } private fun whichPlantedFoot(bodyPart: BodyPart) = when (bodyPart) { - BodyPart.LEFT_LOWER_LEG, BodyPart.LEFT_FOOT -> PlantedFoot.LEFT - BodyPart.RIGHT_LOWER_LEG, BodyPart.RIGHT_FOOT -> PlantedFoot.RIGHT + BodyPart.LEFT_FOOT -> PlantedFoot.LEFT + BodyPart.RIGHT_FOOT -> PlantedFoot.RIGHT else -> PlantedFoot.NONE } @@ -116,25 +123,24 @@ object FootLocalizer { } fun getPlantedFoot(fk: ComputedSkeleton, lastPlantedFoot: PlantedFoot): PlantedFoot { - // TODO start with foot, fallback to ankles, else it's nil - val leftLowerLeg = fk[BodyPart.LEFT_LOWER_LEG] ?: return PlantedFoot.NONE - val rightLowerLeg = fk[BodyPart.RIGHT_LOWER_LEG] ?: return PlantedFoot.NONE + val leftFoot = fk[BodyPart.LEFT_FOOT] ?: return PlantedFoot.NONE + val rightFoot = fk[BodyPart.RIGHT_FOOT] ?: return PlantedFoot.NONE // If foot is locked, use that TODO should maybe not use that and just check y position instead -// if (isFootLocked(leftLowerLeg, lastPlantedFoot)) return PlantedFoot.LEFT -// if (isFootLocked(rightLowerLeg, lastPlantedFoot)) return PlantedFoot.RIGHT +// if (isFootLocked(leftFoot, lastPlantedFoot)) return PlantedFoot.LEFT +// if (isFootLocked(rightFoot, lastPlantedFoot)) return PlantedFoot.RIGHT // Else, use velocity and accel to pick the foot who moves the least - val leftVelocityAccelRatio = velocityAccelRatio(leftLowerLeg) - val rightVelocityAccelRatio = velocityAccelRatio(rightLowerLeg) + val leftVelocityAccelRatio = velocityAccelRatio(leftFoot) + val rightVelocityAccelRatio = velocityAccelRatio(rightFoot) return if (leftVelocityAccelRatio < rightVelocityAccelRatio && leftVelocityAccelRatio < MAX_FOOT_PERCENTAGE && - leftLowerLeg.acceleration.y < MAX_ACCEL_UP + leftFoot.acceleration.y < MAX_ACCEL_UP ) { PlantedFoot.LEFT } else if (rightVelocityAccelRatio < leftVelocityAccelRatio && rightVelocityAccelRatio < MAX_FOOT_PERCENTAGE && - rightLowerLeg.acceleration.y < MAX_ACCEL_UP + rightFoot.acceleration.y < MAX_ACCEL_UP ) { PlantedFoot.RIGHT } else { diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/acceleration-fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/acceleration-fallback.kt new file mode 100644 index 000000000..b5a4715e9 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/acceleration-fallback.kt @@ -0,0 +1,28 @@ +package dev.slimevr.skeleton.inputprocessors + +import dev.slimevr.skeleton.InputSkeleton +import dev.slimevr.skeleton.SkeletonInputProcessor +import dev.slimevr.skeleton.forEachBone +import dev.slimevr.skeleton.iterateBodyPartHierarchy +import io.github.axisangles.ktmath.Vector3 +import kotlin.collections.set + +/** + * Handles replacing accelerations of boneInputs that are not actively receiving any by + * falling back to their parent's acceleration. + */ +class AccelerationFallbackInputProcessor : SkeletonInputProcessor { + override fun process(mutableInputSkeleton: InputSkeleton, skeletonHeight: Float) { + mutableInputSkeleton.forEachBone { parentPart, parentBone -> + if (!parentBone.isAccelerationActive) return@forEachBone // Parent needs to be active + + val parentAccel = parentBone.acceleration + for (childPart in iterateBodyPartHierarchy(parentPart, true)) { + val childBone = mutableInputSkeleton[childPart.second] ?: continue + if (childBone.isAccelerationActive) continue // Child needs to be inactive + + mutableInputSkeleton[childPart.second] = childBone.copy(acceleration = parentAccel) + } + } + } +} diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt index 7993e602d..78fd27840 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-yaw-fallback.kt @@ -20,7 +20,6 @@ class BoneYawFallbackInputProcessor : SkeletonInputProcessor { for (childPart in iterateBodyPartHierarchy(parentPart, true)) { val childBone = mutableInputSkeleton[childPart.second] ?: continue if (childBone.isRotationActive) continue // Child needs to be inactive - if (parentYaw == childBone.rotation) continue mutableInputSkeleton[childPart.second] = childBone.copy(rotation = parentYaw) } diff --git a/server/core/src/main/java/dev/slimevr/skeleton/module.kt b/server/core/src/main/java/dev/slimevr/skeleton/module.kt index 4c1b1a255..2527a3b16 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/module.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/module.kt @@ -8,6 +8,7 @@ import dev.slimevr.skeleton.computedprocessors.VelocityComputedProcessor import dev.slimevr.skeleton.fkprocessors.FootPlantFkProcessor import dev.slimevr.skeleton.fkprocessors.LocalizerFkProcessor import dev.slimevr.skeleton.fkprocessors.ToeSnapFkProcessor +import dev.slimevr.skeleton.inputprocessors.AccelerationFallbackInputProcessor import dev.slimevr.skeleton.inputprocessors.BoneDirectLinkInputProcessor import dev.slimevr.skeleton.inputprocessors.BoneYawFallbackInputProcessor import dev.slimevr.skeleton.inputprocessors.ConstraintInputProcessor @@ -177,7 +178,7 @@ fun buildBones(boneInputs: InputSkeleton, changedParts: Set = headPart } sealed interface SkeletonActions { - data class SetBonePose(val bodyPart: BodyPart, val trackerOffset: Vector3, val expectedTps: UShort, val rotation: Quaternion, val acceleration: Vector3, val position: Vector3?) : SkeletonActions + data class SetBonePose(val bodyPart: BodyPart, val trackerOffset: Vector3, val expectedTps: UShort, val rotation: Quaternion, val acceleration: Vector3?, val position: Vector3?) : SkeletonActions data class DisableBone(val bodyPart: BodyPart) : SkeletonActions data class SetProportions(val lengths: Map) : SkeletonActions data class PauseTracking(val pause: Boolean) : SkeletonActions @@ -274,6 +275,7 @@ class Skeleton( FingerImputeInputProcessor(), ToeActiveLinkInputProcessor(), ConstraintInputProcessor(settings), + AccelerationFallbackInputProcessor(), ), fkComputedProcessors = listOf( VelocityComputedProcessor(), diff --git a/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt b/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt index 3c6dedd6d..539889625 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt @@ -7,6 +7,9 @@ import solarxr_protocol.datatypes.BodyPart fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when (action) { is SkeletonActions.SetBonePose -> { val bone = state.boneInputs[action.bodyPart] ?: return state + // Rotation always non-null and active + // Accel always non-null but not always active + // Position not always non-null and not always active state.copy( boneInputs = state.boneInputs.mutateCopy { it[action.bodyPart] = bone.copy( @@ -14,8 +17,8 @@ fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when expectedTps = bone.expectedTps, rotation = action.rotation, isRotationActive = true, - acceleration = action.acceleration, - isAccelerationActive = true, + acceleration = action.acceleration ?: Vector3.ZERO, + isAccelerationActive = action.acceleration != null, position = action.position, isPositionActive = action.position != null, ) diff --git a/server/core/src/main/java/dev/slimevr/solarxr/datafeed/datafeed.kt b/server/core/src/main/java/dev/slimevr/solarxr/datafeed/datafeed.kt index ff012d09f..4c9b8c72b 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/datafeed/datafeed.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/datafeed/datafeed.kt @@ -81,8 +81,8 @@ private fun createTracker(device: DeviceState, tracker: TrackerState, trackerMas }, tps = if (trackerMask.tps) tracker.tps else null, temp = if (trackerMask.temp && tracker.imuTemp != null) tracker.imuTemp else null, - rawAcceleration = if (trackerMask.rawAcceleration) tracker.rawAcceleration.toVec3f() else null, - linearAcceleration = if (trackerMask.linearAcceleration) tracker.acceleration.toVec3f() else null, + rawAcceleration = if (trackerMask.rawAcceleration) tracker.rawAcceleration?.toVec3f() else null, + linearAcceleration = if (trackerMask.linearAcceleration) tracker.acceleration?.toVec3f() else null, rotationReferenceAdjusted = if (trackerMask.rotationReferenceAdjusted) tracker.rotation.toQuat() else null, rotationIdentityAdjusted = if (trackerMask.rotationIdentityAdjusted) tracker.rotation.toQuat() else null, // FIXME: uses reference adjusted rawMagneticVector = if (trackerMask.rawMagneticVector && tracker.magStatus == MagnetometerStatus.ENABLED) tracker.rawMagnetometer.toVec3f() else null, diff --git a/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt b/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt index 25e0f4b97..24767599e 100644 --- a/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt @@ -108,6 +108,8 @@ class TapDetectionBasicBehaviour : TapDetectionBehaviour { .map { it.rawAcceleration to it.motion } .distinctUntilChanged() .onEach { (rawAcceleration, motionState) -> + if (rawAcceleration == null) return@onEach + // Is this tracker over threshold for false positive prevention? val isOverThreshold = if (rawAcceleration.lenSq() > ALLOWED_BODY_ACCEL_SQUARED) { trackersOverThreshold.add(trackerTapDetectionState.trackerId) 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 16f20c183..f4af657ab 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/module.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/module.kt @@ -75,8 +75,8 @@ data class TrackerState( val rawRotation: RawRotation, val rotation: CalibratedRotation, val rotationDirty: Boolean, - val rawAcceleration: RawAcceleration, - val acceleration: CalibratedAcceleration, + val rawAcceleration: RawAcceleration?, + val acceleration: CalibratedAcceleration?, val rawMagnetometer: Vector3, val position: Vector3?, val imuTemp: Float?, @@ -217,8 +217,8 @@ class Tracker( rawRotation = Quaternion.IDENTITY, rotation = Quaternion.IDENTITY, rotationDirty = false, - rawAcceleration = Vector3.ZERO, - acceleration = Vector3.ZERO, + rawAcceleration = null, + acceleration = null, rawMagnetometer = Vector3.ZERO, position = null, imuTemp = null, 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 13e53e3ef..c3835312d 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/reducer.kt @@ -36,11 +36,11 @@ fun reduce( } // Other inputs - val rawAcceleration: RawAcceleration = action.acceleration ?: state.rawAcceleration + val rawAcceleration: RawAcceleration? = action.acceleration ?: state.rawAcceleration val rawMagnetometer = action.magnetometer ?: state.rawMagnetometer val position = action.position ?: state.position - // Rotation calibration + // Rotation calibration: going from raw rotation to rotation since reset val rotation: CalibratedRotation = if (action.rotation != null) { applyFullCalibration(correctedRawRotation, state) @@ -49,10 +49,11 @@ fun reduce( state.rotation } - // Accel calibration - val acceleration: CalibratedAcceleration = + // 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(rawAcceleration, correctedRawRotation, state) + applyFullCalibration(action.acceleration, correctedRawRotation, state) } else { state.acceleration }