UNPKG

@openhps/sphero

Version:

Open Hybrid Positioning System - Sphero component

100 lines 4.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SpheroSensor = exports.SpheroSensorSource = void 0; const core_1 = require("@openhps/core"); const data_1 = require("../data"); const dist_1 = require("../../lib/server/lib/dist"); class SpheroSensorSource extends core_1.SourceNode { constructor(options) { super(options); this._calibrated = false; this._calibrated = this.options.skipCalibration; this.once('build', this._initSensors.bind(this)); } get toy() { const spheroObject = this.source; return spheroObject.toy; } _initSensors() { return new Promise((resolve, reject) => { this.referenceSpace = new core_1.ReferenceSpace(this.graph.referenceSpace); const spheroObject = this.source; spheroObject.toy.on(dist_1.Event.onSensor, this._onSensorEvent.bind(this)); if (!this.options.skipSensorConfiguration) { spheroObject.toy .configureSensorStream(this.options.interval) .then(() => { resolve(); }) .catch(reject); } else { resolve(); } }); } _onSensorEvent(event) { if (!this._calibrated) { // Start orientation this.referenceSpace.rotation(core_1.Quaternion.fromEuler({ yaw: -event.angles.yaw, pitch: -event.angles.pitch, roll: -event.angles.roll, unit: core_1.AngleUnit.DEGREE, })); // Start origin should be (0, 0) this.referenceSpace.translation(-event.locator.position.x, -event.locator.position.y); // Apply axis rotation to match OpenHPS this._calibrated = true; } const spheroObject = this.source; const position = spheroObject.getPosition() || new core_1.Absolute2DPosition(0, 0); position.timestamp = core_1.TimeService.now(); position.unit = core_1.LengthUnit.CENTIMETER; if (this.options.sensors.includes(SpheroSensor.VELOCITY)) { position.velocity.linear = new core_1.LinearVelocity(event.locator.velocity.x, event.locator.velocity.y, 0, core_1.LinearVelocityUnit.CENTIMETER_PER_SECOND); } if (this.options.sensors.includes(SpheroSensor.GYROSCOPE)) { position.velocity.angular = new core_1.AngularVelocity(event.gyro.filtered.x, event.gyro.filtered.y, event.gyro.filtered.z, core_1.AngularVelocityUnit.DEGREE_PER_SECOND); } if (this.options.sensors.includes(SpheroSensor.LOCATION)) { position.unit = core_1.LengthUnit.CENTIMETER; position.x = event.locator.position.x; position.y = event.locator.position.y; position.orientation = core_1.Orientation.fromEuler({ yaw: event.angles.yaw, pitch: event.angles.pitch, roll: event.angles.roll, unit: core_1.AngleUnit.DEGREE, }); } spheroObject.setPosition(position, this.referenceSpace); // Clone the information to the sphero data frame const frame = new data_1.SpheroDataFrame(spheroObject); const angularVelocity = new core_1.Gyroscope(this.uid + '_gyro'); angularVelocity.value = position.velocity.angular.clone(); const linearVelocity = new core_1.LinearVelocitySensor(this.uid + '_linearvel'); linearVelocity.value = position.velocity.linear.clone(); const relativeOrientation = new core_1.RelativeOrientationSensor(this.uid + '_relativeorientation'); relativeOrientation.value = position.orientation.clone(); const linearAcceleration = new core_1.LinearAccelerationSensor(this.uid + '_linearaccl'); linearAcceleration.value = new core_1.Acceleration(event.accelerometer.filtered.x, event.accelerometer.filtered.y, event.accelerometer.filtered.z); frame.x = position.x; frame.y = position.y; this.push(frame); } onPull() { return new Promise((resolve) => { resolve(new data_1.SpheroDataFrame(this.source)); }); } } exports.SpheroSensorSource = SpheroSensorSource; var SpheroSensor; (function (SpheroSensor) { SpheroSensor[SpheroSensor["LOCATION"] = 0] = "LOCATION"; SpheroSensor[SpheroSensor["GYROSCOPE"] = 1] = "GYROSCOPE"; SpheroSensor[SpheroSensor["ACCELEROMETER"] = 2] = "ACCELEROMETER"; SpheroSensor[SpheroSensor["VELOCITY"] = 3] = "VELOCITY"; })(SpheroSensor = exports.SpheroSensor || (exports.SpheroSensor = {})); //# sourceMappingURL=SpheroSensorSource.js.map