Merge branch 'main' into fixAxis

This commit is contained in:
Ilia Ki authored and GitHub committed 2025-05-13 02:06:14 +07:00
commit d3b57048e9
8 files changed
+178 -35

No files matched your search

@@ -9,7 +9,7 @@ import { PerspectiveCamera, Vector3 } from 'three';
import { Button } from '@/components/commons/Button';
import { QuatObject } from '@/maths/quaternion';
import { useLocalization } from '@fluent/react';
import { Vector3Object } from '@/maths/vector3';
import { Vector3Object, Vector3FromVec3fT } from '@/maths/vector3';
import { Gltf } from '@react-three/drei';
import { ErrorBoundary } from 'react-error-boundary';
@@ -28,12 +28,20 @@ export function TrackerModel({ model }: { model: string }) {
function SceneRenderer({
quat,
vec,
mag,
model,
}: {
quat: QuatObject;
vec: Vector3Object;
mag: Vector3Object;
model: string;
}) {
const magDir = new Vector3(mag.x, mag.y, mag.z);
const magLen = magDir.length();
const magMag = Math.sqrt(magLen / 100); // normalize magnituge
if (magLen > 0)
magDir.multiplyScalar(1/ magLen);
return (
<Canvas
className="container"
@@ -56,23 +64,17 @@ function SceneRenderer({
<arrowHelper
args={[
new Vector3(-Math.sign(vec.x), 0, 0),
Vector3FromVec3fT(vec).normalize(),
new Vector3(0, 0, 0),
Math.sqrt(Math.abs(vec.x)) * 2,
Math.sqrt(Vector3FromVec3fT(vec).length()) * 2,
]}
/>
<arrowHelper
args={[
new Vector3(0, Math.sign(vec.y), 0),
new Vector3(0, 0, 0),
Math.sqrt(Math.abs(vec.y)) * 2,
]}
/>
<arrowHelper
args={[
new Vector3(0, 0, -Math.sign(vec.z)),
new Vector3(0, 0, 0),
Math.sqrt(Math.abs(vec.z)) * 2,
magDir,
magDir.clone().multiplyScalar(-magMag),
2 * magMag,
THREE.Color.NAMES.aqua,
]}
/>
@@ -116,6 +118,9 @@ export function IMUVisualizerWidget({ tracker }: { tracker: TrackerDataT }) {
tracker?.linearAcceleration ||
tracker?.rawAcceleration ||
new THREE.Vector3();
const mag =
tracker?.rawMagneticVector ||
new THREE.Vector3();
return (
<div className="bg-background-70 flex flex-col p-3 rounded-lg gap-2">
@@ -157,6 +162,17 @@ export function IMUVisualizerWidget({ tracker }: { tracker: TrackerDataT }) {
</div>
)}
{tracker.rawMagneticVector && (
<div className="flex justify-between">
<Typography color="secondary">
{l10n.getString('tracker-infos-magnetometer')}
</Typography>
<Typography>
{formatVector3(tracker.rawMagneticVector, 1)}
</Typography>
</div>
)}
{!enabled && (
<Button
variant="secondary"
@@ -189,6 +205,7 @@ export function IMUVisualizerWidget({ tracker }: { tracker: TrackerDataT }) {
<SceneRenderer
quat={{ ...quat }}
vec={{ ...vec }}
mag={{ ...mag }}
model={
isExtension ? '/models/extension.gltf' : '/models/tracker.gltf'
}
+1
View File
@@ -17,6 +17,7 @@ export function useDataFeedConfig() {
trackerData.rotationReferenceAdjusted = true;
trackerData.rotationIdentityAdjusted = true;
trackerData.tps = true;
trackerData.rawMagneticVector = true;
const dataMask = new DeviceDataMaskT();
dataMask.deviceData = true;
@@ -3,6 +3,7 @@ package dev.slimevr.protocol.datafeed;
import com.google.flatbuffers.FlatBufferBuilder;
import dev.slimevr.tracking.trackers.Device;
import dev.slimevr.tracking.trackers.Tracker;
import dev.slimevr.tracking.trackers.udp.MagnetometerStatus;
import dev.slimevr.tracking.trackers.udp.UDPDevice;
import io.github.axisangles.ktmath.Quaternion;
import io.github.axisangles.ktmath.Vector3;
@@ -80,6 +81,16 @@ public class DataFeedBuilder {
return TrackerId.endTrackerId(fbb);
}
public static int createVec3(FlatBufferBuilder fbb, Vector3 vec) {
return Vec3f
.createVec3f(
fbb,
vec.getX(),
vec.getY(),
vec.getZ()
);
}
public static int createQuat(FlatBufferBuilder fbb, Quaternion quaternion) {
return Quat
.createQuat(
@@ -146,14 +157,7 @@ public class DataFeedBuilder {
}
public static int createTrackerPosition(FlatBufferBuilder fbb, Tracker tracker) {
Vector3 pos = tracker.getPosition();
return Vec3f
.createVec3f(
fbb,
pos.getX(),
pos.getY(),
pos.getZ()
);
return createVec3(fbb, tracker.getPosition());
}
public static int createTrackerRotation(FlatBufferBuilder fbb, Tracker tracker) {
@@ -161,14 +165,11 @@ public class DataFeedBuilder {
}
public static int createTrackerAcceleration(FlatBufferBuilder fbb, Tracker tracker) {
Vector3 accel = tracker.getAcceleration();
return Vec3f
.createVec3f(
fbb,
accel.getX(),
accel.getY(),
accel.getZ()
);
return createVec3(fbb, tracker.getAcceleration());
}
public static int createTrackerMagneticVector(FlatBufferBuilder fbb, Tracker tracker) {
return createVec3(fbb, tracker.getMagVector());
}
public static int createTrackerTemperature(FlatBufferBuilder fbb, Tracker tracker) {
@@ -233,6 +234,9 @@ public class DataFeedBuilder {
if (mask.getTps()) {
TrackerData.addTps(fbb, (int) tracker.getTps());
}
if (mask.getRawMagneticVector() && tracker.getMagStatus() == MagnetometerStatus.ENABLED) {
TrackerData.addRawMagneticVector(fbb, createTrackerMagneticVector(fbb, tracker));
}
return TrackerData.endTrackerData(fbb);
}
@@ -93,6 +93,7 @@ class Tracker @JvmOverloads constructor(
private var timeAtLastUpdate: Long = System.currentTimeMillis()
private var _rotation = Quaternion.IDENTITY
private var _acceleration = Vector3.NULL
private var _magVector = Vector3.NULL
var position = Vector3.NULL
val resetsHandler: TrackerResetsHandler = TrackerResetsHandler(this)
val filteringHandler: TrackerFilteringHandler = TrackerFilteringHandler()
@@ -425,6 +426,22 @@ class Tracker @JvmOverloads constructor(
}
}
/**
* Gets the magnetic field vector, in mGauss.
*/
fun getMagVector() = if (needsReset) {
resetsHandler.getReferenceAdjustedAccel(_rotation, _magVector)
} else {
_magVector
}
/**
* Sets the magnetic field vector.
*/
fun setMagVector(vec: Vector3) {
this._magVector = vec
}
/**
* Gets the current TPS of the tracker
*/
@@ -3,6 +3,7 @@ package dev.slimevr.unit
import com.jme3.math.FastMath
import io.github.axisangles.ktmath.EulerOrder
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import org.junit.jupiter.api.AssertionFailureBuilder
import kotlin.math.abs
@@ -80,4 +81,9 @@ object TrackerTestUtils {
FastMath.isApproxEqual(q1.x, q2.x, tolerance) &&
FastMath.isApproxEqual(q1.y, q2.y, tolerance) &&
FastMath.isApproxEqual(q1.z, q2.z, tolerance)
fun vectorApproxEqual(v1: Vector3, v2: Vector3, tolerance: Float = FastMath.ZERO_TOLERANCE): Boolean =
FastMath.isApproxEqual(v1.x, v2.x, tolerance) &&
FastMath.isApproxEqual(v1.y, v2.y, tolerance) &&
FastMath.isApproxEqual(v1.z, v2.z, tolerance)
}
@@ -0,0 +1,62 @@
package dev.slimevr.unit
import dev.slimevr.tracking.processor.HumanPoseManager
import dev.slimevr.unit.TrackerTestUtils.quatApproxEqual
import dev.slimevr.unit.TrackerTestUtils.vectorApproxEqual
import io.github.axisangles.ktmath.Quaternion
import io.github.axisangles.ktmath.Vector3
import org.junit.jupiter.api.Test
import kotlin.test.assertNotNull
class TrackingPauseTests {
val resetSource = "Unit Test"
@Test
fun testTrackingPause() {
val trackers = TestTrackerSet()
// Initialize skeleton and everything
val hpm = HumanPoseManager(trackers.allL)
// TODO: This being enabled makes waist position infinity initially but not
// later? Something weird is going on here...
hpm.setLegTweaksEnabled(false)
hpm.update()
// Store expected tracker data
val expected = mutableMapOf<Int, Pair<Quaternion, Vector3>>()
for (tracker in hpm.computedTrackers) {
expected.put(tracker.id, Pair(tracker.getRotation(), tracker.position))
}
// Pause tracking, nothing should move after this
hpm.setPauseTracking(true, resetSource)
// Randomize tracker orientations, this should not affect the skeleton!
for ((i, tracker) in trackers.set.withIndex()) {
tracker.setRotation(TrackerTestUtils.testRotFromIndex(i))
}
// Tick the skeleton with random tracker rotations
hpm.update()
// Since we paused before moving anything, the output should still be identity
for (tracker in hpm.computedTrackers) {
val trackerExpected = expected[tracker.id]
assertNotNull(trackerExpected)
val expectedRot = trackerExpected.first
val expectedPos = trackerExpected.second
val actualRot = tracker.getRotation()
val actualPos = tracker.position
assert(quatApproxEqual(expectedRot, actualRot)) {
"\"${tracker.name}\" moved after being paused. Expected <$expectedRot>, actual <$actualRot>."
}
assert(vectorApproxEqual(expectedPos, actualPos)) {
"\"${tracker.name}\" moved after being paused. Expected <$expectedPos>, actual <$actualPos>."
}
}
}
}
@@ -216,6 +216,7 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
var devicesDataReceived = false
val q = intArrayOf(0, 0, 0, 0)
val a = intArrayOf(0, 0, 0)
val m = intArrayOf(0, 0, 0)
for ((hidDevice, deviceList) in devicesByHID) {
val dataReceived: ByteArray = try {
hidDevice.readAll(0) // multiples 64 bytes
@@ -317,7 +318,7 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
// Q15 as short big endian
q[j] = dataReceived[i + 2 + j * 2 + 1].toInt() shl 8 or dataReceived[i + 2 + j * 2].toUByte().toInt()
}
for (j in 0..2) { // accel received as fixed 7, in m/s
for (j in 0..2) { // accel received as fixed 7, in m/s^2
// Q7 as short big endian
a[j] = dataReceived[i + 10 + j * 2 + 1].toInt() shl 8 or dataReceived[i + 10 + j * 2].toUByte().toInt()
}
@@ -335,7 +336,7 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
q[0] = (q_buf and 1023u).toInt()
q[1] = (q_buf shr 10 and 2047u).toInt()
q[2] = (q_buf shr 21 and 2047u).toInt()
for (j in 0..2) { // accel received as fixed 7, in m/s
for (j in 0..2) { // accel received as fixed 7, in m/s^2
// Q7 as short big endian
a[j] = dataReceived[i + 9 + j * 2 + 1].toInt() shl 8 or dataReceived[i + 9 + j * 2].toUByte().toInt()
}
@@ -348,6 +349,17 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
rssi = dataReceived[i + 15].toUByte().toInt()
}
4 -> { // full precision quat and mag, no extra data
for (j in 0..3) { // quat received as fixed Q15
// Q15 as short big endian
q[j] = dataReceived[i + 2 + j * 2 + 1].toInt() shl 8 or dataReceived[i + 2 + j * 2].toUByte().toInt()
}
for (j in 0..2) { // mag received as fixed 10, in gauss
// Q10 as short big endian
m[j] = dataReceived[i + 10 + j * 2 + 1].toInt() shl 8 or dataReceived[i + 10 + j * 2].toUByte().toInt()
}
}
else -> {
}
}
@@ -394,7 +406,7 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
}
// Assign rotation and acceleration
if (packetType == 1) {
if (packetType == 1 || packetType == 4) {
// 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())
@@ -422,11 +434,31 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
tracker.setRotation(rot)
}
if (packetType == 1 || packetType == 2) {
// TODO: Acceleration "was" wrong
// Acceleration is in local device frame
// On flat surface / face up:
// Right side of the device is +X
// Front side (facing up) is +Z
// Mounted on body / standing up:
// Top side of the device is +Y
// Front side (facing out) is +Z
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
acceleration = axisOffset(acceleration)
tracker.setAcceleration(acceleration)
}
if (packetType == 4) {
// Magnetometer is in local device frame
// On flat surface / face up:
// Right side of the device is +X
// Front side (facing up) is +Z
// Mounted on body / standing up:
// Top side of the device is +Y
// Front side (facing out) is +Z
val scaleMag = 1000 / (1 shl 10).toFloat() // compile time evaluation, and change gauss to milligauss
var magnetometer = Vector3(m[0].toFloat(), m[1].toFloat(), m[2].toFloat()).times(scaleMag) // no division
tracker.setMagVector(magnetometer)
}
if (packetType == 1 || packetType == 2 || packetType == 4) {
tracker.dataTick() // only data tick if there is rotation data
}
@@ -463,10 +495,14 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
// 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)
}
// Quickly reattaching a device may not be detected, so always try to open existing devices
for (device in devicesByHID.keys) {
device.open()
}
hidDeviceList.removeAll(devicesByHID.keys) // addList
for (device in hidDeviceList) {
checkConfigureDevice(device)
}