forked from Blendman974/faceclaw
Simplify glanceboard-trigger angle setting; fix incorrectly-rotated accelerometer readings on G1
The glanceboard head-tilt option was more complicated than necessary, because it wanted the user to take two angle measurements, a baseline angle and a tilt angle. That was a workaround to not knowing the accelerometer's orientation/calibration, but actually, we do know those. (Except, as it turns out, on the G1, the accelerometer orientation is different than it is on the G2, and there was a bug where EvenHub apps got their accelerometer readings in the wrong orientation. So for example the EvenHub Level app didn't work correctly.)
This commit is contained in:
1 parent
37d81d0308
commit
e09f8eae96
17 files changed
+255
-281
No files matched your search
@@ -1,67 +0,0 @@
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
|
||||
const {
|
||||
MAX_GLANCE_ANGLE,
|
||||
MIN_GLANCE_ANGLE,
|
||||
angleBetweenDegrees,
|
||||
glanceAngleForTilt,
|
||||
meanVector,
|
||||
roundGlanceAngle,
|
||||
} = require("../.test-build/app/g2/head-tilt-math.js");
|
||||
|
||||
const rad = (deg) => (deg * Math.PI) / 180;
|
||||
|
||||
/**
|
||||
* Gravity (m/s^2) after pitching the head by pitchDeg, in a sensor frame
|
||||
* mounted so that no axis lines up with the head: the level reading points
|
||||
* along an arbitrary direction, and the pitch rotates it about an arbitrary
|
||||
* axis perpendicular to that (Rodrigues' formula).
|
||||
*/
|
||||
function gravityPitched(pitchDeg) {
|
||||
const v = normalize({ x: 0.3, y: -0.9, z: 0.2 });
|
||||
const k0 = { x: 0.9, y: 0.4, z: 0.45 };
|
||||
const dot = k0.x * v.x + k0.y * v.y + k0.z * v.z;
|
||||
const k = normalize({ x: k0.x - dot * v.x, y: k0.y - dot * v.y, z: k0.z - dot * v.z });
|
||||
const c = Math.cos(rad(pitchDeg));
|
||||
const s = Math.sin(rad(pitchDeg));
|
||||
const cross = { x: k.y * v.z - k.z * v.y, y: k.z * v.x - k.x * v.z, z: k.x * v.y - k.y * v.x };
|
||||
return { x: 9.8 * (v.x * c + cross.x * s), y: 9.8 * (v.y * c + cross.y * s), z: 9.8 * (v.z * c + cross.z * s) };
|
||||
}
|
||||
|
||||
function normalize(v) {
|
||||
const m = Math.hypot(v.x, v.y, v.z);
|
||||
return { x: v.x / m, y: v.y / m, z: v.z / m };
|
||||
}
|
||||
|
||||
test("the tilt between two readings does not depend on the sensor's axes, past 45° too", () => {
|
||||
const level = gravityPitched(0);
|
||||
for (const pitch of [5, 20, 30, 45, 50, 60, 75]) {
|
||||
assert.ok(Math.abs(angleBetweenDegrees(level, gravityPitched(pitch)) - pitch) < 1e-6, `pitch ${pitch}`);
|
||||
}
|
||||
// From a non-zero level reading the difference is what counts.
|
||||
assert.ok(Math.abs(angleBetweenDegrees(gravityPitched(-10), gravityPitched(40)) - 50) < 1e-6);
|
||||
assert.ok(Math.abs(angleBetweenDegrees(level, level)) < 1e-6);
|
||||
});
|
||||
|
||||
test("meanVector averages and rejects empty or zero input", () => {
|
||||
assert.deepEqual(meanVector([{ x: 1, y: 2, z: 3 }, { x: 3, y: 2, z: 1 }]), { x: 2, y: 2, z: 2 });
|
||||
assert.equal(meanVector([]), null);
|
||||
assert.equal(meanVector([{ x: 0, y: 0, z: 0 }]), null);
|
||||
});
|
||||
|
||||
test("roundGlanceAngle rounds to 5° within the setting's range", () => {
|
||||
assert.equal(roundGlanceAngle(27.4), 25);
|
||||
assert.equal(roundGlanceAngle(27.5), 30);
|
||||
assert.equal(roundGlanceAngle(1), MIN_GLANCE_ANGLE);
|
||||
assert.equal(roundGlanceAngle(80), MAX_GLANCE_ANGLE);
|
||||
});
|
||||
|
||||
test("glanceAngleForTilt reports a head that did not move or went too far instead of clamping", () => {
|
||||
assert.deepEqual(glanceAngleForTilt(31), { ok: true, rounded: 30 });
|
||||
assert.deepEqual(glanceAngleForTilt(52), { ok: true, rounded: 50 });
|
||||
assert.deepEqual(glanceAngleForTilt(3), { ok: true, rounded: 5 });
|
||||
assert.equal(glanceAngleForTilt(0.5).ok, false);
|
||||
assert.equal(glanceAngleForTilt(65).ok, false);
|
||||
assert.equal(glanceAngleForTilt(NaN).ok, false);
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
|
||||
const { imuPitchDegrees } = require("../.test-build/app/native/imu-types.js");
|
||||
|
||||
const rad = (deg) => (deg * Math.PI) / 180;
|
||||
|
||||
/**
|
||||
* A reading at rest in the canonical frame (+x forward, +y left, +z up), in
|
||||
* m/s^2 to show the scale does not matter: the head pitched up by pitchDeg,
|
||||
* then rolled by rollDeg.
|
||||
*/
|
||||
function reading(pitchDeg, rollDeg = 0) {
|
||||
const p = rad(pitchDeg);
|
||||
const r = rad(rollDeg);
|
||||
return { x: 9.8 * Math.sin(p), y: 9.8 * Math.cos(p) * Math.sin(r), z: 9.8 * Math.cos(p) * Math.cos(r) };
|
||||
}
|
||||
|
||||
test("imuPitchDegrees reads the head's pitch from one reading, past 45° and with the head rolled", () => {
|
||||
for (const pitch of [-30, 0, 5, 20, 45, 50, 60, 75]) {
|
||||
for (const roll of [0, 30, -40]) {
|
||||
assert.ok(Math.abs(imuPitchDegrees(reading(pitch, roll)) - pitch) < 1e-6, `pitch ${pitch} roll ${roll}`);
|
||||
}
|
||||
}
|
||||
// The poses measured on a G2 (in g).
|
||||
const s = Math.SQRT1_2;
|
||||
assert.ok(Math.abs(imuPitchDegrees({ x: 0, y: 0, z: 1 })) < 1e-9);
|
||||
assert.ok(Math.abs(imuPitchDegrees({ x: s, y: 0, z: s }) - 45) < 1e-9);
|
||||
assert.ok(Math.abs(imuPitchDegrees({ x: 0, y: s, z: s })) < 1e-9);
|
||||
});
|
||||
@@ -1130,7 +1130,8 @@ class G1FeaturesTest {
|
||||
}
|
||||
s.core.setImuReportEnabled(true, 200)
|
||||
assertTrue(awaitTrue(3_000) { lock.withLock { readings.size >= 5 } }, "samples: " + lock.withLock { readings.size } + "\n" + s.tail())
|
||||
assertEquals(listOf(0.1, -1.0, 0.25, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() })
|
||||
// The G1's (x, y, z) arrive in the G2's frame as (-z, -x, y) (ImuOrientation).
|
||||
assertEquals(listOf(-0.25, -0.1, -1.0, BleProtocol.EVENT_SOURCE_GLASSES_R.toDouble()), lock.withLock { readings.first() })
|
||||
assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_STREAM } })
|
||||
s.core.setImuReportEnabled(false, 0)
|
||||
assertTrue(awaitTrue(1_000) { s.glasses.lock.withLock { s.glasses.rightArm.props[G1Protocol.PROP_IMU_RATE] == G1Protocol.IMU_RATE_IDLE } })
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.sqrt
|
||||
import kotlin.test.*
|
||||
|
||||
class ImuOrientationTest {
|
||||
private val s = sqrt(0.5)
|
||||
|
||||
private fun assertVec(expected: DoubleArray, actual: DoubleArray, what: String) {
|
||||
for (i in 0..2) assertTrue(abs(expected[i] - actual[i]) < 1e-9, "$what: expected ${expected.toList()}, got ${actual.toList()}")
|
||||
}
|
||||
|
||||
/** The poses measured on hardware (level, 45° up, 45° to the right) read the G2's values on every model. */
|
||||
@Test fun measuredPosesMatchTheG2Frame() {
|
||||
val level = doubleArrayOf(0.0, 0.0, 1.0)
|
||||
val up = doubleArrayOf(s, 0.0, s)
|
||||
val right = doubleArrayOf(0.0, s, s)
|
||||
val g1 = listOf(doubleArrayOf(0.0, 1.0, 0.0), doubleArrayOf(0.0, s, -s), doubleArrayOf(-s, s, 0.0))
|
||||
for ((name, canonical, raw) in listOf(Triple("level", level, g1[0]), Triple("up", up, g1[1]), Triple("right", right, g1[2]))) {
|
||||
assertVec(canonical, ImuOrientation.toCanonical(GlassesType.G1, raw[0], raw[1], raw[2]), "G1 $name")
|
||||
assertVec(canonical, ImuOrientation.toCanonical(GlassesType.G2, canonical[0], canonical[1], canonical[2]), "G2 $name")
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -46,7 +46,7 @@
|
||||
"../app/g2/glasses-artwork.ts",
|
||||
"../app/g2/glasses-display-state.ts",
|
||||
"../app/g2/glance-state.ts",
|
||||
"../app/g2/head-tilt-math.ts",
|
||||
"../app/native/imu-types.ts",
|
||||
"../app/ui/shell/notification-modal-queue.ts",
|
||||
"../app/ui/shell/notification-popup-delay.ts",
|
||||
"../app/ui/shell/notification-popup-timeout.ts",
|
||||
|
||||
Reference in new issue
Block a user