@openhps/sphero
Version:
Open Hybrid Positioning System - Sphero component
96 lines • 4.66 kB
JavaScript
import { SourceNode, LinearVelocity, Absolute2DPosition, AngularVelocity, Quaternion, AngleUnit, ReferenceSpace, AngularVelocityUnit, LengthUnit, LinearVelocityUnit, Acceleration, TimeService, Orientation, Gyroscope, LinearAccelerationSensor, RelativeOrientationSensor, LinearVelocitySensor, } from '@openhps/core';
import { SpheroDataFrame } from '../data';
import { Event } from '../../lib/server/lib/dist';
export class SpheroSensorSource extends 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 ReferenceSpace(this.graph.referenceSpace);
const spheroObject = this.source;
spheroObject.toy.on(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(Quaternion.fromEuler({
yaw: -event.angles.yaw,
pitch: -event.angles.pitch,
roll: -event.angles.roll,
unit: 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 Absolute2DPosition(0, 0);
position.timestamp = TimeService.now();
position.unit = LengthUnit.CENTIMETER;
if (this.options.sensors.includes(SpheroSensor.VELOCITY)) {
position.velocity.linear = new LinearVelocity(event.locator.velocity.x, event.locator.velocity.y, 0, LinearVelocityUnit.CENTIMETER_PER_SECOND);
}
if (this.options.sensors.includes(SpheroSensor.GYROSCOPE)) {
position.velocity.angular = new AngularVelocity(event.gyro.filtered.x, event.gyro.filtered.y, event.gyro.filtered.z, AngularVelocityUnit.DEGREE_PER_SECOND);
}
if (this.options.sensors.includes(SpheroSensor.LOCATION)) {
position.unit = LengthUnit.CENTIMETER;
position.x = event.locator.position.x;
position.y = event.locator.position.y;
position.orientation = Orientation.fromEuler({
yaw: event.angles.yaw,
pitch: event.angles.pitch,
roll: event.angles.roll,
unit: AngleUnit.DEGREE,
});
}
spheroObject.setPosition(position, this.referenceSpace);
// Clone the information to the sphero data frame
const frame = new SpheroDataFrame(spheroObject);
const angularVelocity = new Gyroscope(this.uid + '_gyro');
angularVelocity.value = position.velocity.angular.clone();
const linearVelocity = new LinearVelocitySensor(this.uid + '_linearvel');
linearVelocity.value = position.velocity.linear.clone();
const relativeOrientation = new RelativeOrientationSensor(this.uid + '_relativeorientation');
relativeOrientation.value = position.orientation.clone();
const linearAcceleration = new LinearAccelerationSensor(this.uid + '_linearaccl');
linearAcceleration.value = new 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 SpheroDataFrame(this.source));
});
}
}
export 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 || (SpheroSensor = {}));
//# sourceMappingURL=SpheroSensorSource.js.map