UNPKG

@openhps/sphero

Version:

Open Hybrid Positioning System - Sphero component

61 lines 2.47 kB
import { __awaiter } from "tslib"; import { SourceNode, AngleUnit, LinearVelocity, TimeUnit, LinearVelocityUnit, Absolute2DPosition, TimeService, LengthUnit, Orientation, } from '@openhps/core'; import { SpheroDataFrame } from '../data'; export class SpheroInputSource extends SourceNode { constructor(source) { super(source); } get toy() { const spheroObject = this.source; return spheroObject.toy; } onPull() { return new Promise((resolve) => { resolve(new SpheroDataFrame(this.source)); }); } roll(speed, heading, flags = []) { return new Promise((resolve, reject) => { const spheroObject = this.source; const position = spheroObject.getPosition() || new Absolute2DPosition(0, 0); position.unit = LengthUnit.CENTIMETER; position.timestamp = TimeService.now(); position.orientation = Orientation.fromEuler({ yaw: heading, pitch: 0, roll: 0, unit: AngleUnit.DEGREE }); position.velocity.linear = new 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, LinearVelocityUnit.METER_PER_SECOND); spheroObject.setPosition(position); const frame = new 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) => __awaiter(this, void 0, void 0, function* () { let driving = true; setTimeout(() => (driving = false), timeUnit.convert(time, TimeUnit.MILLISECOND)); while (driving) { yield this.roll(speed, heading, flags); } this.roll(0, heading, flags) .then(() => { resolve(); }) .catch((ex) => { reject(ex); }); })); } } //# sourceMappingURL=SpheroInputSource.js.map