Move VMCConfig + TapDetection solarxr

This commit is contained in:
Erimel committed 2026-06-16 23:12:24 -04:00
1 parent 0755413b87
commit 2f5b2eaa94
9 files changed
+88 -21

No files matched your search

@@ -2,15 +2,12 @@ package dev.slimevr.config
import dev.slimevr.context.Behaviour
import dev.slimevr.context.Context
import dev.slimevr.vmc.VMCConfig
import io.github.axisangles.ktmath.Quaternion
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancelAndJoin
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.intOrNull
import kotlinx.serialization.json.jsonObject
@@ -50,6 +47,17 @@ data class TapDetectionConfig (
) {
}
@Serializable
data class VMCConfig(
val enabled: Boolean = true,
val portOut: Int = 39539,
val portIn: Int = 39540,
val address: String = "172.17.0.1",
val mirrorTracking: Boolean = false,
val anchorAtHips: Boolean = false,
val vrmJson: String? = null,
)
@Serializable
data class SettingsConfigState(
val trackerPort: Int = 6969,
@@ -58,14 +66,12 @@ data class SettingsConfigState(
val trackers: Map<String, TrackerConfig> = emptyMap(),
val globalMagEnabled: Boolean = true,
val allowedUdpDevices: Set<String> = emptySet(),
val vrcOscConfig: VRCOSCConfig = defaultVrcOscConfig(),
val vrcOscConfig: VRCOSCConfig = VRCOSCConfig(),
val vmcConfig: VMCConfig = VMCConfig(),
val version: Int = SETTINGS_CONFIG_VERSION,
val tapDetectionConfig: TapDetectionConfig = TapDetectionConfig(),
)
private fun defaultVrcOscConfig() = VRCOSCConfig()
private fun migrateSettingsConfig(json: JsonObject): JsonObject {
val version = json["version"]?.jsonPrimitive?.intOrNull ?: 0
return when {
@@ -6,6 +6,7 @@ import dev.slimevr.VRServerActions
import dev.slimevr.context.Behaviour
import dev.slimevr.context.Context
import dev.slimevr.context.ManagedContext
import dev.slimevr.tracker.TrackerTapDetectionBehaviour
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import solarxr_protocol.MessageBundle
@@ -71,9 +72,10 @@ class SolarXRBridge(
appContext.vrcConfigManager?.let { vrc ->
add(VrcBehaviour(vrc, appContext.server, userHeight = { appContext.skeleton.context.state.value.userHeight }))
}
add(TapDetectionBehaviour(appContext.config.settings))
add(VrcOscBehaviour(appContext.vrcOscManager))
add(VmcBehaviour(appContext.vmcManager))
add(SettingsBehaviour(appContext.vmcManager))
add(SettingsBehaviour(appContext.config.settings, appContext.vmcManager))
add(HeightCalibrationBehaviour(appContext.heightCalibrationManager))
add(ProvisioningBehaviour(appContext.server, appContext.provisioningManager))
add(SkeletonBehaviour(appContext.config.userConfig, appContext.skeleton))
@@ -1,5 +1,6 @@
package dev.slimevr.solarxr
import dev.slimevr.config.Settings
import dev.slimevr.vmc.VMCManager
import solarxr_protocol.rpc.SettingsRequest
import solarxr_protocol.rpc.SettingsResponse
@@ -10,12 +11,14 @@ import solarxr_protocol.rpc.SettingsResponse
// TODO: Would be nice to split settings based on their solarxr behaviour.
// TODO: Almost all build functions could be their own request. making it easier to work with
class SettingsBehaviour(
private val settings: Settings,
private val vmcManager: VMCManager,
) : SolarXRBridgeBehaviour {
override fun observe(receiver: SolarXRBridge) {
receiver.rpcDispatcher.on<SettingsRequest> {
receiver.sendRpc(
SettingsResponse(
tapDetectionSettings = buildTapDetectionSettings(settings.context.state.value.data.tapDetectionConfig),
vmcOsc = buildVmcOscSettings(vmcManager.context.state.value.config),
),
)
@@ -0,0 +1,54 @@
package dev.slimevr.solarxr
import dev.slimevr.config.Settings
import dev.slimevr.config.SettingsActions
import dev.slimevr.config.TapDetectionConfig
import solarxr_protocol.rpc.ChangeSettingsRequest
import solarxr_protocol.rpc.TapDetectionSettings
class TapDetectionBehaviour(
private val settings: Settings,
) : SolarXRBridgeBehaviour {
override fun observe(receiver: SolarXRBridge) {
receiver.rpcDispatcher.on<ChangeSettingsRequest> { req ->
val tapDetectionSettings = req.tapDetectionSettings ?: return@on
val oldConfig = settings.context.state.value.data.tapDetectionConfig
val newConfig = TapDetectionConfig(
yawResetDelay = tapDetectionSettings.yawResetDelay ?: oldConfig.yawResetDelay,
fullResetDelay = tapDetectionSettings.fullResetDelay ?: oldConfig.fullResetDelay,
mountingResetDelay = tapDetectionSettings.mountingResetDelay ?: oldConfig.mountingResetDelay,
yawResetEnabled = tapDetectionSettings.yawResetEnabled ?: false,
fullResetEnabled = tapDetectionSettings.fullResetEnabled ?: false,
mountingResetEnabled = tapDetectionSettings.mountingResetEnabled ?: false,
yawResetTaps = tapDetectionSettings.yawResetTaps?.toInt() ?: oldConfig.yawResetTaps,
fullResetTaps = tapDetectionSettings.fullResetTaps?.toInt() ?: oldConfig.fullResetTaps,
mountingResetTaps = tapDetectionSettings.mountingResetTaps?.toInt() ?: oldConfig.mountingResetTaps,
yawResetBodyPart = tapDetectionSettings.yawResetTracker ?: oldConfig.yawResetBodyPart,
fullResetBodyPart = tapDetectionSettings.fullResetTracker ?: oldConfig.fullResetBodyPart,
mountingResetBodyPart = tapDetectionSettings.mountingResetTracker ?: oldConfig.mountingResetBodyPart,
numberTrackersOverThreshold = tapDetectionSettings.numberTrackersOverThreshold?.toInt() ?: oldConfig.numberTrackersOverThreshold,
)
settings.context.dispatch(SettingsActions.Update { copy(tapDetectionConfig = newConfig) })
// TODO setupMode
}
}
}
fun buildTapDetectionSettings(config: TapDetectionConfig): TapDetectionSettings = TapDetectionSettings(
yawResetDelay = config.yawResetDelay,
fullResetDelay = config.fullResetDelay,
mountingResetDelay = config.mountingResetDelay,
yawResetEnabled = config.yawResetEnabled,
fullResetEnabled = config.fullResetEnabled,
mountingResetEnabled = config.mountingResetEnabled,
yawResetTaps = config.yawResetTaps.toUByte(),
fullResetTaps = config.fullResetTaps.toUByte(),
mountingResetTaps = config.mountingResetTaps.toUByte(),
yawResetTracker = config.yawResetBodyPart,
fullResetTracker = config.fullResetBodyPart,
mountingResetTracker = config.mountingResetBodyPart,
numberTrackersOverThreshold = config.numberTrackersOverThreshold.toUByte()
)
@@ -1,7 +1,7 @@
package dev.slimevr.solarxr
import dev.slimevr.config.VMCConfig
import dev.slimevr.vmc.VMCActions
import dev.slimevr.vmc.VMCConfig
import dev.slimevr.vmc.VMCManager
import solarxr_protocol.rpc.ChangeSettingsRequest
import solarxr_protocol.rpc.OSCSettings
@@ -84,6 +84,8 @@ class TrackerTapDetectionBehaviour : TrackerBehaviour {
.map { it.context.state.value.bodyPart }
.toSet()
// Get reference rotation for reset actions
// TODO need a helper method or something for this
val referenceRotation = serverState.trackers.firstNotNullOfOrNull {
val trackerState = it.value.context.state.value
if (trackerState.status == TrackerStatus.OK && trackerState.bodyPart == BodyPart.HEAD) {
@@ -98,8 +100,9 @@ class TrackerTapDetectionBehaviour : TrackerBehaviour {
val mountingResetBodyPart = listOf(tapDetectionConfig.mountingResetBodyPart, BodyPart.RIGHT_UPPER_LEG, BodyPart.RIGHT_LOWER_LEG)
.firstOrNull { it in trackersBodyParts }
// Switch case for each possible action
val (actionToExecute, tapsNeeded, actionDelay) = when (receiver.context.state.value.bodyPart) {
null -> Triple(null, 0, 0f)
null -> Triple(null, 0, 0f) // BodyParts above could be null
yawResetBodyPart if tapDetectionConfig.yawResetEnabled ->
Triple(TrackerActions.YawReset(referenceRotation), tapDetectionConfig.yawResetTaps, tapDetectionConfig.yawResetDelay)
fullResetBodyPart if tapDetectionConfig.fullResetEnabled ->
@@ -124,7 +127,10 @@ class TrackerTapDetectionBehaviour : TrackerBehaviour {
val now = System.nanoTime()
// Get the acceleration of the tracker and store it
accelList.add(currentTracker.rawAcceleration.len() to now)
// Remove old stored accelerations (if they are too old)
while (accelList.isNotEmpty() && now - accelList.first().second > CLUMP_TIME_NS) {
accelList.removeFirst()
}
@@ -133,18 +139,22 @@ class TrackerTapDetectionBehaviour : TrackerBehaviour {
val min = accelList.minOfOrNull { it.first } ?: 0f
val accelDelta = max - min
// Check for a single tap
if (accelDelta > NEEDED_ACCEL_DELTA && !waitForLowAccel) {
val othersOverThreshold = receiver.appContext.server.context.state.value.trackers.values
.count { it.context.state.value.id != currentTracker.id && it.context.state.value.rawAcceleration.lenSq() > ALLOWED_BODY_ACCEL_SQUARED }
if (othersOverThreshold < context.numberTrackersOverThreshold) {
tapTimestamps.add(now)
// After a tap, a lower acceleration is needed before another one
waitForLowAccel = true
}
}
// Achieved low accel?
if (max < ALLOWED_BODY_ACCEL) waitForLowAccel = false
if (tapTimestamps.isNotEmpty()) {
// Remove old stored taps (if they are too old)
val totalWindowNs = (TAP_WINDOW_PER_TAP_NS * tapTimestamps.size).toLong()
while (tapTimestamps.isNotEmpty() && now - tapTimestamps.first() > totalWindowNs) {
tapTimestamps.removeFirst()
@@ -152,6 +162,7 @@ class TrackerTapDetectionBehaviour : TrackerBehaviour {
if (tapTimestamps.isNotEmpty() && now - tapTimestamps.last() > TAP_WINDOW_PER_TAP_NS.toLong()) {
if (tapTimestamps.size >= context.tapsNeeded) {
// Taps completed!
receiver.context.scope.safeLaunch {
AppLogger.tracker.info("TapDetection triggered ${context.actionToExecute}")
delay((context.actionDelay * 1000).toLong().milliseconds)
@@ -3,6 +3,7 @@ package dev.slimevr.vmc
import dev.slimevr.AppLogger
import dev.slimevr.config.Settings
import dev.slimevr.config.SettingsActions
import dev.slimevr.config.VMCConfig
import dev.slimevr.context.Behaviour
import dev.slimevr.osc.OscArg
import dev.slimevr.osc.OscBundle
@@ -1,6 +1,7 @@
package dev.slimevr.vmc
import com.jme3.math.FastMath
import dev.slimevr.config.VMCConfig
import dev.slimevr.skeleton.BoneState
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
@@ -1,22 +1,11 @@
package dev.slimevr.vmc
import dev.slimevr.Phase1ContextProvider
import dev.slimevr.config.VMCConfig
import dev.slimevr.context.Behaviour
import dev.slimevr.context.Context
import dev.slimevr.skeleton.Skeleton
import kotlinx.coroutines.CoroutineScope
import kotlinx.serialization.Serializable
@Serializable
data class VMCConfig(
val enabled: Boolean = true,
val portOut: Int = 39539,
val portIn: Int = 39540,
val address: String = "172.17.0.1",
val mirrorTracking: Boolean = false,
val anchorAtHips: Boolean = false,
val vrmJson: String? = null,
)
data class VMCState(val config: VMCConfig = VMCConfig())