UNPKG

@openhps/sphero

Version:

Open Hybrid Positioning System - Sphero component

65 lines 2.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SpheroInputSource = void 0; const tslib_1 = require("tslib"); const core_1 = require("@openhps/core"); const data_1 = require("../data"); class SpheroInputSource extends core_1.SourceNode { constructor(source) { super(source); } get toy() { const spheroObject = this.source; return spheroObject.toy; } onPull() { return new Promise((resolve) => { resolve(new data_1.SpheroDataFrame(this.source)); }); } roll(speed, heading, flags = []) { return new Promise((resolve, reject) => { const spheroObject = this.source; const position = spheroObject.getPosition() || new core_1.Absolute2DPosition(0, 0); position.unit = core_1.LengthUnit.CENTIMETER; position.timestamp = core_1.TimeService.now(); position.orientation = core_1.Orientation.fromEuler({ yaw: heading, pitch: 0, roll: 0, unit: core_1.AngleUnit.DEGREE }); position.velocity.linear = new core_1.LinearVelocity( // Sphero Mini top speed is 1m/s // https://support.sphero.com/article/6drb2qggx4-sphero-mini-faq#:~:text=How%20fast%20is%20Sphero%20Mini,of%201%20meter%20per%20second. (1.0 / 255) * speed, 0, 0, core_1.LinearVelocityUnit.METER_PER_SECOND); spheroObject.setPosition(position); const frame = new data_1.SpheroDataFrame(spheroObject); spheroObject.toy .roll(speed, heading, flags) .then(() => { return this.push(frame); }) .then(() => { setTimeout(() => { resolve(); }, 10); }) .catch(reject); }); } rollTime(speed, heading, time, timeUnit, flags = []) { // eslint-disable-next-line return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () { let driving = true; setTimeout(() => (driving = false), timeUnit.convert(time, core_1.TimeUnit.MILLISECOND)); while (driving) { yield this.roll(speed, heading, flags); } this.roll(0, heading, flags) .then(() => { resolve(); }) .catch((ex) => { reject(ex); }); })); } } exports.SpheroInputSource = SpheroInputSource; //# sourceMappingURL=SpheroInputSource.js.map