Skip to content

Commit 67e5fe1

Browse files
committed
Fix acceleration axis
1 parent 01d2f5a commit 67e5fe1

File tree

5 files changed

+21
-22
lines changed

5 files changed

+21
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ local.properties
4646

4747
# Ignore temporary config
4848
vrconfig.yml.tmp
49+
*.DS_Store

server/core/src/main/java/dev/slimevr/tracking/trackers/Tracker.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dev.slimevr.tracking.trackers.TrackerPosition.Companion.getByDesignation
66
import dev.slimevr.tracking.trackers.udp.IMUType
77
import dev.slimevr.tracking.trackers.udp.MagnetometerStatus
88
import dev.slimevr.tracking.trackers.udp.TrackerDataType
9+
import io.eiren.math.FloatMath.INV_SQRT_TWO
910
import io.eiren.util.BufferedTimer
1011
import io.github.axisangles.ktmath.Quaternion
1112
import io.github.axisangles.ktmath.Vector3
@@ -437,4 +438,13 @@ class Tracker @JvmOverloads constructor(
437438
fun resetFilteringQuats() {
438439
filteringHandler.resetMovingAverage(_rotation)
439440
}
441+
442+
companion object {
443+
/**
444+
* Changes from IMU axis to OpenGL/SteamVR axis
445+
*/
446+
fun axisOffset(v: Vector3): Vector3 = Vector3(v.x, v.z, -v.y)
447+
private val rotationOffset = Quaternion(INV_SQRT_TWO, -INV_SQRT_TWO, 0f, 0f)
448+
fun axisOffset(q: Quaternion): Quaternion = rotationOffset * q
449+
}
440450
}

server/core/src/main/java/dev/slimevr/tracking/trackers/udp/TrackersUDPServer.kt

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package dev.slimevr.tracking.trackers.udp
22

