Skip to content

Commit

Permalink
feat: emulated trackers
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevMinerTV committed Jun 2, 2024
1 parent de1562a commit 6c758ae
Show file tree
Hide file tree
Showing 11 changed files with 549 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-baboons-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@slimevr/tracker-emulation': patch
---

implement basic emulated trackers
31 changes: 31 additions & 0 deletions apps/emulated-tracker-demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@slimevr/emulated-tracker-demo",
"version": "0.1.6",
"main": "dist/index.js",
"license": "(MIT OR Apache-2.0)",
"private": true,
"author": {
"name": "DevMiner",
"email": "devminer@devminer.xyz"
},
"dependencies": {
"@slimevr/common": "workspace:*",
"@slimevr/firmware-protocol": "workspace:*",
"@slimevr/tracker-emulation": "workspace:*",
"quaternion": "^1.5.1"
},
"devDependencies": {
"@slimevr/tsconfig": "workspace:*",
"@types/node": "^18.17.2",
"concurrently": "^8.2.0",
"nodemon": "^3.0.1",
"typescript": "^5.1.6"
},
"scripts": {
"dev": "concurrently \"tsc -w\" \"nodemon .\"",
"build": "tsc"
},
"engines": {
"node": ">=16.0.0"
}
}
99 changes: 99 additions & 0 deletions apps/emulated-tracker-demo/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { MACAddress, Quaternion } from '@slimevr/common';
import {
BoardType,
FirmwareFeatureFlags,
MCUType,
RotationDataType,
SensorStatus,
SensorType
} from '@slimevr/firmware-protocol';
import { EmulatedSensor, EmulatedTracker } from '@slimevr/tracker-emulation';
import BetterQuaternion from 'quaternion';

const tracker = new EmulatedTracker(
MACAddress.random(),
'0.0.1',
new FirmwareFeatureFlags(new Map()),
BoardType.CUSTOM,
MCUType.ESP8266
);
const sensors: EmulatedSensor[] = [];

const main = async () => {
tracker.on('ready', () => {
console.log('searching for server...');
});

tracker.on('connected-to-server', async (ip, port) => {
console.log('connected to server', ip, port);
});

tracker.on('unknown-incoming-packet', (packet) => {
console.log('unknown packet type', packet.type);
});

tracker.on('error', (err) => console.error(err));

await tracker.init();

for (let i = 0; i < 3; i++) {
sensors.push(await tracker.addSensor(SensorType.UNKNOWN, SensorStatus.OK));
}

//#region sin wave battery level
{
let i = 0;
setInterval(() => {
tracker.changeBatteryLevel(Math.sin(i) * 0.5 + 3.7, Math.sin(i) * 100);
i += 0.1;
}, 1000).unref();
}
//#endregion

//#region sin wave temperature
{
let i = 0;
setInterval(() => {
sensors.forEach((sensor) => sensor.sendTemperature(Math.sin(i) * 10 + 25));
i += 0.1;
}, 1000).unref();
}
//#endregion

//#region sin wave magnetometer accuracy
{
let i = 0;
setInterval(() => {
sensors.forEach((sensor) => sensor.sendMagnetometerAccuracy(Math.sin(i) * 3 + 3));
i += 0.1;
}, 1000).unref();
}
//#endregion

//#region sin wave signal strength
{
let i = 0;
setInterval(() => {
sensors.forEach((sensor) => sensor.sendSignalStrength(Math.sin(i) * 127));
i += 0.1;
}, 1000).unref();
}
//#endregion

//#region
{
let i = 0;
setInterval(() => {
sensors.forEach((sensor, idx) => {
const v = i / 5;
const better = BetterQuaternion.fromEuler(idx == 0 ? v : 0, idx == 1 ? v : 0, idx == 2 ? v : 0);

sensor.sendRotation(RotationDataType.NORMAL, new Quaternion(better.x, better.y, better.z, better.w), 0);
});

i += 0.1;
}, 100).unref();
}
};

main();
9 changes: 9 additions & 0 deletions apps/emulated-tracker-demo/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "@slimevr/tsconfig/node.json",
"include": ["src/**/*.ts"],
"exclude": ["dist", "node_modules"],
"compilerOptions": {
"target": "ES2022",
"outDir": "./dist"
}
}
3 changes: 3 additions & 0 deletions packages/tracker-emulation/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.turbo
CHANGELOG.md
tsconfig.json
39 changes: 39 additions & 0 deletions packages/tracker-emulation/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "@slimevr/tracker-emulation",
"version": "0.0.0",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"license": "(MIT OR Apache-2.0)",
"author": {
"name": "DevMiner",
"email": "devminer@devminer.xyz"
},
"repository": {
"type": "git",
"url": "https://github.com/SlimeVR/slimevr-node.git/tree/master/packages/tracker-emulation"
},
"bugs": {
"url": "https://github.com/SlimeVR/slimevr-node/issues"
},
"publishConfig": {
"access": "public"
},
"scripts": {
"build": "tsc",
"prepack": "pnpm build",
"watch": "pnpm build -- --watch",
"clean": "rimraf dist"
},
"dependencies": {
"@slimevr/common": "workspace:*",
"@slimevr/firmware-protocol": "workspace:*",
"strict-event-emitter-types": "^2.0.0"
},
"devDependencies": {
"@slimevr/tsconfig": "workspace:*",
"@types/node": "^18.17.2",
"rimraf": "^5.0.1",
"typescript": "^5.4.5"
}
}
48 changes: 48 additions & 0 deletions packages/tracker-emulation/src/EmulatedSensor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Quaternion, Vector } from '@slimevr/common';
import { RotationDataType, SensorStatus, SensorType, ServerBoundSensorInfoPacket } from '@slimevr/firmware-protocol';
import { EmulatedTracker } from './EmulatedTracker';

export class EmulatedSensor {
constructor(
private readonly tracker: EmulatedTracker,
readonly id: number,
readonly type: SensorType,
private _status: SensorStatus
) {}

get status() {
return this._status;
}

async sendSensorInfo() {
await this.tracker.sendPacketToServer(new ServerBoundSensorInfoPacket(this.id, this._status, this.type));
}

async sendRotation(dataType: RotationDataType, rotation: Quaternion, accuracyInfo: number) {
await this.tracker.sendRotationData(this.id, dataType, rotation, accuracyInfo);
}

async sendAcceleration(acceleration: Vector) {
await this.tracker.sendAcceleration(this.id, acceleration);
}

async sendTemperature(temperature: number) {
await this.tracker.sendTemperature(this.id, temperature);
}

async sendMagnetometerAccuracy(accuracy: number) {
await this.tracker.sendMagnetometerAccuracy(this.id, accuracy);
}

/**
* @param signalStrength The signal strength to send to the server, must be between -127 and 127
*/
async sendSignalStrength(signalStrength: number) {
await this.tracker.sendSignalStrength(this.id, signalStrength);
}

async changeStatus(status: SensorStatus) {
this._status = status;
await this.sendSensorInfo();
}
}
Loading

0 comments on commit 6c758ae

Please sign in to comment.