Remove ReferenceAdjustedTracker (#394)

This commit is contained in:
Erimel authored and GitHub committed 2022-12-16 18:37:28 +03:00
1 parent 88f649ae5c
commit e7cf1a3008
11 files changed
+183 -297

No files matched your search

@@ -1436,8 +1436,8 @@ public final class Quaternion implements Cloneable, java.io.Serializable {
float invNorm = 1.0f / norm;
return new Quaternion(-x * invNorm, -y * invNorm, -z * invNorm, w * invNorm);
}
// return an invalid result to flag the error
return null;
// return itself since it has no inverse
return this;
}
/**
@@ -150,6 +150,18 @@ public class PoseFrameTracker implements Tracker, Iterable<TrackerFrame> {
return false;
}
@Override
public boolean getRawRotation(Quaternion store) {
TrackerFrame frame = safeGetFrame();
if (frame != null && frame.hasData(TrackerFrameData.ROTATION)) {
store.set(frame.rotation);
return true;
}
store.set(Quaternion.IDENTITY);
return false;
}
@Override
public boolean getPosition(Vector3f store) {
TrackerFrame frame = safeGetFrame();
@@ -94,6 +94,17 @@ public final class TrackerFrame implements Tracker {
return false;
}
@Override
public boolean getRawRotation(Quaternion store) {
if (hasData(TrackerFrameData.ROTATION)) {
store.set(rotation);
return true;
}
store.set(Quaternion.IDENTITY);
return false;
}
@Override
public boolean getPosition(Vector3f store) {
if (hasData(TrackerFrameData.POSITION)) {
@@ -174,7 +174,7 @@ public class RPCHandler extends ProtocolHandler<RpcMessageHeader>
this.api.server.resetTrackers();
if (req.resetType() == ResetType.Mounting)
this.api.server.resetTrackersMounting();
LogManager.severe("[WebSocketAPI] Reset performed");
LogManager.info("[WebSocketAPI] Reset performed");
}
public void onAssignTrackerRequest(GenericConnection conn, RpcMessageHeader messageHeader) {
@@ -80,6 +80,12 @@ public class ComputedTracker implements Tracker, TrackerWithTPS {
return true;
}
@Override
public boolean getRawRotation(Quaternion store) {
store.set(rotation);
return true;
}
@Override
public boolean getAcceleration(Vector3f store) {
store.set(0, 0, 0);
@@ -23,12 +23,17 @@ public class IMUTracker
// public final Vector3f gyroVector = new Vector3f();
public final Vector3f accelVector = new Vector3f();
public final Vector3f magVector = new Vector3f();
// public final Vector3f magVector = new Vector3f();
public final Quaternion rotQuaternion = new Quaternion();
public final Quaternion rotMagQuaternion = new Quaternion();
public final Quaternion rotAdjust = new Quaternion();
public final Quaternion mountAdjust = new Quaternion();
public final UDPDevice device;
public final int trackerNum;
public final Vector3f rotVector = new Vector3f();
public final Quaternion gyroFix = new Quaternion();
public final Quaternion attachmentFix = new Quaternion();
public final Quaternion mountRotFix = new Quaternion();
public final Quaternion yawFix = new Quaternion();
protected final Quaternion correction = new Quaternion();
protected final int trackerId;
protected final String name;
@@ -70,10 +75,12 @@ public class IMUTracker
this.descriptiveName = descriptiveName;
this.vrserver = vrserver;
setFiltering(
vrserver.getConfigManager().getVrConfig().getFilters().enumGetType(),
vrserver.getConfigManager().getVrConfig().getFilters().getAmount()
);
if (vrserver != null) {
setFiltering(
vrserver.getConfigManager().getVrConfig().getFilters().enumGetType(),
vrserver.getConfigManager().getVrConfig().getFilters().getAmount()
);
}
}
@Override
@@ -89,16 +96,15 @@ public class IMUTracker
@Override
public void readConfig(TrackerConfig config) {
// Loading a config is an act of user editing, therefore it shouldn't
// not be
// allowed if editing is not allowed
// be allowed if editing is not allowed
if (userEditable()) {
setCustomName(config.getCustomName());
if (config.getMountingRotation() != null) {
mounting = config.getMountingRotation();
rotAdjust.set(config.getMountingRotation());
mountAdjust.set(config.getMountingRotation());
} else {
rotAdjust.loadIdentity();
mountAdjust.loadIdentity();
}
Optional<TrackerPosition> trackerPosition = TrackerPosition
.getByDesignation(config.getDesignation());
@@ -117,9 +123,9 @@ public class IMUTracker
public void setMountingRotation(Quaternion mr) {
mounting = mr;
if (mounting != null) {
rotAdjust.set(mounting);
mountAdjust.set(mounting);
} else {
rotAdjust.loadIdentity();
mountAdjust.loadIdentity();
}
}
@@ -189,9 +195,15 @@ public class IMUTracker
store.set(rotQuaternion);
}
// correction.mult(store, store); // Correction is not used now to
// prevent
// accidental errors while debugging other things
store.multLocal(rotAdjust);
// prevent accidental errors while debugging other things
store.multLocal(mountAdjust);
adjustInternal(store);
return true;
}
@Override
public boolean getRawRotation(Quaternion store) {
store.set(rotQuaternion);
return true;
}
@@ -250,27 +262,106 @@ public class IMUTracker
this.batteryVoltage = voltage;
}
/**
* Reset the tracker so that its current rotation is counted as (0, <HMD
* Yaw>, 0). This allows the tracker to be strapped to body at any pitch and
* roll.
* <p>
* Performs {@link #resetYaw(Quaternion)} for yaw drift correction.
*/
@Override
public void resetFull(Quaternion reference) {
fixGyroscope(getMountedAdjustedRotation());
fixAttachment(getMountedAdjustedRotation());
resetYaw(reference);
}
/**
* Does not perform actual gyro reset to reference, that's the task of
* reference adjusted tracker. Only aligns gyro with magnetometer if it's
* reliable
* Reset the tracker so that it's current yaw rotation is counted as <HMD
* Yaw>. This allows the tracker to have yaw independent of the HMD. Tracker
* should still report yaw as if it was mounted facing HMD, mounting
* position should be corrected in the source. Also aligns gyro magnetometer
* if it's reliable.
*/
@Override
public void resetYaw(Quaternion reference) {
fixYaw(reference);
if (magCalibrationStatus >= CalibrationAccuracy.HIGH.status) {
magnetometerCalibrated = true;
// During calibration set correction to match magnetometer readings
// exactly
// TODO : Correct only yaw
correction.set(rotQuaternion).inverseLocal().multLocal(rotMagQuaternion);
}
}
protected void adjustInternal(Quaternion store) {
gyroFix.mult(store, store);
store.multLocal(attachmentFix);
store.multLocal(mountRotFix);
yawFix.mult(store, store);
}
private Quaternion getMountedAdjustedRotation() {
return rotQuaternion.mult(mountAdjust);
}
private void fixGyroscope(Quaternion sensorRotation) {
sensorRotation.fromAngles(0, sensorRotation.getYaw(), 0);
gyroFix.set(sensorRotation).inverseLocal();
}
private void fixAttachment(Quaternion sensorRotation) {
gyroFix.mult(sensorRotation, sensorRotation);
attachmentFix.set(sensorRotation).inverseLocal();
}
@Override
public void resetMounting(boolean reverseYaw) {
// Get the current calibrated rotation
Quaternion buffer = getMountedAdjustedRotation();
gyroFix.mult(buffer, buffer);
buffer.multLocal(attachmentFix);
// Reset the vector for the rotation to point straight up
rotVector.set(0f, 1f, 0f);
// Rotate the vector by the quat, then flatten and normalize the vector
buffer.multLocal(rotVector).setY(0f).normalizeLocal();
// Calculate the yaw angle using tan
// Just use an angle offset of zero for unsolvable circumstances
float yawAngle = FastMath.isApproxZero(rotVector.x) && FastMath.isApproxZero(rotVector.z)
? 0f
: FastMath.atan2(rotVector.x, rotVector.z);
// Make an adjustment quaternion from the angle
buffer.fromAngles(0f, reverseYaw ? yawAngle : yawAngle - FastMath.PI, 0f);
Quaternion lastRotAdjust = mountRotFix.clone();
mountRotFix.set(buffer);
// Get the difference from the last adjustment
buffer.multLocal(lastRotAdjust.inverseLocal());
// Apply the yaw rotation difference to the yaw fix quaternion
yawFix.multLocal(buffer.inverseLocal());
}
private void fixYaw(Quaternion reference) {
// Use only yaw HMD rotation
Quaternion targetRotation = reference.clone();
targetRotation.fromAngles(0, targetRotation.getYaw(), 0);
Quaternion sensorRotation = getMountedAdjustedRotation();
gyroFix.mult(sensorRotation, sensorRotation);
sensorRotation.multLocal(attachmentFix);
sensorRotation.multLocal(mountRotFix);
sensorRotation.fromAngles(0, sensorRotation.getYaw(), 0);
yawFix.set(sensorRotation).inverseLocal().multLocal(targetRotation);
}
/**
* Calculate correction between normal and magnetometer readings up to
* accuracy threshold
@@ -280,10 +371,6 @@ public class IMUTracker
// TODO Print "jump" length when correcting if it's more than 1 degree
}
@Override
public void resetMounting(boolean reverseYaw) {
}
@Override
public TrackerPosition getBodyPosition() {
return bodyPosition;
@@ -1,232 +0,0 @@
package dev.slimevr.vr.trackers;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import dev.slimevr.config.TrackerConfig;
import dev.slimevr.vr.Device;
public class ReferenceAdjustedTracker<E extends Tracker> implements Tracker {
public final E tracker;
// Vector for mounting rotation correction (tilt direction)
public final Vector3f rotVector = new Vector3f();
public final Quaternion gyroFix = new Quaternion();
public final Quaternion attachmentFix = new Quaternion();
public final Quaternion mountRotFix = new Quaternion();
public final Quaternion yawFix = new Quaternion();
protected float confidenceMultiplier = 1.0f;
public ReferenceAdjustedTracker(E tracker) {
this.tracker = tracker;
}
@Override
public boolean userEditable() {
return this.tracker.userEditable();
}
@Override
public void readConfig(TrackerConfig config) {
this.tracker.readConfig(config);
}
@Override
public void writeConfig(TrackerConfig config) {
this.tracker.writeConfig(config);
}
/**
* Reset the tracker so that its current rotation is counted as (0, <HMD
* Yaw>, 0). This allows the tracker to be strapped to body at any pitch and
* roll.
* <p>
* Performs {@link #resetYaw(Quaternion)} for yaw drift correction.
*/
@Override
public void resetFull(Quaternion reference) {
tracker.resetFull(reference);
Quaternion sensorRotation = new Quaternion();
tracker.getRotation(sensorRotation);
fixGyroscope(sensorRotation.clone());
fixAttachment(sensorRotation.clone());
fixYaw(reference);
}
/**
* Reset the tracker so that it's current yaw rotation is counted as <HMD
* Yaw>. This allows the tracker to have yaw independent of the HMD. Tracker
* should still report yaw as if it was mounted facing HMD, mounting
* position should be corrected in the source.
*/
@Override
public void resetYaw(Quaternion reference) {
tracker.resetYaw(reference);
fixYaw(reference);
}
private void fixGyroscope(Quaternion sensorRotation) {
sensorRotation.fromAngles(0, sensorRotation.getYaw(), 0);
gyroFix.set(sensorRotation).inverseLocal();
}
private void fixAttachment(Quaternion sensorRotation) {
gyroFix.mult(sensorRotation, sensorRotation);
attachmentFix.set(sensorRotation).inverseLocal();
}
@Override
public void resetMounting(boolean reverseYaw) {
tracker.resetMounting(reverseYaw);
// Get the current calibrated rotation
Quaternion buffer = new Quaternion();
tracker.getRotation(buffer);
gyroFix.mult(buffer, buffer);
buffer.multLocal(attachmentFix);
// Reset the vector for the rotation to point straight up
rotVector.set(0f, 1f, 0f);
// Rotate the vector by the quat, then flatten and normalize the vector
buffer.multLocal(rotVector).setY(0f).normalizeLocal();
// Calculate the yaw angle using tan
// Just use an angle offset of zero for unsolvable circumstances
float yawAngle = FastMath.isApproxZero(rotVector.x) && FastMath.isApproxZero(rotVector.z)
? 0f
: FastMath.atan2(rotVector.x, rotVector.z);
// Make an adjustment quaternion from the angle
buffer.fromAngles(0f, reverseYaw ? yawAngle : yawAngle - FastMath.PI, 0f);
Quaternion lastRotAdjust = new Quaternion(mountRotFix);
mountRotFix.set(buffer);
// Get the difference from the last adjustment
buffer.multLocal(lastRotAdjust.inverseLocal());
// Apply the yaw rotation difference to the yaw fix quaternion
yawFix.multLocal(buffer.inverseLocal());
}
private void fixYaw(Quaternion reference) {
// Use only yaw HMD rotation
Quaternion targetRotation = new Quaternion(reference);
targetRotation.fromAngles(0, targetRotation.getYaw(), 0);
Quaternion sensorRotation = new Quaternion();
tracker.getRotation(sensorRotation);
gyroFix.mult(sensorRotation, sensorRotation);
sensorRotation.multLocal(attachmentFix);
sensorRotation.multLocal(mountRotFix);
sensorRotation.fromAngles(0, sensorRotation.getYaw(), 0);
yawFix.set(sensorRotation).inverseLocal().multLocal(targetRotation);
}
protected void adjustInternal(Quaternion store) {
gyroFix.mult(store, store);
store.multLocal(attachmentFix);
store.multLocal(mountRotFix);
yawFix.mult(store, store);
}
@Override
public boolean getRotation(Quaternion store) {
tracker.getRotation(store);
adjustInternal(store);
return true;
}
@Override
public boolean getAcceleration(Vector3f store) {
return tracker.getAcceleration(store);
}
@Override
public boolean getPosition(Vector3f store) {
return tracker.getPosition(store);
}
@Override
public String getName() {
return tracker.getName();
}
@Override
public TrackerStatus getStatus() {
return tracker.getStatus();
}
@Override
public float getConfidenceLevel() {
return tracker.getConfidenceLevel() * confidenceMultiplier;
}
@Override
public TrackerPosition getBodyPosition() {
return tracker.getBodyPosition();
}
@Override
public void setBodyPosition(TrackerPosition position) {
tracker.setBodyPosition(position);
}
@Override
public void tick() {
tracker.tick();
}
@Override
public boolean hasRotation() {
return tracker.hasRotation();
}
@Override
public boolean hasPosition() {
return tracker.hasPosition();
}
@Override
public boolean isComputed() {
return tracker.isComputed();
}
@Override
public int getTrackerId() {
return tracker.getTrackerId();
}
@Override
public int getTrackerNum() {
return tracker.getTrackerNum();
}
@Override
public Device getDevice() {
return tracker.getDevice();
}
@Override
public Tracker get() {
return this.tracker;
}
@Override
public String getDisplayName() {
return this.tracker.getDisplayName();
}
@Override
public String getCustomName() {
return this.tracker.getCustomName();
}
}
@@ -20,6 +20,8 @@ public interface Tracker {
boolean getRotation(Quaternion store);
boolean getRawRotation(Quaternion store);
boolean getAcceleration(Vector3f store);
String getName();
@@ -64,9 +66,7 @@ public interface Tracker {
/**
* Returns the real tracker behind this tracker. In case this tracker
* transforms another tracker like {@link ReferenceAdjustedTracker}, this
* will return the tracker it modifies. Otherwise, it will return itself.
* Returns the real tracker behind this tracker.
*/
Tracker get();
@@ -6,13 +6,11 @@ import com.jme3.math.Vector3f;
import dev.slimevr.Main;
import dev.slimevr.NetworkProtocol;
import dev.slimevr.vr.trackers.IMUTracker;
import dev.slimevr.vr.trackers.ReferenceAdjustedTracker;
import dev.slimevr.vr.trackers.Tracker;
import dev.slimevr.vr.trackers.TrackerStatus;
import io.eiren.util.Util;
import io.eiren.util.collections.FastList;
import io.eiren.util.logging.LogManager;
import org.apache.commons.lang3.ArrayUtils;
import java.io.IOException;
@@ -239,12 +237,9 @@ public class TrackersUDPServer extends Thread {
this,
Main.vrServer
);
ReferenceAdjustedTracker<IMUTracker> adjustedTracker = new ReferenceAdjustedTracker<>(
imu
);
connection.getTrackers().add(imu);
trackersConsumer.accept(adjustedTracker);
trackersConsumer.accept(imu);
LogManager
.info(
"[TrackerServer] Added sensor "