📜 Makes data sync more seamless
This commit is contained in:
+2
-1
@@ -6,7 +6,7 @@
|
||||
import Controller from './routes/Controller.svelte';
|
||||
import { fileService } from '$lib/services';
|
||||
import Settings from './routes/Settings.svelte';
|
||||
import { jointNames, model } from '$lib/store';
|
||||
import { jointNames, model, outControllerData } from '$lib/store';
|
||||
import { loadModelAsync } from '$lib/utilities';
|
||||
import { socketLocation } from '$lib/utilities';
|
||||
import type { Result } from '$lib/utilities/result';
|
||||
@@ -14,6 +14,7 @@
|
||||
export let url = window.location.pathname;
|
||||
onMount(async () => {
|
||||
socketService.connect(socketLocation);
|
||||
socketService.addPublisher(outControllerData);
|
||||
registerFetchIntercept();
|
||||
const modelRes = await loadModelAsync('/spot_micro.urdf.xacro');
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<script lang="ts">
|
||||
import nipplejs from 'nipplejs';
|
||||
import { onMount } from 'svelte';
|
||||
import { throttler, toUint8 } from '$lib/utilities';
|
||||
import { throttler, toInt8 } from '$lib/utilities';
|
||||
import socketService from '$lib/services/socket-service';
|
||||
import { emulateModel, input, outControllerData } from '$lib/store';
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
|
||||
let mode = 'rest'; // 'rest' | 'stand' | 'stand+' | 'walk'
|
||||
|
||||
let data = new Uint8Array(6);
|
||||
let data = new Int8Array(6);
|
||||
|
||||
onMount(() => {
|
||||
left = nipplejs.create({
|
||||
@@ -67,16 +67,16 @@
|
||||
|
||||
const updateData = () => {
|
||||
data[0] = 0;
|
||||
data[1] = toUint8($input.left.x, -1, 1);
|
||||
data[2] = toUint8($input.left.y, -1, 1);
|
||||
data[3] = toUint8($input.right.x, -1, 1);
|
||||
data[4] = toUint8($input.right.y, -1, 1);
|
||||
data[5] = toUint8($input.height, 0, 100);
|
||||
data[6] = toUint8($input.speed, 0, 100);
|
||||
data[1] = toInt8($input.left.x, -1, 1);
|
||||
data[2] = toInt8($input.left.y, -1, 1);
|
||||
data[3] = toInt8($input.right.x, -1, 1);
|
||||
data[4] = toInt8($input.right.y, -1, 1);
|
||||
data[5] = toInt8($input.height, 0, 100);
|
||||
data[6] = toInt8($input.speed, 0, 100);
|
||||
|
||||
outControllerData.set(data);
|
||||
|
||||
if (!$emulateModel) socketService.send(data);
|
||||
if (!$emulateModel) socketService.send(data);
|
||||
};
|
||||
</script>
|
||||
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
import { CanvasTexture, CircleGeometry, Mesh, MeshBasicMaterial } from 'three';
|
||||
import socketService from '$lib/services/socket-service';
|
||||
import uzip from 'uzip';
|
||||
import { model, outControllerData } from '$lib/store';
|
||||
import { model } from '$lib/store';
|
||||
import { ForwardKinematics } from '$lib/kinematic';
|
||||
import { location } from '$lib/utilities';
|
||||
import { fileService } from '$lib/services';
|
||||
import { servoAngles, mpu } from '$lib/stores';
|
||||
import SceneBuilder from '$lib/sceneBuilder';
|
||||
import { lerp } from 'three/src/math/MathUtils';
|
||||
import { lerp, degToRad } from 'three/src/math/MathUtils';
|
||||
|
||||
let sceneManager: SceneBuilder;
|
||||
let canvas: HTMLCanvasElement, streamCanvas: HTMLCanvasElement, stream: HTMLImageElement;
|
||||
@@ -47,21 +47,9 @@
|
||||
psi: number;
|
||||
}
|
||||
|
||||
const degToRad = (val: number) => val * (Math.PI / 180);
|
||||
|
||||
onMount(async () => {
|
||||
await cacheModelFiles();
|
||||
await createScene();
|
||||
|
||||
outControllerData.subscribe((data) => {
|
||||
socketService.send(
|
||||
JSON.stringify({
|
||||
type: 'kinematic/bodystate',
|
||||
angles: [0, (data[1] - 128) / 3, (data[2] - 128) / 4],
|
||||
position: [(data[4] - 128) / 2, data[5], (data[3] - 128) / 2]
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
onDestroy(() => {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { isConnected, socketData } from '$lib/stores';
|
||||
import { Result, Ok } from '$lib/utilities';
|
||||
import { resultService } from '$lib/services';
|
||||
import { type WebSocketJsonMsg } from '$lib/models';
|
||||
import type { Writable } from 'svelte/store';
|
||||
|
||||
type WebsocketOutData = string | ArrayBufferLike | Blob | ArrayBufferView;
|
||||
|
||||
@@ -37,6 +38,10 @@ class SocketService {
|
||||
return Result.err('The connection is not open');
|
||||
}
|
||||
|
||||
public addPublisher(store: Writable<WebsocketOutData>, type?: string) {
|
||||
store.subscribe((data) => this.send(type ? JSON.stringify({ type, data }) : data));
|
||||
}
|
||||
|
||||
private handleConnected(): void {
|
||||
isConnected.set(true);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ export const input = writable({
|
||||
speed: 0
|
||||
});
|
||||
|
||||
export const outControllerData = writable(new Uint8Array([0, 128, 128, 128, 128, 70, 0]));
|
||||
export const outControllerData = writable(new Int8Array([0, 0, 0, 0, 0, 70, 0]));
|
||||
|
||||
export const jointNames = persistentStore('joint_names', []);
|
||||
|
||||
|
||||
@@ -3,3 +3,9 @@ export const toUint8 = (number: number, min: number, max: number) => {
|
||||
let scaled = ((number - min) / (max - min)) * 255;
|
||||
return Math.round(scaled) & 0xff;
|
||||
};
|
||||
|
||||
export const toInt8 = (number: number, min: number, max: number) => {
|
||||
number = Math.max(min, Math.min(max, number));
|
||||
let scaled = ((number - min) / (max - min)) * 255 - 128;
|
||||
return Math.max(-128, Math.min(127, Math.round(scaled))) | 0;
|
||||
};
|
||||
Reference in New Issue
Block a user