3-
import com.jme3.math.FastMath
43
import dev.slimevr.NetworkProtocol
54
import dev.slimevr.VRServer
65
import dev.slimevr.config.config
76
import dev.slimevr.protocol.rpc.MAG_TIMEOUT
87
import dev.slimevr.tracking.trackers.*
8+
import dev.slimevr.tracking.trackers.Tracker.Companion.axisOffset
99
import io.eiren.util.Util
1010
import io.eiren.util.collections.FastList
1111
import io.eiren.util.logging.LogManager
12-
import io.github.axisangles.ktmath.Quaternion.Companion.fromRotationVector
13-
import io.github.axisangles.ktmath.Vector3
1412
import kotlinx.coroutines.*
1513
import org.apache.commons.lang3.ArrayUtils
1614
import solarxr_protocol.rpc.ResetType
@@ -387,13 +385,12 @@ class TrackersUDPServer(private val port: Int, name: String, private val tracker
387385

388386
is RotationPacket -> {
389387
var rot = packet.rotation
390-
rot = AXES_OFFSET.times(rot)
388+
rot = axisOffset(rot)
391389
tracker = connection?.getTracker(packet.sensorId)
392390
if (tracker == null) return
393391
tracker.setRotation(rot)
394392
if (packet is UDPPacket23RotationAndAcceleration) {
395-
// Switch x and y around to adjust for different axes
396-
tracker.setAcceleration(Vector3(packet.acceleration.y, packet.acceleration.x, packet.acceleration.z))
393+
tracker.setAcceleration(axisOffset(packet.acceleration))
397394
}
398395
tracker.dataTick()
399396
}
@@ -402,7 +399,7 @@ class TrackersUDPServer(private val port: Int, name: String, private val tracker
402399
tracker = connection?.getTracker(packet.sensorId)
403400
if (tracker == null) return
404401
var rot17 = packet.rotation
405-
rot17 = AXES_OFFSET * rot17
402+
rot17 = axisOffset(rot17)
406403
when (packet.dataType) {
407404
UDPPacket17RotationData.DATA_TYPE_NORMAL -> {
408405
tracker.setRotation(rot17)
@@ -425,8 +422,7 @@ class TrackersUDPServer(private val port: Int, name: String, private val tracker
425422
is UDPPacket4Acceleration -> {
426423
tracker = connection?.getTracker(packet.sensorId)
427424
if (tracker == null) return
428-
// Switch x and y around to adjust for different axes
429-
tracker.setAcceleration(Vector3(packet.acceleration.y, packet.acceleration.x, packet.acceleration.z))
425+
tracker.setAcceleration(axisOffset(packet.acceleration))
430426
}
431427

432428
is UDPPacket10PingPong -> {
@@ -600,10 +596,6 @@ class TrackersUDPServer(private val port: Int, name: String, private val tracker
600596
}
601597

602598
companion object {
603-
/**
604-
* Change between IMU axes and OpenGL/SteamVR axes
605-
*/
606-
private val AXES_OFFSET = fromRotationVector(-FastMath.HALF_PI, 0f, 0f)
607599
private const val RESET_SOURCE_NAME = "TrackerServer"
608600
private fun packetToString(packet: DatagramPacket?): String {
609601
val sb = StringBuilder()

server/core/src/main/java/io/eiren/math/FloatMath.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ object FloatMath {
1717
const val ZERO_TOLERANCE_D: Double = 0.0001
1818

1919
val SQRT_TWO: Float = kotlin.math.sqrt(2.0).toFloat()
20-
val INV_SQRT_TWO: Float = 1f / SQRT_TWO
20+
val INV_SQRT_TWO: Float = (1.0 / kotlin.math.sqrt(2.0)).toFloat()
2121
val SQRT_THREE: Float = kotlin.math.sqrt(3.0).toFloat()
2222
val INV_SQRT_THREE: Float = 1f / SQRT_THREE
2323
const val TWO_FPI: Float = PI * 2

server/desktop/src/main/java/dev/slimevr/desktop/tracking/trackers/hid/TrackersHID.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package dev.slimevr.desktop.tracking.trackers.hid
22

3-
import com.jme3.math.FastMath
43
import dev.slimevr.VRServer
54
import dev.slimevr.tracking.trackers.Device
65
import dev.slimevr.tracking.trackers.Tracker
6+
import dev.slimevr.tracking.trackers.Tracker.Companion.axisOffset
77
import dev.slimevr.tracking.trackers.TrackerStatus
88
import dev.slimevr.tracking.trackers.udp.BoardType
99
import dev.slimevr.tracking.trackers.udp.IMUType
1010
import dev.slimevr.tracking.trackers.udp.MCUType
1111
import io.eiren.util.logging.LogManager
1212
import io.github.axisangles.ktmath.Quaternion
13-
import io.github.axisangles.ktmath.Quaternion.Companion.fromRotationVector
1413
import io.github.axisangles.ktmath.Vector3
1514
import org.hid4java.HidDevice
1615
import org.hid4java.HidException
@@ -389,7 +388,7 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
389388
// x y z w -> w x y z
390389
var rot = Quaternion(q[3].toFloat(), q[0].toFloat(), q[1].toFloat(), q[2].toFloat())
391390
val scaleRot = 1 / (1 shl 15).toFloat() // compile time evaluation
392-
rot = AXES_OFFSET.times(scaleRot).times(rot) // no division
391+
rot = axisOffset(rot * scaleRot) // no division
393392
tracker.setRotation(rot)
394393
}
395394
if (packetType == 2) {
@@ -408,13 +407,14 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
408407
val s = sin(a)
409408
val k = s * invSqrtD
410409
var rot = Quaternion(cos(a), k * v[0], k * v[1], k * v[2])
411-
rot = AXES_OFFSET.times(rot) // no division
410+
rot = axisOffset(rot) // no division
412411
tracker.setRotation(rot)
413412
}
414413
if (packetType == 1 || packetType == 2) {
415414
// TODO: Acceleration "was" wrong
416415
val scaleAccel = 1 / (1 shl 7).toFloat() // compile time evaluation
417416
var acceleration = Vector3(a[0].toFloat(), a[1].toFloat(), a[2].toFloat()).times(scaleAccel) // no division
417+
acceleration = axisOffset(acceleration)
418418
tracker.setAcceleration(acceleration)
419419
tracker.dataTick() // only data tick if there is rotation data
420420
}
@@ -481,10 +481,6 @@ class TrackersHID(name: String, private val trackersConsumer: Consumer<Tracker>)
481481
}
482482

483483
companion object {
484-
/**
485-
* Change between IMU axes and OpenGL/SteamVR axes
486-
*/
487-
private val AXES_OFFSET = fromRotationVector(-FastMath.HALF_PI, 0f, 0f)
488484
private const val resetSourceName = "TrackerServer"
489485
}
490486
}

0 commit comments

Comments
 (0)