Compare commits

...
7 Commits
10 changed files with 137 additions and 35 deletions

No files matched your search

+1 -1
View File
@@ -255,7 +255,7 @@ jobs:
- name: Build
run: |
rustup target add aarch64-apple-darwin
rustup target add x86_64-apple-darwin
pnpm i
pnpm run tauri build --target universal-apple-darwin
+1 -1
View File
@@ -15,7 +15,7 @@ export function waitUntil(
tries?: number
): Promise<void> {
return new Promise((resolve, rej) => {
const isPromise = typeof condition() === 'boolean';
const isPromise = typeof condition() !== 'boolean';
const interval = setInterval(() => {
if (tries && --tries === 0) {
error(new Error('waitUntil ran out of tries'));
@@ -308,12 +308,26 @@ class VRServer @JvmOverloads constructor(
queueTask { humanPoseManager.clearTrackersMounting(resetSourceName) }
}
fun getPauseTracking(): Boolean = humanPoseManager.getPauseTracking()
fun setPauseTracking(pauseTracking: Boolean, sourceName: String?) {
queueTask { humanPoseManager.setPauseTracking(pauseTracking, sourceName) }
queueTask {
humanPoseManager.setPauseTracking(pauseTracking, sourceName)
// Toggle trackers as they don't toggle when tracking is paused
if (this.getVRBridge(ISteamVRBridge::class.java)?.updateShareSettingsAutomatically() == true) {
RPCSettingsHandler.sendSteamVRUpdatedSettings(protocolAPI, protocolAPI.rpcHandler)
}
}
}
fun togglePauseTracking(sourceName: String?) {
queueTask { humanPoseManager.togglePauseTracking(sourceName) }
queueTask {
humanPoseManager.togglePauseTracking(sourceName)
// Toggle trackers as they don't toggle when tracking is paused
if (this.getVRBridge(ISteamVRBridge::class.java)?.updateShareSettingsAutomatically() == true) {
RPCSettingsHandler.sendSteamVRUpdatedSettings(protocolAPI, protocolAPI.rpcHandler)
}
}
}
fun scheduleResetTrackersFull(resetSourceName: String?, delay: Long) {
@@ -461,7 +461,7 @@ class RPCHandler(private val api: ProtocolAPI) : ProtocolHandler<RpcMessageHeade
val req = messageHeader
.message(SetPauseTrackingRequest()) as? SetPauseTrackingRequest ?: return
api.server.humanPoseManager.setPauseTracking(req.pauseTracking(), RESET_SOURCE_NAME)
api.server.setPauseTracking(req.pauseTracking(), RESET_SOURCE_NAME)
}
fun onHeightRequest(conn: GenericConnection, messageHeader: RpcMessageHeader?) {
@@ -690,6 +690,8 @@ class HumanPoseManager(val server: VRServer?) {
get() = skeletonConfigManager.userHeightFromOffsets
// #endregion
fun getPauseTracking(): Boolean = skeleton.getPauseTracking()
fun setPauseTracking(pauseTracking: Boolean, sourceName: String?) {
skeleton.setPauseTracking(pauseTracking, sourceName)
}
@@ -44,6 +44,11 @@ enum class ServerFeatureFlags {
/** Server can parse bundle packets: `PACKET_BUNDLE` = 100 (0x64). */
PROTOCOL_BUNDLE_SUPPORT,
/** Server can parse bundle packets with compact headers and packed IMU rotation/acceleration frames:
- `PACKET_BUNDLE_COMPACT` = 101 (0x65),
- `PACKET_ROTATION_AND_ACCELERATION` = 23 (0x17). */
PROTOCOL_BUNDLE_COMPACT_SUPPORT,
// Add new flags here
BITS_TOTAL, ;
@@ -51,6 +56,7 @@ enum class ServerFeatureFlags {
companion object {
val flagsEnabled: Set<ServerFeatureFlags> = setOf(
PROTOCOL_BUNDLE_SUPPORT,
PROTOCOL_BUNDLE_COMPACT_SUPPORT,
// Add enabled flags here
)
@@ -322,7 +322,8 @@ class TrackersUDPServer(private val port: Int, name: String, private val tracker
if (tracker == null) return
tracker.setRotation(rot)
if (packet is UDPPacket23RotationAndAcceleration) {
tracker.setAcceleration(packet.acceleration)
// Switch x and y around to adjust for different axes
tracker.setAcceleration(Vector3(packet.acceleration.y, packet.acceleration.x, packet.acceleration.z))
}
tracker.dataTick()
}
@@ -40,6 +40,25 @@ class UDPProtocolParser {
buf.position(bundlePacketStart + bundlePacketLen)
}
return bundlePackets.toTypedArray()
} else if (packetId == PACKET_BUNDLE_COMPACT) {
bundlePackets.clear()
while (buf.hasRemaining()) {
val bundlePacketLen = Math.min(buf.get().toUByte().toInt(), buf.remaining()) // 1 byte
if (bundlePacketLen == 0) continue
val bundlePacketStart = buf.position()
val bundleBuf = buf.slice()
bundleBuf.limit(bundlePacketLen)
val bundlePacketId = bundleBuf.get().toUByte().toInt() // 1 byte
val newPacket = getNewPacket(bundlePacketId)
newPacket?.let {
newPacket.readData(bundleBuf)
bundlePackets.add(newPacket)
}
buf.position(bundlePacketStart + bundlePacketLen)
}
return bundlePackets.toTypedArray()
}
val newPacket = getNewPacket(packetId)
@@ -129,6 +148,7 @@ class UDPProtocolParser {
const val PACKET_FEATURE_FLAGS = 22
const val PACKET_ROTATION_AND_ACCELERATION = 23
const val PACKET_BUNDLE = 100
const val PACKET_BUNDLE_COMPACT = 101
const val PACKET_PROTOCOL_CHANGE = 200
private val HANDSHAKE_BUFFER = ByteArray(64)
private val bundlePackets = ArrayList<UDPPacket>(128)
@@ -20,7 +20,7 @@ import solarxr_protocol.rpc.StatusDataUnion
import solarxr_protocol.rpc.StatusSteamVRDisconnectedT
abstract class SteamVRBridge(
server: VRServer,
protected val server: VRServer,
threadName: String,
bridgeName: String,
protected val bridgeSettingsKey: String,
@@ -53,7 +53,9 @@ abstract class SteamVRBridge(
}
override fun updateShareSettingsAutomatically(): Boolean {
if (!config.automaticSharedTrackersToggling) return false
// Return false if automatic trackers is disabled or if tracking is paused
if (!config.automaticSharedTrackersToggling || server.getPauseTracking()) return false
val skeleton = instance.humanPoseManager.skeleton
val isWaistSteamVr = skeleton.hipTracker?.device?.isOpenVrDevice == true ||
skeleton.waistTracker?.device?.isOpenVrDevice == true
@@ -110,6 +112,7 @@ abstract class SteamVRBridge(
changeShareSettings(TrackerRole.RIGHT_ELBOW, hasRightElbow && !isRightElbowSteamVr)
// Hands aren't touched as they will override the controller's tracking
// Return true to say that trackers were successfully toggled automatically
return true
}
@@ -16,9 +16,14 @@ import org.hid4java.HidServices
import org.hid4java.HidServicesListener
import org.hid4java.HidServicesSpecification
import org.hid4java.event.HidServicesEvent
import org.hid4java.jna.HidApi
import org.hid4java.jna.HidDeviceInfoStructure
import java.util.function.Consumer
import kotlin.experimental.and
private const val HID_TRACKER_RECEIVER_VID = 0x2FE3
private const val HID_TRACKER_RECEIVER_PID = 0x5652
/**
* Receives trackers data by UDP using extended owoTrack protocol.
*/
@@ -39,10 +44,16 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
dataReadThread.isDaemon = true
dataReadThread.name = "hid4java data reader"
dataReadThread.start()
// We use hid4java but actually do not start the service ever, because it will just enumerate everything and cause problems
// Do enumeration ourself
val deviceEnumerateThread = Thread(deviceEnumerateRunnable)
deviceEnumerateThread.isDaemon = true
deviceEnumerateThread.name = "hid4java device enumerator"
deviceEnumerateThread.start()
}
private fun checkConfigureDevice(hidDevice: HidDevice) {
if (hidDevice.vendorId == 0x2FE3 && hidDevice.productId == 0x5652) { // TODO: Use correct ids
if (hidDevice.vendorId == HID_TRACKER_RECEIVER_VID && hidDevice.productId == HID_TRACKER_RECEIVER_PID) { // TODO: Use correct ids
if (hidDevice.isClosed) {
check(hidDevice.open()) { "Unable to open device" }
}
@@ -141,13 +152,6 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
@get:Synchronized
private val dataReadRunnable: Runnable
get() = Runnable {
try {
sleep(100) // Delayed start
} catch (e: InterruptedException) {
currentThread().interrupt()
return@Runnable
}
hidServices.start()
while (true) {
try {
sleep(0) // Possible performance impact
@@ -159,9 +163,31 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
}
}
@get:Synchronized
private val deviceEnumerateRunnable: Runnable
get() = Runnable {
try {
sleep(100) // Delayed start
} catch (e: InterruptedException) {
currentThread().interrupt()
return@Runnable
}
while (true) {
try {
sleep(1000)
} catch (e: InterruptedException) {
currentThread().interrupt()
break
}
deviceEnumerate() // not in try catch?
}
}
private fun dataRead() {
synchronized(devicesByHID) {
var devicesPresent = false
val q = intArrayOf(0, 0, 0, 0)
val a = intArrayOf(0, 0, 0)
for ((hidDevice, deviceList) in devicesByHID) {
val dataReceived: Array<Byte> = try {
hidDevice.read(80, 0) // Read up to 80 bytes
@@ -183,20 +209,17 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
val idCombination = dataReceived[i + 1].toInt()
val rssi = -dataReceived[i + 2].toInt()
val battery = dataReceived[i + 3].toInt()
val battery_mV = dataReceived[i + 5].toInt() and 255 shl 8 or (dataReceived[i + 4].toInt() and 255)
val q = floatArrayOf(0f, 0f, 0f, 0f)
val a = floatArrayOf(0f, 0f, 0f)
for (j in 0..3) { // quat received as fixed 14
var buf =
dataReceived[i + 6 + j * 2 + 1].toInt() and 255 shl 8 or (dataReceived[i + 6 + j * 2].toInt() and 255)
buf -= 32768 // uint to int
q[j] = buf / (1 shl 14).toFloat() // fixed 14 to float
// ushort big endian
val battery_mV = dataReceived[i + 5].toUByte().toInt() shl 8 or dataReceived[i + 4].toUByte().toInt()
// Q15: 1 is represented as 0x7FFF, -1 as 0x8000
// The sender can use integer saturation to avoid overflow
for (j in 0..3) { // quat received as fixed Q15
// Q15 as short big endian
q[j] = dataReceived[i + 6 + j * 2 + 1].toInt() shl 8 or dataReceived[i + 6 + j * 2].toUByte().toInt()
}
for (j in 0..2) { // accel received as fixed 7, in m/s
var buf =
dataReceived[i + 14 + j * 2 + 1].toInt() and 255 shl 8 or (dataReceived[i + 14 + j * 2].toInt() and 255)
buf -= 32768 // uint to int
a[j] = buf / (1 shl 7).toFloat() // fixed 7 to float
// Q15 as short big endian
a[j] = dataReceived[i + 14 + j * 2 + 1].toInt() shl 8 or dataReceived[i + 14 + j * 2].toUByte().toInt()
}
val trackerId = idCombination and 0b1111
val deviceId = (idCombination shr 4) and 0b1111
@@ -209,11 +232,17 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
// tracker.batteryVoltage = if (battery and 128 == 128) 5f else 0f // Charge status
tracker.batteryVoltage = battery_mV.toFloat() * 0.001f
tracker.batteryLevel = (battery and 127).toFloat()
var rot = Quaternion(q[0], q[1], q[2], q[3])
rot = AXES_OFFSET.times(rot)
// The data comes in the same order as in the UDP protocol
// x y z w -> w x y z
var rot = Quaternion(q[3].toFloat(), q[0].toFloat(), q[1].toFloat(), q[2].toFloat())
val scaleRot = 1 / (1 shl 15).toFloat() // compile time evaluation
rot = AXES_OFFSET.times(scaleRot).times(rot) // no division
tracker.setRotation(rot)
// TODO: I think the acceleration is wrong???
var acceleration = Vector3(a[0], a[1], a[2])
// Yes it was. And rotation was wrong too.
// At lease we have fixed the fixed point decoding.
val scaleAccel = 1 / (1 shl 7).toFloat() // compile time evaluation
var acceleration = Vector3(a[0].toFloat(), a[1].toFloat(), a[2].toFloat()).times(scaleAccel) // no division
tracker.setAcceleration(acceleration)
tracker.dataTick()
i += 20
@@ -227,25 +256,52 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
}
}
private fun deviceEnumerate() {
var root: HidDeviceInfoStructure? = null
try {
root = HidApi.enumerateDevices(HID_TRACKER_RECEIVER_VID, HID_TRACKER_RECEIVER_PID) // TODO: change to proper vendorId and productId, need to enum all appropriate productId
} catch (e: Throwable) {
LogManager.severe("[TrackerServer] Couldn't enumerate HID devices", e)
}
val hidDeviceList: MutableList<HidDevice> = mutableListOf()
if (root != null) {
var hidDeviceInfoStructure: HidDeviceInfoStructure? = root
do {
hidDeviceList.add(HidDevice(hidDeviceInfoStructure, null, hidServicesSpecification))
hidDeviceInfoStructure = hidDeviceInfoStructure?.next()
} while (hidDeviceInfoStructure != null)
HidApi.freeEnumeration(root)
}
synchronized(devicesByHID) {
// Work on devicesByHid and add/remove as necessary
val removeList: MutableList<HidDevice> = devicesByHID.keys.toMutableList()
removeList.removeAll(hidDeviceList)
hidDeviceList.removeAll(devicesByHID.keys) // addList
for (device in removeList) {
removeDevice(device)
}
for (device in hidDeviceList) {
checkConfigureDevice(device)
}
}
}
override fun run() { // Doesn't seem to run
}
fun getDevices(): List<Device> = devices
// We don't use these
override fun hidDeviceAttached(event: HidServicesEvent) {
checkConfigureDevice(event.hidDevice)
}
override fun hidDeviceDetached(event: HidServicesEvent) {
removeDevice(event.hidDevice)
}
override fun hidFailure(event: HidServicesEvent) {
// TODO:
}
override fun hidDataReceived(p0: HidServicesEvent?) {
// Seems to have issues with the size of data returned
}
companion object {