DeviceMotionEvent and DeviceOrientationEvent report the motion of the wearer’s head, while the Meta Neural Band reports the motion of the wearer’s wrist. Wrist orientation cannot be derived from the head APIs, and the two surfaces are read completely differently.RelativeOrientationSensor, selected with a sensorId of emg-wrist.const wrist = new RelativeOrientationSensor({ sensorId: 'emg-wrist' });
wrist.onreading = () => { /* wrist.quaternion */ };
wrist.start();
start(), but occasionally, it never resolves. Treat a sudden sign inversion early in a stream as the frame settling rather than as wearer input, and do not capture a reference until readings are stable.typeof RelativeOrientationSensor. On hosts without the wrist sensor, this name still resolves to the browser’s own constructor, which ignores sensorId entirely. You would silently subscribe to the orientation of the host device instead of the band.function hasWristSensor() {
const Ctor = window.RelativeOrientationSensor;
return typeof Ctor === 'function' && Ctor.__imuQuatWrapped === true;
}
sensor.quaternion and a sensor.timestamp.const wrist = new RelativeOrientationSensor({ sensorId: 'emg-wrist' });
wrist.addEventListener('reading', () => {
// Reorder into W3C [x, y, z, w] with the helpers in "Component order" below.
const q = toW3C(wrist.quaternion, wristQuaternionOrder());
if (!q) return;
const [x, y, z, w] = q;
});
wrist.addEventListener('error', (e) => {
// `NotAllowedError` when permission is denied or revoked mid-stream.
// `NotReadableError` when the stream cannot start, most often a band that is not being worn. Retry this with a backoff.
console.warn('wrist sensor stopped:', e.error.name);
});
wrist.start();
frequency option is accepted as a best-effort hint but is not yet honored, and the underlying source runs at a fixed rate. Derive your own delta time from timestamp between readings rather, than assume a fixed interval.timestamp records when the reading is delivered to your page, not when the band samples it, so it carries some scheduling jitter.[w, x, y, z], cyclically shifted by one from the [x, y, z, w] that the W3C Orientation Sensor specification defines. Newer builds say which order they use in RelativeOrientationSensor.__imuQuatOrder, either 'wxyz' or 'xyzw'. It applies to the emg-wrist sensor only and appears together with the __imuQuatWrapped marker. Read it once the marker is present, treat a missing value as 'wxyz', and reorder once at the boundary so everything downstream stays in spec order.// The order the runtime fills `quaternion` in. Builds without the property send w-first.
function wristQuaternionOrder() {
return window.RelativeOrientationSensor.__imuQuatOrder || 'wxyz';
}
// Reorder an incoming reading into W3C [x, y, z, w].
function toW3C(q, order) {
if (!q || q.length < 4) return null;
return order === 'xyzw' ? [q[0], q[1], q[2], q[3]] : [q[1], q[2], q[3], q[0]];
}
[x, y, z, w].const FOREARM_AXIS = [0, 1, 0];
// Signed twist about the forearm axis, in radians, from a W3C [x, y, z, w] quaternion.
function twistAngle(q) {
const d = q[0] * FOREARM_AXIS[0] + q[1] * FOREARM_AXIS[1] + q[2] * FOREARM_AXIS[2];
return 2 * Math.atan2(d, q[3]);
}
// Wrap to [-PI, PI) so the step from +179 to -179 degrees reads as 2 degrees, not 358.
function wrapAngle(a) {
const x = (a + Math.PI) % (2 * Math.PI);
return (x < 0 ? x + 2 * Math.PI : x) - Math.PI;
}
let reference = null;
wrist.addEventListener('reading', () => {
// Reorder first. Passing a raw w-first reading to twistAngle projects onto the
// wrong components and yields an angle that moves but means nothing.
const q = toW3C(wrist.quaternion, wristQuaternionOrder());
if (!q) return;
const angle = twistAngle(q);
if (reference === null) reference = angle;
const roll = wrapAngle(angle - reference); // offset from the reference, wrapped to +/-180 degrees
});
// Rotate a vector by a W3C [x, y, z, w] quaternion.
function rotate(q, v) {
const [x, y, z, w] = q;
const [vx, vy, vz] = v;
const tx = 2 * (y * vz - z * vy);
const ty = 2 * (z * vx - x * vz);
const tz = 2 * (x * vy - y * vx);
return [
vx + w * tx + (y * tz - z * ty),
vy + w * ty + (z * tx - x * tz),
vz + w * tz + (x * ty - y * tx),
];
}
// Where the forearm is aimed, in the world frame. The unit is as long as the reading is.
function pointingDirection(q) {
return rotate(q, FOREARM_AXIS);
}
stop(). The subscription is not merely how you read the samples-- it is what makes them exist.start() once when your input becomes relevant and keep the sensor running for as long as you need continuous readings.stop() when you are done. Leaving a subscription open keeps the band in its higher-power streaming mode and costs user battery.start() to be unreliable while the band’s fusion settles. Discard them before capturing a reference, or you will zero your control at a pose the wearer was never in.NotReadableError after about five seconds, but it does not always manage to. Keep your own silence timer as a backstop, and set it comfortably longer than five seconds so the platform gets to name the failure first.DeviceMotionEvent and DeviceOrientationEvent. Since no separate wrist permission exists, granting sensor access once covers both the head and wrist surfaces for your domain.start() triggers the permission prompt if the wearer has not already decided. Handle the outcome on the sensor’s error event rather than a promise:error event with NotAllowedError.error event with NotAllowedError. Re-calling start() will not restore it until the wearer re-grants the permission.| Consider | Avoid |
|---|---|
Feature-detect with the __imuQuatWrapped marker. | typeof RelativeOrientationSensor, which silently subscribes to the wrong device. |
Reorder each reading into [x, y, z, w] once, at the boundary, using __imuQuatOrder. | Passing the raw array to code that expects specification order, or hardcoding the order. |
Derive the one quantity your interaction needs from the quaternion. | Decomposing to Euler angles and steering from a single axis. |
Treat any angle as an offset from a reference you captured. | Trusting an absolute heading, which drifts. |
Hold one subscription for the life of the interaction. | Creating and destroying a sensor per gesture. |
Derive delta time from timestamp. | Assuming readings arrive at the frequency you requested. |
Offer a pinch or keyboard path to the same action. | Making wrist movement the only way to complete a task. |
Stop the sensor when the input is no longer needed. | Leaving the band in continuous mode for the whole session. |