@openhps/web-sensors
Version:
Open Hybrid Positioning System - Web Sensors API
56 lines • 2.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SensorSourceNode = void 0;
const core_1 = require("@openhps/core");
/**
* IMU source using the HTML5 browser API for device motion and device orientation.
* @category Source node
*/
class SensorSourceNode extends core_1.SourceNode {
constructor(options) {
var _a;
super(options);
this.options.source = (_a = this.options.source) !== null && _a !== void 0 ? _a : new core_1.DataObject(this.uid);
if (this.options.autoStart) {
this.once('build', this.start.bind(this));
}
this.once('destroy', this.stop.bind(this));
}
requestPermission() {
return Promise.resolve();
}
start() {
return new Promise((resolve) => {
const sensorUID = this.source ? this.source.uid : this.uid;
window.addEventListener('devicemotion', (event) => {
// Create a new data frame for the orientation change
const dataFrame = new core_1.DataFrame();
const frequency = 1000 / event.interval;
dataFrame.addSensor(new core_1.Accelerometer(sensorUID + '_accl', new core_1.Acceleration(event.accelerationIncludingGravity.x, event.accelerationIncludingGravity.y, event.accelerationIncludingGravity.z, core_1.AccelerationUnit.METER_PER_SECOND_SQUARE), frequency));
dataFrame.addSensor(new core_1.Gyroscope(sensorUID + '_gyro', new core_1.AngularVelocity(event.rotationRate.beta, event.rotationRate.gamma, event.rotationRate.alpha, core_1.AngularVelocityUnit.RADIAN_PER_SECOND), frequency));
dataFrame.addSensor(new core_1.Accelerometer(sensorUID + '_linearaccl', new core_1.Acceleration(event.acceleration.x, event.acceleration.y, event.acceleration.z, core_1.AccelerationUnit.METER_PER_SECOND_SQUARE), frequency));
const source = this.source;
source.getPosition().angularVelocity = dataFrame.getSensor(core_1.Gyroscope).value;
dataFrame.addSensor(new core_1.AbsoluteOrientationSensor(sensorUID + '_orientation', source.getPosition().orientation));
dataFrame.source = source;
this.push(dataFrame);
}, true);
window.addEventListener('deviceorientation', (event) => {
const source = this.source;
source.getPosition().orientation = core_1.Orientation.fromEuler([event.beta, event.gamma, event.alpha]);
});
this.logger('debug', 'Browser orientation and motion events registered!');
resolve();
});
}
stop() {
return Promise.resolve();
}
onPull() {
return new Promise((resolve) => {
resolve(undefined);
});
}
}
exports.SensorSourceNode = SensorSourceNode;
//# sourceMappingURL=SensorSourceNode.js.map