diff --git a/server/core/src/main/java/dev/slimevr/resets/module.kt b/server/core/src/main/java/dev/slimevr/resets/module.kt index 96b7d8847..27c0e343a 100644 --- a/server/core/src/main/java/dev/slimevr/resets/module.kt +++ b/server/core/src/main/java/dev/slimevr/resets/module.kt @@ -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) { diff --git a/server/core/src/main/java/dev/slimevr/stepmounting/behaviours.kt b/server/core/src/main/java/dev/slimevr/stepmounting/behaviours.kt index 2f71f65b5..129da8a32 100644 --- a/server/core/src/main/java/dev/slimevr/stepmounting/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/stepmounting/behaviours.kt @@ -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) = 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, diff --git a/server/core/src/main/java/dev/slimevr/stepmounting/module.kt b/server/core/src/main/java/dev/slimevr/stepmounting/module.kt index 3dde32615..20a54dd6c 100644 --- a/server/core/src/main/java/dev/slimevr/stepmounting/module.kt +++ b/server/core/src/main/java/dev/slimevr/stepmounting/module.kt @@ -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."), ) } } diff --git a/server/core/src/test/java/dev/slimevr/TestServer.kt b/server/core/src/test/java/dev/slimevr/TestServer.kt index 975eb9698..660e33d3b 100644 --- a/server/core/src/test/java/dev/slimevr/TestServer.kt +++ b/server/core/src/test/java/dev/slimevr/TestServer.kt @@ -34,7 +34,6 @@ import dev.slimevr.skeleton.buildBones import dev.slimevr.solarxr.rpc.ServerInfos import dev.slimevr.stepmounting.StepMountingManager import dev.slimevr.stepmounting.StepMountingState -import dev.slimevr.stepmounting.StepMountingStatus import dev.slimevr.tapdetection.TapDetectionManager import dev.slimevr.tracker.Motion import dev.slimevr.tracker.SessionCalibration @@ -57,6 +56,7 @@ import solarxr_protocol.datatypes.BodyPart import solarxr_protocol.datatypes.DeviceOrigin import solarxr_protocol.datatypes.TrackerStatus import solarxr_protocol.datatypes.hardware_info.ImuType +import solarxr_protocol.rpc.StepMountingStatus import solarxr_protocol.rpc.UserHeightCalibrationStatus import dev.slimevr.config.reduce as reduceConfig import dev.slimevr.heightcalibration.reduce as reduceHeightCalibration diff --git a/solarxr-protocol b/solarxr-protocol index 5f646a000..7a5e15c28 160000 --- a/solarxr-protocol +++ b/solarxr-protocol @@ -1 +1 @@ -Subproject commit 5f646a000e5b4aaac9e0e178532c4513fc6c9c0b +Subproject commit 7a5e15c28bb7a0a15b722a93077a7abf8d97a845