Step Mounting error threshold & SolarXR status

This commit is contained in:
Butterscotch! committed 2026-10-09 16:43:22 -04:00
1 parent 86cb892936
commit 6de710816e
5 files changed
+51 -45

No files matched your search

@@ -12,7 +12,6 @@ import dev.slimevr.sentry.ErrorReportingManager
import dev.slimevr.skeleton.Skeleton
import dev.slimevr.skeleton.SkeletonActions
import dev.slimevr.stepmounting.StepMountingManager
import dev.slimevr.stepmounting.StepMountingStatus
import dev.slimevr.tracker.Tracker
import dev.slimevr.tracker.TrackerActions
import dev.slimevr.tracker.behaviours.TrackerRotationRefreshBehaviour
@@ -40,11 +39,11 @@ import solarxr_protocol.rpc.ResetLifecycle
import solarxr_protocol.rpc.ResetStatusResponse
import solarxr_protocol.rpc.ResetType
import solarxr_protocol.rpc.StepMountingDetail
import solarxr_protocol.rpc.StepMountingStatus
import kotlin.collections.contains
import kotlin.collections.listOf
import kotlin.time.Duration.Companion.seconds
import kotlin.time.TimeMark
import solarxr_protocol.rpc.StepMountingStatus as RpcStepMountingStatus
data class ResetsState(
val canDoYawReset: Boolean,
@@ -190,32 +189,31 @@ class ResetsManager(
}
}
private suspend fun runStepMounting(onPhase: suspend (RpcStepMountingStatus) -> Unit): RpcStepMountingStatus? {
stepMountingManager.cancel()
private suspend fun runStepMounting(onPhase: suspend (StepMountingStatus) -> Unit): StepMountingStatus? {
stepMountingManager.start()
try {
val lastStatus = stepMountingManager.context.state
.map { it.status }
.distinctUntilChanged()
.onEach { status -> stepMountingPhase(status)?.let { onPhase(it) } }
.first { it == StepMountingStatus.DONE || stepMountingFailure(it) != null }
return stepMountingFailure(lastStatus)
.onEach(onPhase)
.first { it == StepMountingStatus.DONE || stepMountingFailure(it) }
return if (stepMountingFailure(lastStatus)) {
lastStatus
} else {
null
}
} finally {
stepMountingManager.cancel()
}
}
private fun stepMountingPhase(status: StepMountingStatus) = when (status) {
StepMountingStatus.WAITING_FOR_MOVEMENT -> RpcStepMountingStatus.WAITING_FOR_MOVEMENT
StepMountingStatus.RECORDING -> RpcStepMountingStatus.RECORDING
StepMountingStatus.PROCESSING -> RpcStepMountingStatus.PROCESSING
else -> null
}
private fun stepMountingFailure(status: StepMountingStatus) = when (status) {
StepMountingStatus.ERROR_NO_DATA -> RpcStepMountingStatus.ERROR_NO_DATA
StepMountingStatus.ERROR_TIMEOUT -> RpcStepMountingStatus.ERROR_TIMEOUT
else -> null
StepMountingStatus.ERROR_NO_DATA,
StepMountingStatus.ERROR_THRESHOLD_EXCEEDED,
StepMountingStatus.ERROR_TIMEOUT,
-> true
else -> false
}
suspend fun clearTrackersMountingReset(resetSourceName: String) {
@@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.takeWhile
import kotlinx.coroutines.withTimeoutOrNull
import solarxr_protocol.datatypes.BodyPart
import solarxr_protocol.rpc.StepMountingStatus
import kotlin.time.TimeSource
internal const val TIMEOUT_MS = 10_000L
@@ -46,18 +47,26 @@ class StepMountingBasicBehaviour : StepMountingBehaviour {
}
}
val coefficients = ButterworthCoefficients(
val COEFFICIENTS = ButterworthCoefficients(
8f,
0.02f,
)
const val startThreshold = 0.4f // in m/s^2
const val endThreshold = 0.5f // in m/s^2
const val minMovementDurationMs = 3000L
const val START_THRESHOLD = 0.4f // in m/s^2
const val END_THRESHOLD = 0.5f // in m/s^2
const val MIN_MOVEMENT_DURATION_MS = 3000L
const val ERROR_THRESHOLD = 1f
fun movementDetector(updates: Flow<TrackerSnapshot>) = flow {
val lowpass = Vector3Butterworth(coefficients)
val lowpass = Vector3Butterworth(COEFFICIENTS)
// var lastTime: ComparableTimeMark? = null
emit(0f)
updates.collect { update ->
// Dynamically control lowpass timestep
/*
val delta = lastTime?.elapsedNow()?.inFloatingSeconds ?: COEFFICIENTS.Ts
lastTime = timeSource.markNow()
lowpass.swapCoefficients(COEFFICIENTS.copy(Ts = delta))
*/
val lowpassAccel = lowpass.filter(update.acceleration)
emit((update.acceleration - lowpassAccel).len())
}
@@ -90,7 +99,7 @@ internal suspend fun runCalibrationSession(
}.stateIn(this)
// Wait for movement to start
isMoving.first { it >= startThreshold }
isMoving.first { it >= START_THRESHOLD }
dispatch(StepMountingStatus.RECORDING)
AppLogger.stepMounting.info("Movement detected, recording...")
@@ -123,8 +132,8 @@ internal suspend fun runCalibrationSession(
}
// Wait for movement to end
delay(minMovementDurationMs)
isMoving.first { it < endThreshold }
delay(MIN_MOVEMENT_DURATION_MS)
isMoving.first { it < END_THRESHOLD }
dispatch(StepMountingStatus.PROCESSING)
AppLogger.stepMounting.info("No more movement detected, processing...")
@@ -136,14 +145,25 @@ internal suspend fun runCalibrationSession(
val headOffset = headRecording.last().position - headRecording.first().position
AppLogger.stepMounting.info("${BodyPart.HEAD}: $headOffset")
trackerRecordings.filter {
val results = trackerRecordings.filter {
it.second.isNotEmpty()
}.map { (tracker, recording) ->
tracker to estimateHeadingAlign(recording, headOffset)
}.forEach { (tracker, result) ->
// TODO: Fail on high error
}.onEach { (tracker, result) ->
AppLogger.stepMounting.info("${tracker.context.state.value.bodyPart}: $result")
}
// Fail on high error
results.firstOrNull { (_, result) ->
result.errorMeters >= ERROR_THRESHOLD
}?.let { (tracker, result) ->
dispatch(StepMountingStatus.ERROR_NO_DATA)
AppLogger.stepMounting.error("Tracker assigned to ${tracker.context.state.value.bodyPart} exceeded the error threshold (${result.errorMeters}m >= ${ERROR_THRESHOLD}m).")
error("Error threshold exceeded.")
}
// Apply to trackers
results.forEach { (tracker, result) ->
tracker.context.dispatch(
TrackerActions.SetStepMounting(
result.headingAlignment,
@@ -19,24 +19,12 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import solarxr_protocol.datatypes.BodyPart
import solarxr_protocol.datatypes.TrackerStatus
import solarxr_protocol.rpc.StepMountingStatus
import kotlin.collections.emptyList
import kotlin.collections.listOf
import kotlin.collections.map
import kotlin.time.ComparableTimeMark
// TODO Put this enum in SolarXR
enum class StepMountingStatus(
val value: UByte,
) {
NONE(0.toUByte()),
WAITING_FOR_MOVEMENT(1.toUByte()),
RECORDING(2.toUByte()),
PROCESSING(3.toUByte()),
DONE(4.toUByte()),
ERROR_NO_DATA(5.toUByte()),
ERROR_TIMEOUT(6.toUByte()),
}
data class HeadsetSnapshot(
val rotation: Quaternion,
val position: Vector3,
@@ -91,7 +79,7 @@ class StepMountingManager(
positionalHead.context.state.map { s ->
HeadsetSnapshot(
rotation = s.rotation,
position = s.position ?: error("Head will always have a position here"),
position = s.position ?: error("Head will always have a position here."),
)
}
}
@@ -109,7 +97,7 @@ class StepMountingManager(
controller to controller.context.state.map { s ->
TrackerSnapshot(
rotation = s.rotation,
acceleration = s.acceleration ?: error("Trackers will always have acceleration in this case"),
acceleration = s.acceleration ?: error("Trackers will always have acceleration in this case."),
)
}
}