Improve reset code and make it so VRTrackers can reset (#480)

This commit is contained in:
Erimel authored and GitHub committed 2023-01-19 04:33:33 +03:00
1 parent 03f9115f75
commit 9415eceb85
2 files changed
+66 -12

No files matched your search

@@ -253,7 +253,7 @@ public class IMUTracker
* Calculates reference-adjusted rotation (with full/quick reset) including
* the mounting orientation (front, back, left, right) and mounting reset
* adjustment. Also taking drift compensation into account.
*
*
* @param store Where to store the calculation result.
*/
@Override
@@ -406,7 +406,7 @@ public class IMUTracker
fixGyroscope(getMountedAdjustedRotation());
fixAttachment(getMountedAdjustedRotation());
makeIdentityAdjustmentQuatsFull();
fixYaw(reference);
fixYaw(getMountedAdjustedRotation(), reference);
calibrateMag();
calculateDrift(rot);
}
@@ -421,7 +421,7 @@ public class IMUTracker
@Override
public void resetYaw(Quaternion reference) {
Quaternion rot = getAdjustedRawRotation();
fixYaw(reference);
fixYaw(getMountedAdjustedRotation(), reference);
makeIdentityAdjustmentQuatsYaw();
calibrateMag();
calculateDrift(rot);
@@ -432,7 +432,7 @@ public class IMUTracker
* mounting-reset-adjusted by applying quaternions produced after
* {@link #resetFull(Quaternion)}, {@link #resetYaw(Quaternion)} and
* {@link #resetMounting(boolean)}.
*
*
* @param store Raw or filtered rotation to mutate.
*/
protected void adjustToReference(Quaternion store) {
@@ -456,13 +456,15 @@ public class IMUTracker
}
private void fixGyroscope(Quaternion sensorRotation) {
sensorRotation = sensorRotation.clone();
sensorRotation.fromAngles(0, sensorRotation.getYaw(), 0);
gyroFix.set(sensorRotation).inverseLocal();
gyroFix.set(sensorRotation.inverseLocal());
}
private void fixAttachment(Quaternion sensorRotation) {
sensorRotation = sensorRotation.clone();
gyroFix.mult(sensorRotation, sensorRotation);
attachmentFix.set(sensorRotation).inverseLocal();
attachmentFix.set(sensorRotation.inverseLocal());
}
@Override
@@ -495,19 +497,19 @@ public class IMUTracker
yawFix.multLocal(buffer.inverseLocal());
}
private void fixYaw(Quaternion reference) {
private void fixYaw(Quaternion sensorRotation, Quaternion reference) {
// Use only yaw HMD rotation
Quaternion targetRotation = reference.clone();
targetRotation.fromAngles(0, targetRotation.getYaw(), 0);
reference = reference.clone();
reference.fromAngles(0, reference.getYaw(), 0);
Quaternion sensorRotation = getMountedAdjustedRotation();
sensorRotation = sensorRotation.clone();
gyroFix.mult(sensorRotation, sensorRotation);
sensorRotation.multLocal(attachmentFix);
sensorRotation.multLocal(mountRotFix);
sensorRotation.fromAngles(0, sensorRotation.getYaw(), 0);
yawFix.set(sensorRotation).inverseLocal().multLocal(targetRotation);
yawFix.set(sensorRotation.inverseLocal().multLocal(reference));
}
private void calibrateMag() {
@@ -523,7 +525,7 @@ public class IMUTracker
* Calculates drift since last reset and store the data related to it in
* driftQuat, timeAtLastReset and timeForLastReset
*/
synchronized private void calculateDrift(Quaternion beforeQuat) {
private void calculateDrift(Quaternion beforeQuat) {
if (compensateDrift && allowDriftCompensation) {
Quaternion rotQuat = getAdjustedRawRotation();
@@ -1,5 +1,6 @@
package dev.slimevr.vr.trackers;
import com.jme3.math.Quaternion;
import dev.slimevr.vr.Device;
import io.eiren.util.BufferedTimer;
@@ -7,6 +8,9 @@ import io.eiren.util.BufferedTimer;
public class VRTracker extends ComputedTracker {
protected BufferedTimer timer = new BufferedTimer(1f);
public final Quaternion mountFix = new Quaternion();
public final Quaternion attachmentFix = new Quaternion();
public final Quaternion yawFix = new Quaternion();
public VRTracker(
int id,
@@ -23,6 +27,54 @@ public class VRTracker extends ComputedTracker {
super(id, name, name, hasRotation, hasPosition, null);
}
@Override
public boolean getRotation(Quaternion store) {
store.set(rotation);
// Don't adjust if tracker is HMD/Head (use raw rotation)
if (super.getBodyPosition() != TrackerPosition.HMD)
adjustInternal(store);
return true;
}
// TODO Reduce code duplication from IMUTracker.java
// Refactor all tracker classes into one?
protected void adjustInternal(Quaternion store) {
store.multLocal(attachmentFix);
store.multLocal(mountFix);
yawFix.mult(store, store);
}
@Override
public void resetFull(Quaternion reference) {
fixAttachment(rotation, reference);
fixYaw(rotation, reference);
}
@Override
public void resetYaw(Quaternion reference) {
fixYaw(rotation, reference);
}
private void fixAttachment(Quaternion sensorRotation, Quaternion reference) {
mountFix.fromAngles(0, reference.getYaw(), 0);
attachmentFix.set(sensorRotation.inverse());
}
private void fixYaw(Quaternion sensorRotation, Quaternion reference) {
// Use only yaw HMD rotation
reference = reference.clone();
reference.fromAngles(0, reference.getYaw(), 0);
sensorRotation = sensorRotation.clone();
sensorRotation.multLocal(attachmentFix);
sensorRotation.multLocal(mountFix);
sensorRotation.fromAngles(0, sensorRotation.getYaw(), 0);
yawFix.set(sensorRotation).inverseLocal().multLocal(reference);
}
@Override
public float getTPS() {
return timer.getAverageFPS();