mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-12-06 08:42:32 -08:00
Redesign of stay align settings
This commit is contained in:
1 parent
36115fbb1a
commit
2909187d00
48 files changed
+2241
-1445
No files matched your search
@@ -162,31 +162,10 @@ data class VMCConfig(
|
||||
val vrmJson: String? = null,
|
||||
)
|
||||
|
||||
// Used in StayAlignedConfig
|
||||
@Serializable
|
||||
data class StayAlignedRelaxedPoseConfig(
|
||||
/** Whether Stay Aligned should adjust the tracker yaws when the player is in this pose. */
|
||||
var enabled: Boolean = false,
|
||||
/** Angle between the upper leg yaw and the centre yaw. */
|
||||
var upperLegAngleInDeg: Float = 0.0f,
|
||||
/** Angle between the lower leg yaw and the centre yaw. */
|
||||
var lowerLegAngleInDeg: Float = 0.0f,
|
||||
/** Angle between the foot and the centre yaw. */
|
||||
var footAngleInDeg: Float = 0.0f,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class StayAlignedConfig(
|
||||
/** Global enable toggle for all poses */
|
||||
var enabled: Boolean = false,
|
||||
/** Standing relaxed pose */
|
||||
val standingRelaxedPose: StayAlignedRelaxedPoseConfig = StayAlignedRelaxedPoseConfig(),
|
||||
/** Sitting relaxed pose */
|
||||
val sittingRelaxedPose: StayAlignedRelaxedPoseConfig = StayAlignedRelaxedPoseConfig(),
|
||||
/** Flat relaxed pose */
|
||||
val flatRelaxedPose: StayAlignedRelaxedPoseConfig = StayAlignedRelaxedPoseConfig(),
|
||||
/** Whether setup has been completed */
|
||||
var setupComplete: Boolean = false,
|
||||
/** Whether Stay Aligned runs. The relaxed poses it uses are in the user config. */
|
||||
val enabled: Boolean = false,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
|
||||
@@ -11,14 +11,32 @@ import kotlinx.serialization.json.decodeFromJsonElement
|
||||
import kotlinx.serialization.json.intOrNull
|
||||
import kotlinx.serialization.json.jsonObject
|
||||
import kotlinx.serialization.json.jsonPrimitive
|
||||
import solarxr_protocol.rpc.StayAlignedRelaxedPose
|
||||
|
||||
private const val USER_CONFIG_VERSION = 1
|
||||
private const val DEFAULT_USER_HEIGHT = 1.6f
|
||||
|
||||
/** A relaxed pose of the user, used by Stay Aligned to correct moving trackers. */
|
||||
@Serializable
|
||||
data class StayAlignedPoseConfig(
|
||||
/** The user recorded this pose. */
|
||||
val recorded: Boolean = false,
|
||||
/** Whether Stay Aligned corrects moving trackers with this pose. */
|
||||
val enabled: Boolean = false,
|
||||
/** Angle between the upper leg yaw and the centre yaw. */
|
||||
val upperLegAngleInDeg: Float = 0.0f,
|
||||
/** Angle between the lower leg yaw and the centre yaw. */
|
||||
val lowerLegAngleInDeg: Float = 0.0f,
|
||||
/** Angle between the foot yaw and the centre yaw. */
|
||||
val footAngleInDeg: Float = 0.0f,
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class UserConfigData(
|
||||
val userHeight: Float = DEFAULT_USER_HEIGHT,
|
||||
val proportions: Map<String, Float> = computeAllDefaultProportionsByBone(DEFAULT_USER_HEIGHT),
|
||||
/** Only the poses that differ from their default are stored. */
|
||||
val stayAlignedPoses: Map<StayAlignedRelaxedPose, StayAlignedPoseConfig> = emptyMap(),
|
||||
val version: Int = USER_CONFIG_VERSION,
|
||||
)
|
||||
|
||||
|
||||
@@ -9,8 +9,8 @@ import dev.slimevr.logging.AppLogger
|
||||
import dev.slimevr.util.stripIpAddressPort
|
||||
import io.ktor.network.selector.SelectorManager
|
||||
import io.ktor.network.sockets.InetSocketAddress
|
||||
import io.ktor.network.sockets.toJavaAddress
|
||||
import io.ktor.network.sockets.aSocket
|
||||
import io.ktor.network.sockets.toJavaAddress
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.currentCoroutineContext
|
||||
import kotlinx.coroutines.isActive
|
||||
|
||||
@@ -42,6 +42,7 @@ object AppLogger {
|
||||
|
||||
val coroutines = noCoLogger("Coroutines")
|
||||
val console = noCoLogger("Console")
|
||||
|
||||
// For the UDP receive thread, which blocks on the socket and handles each datagram inline. A
|
||||
// suspend logger would need a coroutine there, and resuming one per packet costs more than the
|
||||
// packet's own work. Same name as udp logger, so its lines read as the rest of the UDP logs
|
||||
|
||||
@@ -199,8 +199,7 @@ class ComputedSkeletonBehaviour(
|
||||
*/
|
||||
private val idleIntervalDuration = 100.milliseconds
|
||||
|
||||
private fun hasActiveInput(boneInputs: InputSkeleton): Boolean =
|
||||
boneInputs.values.any { it.isRotationActive || it.isAccelerationActive || it.isPositionActive }
|
||||
private fun hasActiveInput(boneInputs: InputSkeleton): Boolean = boneInputs.values.any { it.isRotationActive || it.isAccelerationActive || it.isPositionActive }
|
||||
|
||||
/** Shortest gap between two tick starts before the later one is pushed to the next slot */
|
||||
private val minimumGap = intervalDuration * 0.75
|
||||
|
||||
@@ -222,7 +222,7 @@ class SolarXRBridge(
|
||||
add(ServerInfosBehaviour(appContext.serverInfos))
|
||||
add(KeybindsBehaviour(appContext.config.settings, appContext.keybindManager))
|
||||
add(SessionCalibrationBehaviour(appContext.resetsManager))
|
||||
add(StayAlignedBehaviour(appContext.config.settings, appContext.server))
|
||||
add(StayAlignedBehaviour(appContext.config.settings, appContext.config.userConfig, appContext.server, appContext.trackingChecklist))
|
||||
}
|
||||
|
||||
fun create(
|
||||
|
||||
@@ -2,6 +2,7 @@ package dev.slimevr.solarxr.rpc
|
||||
|
||||
import dev.slimevr.config.GlobalConfigState
|
||||
import dev.slimevr.config.SettingsConfigState
|
||||
import dev.slimevr.config.UserConfigData
|
||||
import dev.slimevr.solarxr.SolarXRBridge
|
||||
import dev.slimevr.solarxr.SolarXRBridgeBehaviour
|
||||
import solarxr_protocol.rpc.BoneRoutingSettingsResponse
|
||||
@@ -14,11 +15,12 @@ import solarxr_protocol.rpc.SettingsDefaultsResponse
|
||||
// answers every domain at once.
|
||||
private fun buildSettingsDefaultsResponse(): SettingsDefaultsResponse {
|
||||
val settings = SettingsConfigState()
|
||||
val user = UserConfigData()
|
||||
val global = GlobalConfigState()
|
||||
return SettingsDefaultsResponse(
|
||||
skeleton = buildSkeletonSettingsResponse(settings.skeletonConfig),
|
||||
resets = buildResetsSettingsResponse(settings.resetsConfig),
|
||||
stayAligned = buildStayAlignedSettingsResponse(settings.stayAlignedConfig),
|
||||
stayAligned = buildStayAlignedSettingsResponse(settings.stayAlignedConfig, user),
|
||||
tapDetection = buildTapDetectionSettingsResponse(settings.tapDetectionConfig),
|
||||
timeout = buildTimeoutSettingsResponse(settings.trackersConfig),
|
||||
hid = buildHidSettingsResponse(settings.hidConfig),
|
||||
|
||||
@@ -4,131 +4,292 @@ import dev.slimevr.VRServer
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.config.SettingsActions
|
||||
import dev.slimevr.config.StayAlignedConfig
|
||||
import dev.slimevr.config.StayAlignedRelaxedPoseConfig
|
||||
import dev.slimevr.config.UserConfig
|
||||
import dev.slimevr.config.UserConfigActions
|
||||
import dev.slimevr.config.UserConfigData
|
||||
import dev.slimevr.logging.AppLogger
|
||||
import dev.slimevr.solarxr.SolarXRBridge
|
||||
import dev.slimevr.solarxr.SolarXRBridgeBehaviour
|
||||
import dev.slimevr.tracker.stayaligned.poses.RelaxedPose
|
||||
import dev.slimevr.tracker.TrackerState
|
||||
import dev.slimevr.tracker.stayaligned.PoseRecordingCheck
|
||||
import dev.slimevr.tracker.stayaligned.StayAlignedDefaults
|
||||
import dev.slimevr.tracker.stayaligned.checkPoseRecording
|
||||
import dev.slimevr.tracker.stayaligned.missingRequiredParts
|
||||
import dev.slimevr.tracker.stayaligned.poses.PlayerPose
|
||||
import dev.slimevr.tracker.stayaligned.poses.STAY_ALIGNED_POSES
|
||||
import dev.slimevr.tracker.stayaligned.poses.hasStayAlignedPoseDefault
|
||||
import dev.slimevr.tracker.stayaligned.poses.stayAlignedPoseConfig
|
||||
import dev.slimevr.tracker.stayaligned.recordedPoseConfig
|
||||
import dev.slimevr.trackingchecklist.TrackingChecklist
|
||||
import dev.slimevr.util.allContextStates
|
||||
import dev.slimevr.util.isActive
|
||||
import kotlinx.coroutines.FlowPreview
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.drop
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import solarxr_protocol.rpc.ChangeStayAlignedEnabledRequest
|
||||
import kotlinx.coroutines.flow.sample
|
||||
import kotlinx.coroutines.launch
|
||||
import solarxr_protocol.rpc.CancelStayAlignedPoseRecordingRequest
|
||||
import solarxr_protocol.rpc.ChangeStayAlignedSettingsRequest
|
||||
import solarxr_protocol.rpc.DetectStayAlignedRelaxedPoseRequest
|
||||
import solarxr_protocol.rpc.ResetStayAlignedRelaxedPoseRequest
|
||||
import solarxr_protocol.rpc.StartStayAlignedPoseRecordingRequest
|
||||
import solarxr_protocol.rpc.StartStayAlignedStatusRequest
|
||||
import solarxr_protocol.rpc.StayAlignedDetectedPoseState
|
||||
import solarxr_protocol.rpc.StayAlignedPose
|
||||
import solarxr_protocol.rpc.StayAlignedPoseCountdown
|
||||
import solarxr_protocol.rpc.StayAlignedPoseRecordingDetail
|
||||
import solarxr_protocol.rpc.StayAlignedPoseRecordingFailure
|
||||
import solarxr_protocol.rpc.StayAlignedPoseRecordingLifecycle
|
||||
import solarxr_protocol.rpc.StayAlignedPoseRecordingStatusResponse
|
||||
import solarxr_protocol.rpc.StayAlignedPoseState
|
||||
import solarxr_protocol.rpc.StayAlignedPoseWaiting
|
||||
import solarxr_protocol.rpc.StayAlignedRecordingTracker
|
||||
import solarxr_protocol.rpc.StayAlignedRelaxedPose
|
||||
import solarxr_protocol.rpc.StayAlignedSettingsRequest
|
||||
import solarxr_protocol.rpc.StayAlignedSettingsResponse
|
||||
import solarxr_protocol.rpc.StayAlignedStatusResponse
|
||||
import solarxr_protocol.rpc.StayAlignedTrackerCorrection
|
||||
import solarxr_protocol.rpc.StopStayAlignedStatusRequest
|
||||
import solarxr_protocol.rpc.TrackingChecklistStepId
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.round
|
||||
import kotlin.time.Duration.Companion.milliseconds
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
internal fun buildStayAlignedSettingsResponse(config: StayAlignedConfig) = StayAlignedSettingsResponse(
|
||||
internal fun buildStayAlignedSettingsResponse(config: StayAlignedConfig, userConfig: UserConfigData) = StayAlignedSettingsResponse(
|
||||
enabled = config.enabled,
|
||||
standingEnabled = config.standingRelaxedPose.enabled,
|
||||
standingUpperLegAngle = config.standingRelaxedPose.upperLegAngleInDeg,
|
||||
standingLowerLegAngle = config.standingRelaxedPose.lowerLegAngleInDeg,
|
||||
standingFootAngle = config.standingRelaxedPose.footAngleInDeg,
|
||||
sittingEnabled = config.sittingRelaxedPose.enabled,
|
||||
sittingUpperLegAngle = config.sittingRelaxedPose.upperLegAngleInDeg,
|
||||
sittingLowerLegAngle = config.sittingRelaxedPose.lowerLegAngleInDeg,
|
||||
sittingFootAngle = config.sittingRelaxedPose.footAngleInDeg,
|
||||
flatEnabled = config.flatRelaxedPose.enabled,
|
||||
flatUpperLegAngle = config.flatRelaxedPose.upperLegAngleInDeg,
|
||||
flatLowerLegAngle = config.flatRelaxedPose.lowerLegAngleInDeg,
|
||||
flatFootAngle = config.flatRelaxedPose.footAngleInDeg,
|
||||
setupComplete = config.setupComplete,
|
||||
poses = STAY_ALIGNED_POSES.map { pose ->
|
||||
val poseConfig = stayAlignedPoseConfig(userConfig, pose)
|
||||
StayAlignedPose(
|
||||
pose = pose,
|
||||
recorded = poseConfig.recorded,
|
||||
hasDefault = hasStayAlignedPoseDefault(pose),
|
||||
enabled = poseConfig.enabled,
|
||||
upperLegAngle = poseConfig.upperLegAngleInDeg,
|
||||
lowerLegAngle = poseConfig.lowerLegAngleInDeg,
|
||||
footAngle = poseConfig.footAngleInDeg,
|
||||
)
|
||||
},
|
||||
)
|
||||
|
||||
internal fun buildStayAlignedStatusResponse(enabled: Boolean, trackerStates: List<TrackerState>, userConfig: UserConfigData) = StayAlignedStatusResponse(
|
||||
enabled = enabled,
|
||||
detectedPose = detectedPose(PlayerPose.of(trackerStates).relaxedPose, userConfig),
|
||||
missingParts = missingRequiredParts(trackerStates),
|
||||
largestCorrection = if (enabled) largestCorrection(trackerStates) else null,
|
||||
)
|
||||
|
||||
private fun largestCorrection(trackerStates: List<TrackerState>): StayAlignedTrackerCorrection? {
|
||||
val corrections = trackerStates.mapNotNull { state ->
|
||||
// A tracker that is not connected keeps its correction but nothing uses it
|
||||
val bodyPart = state.bodyPart?.takeIf { state.status.isActive() } ?: return@mapNotNull null
|
||||
StayAlignedTrackerCorrection(bodyPart = bodyPart, degrees = round(abs(state.stayAlignedData.yawCorrection.toDeg()) * 10f) / 10f)
|
||||
}
|
||||
return corrections.maxByOrNull { it.degrees }?.takeIf { it.degrees > 0f }
|
||||
}
|
||||
|
||||
private fun detectedPose(pose: StayAlignedRelaxedPose, userConfig: UserConfigData): StayAlignedDetectedPoseState? {
|
||||
if (pose == StayAlignedRelaxedPose.UNKNOWN) return null
|
||||
val config = stayAlignedPoseConfig(userConfig, pose)
|
||||
val state = when {
|
||||
config.enabled -> StayAlignedPoseState.ENABLED
|
||||
!config.recorded && !hasStayAlignedPoseDefault(pose) -> StayAlignedPoseState.NOT_RECORDED
|
||||
else -> StayAlignedPoseState.DISABLED
|
||||
}
|
||||
return StayAlignedDetectedPoseState(pose = pose, state = state)
|
||||
}
|
||||
|
||||
class StayAlignedBehaviour(
|
||||
private val settings: Settings,
|
||||
private val userConfig: UserConfig,
|
||||
private val server: VRServer,
|
||||
private val trackingChecklist: TrackingChecklist,
|
||||
) : SolarXRBridgeBehaviour {
|
||||
override fun observe(receiver: SolarXRBridge) {
|
||||
// Send config
|
||||
observeSettings(receiver)
|
||||
observeStatus(receiver)
|
||||
observeRecording(receiver)
|
||||
}
|
||||
|
||||
private suspend fun sendConfig(receiver: SolarXRBridge) {
|
||||
receiver.sendRpc(buildStayAlignedSettingsResponse(settings.context.state.value.data.stayAlignedConfig, userConfig.context.state.value.data))
|
||||
}
|
||||
|
||||
private fun observeSettings(receiver: SolarXRBridge) {
|
||||
receiver.rpcDispatcher.on<StayAlignedSettingsRequest> {
|
||||
sendConfig(receiver, settings)
|
||||
sendConfig(receiver)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
// Push config changes, including profile switches
|
||||
settings.context.state
|
||||
.map { buildStayAlignedSettingsResponse(it.data.stayAlignedConfig) }
|
||||
combine(
|
||||
settings.context.state.map { it.data.stayAlignedConfig }.distinctUntilChanged(),
|
||||
userConfig.context.state.map { it.data }.distinctUntilChanged(),
|
||||
::buildStayAlignedSettingsResponse,
|
||||
)
|
||||
.distinctUntilChanged()
|
||||
.drop(1)
|
||||
.onEach(receiver::sendRpc)
|
||||
.launchIn(receiver.context.scope)
|
||||
|
||||
// Receive config
|
||||
receiver.rpcDispatcher.on<ChangeStayAlignedSettingsRequest> { req ->
|
||||
settings.context.dispatch(
|
||||
SettingsActions.Update {
|
||||
copy(
|
||||
stayAlignedConfig = StayAlignedConfig(
|
||||
setupComplete = if (req.enabled) true else stayAlignedConfig.setupComplete,
|
||||
enabled = req.enabled,
|
||||
standingRelaxedPose = stayAlignedConfig.standingRelaxedPose.copy(
|
||||
enabled = req.standingEnabled,
|
||||
),
|
||||
sittingRelaxedPose = stayAlignedConfig.sittingRelaxedPose.copy(
|
||||
enabled = req.sittingEnabled,
|
||||
),
|
||||
flatRelaxedPose = stayAlignedConfig.flatRelaxedPose.copy(
|
||||
enabled = req.flatEnabled,
|
||||
),
|
||||
),
|
||||
)
|
||||
settings.context.dispatch(SettingsActions.Update { copy(stayAlignedConfig = StayAlignedConfig(enabled = req.enabled)) })
|
||||
userConfig.context.dispatch(
|
||||
UserConfigActions.Update {
|
||||
// Only the poses that change are stored, so the others keep following their default.
|
||||
// A pose can only be turned on once it is recorded, or when it has a default
|
||||
val toggled = req.poseToggles.orEmpty()
|
||||
.filter { stayAlignedPoseConfig(this, it.pose).enabled != it.enabled }
|
||||
.filter { !it.enabled || stayAlignedPoseConfig(this, it.pose).recorded || hasStayAlignedPoseDefault(it.pose) }
|
||||
.associate { it.pose to stayAlignedPoseConfig(this, it.pose).copy(enabled = it.enabled) }
|
||||
copy(stayAlignedPoses = stayAlignedPoses + toggled)
|
||||
},
|
||||
)
|
||||
sendConfig(receiver, settings)
|
||||
sendConfig(receiver)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
// Receive enabled
|
||||
receiver.rpcDispatcher.on<ChangeStayAlignedEnabledRequest> { req ->
|
||||
settings.context.dispatch(
|
||||
SettingsActions.Update {
|
||||
copy(
|
||||
stayAlignedConfig = stayAlignedConfig.copy(
|
||||
setupComplete = if (req.enabled) true else stayAlignedConfig.setupComplete,
|
||||
enabled = req.enabled,
|
||||
),
|
||||
)
|
||||
},
|
||||
)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
// Detect pose
|
||||
receiver.rpcDispatcher.on<DetectStayAlignedRelaxedPoseRequest> { req ->
|
||||
val pose = req.pose
|
||||
val trackerStates = server.context.state.value.trackers.values.map { it.context.state.value }
|
||||
val relaxedPose = RelaxedPose.fromTrackers(trackerStates)
|
||||
updatePoseInConfig(pose, StayAlignedRelaxedPoseConfig(true, relaxedPose.upperLeg.toDeg(), relaxedPose.lowerLeg.toDeg(), relaxedPose.foot.toDeg()))
|
||||
AppLogger.stayAligned.info("Set relaxed pose $pose with $relaxedPose")
|
||||
sendConfig(receiver, settings)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
// Reset pose
|
||||
receiver.rpcDispatcher.on<ResetStayAlignedRelaxedPoseRequest> { req ->
|
||||
val pose = req.pose
|
||||
updatePoseInConfig(pose, StayAlignedRelaxedPoseConfig(false, 0f, 0f, 0f))
|
||||
AppLogger.stayAligned.info("Reset relaxed pose $pose")
|
||||
sendConfig(receiver, settings)
|
||||
if (req.pose == StayAlignedRelaxedPose.UNKNOWN) return@on
|
||||
userConfig.context.dispatch(UserConfigActions.Update { copy(stayAlignedPoses = stayAlignedPoses - req.pose) })
|
||||
AppLogger.stayAligned.info("Reset relaxed pose ${req.pose}")
|
||||
sendConfig(receiver)
|
||||
}.launchIn(receiver.context.scope)
|
||||
}
|
||||
|
||||
private suspend fun sendConfig(receiver: SolarXRBridge, settings: Settings) {
|
||||
receiver.sendRpc(buildStayAlignedSettingsResponse(settings.context.state.value.data.stayAlignedConfig))
|
||||
@OptIn(FlowPreview::class)
|
||||
private fun observeStatus(receiver: SolarXRBridge) {
|
||||
var statusJob: Job? = null
|
||||
|
||||
receiver.rpcDispatcher.on<StartStayAlignedStatusRequest> {
|
||||
statusJob?.cancel()
|
||||
statusJob = combine(
|
||||
settings.context.state.map { it.data.stayAlignedConfig.enabled }.distinctUntilChanged(),
|
||||
userConfig.context.state.map { it.data }.distinctUntilChanged(),
|
||||
allContextStates(server.context.state, { state -> state.trackers.values }) { tracker ->
|
||||
tracker.context.state.sample(STATUS_SAMPLE_INTERVAL)
|
||||
},
|
||||
) { enabled, userConfigData, trackers -> buildStayAlignedStatusResponse(enabled, trackers, userConfigData) }
|
||||
.distinctUntilChanged()
|
||||
.onEach(receiver::sendRpc)
|
||||
.launchIn(receiver.context.scope)
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
receiver.rpcDispatcher.on<StopStayAlignedStatusRequest> {
|
||||
statusJob?.cancel()
|
||||
statusJob = null
|
||||
}.launchIn(receiver.context.scope)
|
||||
}
|
||||
|
||||
private fun updatePoseInConfig(pose: StayAlignedRelaxedPose, poseConfig: StayAlignedRelaxedPoseConfig) {
|
||||
settings.context.dispatch(
|
||||
SettingsActions.Update {
|
||||
copy(
|
||||
stayAlignedConfig = when (pose) {
|
||||
StayAlignedRelaxedPose.STANDING -> stayAlignedConfig.copy(standingRelaxedPose = poseConfig)
|
||||
StayAlignedRelaxedPose.SITTING -> stayAlignedConfig.copy(sittingRelaxedPose = poseConfig)
|
||||
StayAlignedRelaxedPose.FLAT -> stayAlignedConfig.copy(flatRelaxedPose = poseConfig)
|
||||
},
|
||||
)
|
||||
},
|
||||
private fun currentTrackerStates(): List<TrackerState> = server.context.state.value.trackers.values.map { it.context.state.value }
|
||||
|
||||
private fun observeRecording(receiver: SolarXRBridge) {
|
||||
var recordingJob: Job? = null
|
||||
var recordingPose: StayAlignedRelaxedPose? = null
|
||||
|
||||
receiver.rpcDispatcher.on<StartStayAlignedPoseRecordingRequest> { req ->
|
||||
if (req.pose == StayAlignedRelaxedPose.UNKNOWN) return@on
|
||||
recordingJob?.cancel()
|
||||
recordingPose = req.pose
|
||||
recordingJob = receiver.context.scope.launch { recordPose(receiver, req.pose) }
|
||||
}.launchIn(receiver.context.scope)
|
||||
|
||||
receiver.rpcDispatcher.on<CancelStayAlignedPoseRecordingRequest> {
|
||||
val job = recordingJob ?: return@on
|
||||
val pose = recordingPose ?: return@on
|
||||
if (!job.isActive) return@on
|
||||
job.cancel()
|
||||
receiver.sendRpc(recordingStatus(pose, StayAlignedPoseRecordingLifecycle.CANCELED, null))
|
||||
}.launchIn(receiver.context.scope)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives the user time to get into the pose, then waits until every tracker that is read is still and
|
||||
* the position matches the pose, and saves the pose. Gives up when that does not happen in time.
|
||||
*/
|
||||
private suspend fun recordPose(receiver: SolarXRBridge, pose: StayAlignedRelaxedPose) {
|
||||
val countdown = StayAlignedDefaults.POSE_RECORDING_COUNTDOWN_SECONDS
|
||||
val timeout = StayAlignedDefaults.POSE_RECORDING_TIMEOUT_SECONDS
|
||||
|
||||
if (missingRequiredParts(currentTrackerStates()).isNotEmpty()) {
|
||||
AppLogger.stayAligned.info("Not recording relaxed pose $pose: trackers are missing")
|
||||
receiver.sendRpc(recordingStatus(pose, StayAlignedPoseRecordingLifecycle.FAILED, null, StayAlignedPoseRecordingFailure.MISSING_TRACKERS))
|
||||
return
|
||||
}
|
||||
|
||||
// A pose measured on trackers that are not reset or mounted would be saved wrong
|
||||
val steps = trackingChecklist.context.state.value.steps
|
||||
if (CALIBRATION_STEPS.any { id -> steps[id]?.let { it.enabled && !it.valid } == true }) {
|
||||
AppLogger.stayAligned.info("Not recording relaxed pose $pose: trackers are not calibrated")
|
||||
receiver.sendRpc(recordingStatus(pose, StayAlignedPoseRecordingLifecycle.FAILED, null, StayAlignedPoseRecordingFailure.NOT_CALIBRATED))
|
||||
return
|
||||
}
|
||||
|
||||
receiver.sendRpc(recordingStatus(pose, StayAlignedPoseRecordingLifecycle.RUNNING, StayAlignedPoseCountdown(0, countdown)))
|
||||
repeat(countdown) { index ->
|
||||
delay(1.seconds)
|
||||
if (index != countdown - 1) {
|
||||
receiver.sendRpc(recordingStatus(pose, StayAlignedPoseRecordingLifecycle.RUNNING, StayAlignedPoseCountdown(index + 1, countdown)))
|
||||
}
|
||||
}
|
||||
|
||||
// The GUI shows the checks and the seconds waited, so both are sent whenever one changes
|
||||
val checks = allContextStates(server.context.state, { state -> state.trackers.values }) { tracker -> tracker.context.state }
|
||||
.map { trackers -> checkPoseRecording(trackers, pose) }
|
||||
.distinctUntilChanged()
|
||||
val waited = flow {
|
||||
repeat(timeout + 1) { second ->
|
||||
emit(second)
|
||||
delay(1.seconds)
|
||||
}
|
||||
}
|
||||
val (check, seconds) = combine(checks, waited, ::Pair)
|
||||
.onEach { (check, seconds) -> receiver.sendRpc(recordingStatus(pose, StayAlignedPoseRecordingLifecycle.RUNNING, waitingDetail(seconds, timeout, check))) }
|
||||
.first { (check, seconds) -> check.failure == StayAlignedPoseRecordingFailure.NONE || seconds == timeout }
|
||||
|
||||
if (check.failure != StayAlignedPoseRecordingFailure.NONE) {
|
||||
AppLogger.stayAligned.info("Relaxed pose $pose not recorded: ${check.failure}")
|
||||
receiver.sendRpc(recordingStatus(pose, StayAlignedPoseRecordingLifecycle.FAILED, waitingDetail(seconds, timeout, check), check.failure))
|
||||
return
|
||||
}
|
||||
|
||||
// The trackers may have changed since the check that passed, so what is saved is measured now
|
||||
val trackers = currentTrackerStates()
|
||||
if (missingRequiredParts(trackers).isNotEmpty()) {
|
||||
receiver.sendRpc(recordingStatus(pose, StayAlignedPoseRecordingLifecycle.FAILED, waitingDetail(seconds, timeout, check), StayAlignedPoseRecordingFailure.MISSING_TRACKERS))
|
||||
return
|
||||
}
|
||||
val recorded = recordedPoseConfig(trackers)
|
||||
userConfig.context.dispatch(UserConfigActions.Update { copy(stayAlignedPoses = stayAlignedPoses + (pose to recorded)) })
|
||||
AppLogger.stayAligned.info("Recorded relaxed pose $pose: upperLeg=${recorded.upperLegAngleInDeg} lowerLeg=${recorded.lowerLegAngleInDeg} foot=${recorded.footAngleInDeg}")
|
||||
receiver.sendRpc(recordingStatus(pose, StayAlignedPoseRecordingLifecycle.DONE, waitingDetail(seconds, timeout, check)))
|
||||
}
|
||||
|
||||
private fun waitingDetail(progressSeconds: Int, durationSeconds: Int, check: PoseRecordingCheck) = StayAlignedPoseWaiting(
|
||||
progress = progressSeconds,
|
||||
duration = durationSeconds,
|
||||
detectedPose = check.detectedPose,
|
||||
trackers = check.parts.map { (part, state) -> StayAlignedRecordingTracker(part = part, state = state) },
|
||||
)
|
||||
|
||||
private fun recordingStatus(
|
||||
pose: StayAlignedRelaxedPose,
|
||||
lifecycle: StayAlignedPoseRecordingLifecycle,
|
||||
detail: StayAlignedPoseRecordingDetail?,
|
||||
failure: StayAlignedPoseRecordingFailure = StayAlignedPoseRecordingFailure.NONE,
|
||||
) = StayAlignedPoseRecordingStatusResponse(pose = pose, lifecycle = lifecycle, detail = detail, failure = failure)
|
||||
|
||||
private companion object {
|
||||
val STATUS_SAMPLE_INTERVAL = 250.milliseconds
|
||||
val CALIBRATION_STEPS = listOf(
|
||||
TrackingChecklistStepId.FULL_RESET,
|
||||
TrackingChecklistStepId.MOUNTING_CALIBRATION,
|
||||
TrackingChecklistStepId.FEET_MOUNTING_CALIBRATION,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.slimevr.tracker.behaviours
|
||||
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.config.UserConfig
|
||||
import dev.slimevr.math.angle.Angle
|
||||
import dev.slimevr.tracker.Tracker
|
||||
import dev.slimevr.tracker.TrackerActions
|
||||
@@ -24,6 +25,7 @@ import solarxr_protocol.datatypes.MagnetometerStatus
|
||||
|
||||
class TrackerStayAlignedBehaviour(
|
||||
private val settings: Settings,
|
||||
private val userConfig: UserConfig,
|
||||
) : TrackerBehaviour {
|
||||
|
||||
override fun observe(receiver: Tracker) {
|
||||
@@ -65,12 +67,12 @@ class TrackerStayAlignedBehaviour(
|
||||
|
||||
val serverFlow = receiver.appContext.server.context.state
|
||||
|
||||
val stayAlignedConfigFlow = settings.context.state.map { it.data.stayAlignedConfig }.distinctUntilChanged()
|
||||
val enabledFlow = settings.context.state.map { it.data.stayAlignedConfig.enabled }.distinctUntilChanged()
|
||||
val imuTypeFlow = receiver.context.state.map { it.imuType }.distinctUntilChanged()
|
||||
val magStatusFlow = receiver.context.state.map { it.magStatus }.distinctUntilChanged()
|
||||
combine(stayAlignedConfigFlow, imuTypeFlow, magStatusFlow, ::Triple)
|
||||
.flatMapLatest { (stayAlignedConfig, imuType, magStatus) ->
|
||||
if (magStatus == MagnetometerStatus.ENABLED || imuType == null || !stayAlignedConfig.enabled) return@flatMapLatest emptyFlow()
|
||||
combine(enabledFlow, imuTypeFlow, magStatusFlow, ::Triple)
|
||||
.flatMapLatest { (enabled, imuType, magStatus) ->
|
||||
if (magStatus == MagnetometerStatus.ENABLED || imuType == null || !enabled) return@flatMapLatest emptyFlow()
|
||||
|
||||
// Ignore every other emission for performance (50FPS instead of 100FPS when at 100TPS)
|
||||
var index = 0
|
||||
@@ -90,7 +92,7 @@ class TrackerStayAlignedBehaviour(
|
||||
state,
|
||||
serverFlow.value.trackers.values.map { it.context.state.value },
|
||||
applyYawCorrection,
|
||||
stayAlignedConfig,
|
||||
userConfig.context.state.value.data,
|
||||
)
|
||||
if (yawCorrectionResult != null) {
|
||||
receiver.context.dispatch(TrackerActions.SetYawCorrection(yawCorrectionResult))
|
||||
|
||||
@@ -185,7 +185,7 @@ class Tracker(
|
||||
TrackerConfigBehaviour(devices, hardwareId),
|
||||
TrackerMotionDetectionBehaviour(),
|
||||
TrackerToSkeletonBehaviour(),
|
||||
TrackerStayAlignedBehaviour(settings),
|
||||
TrackerStayAlignedBehaviour(settings, appContext.config.userConfig),
|
||||
)
|
||||
val context = Context.create(
|
||||
initialState = trackerState,
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package dev.slimevr.tracker.stayaligned
|
||||
|
||||
import dev.slimevr.math.angle.Angle
|
||||
import dev.slimevr.tracker.stayaligned.poses.RelaxedPose
|
||||
import solarxr_protocol.datatypes.hardware_info.ImuType
|
||||
|
||||
/**
|
||||
@@ -9,14 +8,9 @@ import solarxr_protocol.datatypes.hardware_info.ImuType
|
||||
* algorithm from a single place.
|
||||
*/
|
||||
object StayAlignedDefaults {
|
||||
// Relaxed pose for kneeling. This isn't that common, so we don't want to ask
|
||||
// players to provide this relaxed pose during setup.
|
||||
val RELAXED_POSE_KNEELING =
|
||||
RelaxedPose(
|
||||
upperLeg = Angle.ofDeg(0.0f),
|
||||
lowerLeg = Angle.ofDeg(0.0f),
|
||||
foot = Angle.ofDeg(0.0f),
|
||||
)
|
||||
// Recording a relaxed pose: time to get into the pose, then the time every tracker has to settle
|
||||
const val POSE_RECORDING_COUNTDOWN_SECONDS = 3
|
||||
const val POSE_RECORDING_TIMEOUT_SECONDS = 10
|
||||
|
||||
// Weights to calculate the average yaw of the skeleton
|
||||
const val CENTRE_ERROR_HEAD_WEIGHT = 0.5f
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package dev.slimevr.tracker.stayaligned
|
||||
|
||||
import dev.slimevr.config.StayAlignedPoseConfig
|
||||
import dev.slimevr.tracker.Motion
|
||||
import dev.slimevr.tracker.TrackerState
|
||||
import dev.slimevr.tracker.stayaligned.poses.PlayerPose
|
||||
import dev.slimevr.tracker.stayaligned.poses.RelaxedPose
|
||||
import solarxr_protocol.rpc.StayAlignedPart
|
||||
import solarxr_protocol.rpc.StayAlignedPoseRecordingFailure
|
||||
import solarxr_protocol.rpc.StayAlignedRecordingTrackerState
|
||||
import solarxr_protocol.rpc.StayAlignedRelaxedPose
|
||||
|
||||
data class PoseRecordingCheck(
|
||||
val parts: Map<StayAlignedPart, StayAlignedRecordingTrackerState>,
|
||||
val detectedPose: StayAlignedRelaxedPose,
|
||||
val failure: StayAlignedPoseRecordingFailure,
|
||||
)
|
||||
|
||||
/** Whether each part that has a tracker is still or moving. */
|
||||
private fun partStates(trackerStates: List<TrackerState>): Map<StayAlignedPart, StayAlignedRecordingTrackerState> = StayAlignedPart.entries.mapNotNull { part ->
|
||||
val trackers = trackersOf(trackerStates, part)
|
||||
when {
|
||||
trackers.isEmpty() -> null
|
||||
trackers.all { it.motion == Motion.RESTING } -> part to StayAlignedRecordingTrackerState.STILL
|
||||
else -> part to StayAlignedRecordingTrackerState.MOVING
|
||||
}
|
||||
}.toMap()
|
||||
|
||||
/** Missing trackers come first, then trackers that move, then the pose. */
|
||||
fun checkPoseRecording(trackerStates: List<TrackerState>, requested: StayAlignedRelaxedPose): PoseRecordingCheck {
|
||||
val parts = partStates(trackerStates)
|
||||
val detectedPose = PlayerPose.of(trackerStates).relaxedPose
|
||||
val failure = when {
|
||||
missingRequiredParts(trackerStates).isNotEmpty() -> StayAlignedPoseRecordingFailure.MISSING_TRACKERS
|
||||
StayAlignedRecordingTrackerState.MOVING in parts.values -> StayAlignedPoseRecordingFailure.NOT_STILL
|
||||
detectedPose == StayAlignedRelaxedPose.UNKNOWN -> StayAlignedPoseRecordingFailure.POSITION_UNKNOWN
|
||||
detectedPose != requested -> StayAlignedPoseRecordingFailure.WRONG_POSE
|
||||
else -> StayAlignedPoseRecordingFailure.NONE
|
||||
}
|
||||
return PoseRecordingCheck(parts, detectedPose, failure)
|
||||
}
|
||||
|
||||
/** The pose the user is in right now, measured from the rotations the trackers had when they started resting. */
|
||||
fun recordedPoseConfig(trackerStates: List<TrackerState>): StayAlignedPoseConfig {
|
||||
val relaxedPose = RelaxedPose.fromTrackers(trackerStates)
|
||||
return StayAlignedPoseConfig(
|
||||
recorded = true,
|
||||
enabled = true,
|
||||
upperLegAngleInDeg = relaxedPose.upperLeg.toDeg(),
|
||||
lowerLegAngleInDeg = relaxedPose.lowerLeg.toDeg(),
|
||||
footAngleInDeg = relaxedPose.foot.toDeg(),
|
||||
)
|
||||
}
|
||||
@@ -4,17 +4,19 @@ import dev.slimevr.tracker.TrackerState
|
||||
import dev.slimevr.tracker.getAllActiveFor
|
||||
import dev.slimevr.tracker.getFirstActiveFor
|
||||
import dev.slimevr.tracker.stayaligned.StayAlignedBodyParts
|
||||
import solarxr_protocol.rpc.StayAlignedRelaxedPose
|
||||
|
||||
/**
|
||||
* The pose of the player.
|
||||
* The pose of the player, with the relaxed pose the user records for it. Sitting on the ground and
|
||||
* lying on the back share one.
|
||||
*/
|
||||
enum class PlayerPose {
|
||||
UNKNOWN,
|
||||
STANDING,
|
||||
SITTING_IN_CHAIR,
|
||||
SITTING_ON_GROUND,
|
||||
LYING_ON_BACK,
|
||||
KNEELING,
|
||||
enum class PlayerPose(val relaxedPose: StayAlignedRelaxedPose) {
|
||||
UNKNOWN(StayAlignedRelaxedPose.UNKNOWN),
|
||||
STANDING(StayAlignedRelaxedPose.STANDING),
|
||||
SITTING_IN_CHAIR(StayAlignedRelaxedPose.SITTING),
|
||||
SITTING_ON_GROUND(StayAlignedRelaxedPose.FLAT),
|
||||
LYING_ON_BACK(StayAlignedRelaxedPose.FLAT),
|
||||
KNEELING(StayAlignedRelaxedPose.KNEELING),
|
||||
;
|
||||
|
||||
companion object {
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package dev.slimevr.tracker.stayaligned.poses
|
||||
|
||||
import dev.slimevr.config.StayAlignedPoseConfig
|
||||
import dev.slimevr.config.UserConfigData
|
||||
import solarxr_protocol.rpc.StayAlignedRelaxedPose
|
||||
|
||||
/** The poses the user can record and turn on, which is all of them but UNKNOWN. */
|
||||
val STAY_ALIGNED_POSES: List<StayAlignedRelaxedPose> = StayAlignedRelaxedPose.entries - StayAlignedRelaxedPose.UNKNOWN
|
||||
|
||||
private val DEFAULT_STAY_ALIGNED_POSES = mapOf(
|
||||
StayAlignedRelaxedPose.KNEELING to StayAlignedPoseConfig(enabled = true),
|
||||
)
|
||||
|
||||
fun hasStayAlignedPoseDefault(pose: StayAlignedRelaxedPose): Boolean = pose in DEFAULT_STAY_ALIGNED_POSES
|
||||
|
||||
fun stayAlignedPoseConfig(data: UserConfigData, pose: StayAlignedRelaxedPose): StayAlignedPoseConfig = data.stayAlignedPoses[pose] ?: DEFAULT_STAY_ALIGNED_POSES[pose] ?: StayAlignedPoseConfig()
|
||||
@@ -1,11 +1,10 @@
|
||||
package dev.slimevr.tracker.stayaligned.poses
|
||||
|
||||
import dev.slimevr.config.StayAlignedConfig
|
||||
import dev.slimevr.config.UserConfigData
|
||||
import dev.slimevr.math.angle.Angle
|
||||
import dev.slimevr.tracker.TrackerState
|
||||
import dev.slimevr.tracker.getFirstActiveFor
|
||||
import dev.slimevr.tracker.stayaligned.StayAlignedBodyParts
|
||||
import dev.slimevr.tracker.stayaligned.StayAlignedDefaults
|
||||
import dev.slimevr.tracker.stayaligned.YawUtils.trackerYaw
|
||||
|
||||
class RelaxedPose(
|
||||
@@ -25,62 +24,24 @@ class RelaxedPose(
|
||||
*/
|
||||
fun forPose(
|
||||
playerPose: PlayerPose,
|
||||
config: StayAlignedConfig,
|
||||
) = when (playerPose) {
|
||||
PlayerPose.STANDING -> {
|
||||
val poseConfig = config.standingRelaxedPose
|
||||
if (poseConfig.enabled) {
|
||||
RelaxedPose(
|
||||
Angle.ofDeg(poseConfig.upperLegAngleInDeg),
|
||||
Angle.ofDeg(poseConfig.lowerLegAngleInDeg),
|
||||
Angle.ofDeg(poseConfig.footAngleInDeg),
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
PlayerPose.SITTING_IN_CHAIR -> {
|
||||
val poseConfig = config.sittingRelaxedPose
|
||||
if (poseConfig.enabled) {
|
||||
RelaxedPose(
|
||||
Angle.ofDeg(poseConfig.upperLegAngleInDeg),
|
||||
Angle.ofDeg(poseConfig.lowerLegAngleInDeg),
|
||||
Angle.ofDeg(poseConfig.footAngleInDeg),
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
PlayerPose.SITTING_ON_GROUND,
|
||||
PlayerPose.LYING_ON_BACK,
|
||||
-> {
|
||||
val poseConfig = config.flatRelaxedPose
|
||||
if (poseConfig.enabled) {
|
||||
RelaxedPose(
|
||||
Angle.ofDeg(poseConfig.upperLegAngleInDeg),
|
||||
Angle.ofDeg(poseConfig.lowerLegAngleInDeg),
|
||||
Angle.ofDeg(poseConfig.footAngleInDeg),
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
PlayerPose.KNEELING ->
|
||||
StayAlignedDefaults.RELAXED_POSE_KNEELING
|
||||
|
||||
else ->
|
||||
null
|
||||
userConfig: UserConfigData,
|
||||
): RelaxedPose? {
|
||||
val poseConfig = stayAlignedPoseConfig(userConfig, playerPose.relaxedPose)
|
||||
if (!poseConfig.enabled) return null
|
||||
return RelaxedPose(
|
||||
Angle.ofDeg(poseConfig.upperLegAngleInDeg),
|
||||
Angle.ofDeg(poseConfig.lowerLegAngleInDeg),
|
||||
Angle.ofDeg(poseConfig.footAngleInDeg),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the relaxed angles from the trackers.
|
||||
*/
|
||||
fun fromTrackers(trackerStates: List<TrackerState>): RelaxedPose {
|
||||
val restingRotation = { state: TrackerState -> state.stayAlignedData.lockedRotation ?: state.rotation }
|
||||
val halfAngleBetween = { left: TrackerState, right: TrackerState ->
|
||||
(trackerYaw(left.rotation) - trackerYaw(right.rotation)) * 0.5f
|
||||
(trackerYaw(restingRotation(left)) - trackerYaw(restingRotation(right))) * 0.5f
|
||||
}
|
||||
|
||||
val upperLegAngle: Angle = trackerStates.getFirstActiveFor(StayAlignedBodyParts.leftUpperLeg)?.let { left ->
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package dev.slimevr.tracker.stayaligned
|
||||
|
||||
import dev.slimevr.tracker.TrackerState
|
||||
import dev.slimevr.tracker.getAllActiveFor
|
||||
import dev.slimevr.tracker.getFirstActiveFor
|
||||
import solarxr_protocol.rpc.StayAlignedPart
|
||||
|
||||
/**
|
||||
* The trackers Stay Aligned needs to correct the yaw of moving trackers: at least one upper body
|
||||
* tracker plus both upper legs and both lower legs. Feet are optional and not part of this.
|
||||
*/
|
||||
class RequiredTrackers(
|
||||
val upperBody: List<TrackerState>,
|
||||
val leftUpperLeg: TrackerState,
|
||||
val rightUpperLeg: TrackerState,
|
||||
val leftLowerLeg: TrackerState,
|
||||
val rightLowerLeg: TrackerState,
|
||||
)
|
||||
|
||||
/** The required trackers, or null when some are missing. */
|
||||
fun requiredTrackers(trackerStates: List<TrackerState>): RequiredTrackers? {
|
||||
val upperBody = trackerStates.getAllActiveFor(StayAlignedBodyParts.upperBodyGroup)
|
||||
if (upperBody.isEmpty()) return null
|
||||
return RequiredTrackers(
|
||||
upperBody = upperBody,
|
||||
leftUpperLeg = trackerStates.getFirstActiveFor(StayAlignedBodyParts.leftUpperLeg) ?: return null,
|
||||
rightUpperLeg = trackerStates.getFirstActiveFor(StayAlignedBodyParts.rightUpperLeg) ?: return null,
|
||||
leftLowerLeg = trackerStates.getFirstActiveFor(StayAlignedBodyParts.leftLowerLeg) ?: return null,
|
||||
rightLowerLeg = trackerStates.getFirstActiveFor(StayAlignedBodyParts.rightLowerLeg) ?: return null,
|
||||
)
|
||||
}
|
||||
|
||||
/** The active trackers of a part of the body. Empty when the user has none. */
|
||||
fun trackersOf(trackerStates: List<TrackerState>, part: StayAlignedPart): List<TrackerState> = when (part) {
|
||||
StayAlignedPart.UPPER_BODY -> trackerStates.getAllActiveFor(StayAlignedBodyParts.upperBodyGroup)
|
||||
StayAlignedPart.LEFT_UPPER_LEG -> listOfNotNull(trackerStates.getFirstActiveFor(StayAlignedBodyParts.leftUpperLeg))
|
||||
StayAlignedPart.RIGHT_UPPER_LEG -> listOfNotNull(trackerStates.getFirstActiveFor(StayAlignedBodyParts.rightUpperLeg))
|
||||
StayAlignedPart.LEFT_LOWER_LEG -> listOfNotNull(trackerStates.getFirstActiveFor(StayAlignedBodyParts.leftLowerLeg))
|
||||
StayAlignedPart.RIGHT_LOWER_LEG -> listOfNotNull(trackerStates.getFirstActiveFor(StayAlignedBodyParts.rightLowerLeg))
|
||||
StayAlignedPart.LEFT_FOOT -> listOfNotNull(trackerStates.getFirstActiveFor(StayAlignedBodyParts.leftFoot))
|
||||
StayAlignedPart.RIGHT_FOOT -> listOfNotNull(trackerStates.getFirstActiveFor(StayAlignedBodyParts.rightFoot))
|
||||
}
|
||||
|
||||
/** Parts without a tracker that Stay Aligned needs. */
|
||||
fun missingRequiredParts(trackerStates: List<TrackerState>): List<StayAlignedPart> = REQUIRED_PARTS.filter { trackersOf(trackerStates, it).isEmpty() }
|
||||
|
||||
private val REQUIRED_PARTS = listOf(
|
||||
StayAlignedPart.UPPER_BODY,
|
||||
StayAlignedPart.LEFT_UPPER_LEG,
|
||||
StayAlignedPart.RIGHT_UPPER_LEG,
|
||||
StayAlignedPart.LEFT_LOWER_LEG,
|
||||
StayAlignedPart.RIGHT_LOWER_LEG,
|
||||
)
|
||||
@@ -1,6 +1,6 @@
|
||||
package dev.slimevr.tracker.stayaligned
|
||||
|
||||
import dev.slimevr.config.StayAlignedConfig
|
||||
import dev.slimevr.config.UserConfigData
|
||||
import dev.slimevr.math.angle.Angle
|
||||
import dev.slimevr.math.angle.AngleErrors
|
||||
import dev.slimevr.tracker.Motion
|
||||
@@ -37,9 +37,9 @@ object TrackerYawCorrection {
|
||||
trackerState: TrackerState,
|
||||
trackerStates: List<TrackerState>,
|
||||
applyYawCorrection: Angle,
|
||||
config: StayAlignedConfig,
|
||||
userConfig: UserConfigData,
|
||||
): Angle? = when (trackerState.motion) {
|
||||
Motion.ROTATING -> adjustMovingTracker(trackerState, trackerStates, applyYawCorrection, config)
|
||||
Motion.ROTATING -> adjustMovingTracker(trackerState, trackerStates, applyYawCorrection, userConfig)
|
||||
|
||||
Motion.RESTING -> adjustLockedTracker(trackerState, applyYawCorrection)
|
||||
|
||||
@@ -97,10 +97,10 @@ object TrackerYawCorrection {
|
||||
trackerState: TrackerState,
|
||||
trackerStates: List<TrackerState>,
|
||||
applyYawCorrection: Angle,
|
||||
config: StayAlignedConfig,
|
||||
userConfig: UserConfigData,
|
||||
): Angle? {
|
||||
val centreYaw = YawUtils.centreYawOfTrackers(trackerStates) ?: return null
|
||||
val relaxedPose = RelaxedPose.forPose(PlayerPose.of(trackerStates), config) ?: return null
|
||||
val relaxedPose = RelaxedPose.forPose(PlayerPose.of(trackerStates), userConfig) ?: return null
|
||||
|
||||
return adjustByError(trackerState, applyYawCorrection) { yawCorrection ->
|
||||
YawErrors().also {
|
||||
|
||||
@@ -3,7 +3,6 @@ package dev.slimevr.tracker.stayaligned
|
||||
import dev.slimevr.math.angle.Angle
|
||||
import dev.slimevr.math.angle.AngleAverage
|
||||
import dev.slimevr.tracker.TrackerState
|
||||
import dev.slimevr.tracker.getAllActiveFor
|
||||
import dev.slimevr.tracker.getFirstActiveFor
|
||||
import dev.slimevr.util.Side
|
||||
import io.github.axisangles.ktmath.EulerOrder
|
||||
@@ -73,12 +72,12 @@ object YawUtils {
|
||||
trackerStates: List<TrackerState>,
|
||||
): Angle? {
|
||||
val head = trackerStates.getFirstActiveFor(StayAlignedBodyParts.head) // Optional
|
||||
val upperBody = trackerStates.getAllActiveFor(StayAlignedBodyParts.upperBodyGroup)
|
||||
if (upperBody.isEmpty()) return null
|
||||
val leftUpperLeg = trackerStates.getFirstActiveFor(StayAlignedBodyParts.leftUpperLeg) ?: return null
|
||||
val rightUpperLeg = trackerStates.getFirstActiveFor(StayAlignedBodyParts.rightUpperLeg) ?: return null
|
||||
val leftLowerLeg = trackerStates.getFirstActiveFor(StayAlignedBodyParts.leftLowerLeg) ?: return null
|
||||
val rightLowerLeg = trackerStates.getFirstActiveFor(StayAlignedBodyParts.rightLowerLeg) ?: return null
|
||||
val required = requiredTrackers(trackerStates) ?: return null
|
||||
val upperBody = required.upperBody
|
||||
val leftUpperLeg = required.leftUpperLeg
|
||||
val rightUpperLeg = required.rightUpperLeg
|
||||
val leftLowerLeg = required.leftLowerLeg
|
||||
val rightLowerLeg = required.rightLowerLeg
|
||||
|
||||
// Need a minimum set of trackers, and the trackers need to be oriented in a
|
||||
// way where we can actually calculate its yaw.
|
||||
|
||||
@@ -4,6 +4,7 @@ import dev.slimevr.VRServer
|
||||
import dev.slimevr.config.Settings
|
||||
import dev.slimevr.networkprofile.NetworkProfileManager
|
||||
import dev.slimevr.resets.ResetBodyParts
|
||||
import dev.slimevr.resets.ResetsManager
|
||||
import dev.slimevr.routing.BoneRoutingManager
|
||||
import dev.slimevr.routing.Routes
|
||||
import dev.slimevr.routing.solarXRDriverConnectedFlow
|
||||
@@ -302,25 +303,33 @@ private fun isImuAssigned(tracker: ChecklistTracker): Boolean = (tracker.origin
|
||||
|
||||
class FullResetCheckBehaviour(
|
||||
private val trackerStates: StateFlow<List<ChecklistTracker>>,
|
||||
private val resetsManager: ResetsManager,
|
||||
) : TrackingChecklistBehaviourType {
|
||||
override fun observe(receiver: TrackingChecklist) {
|
||||
trackerStates
|
||||
.map { trackers ->
|
||||
val assigned = trackers.filter { isImuAssigned(it) }
|
||||
val pending = assigned.filter { it.needsFullReset }
|
||||
TrackingChecklistStep(
|
||||
valid = pending.isEmpty(),
|
||||
enabled = assigned.isNotEmpty(),
|
||||
ignorable = false,
|
||||
visibility = TrackingChecklistStepVisibility.ALWAYS,
|
||||
extraData = if (pending.isNotEmpty()) {
|
||||
TrackingChecklistTrackerReset(trackersId = pending.map { it.id.toUShort() })
|
||||
} else {
|
||||
null
|
||||
},
|
||||
)
|
||||
val mountingPending = receiver.context.state
|
||||
.map { state ->
|
||||
listOf(TrackingChecklistStepId.MOUNTING_CALIBRATION, TrackingChecklistStepId.FEET_MOUNTING_CALIBRATION)
|
||||
.any { id -> state.steps[id]?.let { it.enabled && !it.valid } == true }
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
val mountingResetAllowed = resetsManager.context.state.map { it.canDoMountingReset }.distinctUntilChanged()
|
||||
|
||||
combine(trackerStates, mountingResetAllowed, mountingPending) { trackers, allowed, pendingMounting ->
|
||||
val assigned = trackers.filter { isImuAssigned(it) }
|
||||
val pending = assigned.filter { it.needsFullReset }
|
||||
TrackingChecklistStep(
|
||||
valid = pending.isEmpty() && (allowed || !pendingMounting),
|
||||
enabled = assigned.isNotEmpty(),
|
||||
ignorable = false,
|
||||
visibility = TrackingChecklistStepVisibility.ALWAYS,
|
||||
extraData = if (pending.isNotEmpty()) {
|
||||
TrackingChecklistTrackerReset(trackersId = pending.map { it.id.toUShort() })
|
||||
} else {
|
||||
null
|
||||
},
|
||||
)
|
||||
}
|
||||
.distinctUntilChanged()
|
||||
.onEach { step -> receiver.context.dispatch(TrackingChecklistActions.UpdateStep(TrackingChecklistStepId.FULL_RESET, step)) }
|
||||
.launchIn(receiver.context.scope)
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ class TrackingChecklist(
|
||||
val stepBehaviours: List<TrackingChecklistBehaviourType> = buildList {
|
||||
add(ReliableReferenceCheckBehaviour(trackerStates))
|
||||
add(TrackerRestCheckBehaviour(trackerStates))
|
||||
add(FullResetCheckBehaviour(trackerStates))
|
||||
add(FullResetCheckBehaviour(trackerStates, appContext.resetsManager))
|
||||
add(MountingMethodCheckBehaviour(appContext.config.settings))
|
||||
add(MountingCalibrationCheckBehaviour(trackerStates, appContext.config.settings))
|
||||
add(TrackerErrorCheckBehaviour(trackerStates))
|
||||
|
||||
Reference in new issue
Block a user