mirror of
https://github.com/SlimeVR/SlimeVR-Server.git
synced 2026-11-26 11:56:27 -08:00
Read mag vector from HID and send to the GUI
This commit is contained in:
5 files changed
+21
-18
No files matched your search
@@ -75,6 +75,7 @@ class HIDDeviceInfoBehaviour : HIDReceiverBehaviour {
|
||||
override fun observe(receiver: HIDReceiver) {
|
||||
receiver.packetEvents.onPacket<HIDDeviceInfo> { packet ->
|
||||
val device = receiver.getDevice(packet.hidId) ?: return@onPacket
|
||||
val deviceState = device.context.state.value
|
||||
|
||||
device.context.dispatch(
|
||||
DeviceActions.Update {
|
||||
@@ -90,17 +91,9 @@ class HIDDeviceInfoBehaviour : HIDReceiverBehaviour {
|
||||
)
|
||||
|
||||
val tracker = receiver.getTracker(packet.hidId)
|
||||
if (tracker == null) {
|
||||
val deviceState = device.context.state.value
|
||||
|
||||
val existingTracker = receiver.appContext.server.context.state.value.trackers.values
|
||||
?: receiver.appContext.server.context.state.value.trackers.values
|
||||
.find { it.context.state.value.hardwareId == deviceState.address && it.context.state.value.origin == DeviceOrigin.HID }
|
||||
|
||||
if (existingTracker != null) {
|
||||
receiver.context.dispatch(HIDReceiverActions.TrackerRegistered(packet.hidId, existingTracker.context.state.value.id))
|
||||
// HID does not have a rest calibration signal
|
||||
existingTracker.context.dispatch(TrackerActions.Update { copy(sensorType = packet.imuType, completedRestCalibration = true) })
|
||||
} else {
|
||||
?: run {
|
||||
val trackerId = receiver.appContext.server.nextHandle()
|
||||
val newTracker = Tracker.create(
|
||||
scope = receiver.appContext.server.context.scope,
|
||||
@@ -112,13 +105,12 @@ class HIDDeviceInfoBehaviour : HIDReceiverBehaviour {
|
||||
appContext = receiver.appContext,
|
||||
)
|
||||
receiver.appContext.server.context.dispatch(VRServerActions.NewTracker(trackerId, newTracker))
|
||||
receiver.context.dispatch(HIDReceiverActions.TrackerRegistered(packet.hidId, trackerId))
|
||||
newTracker
|
||||
}
|
||||
} else {
|
||||
// HID does not have a rest calibration signal
|
||||
tracker.context.dispatch(TrackerActions.Update { copy(sensorType = packet.imuType, completedRestCalibration = true) })
|
||||
tracker.context.dispatch(TrackerActions.SetStatus(TrackerStatus.OK))
|
||||
}
|
||||
receiver.context.dispatch(HIDReceiverActions.TrackerRegistered(packet.hidId, tracker.context.state.value.id))
|
||||
// HID does not have a rest calibration signal
|
||||
tracker.context.dispatch(TrackerActions.Update { copy(sensorType = packet.imuType, completedRestCalibration = true, magStatus = packet.magStatus) })
|
||||
tracker.context.dispatch(TrackerActions.SetStatus(TrackerStatus.OK))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -137,7 +129,7 @@ class HIDRotationBehaviour : HIDReceiverBehaviour {
|
||||
|
||||
receiver.packetEvents.onPacket<HIDRotationMag> { packet ->
|
||||
val tracker = receiver.getTracker(packet.hidId) ?: return@onPacket
|
||||
tracker.context.dispatch(TrackerActions.SetRotation(rotation = packet.rotation))
|
||||
tracker.context.dispatch(TrackerActions.SetRotation(rotation = packet.rotation, magnetometer = packet.magnetometer))
|
||||
}
|
||||
|
||||
receiver.packetEvents.onPacket<HIDRotationButton> { packet ->
|
||||
|
||||
@@ -2,6 +2,7 @@ package dev.slimevr.hid
|
||||
|
||||
import io.github.axisangles.ktmath.Quaternion
|
||||
import io.github.axisangles.ktmath.Vector3
|
||||
import solarxr_protocol.datatypes.MagnetometerStatus
|
||||
import solarxr_protocol.datatypes.TrackerStatus
|
||||
import solarxr_protocol.datatypes.hardware_info.BoardType
|
||||
import solarxr_protocol.datatypes.hardware_info.ImuType
|
||||
@@ -28,6 +29,7 @@ data class HIDDeviceInfo(
|
||||
val batteryLevel: Float?,
|
||||
val batteryVoltage: Float,
|
||||
val rssi: Int,
|
||||
val magStatus: MagnetometerStatus,
|
||||
) : HIDPacket
|
||||
|
||||
/** Full-precision Q15 quaternion + Q7 acceleration (type 1). */
|
||||
@@ -162,6 +164,7 @@ private fun parseSingleHIDPacket(data: ByteArray, i: Int): HIDPacket? {
|
||||
val brdId = data[i + 5].toUByte().toInt()
|
||||
val mcuId = data[i + 6].toUByte().toInt()
|
||||
val imuId = data[i + 8].toUByte().toInt()
|
||||
val magId = data[i + 9].toUByte()
|
||||
val fwDate = data[i + 11].toUByte().toInt() shl 8 or data[i + 10].toUByte().toInt()
|
||||
val fwMajor = data[i + 12].toUByte().toInt()
|
||||
val fwMinor = data[i + 13].toUByte().toInt()
|
||||
@@ -179,6 +182,7 @@ private fun parseSingleHIDPacket(data: ByteArray, i: Int): HIDPacket? {
|
||||
batteryLevel = decodeBattery(batt),
|
||||
batteryVoltage = decodeBatteryVoltage(battV),
|
||||
rssi = -rssi,
|
||||
magStatus = MagnetometerStatus.fromValue(magId) ?: MagnetometerStatus.NOT_SUPPORTED,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ import solarxr_protocol.data_feed.tracker.TrackerDataMask
|
||||
import solarxr_protocol.data_feed.tracker.TrackerInfo
|
||||
import solarxr_protocol.datatypes.DeviceId
|
||||
import solarxr_protocol.datatypes.Ipv4Address
|
||||
import solarxr_protocol.datatypes.MagnetometerStatus
|
||||
import solarxr_protocol.datatypes.Temperature
|
||||
import solarxr_protocol.datatypes.TrackerId
|
||||
import solarxr_protocol.datatypes.hardware_info.HardwareInfo
|
||||
@@ -66,6 +67,7 @@ private fun createTracker(device: DeviceState, tracker: TrackerState, trackerMas
|
||||
linearAcceleration = if (trackerMask.linearAcceleration == true) tracker.acceleration.let { Vec3f(it.x, it.y, it.z) } else null,
|
||||
rotationReferenceAdjusted = if (trackerMask.rotationReferenceAdjusted == true) tracker.rotation.let { Quat(it.x, it.y, it.z, it.w) } else null,
|
||||
rotationIdentityAdjusted = if (trackerMask.rotationIdentityAdjusted == true) tracker.rotation.let { Quat(it.x, it.y, it.z, it.w) } else null, // FIXME: uses reference adjusted
|
||||
rawMagneticVector = if (trackerMask.rawMagneticVector == true && tracker.magStatus == MagnetometerStatus.ENABLED) tracker.rawMagnetometer.let { Vec3f(it.x, it.y, it.z) } else null,
|
||||
)
|
||||
|
||||
private fun createDevice(
|
||||
|
||||
@@ -242,11 +242,14 @@ class TrackerBasicBehaviour : TrackerBehaviour {
|
||||
else -> rawAcceleration
|
||||
}
|
||||
|
||||
val rawMagnetometer = action.magnetometer ?: state.rawMagnetometer
|
||||
|
||||
state.copy(
|
||||
rawRotation = rawRotation,
|
||||
rotation = rotation,
|
||||
rawAcceleration = rawAcceleration,
|
||||
acceleration = acceleration,
|
||||
rawMagnetometer = rawMagnetometer,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ data class TrackerState(
|
||||
val rotation: CalibratedRotation,
|
||||
val rawAcceleration: RawAcceleration,
|
||||
val acceleration: CalibratedAcceleration,
|
||||
val rawMagnetometer: Vector3, // TODO apply calibration
|
||||
val deviceId: Int,
|
||||
val origin: DeviceOrigin,
|
||||
val tps: UShort,
|
||||
@@ -43,7 +44,7 @@ sealed interface TrackerActions {
|
||||
data class Update(val transform: TrackerState.() -> TrackerState) : TrackerActions
|
||||
data class SetMagStatus(val status: MagnetometerStatus) : TrackerActions
|
||||
data class SetStatus(val status: TrackerStatus) : TrackerActions
|
||||
data class SetRotation(val rotation: Quaternion? = null, val acceleration: Vector3? = null) : TrackerActions
|
||||
data class SetRotation(val rotation: Quaternion? = null, val acceleration: Vector3? = null, val magnetometer: Vector3? = null) : TrackerActions
|
||||
data class FullReset(val referenceRotation: Quaternion) : TrackerActions
|
||||
data class YawReset(val referenceRotation: Quaternion) : TrackerActions
|
||||
data class MountingReset(val referenceRotation: Quaternion) : TrackerActions
|
||||
@@ -80,6 +81,7 @@ class Tracker(
|
||||
rotation = Quaternion.IDENTITY,
|
||||
rawAcceleration = Vector3.NULL,
|
||||
acceleration = Vector3.NULL,
|
||||
rawMagnetometer = Vector3.NULL,
|
||||
bodyPart = null,
|
||||
mountingOrientation = null,
|
||||
origin = origin,
|
||||
|
||||
Reference in new issue
Block a user