mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-06 08:42:32 -08:00
Fix reset processors race
This commit is contained in:
1 parent
81a1ab0089
commit
e50469d46b
8 files changed
+56
-28
No files matched your search
@@ -93,11 +93,6 @@ class ResetsManager(val context: ResetsContext, val server: VRServer, val settin
|
||||
skeleton.context.dispatch(SkeletonActions.ResetFloorLevel)
|
||||
}
|
||||
|
||||
// Reset the skeleton processors that store temporary data (smoothing, localizer, skating correction, etc.)
|
||||
delay(10) // TODO REMOVE Temporary patch to prevent old data from surviving reset at least.
|
||||
// TODO Skeleton runs in a different thread, causing a race condition. The following line needs to run with the skeleton thread locked.
|
||||
skeleton.resetProcessors(resetType)
|
||||
|
||||
// Update state and config
|
||||
context.dispatch(ResetsActions.EndReset(resetType, bodyParts, settings.context.state.value.data.resetsConfig.resetMountingFeet))
|
||||
settings.context.dispatch(
|
||||
|
||||
@@ -36,7 +36,7 @@ class ProportionsBehaviour(private val userConfig: UserConfig) : SkeletonBehavio
|
||||
.onEach { proportions ->
|
||||
if (proportions.isNotEmpty()) {
|
||||
receiver.context.dispatch(SkeletonActions.SetProportions(configToBoneValues(proportions)))
|
||||
receiver.resetProcessors(ResetType.FULL)
|
||||
receiver.context.dispatch(SkeletonActions.RequestProcessorReset(ResetType.FULL))
|
||||
}
|
||||
}
|
||||
.launchIn(receiver.context.scope)
|
||||
@@ -202,6 +202,10 @@ class ComputedSkeletonBehaviour(
|
||||
}.asCoroutineDispatcher()
|
||||
receiver.context.scope.coroutineContext[Job]?.invokeOnCompletion { dispatcher.close() }
|
||||
|
||||
val resettableProcessors = (inputProcessors + fkComputedProcessors + fkProcessors + targetProcessors + ikComputedProcessors)
|
||||
.filterIsInstance<ResettableSkeletonProcessor>()
|
||||
.distinct()
|
||||
|
||||
var nextTick = timeSource.markNow()
|
||||
val fkChangedParts = mutableSetOf<BodyPart>()
|
||||
val timings = TickTimings(hz, 10.seconds, intervalDuration)
|
||||
@@ -215,6 +219,14 @@ class ComputedSkeletonBehaviour(
|
||||
val processTime = measureTime {
|
||||
val targetState = receiver.context.state.value
|
||||
|
||||
val pendingResets = targetState.processorResets
|
||||
if (pendingResets.isNotEmpty()) {
|
||||
for (resetType in pendingResets.distinct()) {
|
||||
for (processor in resettableProcessors) processor.reset(resetType)
|
||||
}
|
||||
receiver.context.dispatch(SkeletonActions.ProcessorResetsApplied(pendingResets.size))
|
||||
}
|
||||
|
||||
val boneInputs = if (targetState.pausedProcessedBoneInputs != null) {
|
||||
// TODO improve pause tracking code, maybe using a processor
|
||||
// Use already-processed paused tracking data except for the head
|
||||
|
||||
@@ -92,6 +92,7 @@ data class SkeletonState(
|
||||
val floorLevel: Float,
|
||||
val paused: Boolean,
|
||||
val pausedProcessedBoneInputs: InputSkeleton?,
|
||||
val processorResets: List<ResetType> = emptyList(),
|
||||
)
|
||||
|
||||
val DEFAULT_BONE_INPUT = BoneInput(
|
||||
@@ -166,6 +167,8 @@ sealed interface SkeletonActions {
|
||||
data class PauseTracking(val pause: Boolean) : SkeletonActions
|
||||
data class SetPausedBoneInputs(val pausedBoneInputs: InputSkeleton) : SkeletonActions
|
||||
data object ResetFloorLevel : SkeletonActions
|
||||
data class RequestProcessorReset(val resetType: ResetType) : SkeletonActions
|
||||
data class ProcessorResetsApplied(val count: Int) : SkeletonActions
|
||||
}
|
||||
|
||||
typealias SkeletonContext = Context<SkeletonState, SkeletonActions>
|
||||
@@ -191,23 +194,17 @@ interface SkeletonTargetProcessor {
|
||||
class Skeleton(
|
||||
val context: SkeletonContext,
|
||||
val computed: MutableSharedFlow<ComputedSkeleton>,
|
||||
private val resettableSkeletonProcessors: Set<ResettableSkeletonProcessor>,
|
||||
) {
|
||||
val currentComputed: ComputedSkeleton get() = computed.replayCache.first()
|
||||
|
||||
fun startObserving() = context.observeAll(this)
|
||||
|
||||
fun resetProcessors(resetType: ResetType) {
|
||||
resettableSkeletonProcessors.forEach { it.reset(resetType) }
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val DEFAULT_HZ = 500
|
||||
|
||||
fun create(scope: CoroutineScope, ctx: Phase1ContextProvider, waiter: PreciseWaiter, hz: Int = DEFAULT_HZ): Skeleton {
|
||||
val settings = ctx.config.settings
|
||||
|
||||
val resettableSkeletonProcessors = mutableSetOf<ResettableSkeletonProcessor>()
|
||||
val behaviours = listOf(
|
||||
ProportionsBehaviour(ctx.config.userConfig),
|
||||
HeightLogBehaviour(),
|
||||
@@ -216,8 +213,8 @@ class Skeleton(
|
||||
hz = hz,
|
||||
waiter = waiter,
|
||||
inputProcessors = listOf(
|
||||
PredictionInputProcessor(settings).also { resettableSkeletonProcessors.add(it) },
|
||||
SmoothingInputProcessor(settings).also { resettableSkeletonProcessors.add(it) },
|
||||
PredictionInputProcessor(settings),
|
||||
SmoothingInputProcessor(settings),
|
||||
HeadPositionFallbackProcessor(settings),
|
||||
BoneYawFallbackInputProcessor(),
|
||||
SpineInputProcessor(settings),
|
||||
@@ -229,19 +226,19 @@ class Skeleton(
|
||||
ConstraintInputProcessor(settings),
|
||||
),
|
||||
fkComputedProcessors = listOf(
|
||||
VelocityComputedProcessor().also { resettableSkeletonProcessors.add(it) },
|
||||
VelocityComputedProcessor(),
|
||||
),
|
||||
fkProcessors = listOf(
|
||||
LocalizerFkProcessor(settings).also { resettableSkeletonProcessors.add(it) },
|
||||
LocalizerFkProcessor(settings),
|
||||
FootPlantFkProcessor(settings),
|
||||
ToeSnapFkProcessor(settings),
|
||||
),
|
||||
targetProcessors = listOf(
|
||||
FloorClipTargetProcessor(settings),
|
||||
SkatingCorrectionTargetProcessor(settings).also { resettableSkeletonProcessors.add(it) },
|
||||
SkatingCorrectionTargetProcessor(settings),
|
||||
),
|
||||
ikComputedProcessors = listOf(
|
||||
VelocityComputedProcessor().also { resettableSkeletonProcessors.add(it) },
|
||||
VelocityComputedProcessor(),
|
||||
),
|
||||
),
|
||||
)
|
||||
@@ -260,7 +257,7 @@ class Skeleton(
|
||||
)
|
||||
computed.tryEmit(buildBones(context.state.value.boneInputs))
|
||||
|
||||
return Skeleton(context, computed, resettableSkeletonProcessors)
|
||||
return Skeleton(context, computed)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -61,4 +61,7 @@ fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when
|
||||
state.copy(floorLevel = 0f)
|
||||
}
|
||||
}
|
||||
|
||||
is SkeletonActions.RequestProcessorReset -> state.copy(processorResets = state.processorResets + action.resetType)
|
||||
is SkeletonActions.ProcessorResetsApplied -> state.copy(processorResets = state.processorResets.drop(action.count))
|
||||
}
|
||||
@@ -2,11 +2,11 @@ package dev.slimevr.tracker.behaviours
|
||||
|
||||
import dev.slimevr.skeleton.SkeletonActions
|
||||
import dev.slimevr.tracker.Tracker
|
||||
import dev.slimevr.tracker.TrackerActions
|
||||
import dev.slimevr.tracker.TrackerBehaviour
|
||||
import dev.slimevr.util.isActive
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.distinctUntilChangedBy
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
@@ -39,16 +39,25 @@ class TrackerToSkeletonBehaviour : TrackerBehaviour {
|
||||
.filter { it.bodyPart != null && it.status.isActive() }
|
||||
|
||||
activeState
|
||||
.distinctUntilChangedBy { it.rotation to it.position }
|
||||
.distinctUntilChanged { old, new -> old.rotation == new.rotation && old.position == new.position && old.pendingSkeletonResets == new.pendingSkeletonResets }
|
||||
.onEach { trackerState ->
|
||||
trackerState.bodyPart?.let { bodyPart ->
|
||||
receiver.appContext.skeleton.context.dispatchAll(
|
||||
listOfNotNull(
|
||||
SkeletonActions.SetBoneRotation(bodyPart, trackerState.rotation),
|
||||
SkeletonActions.SetBoneAcceleration(bodyPart, trackerState.acceleration),
|
||||
if (trackerState.position != null) SkeletonActions.SetBonePosition(bodyPart, trackerState.position) else null,
|
||||
),
|
||||
val pendingResets = trackerState.pendingSkeletonResets
|
||||
val poseActions = listOfNotNull(
|
||||
SkeletonActions.SetBoneRotation(bodyPart, trackerState.rotation),
|
||||
SkeletonActions.SetBoneAcceleration(bodyPart, trackerState.acceleration),
|
||||
if (trackerState.position != null) SkeletonActions.SetBonePosition(bodyPart, trackerState.position) else null,
|
||||
)
|
||||
receiver.appContext.skeleton.context.dispatchAll(
|
||||
if (pendingResets.isEmpty()) {
|
||||
poseActions
|
||||
} else {
|
||||
poseActions + pendingResets.map { SkeletonActions.RequestProcessorReset(it) }
|
||||
},
|
||||
)
|
||||
if (pendingResets.isNotEmpty()) {
|
||||
receiver.context.dispatch(TrackerActions.ClearPendingSkeletonResets(pendingResets.size))
|
||||
}
|
||||
lastBodyPartSent = trackerState.bodyPart
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import solarxr_protocol.datatypes.MountingMethod
|
||||
import solarxr_protocol.datatypes.TrackerStatus
|
||||
import solarxr_protocol.datatypes.hardware_info.ImuType
|
||||
import solarxr_protocol.datatypes.hardware_info.TrackerDataType
|
||||
import solarxr_protocol.rpc.ResetType
|
||||
import kotlin.time.Duration
|
||||
|
||||
// A tracker is initialized as ROTATING to prevent TapDetection and Stay Aligned from running immediately.
|
||||
@@ -85,6 +86,7 @@ data class TrackerState(
|
||||
val motion: Motion,
|
||||
val yawResetSmoothing: YawResetSmoothing?,
|
||||
val stayAlignedData: StayAlignedData,
|
||||
val pendingSkeletonResets: List<ResetType> = emptyList(),
|
||||
)
|
||||
|
||||
fun List<TrackerState>.getFirstActiveFor(bodyPart: BodyPart): TrackerState? = this.firstOrNull { it.bodyPart == bodyPart && it.status.isActive() }
|
||||
@@ -104,6 +106,7 @@ sealed interface TrackerActions {
|
||||
data class TickYawResetSmoothing(val heading: HeadingCorrection, val done: Boolean) : TrackerActions
|
||||
data class PoseMountingReset(val referenceRotation: Quaternion, val yawOffset: Float) : TrackerActions
|
||||
data object ClearMountingReset : TrackerActions
|
||||
data class ClearPendingSkeletonResets(val count: Int) : TrackerActions
|
||||
data class SetMotion(val motion: Motion) : TrackerActions
|
||||
data class SetYawCorrection(val yawCorrection: Angle) : TrackerActions
|
||||
data class SetStayAlignedEnabled(val enabled: Boolean) : TrackerActions
|
||||
|
||||
@@ -3,6 +3,7 @@ package dev.slimevr.tracker
|
||||
import io.github.axisangles.ktmath.Quaternion
|
||||
import solarxr_protocol.datatypes.BodyPart
|
||||
import solarxr_protocol.datatypes.MountingMethod
|
||||
import solarxr_protocol.rpc.ResetType
|
||||
import kotlin.time.Duration
|
||||
|
||||
fun reduce(
|
||||
@@ -131,6 +132,7 @@ fun reduce(
|
||||
lastReference = action.referenceRotation,
|
||||
// Full reset snaps: cancel any in-progress yaw smoothing.
|
||||
yawResetSmoothing = null,
|
||||
pendingSkeletonResets = state.pendingSkeletonResets + ResetType.FULL,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -153,6 +155,7 @@ fun reduce(
|
||||
to = newHeading,
|
||||
duration = action.smoothTime,
|
||||
),
|
||||
pendingSkeletonResets = state.pendingSkeletonResets + ResetType.YAW,
|
||||
)
|
||||
} else {
|
||||
// Snap: apply the new heading immediately (default, no smoothing configured).
|
||||
@@ -160,6 +163,7 @@ fun reduce(
|
||||
sessionCalibration = cal.copy(headingCorrection = newHeading),
|
||||
lastReference = action.referenceRotation,
|
||||
yawResetSmoothing = null,
|
||||
pendingSkeletonResets = state.pendingSkeletonResets + ResetType.YAW,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -181,6 +185,7 @@ fun reduce(
|
||||
sessionCalibration = state.sessionCalibration.copy(headingAlignment = headingAlignment),
|
||||
lastReference = action.referenceRotation,
|
||||
lastMountingMethod = MountingMethod.POSE,
|
||||
pendingSkeletonResets = state.pendingSkeletonResets + ResetType.POSE_MOUNTING,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -191,6 +196,10 @@ fun reduce(
|
||||
)
|
||||
}
|
||||
|
||||
is TrackerActions.ClearPendingSkeletonResets -> {
|
||||
state.copy(pendingSkeletonResets = state.pendingSkeletonResets.drop(action.count))
|
||||
}
|
||||
|
||||
is TrackerActions.SetMotion -> {
|
||||
state.copy(
|
||||
motion = action.motion,
|
||||
|
||||
Reference in new issue
Block a user