diff --git a/index.html b/index.html new file mode 100644 index 0000000..4c8e669 --- /dev/null +++ b/index.html @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/script.js b/script.js index 99ef0fe..2691ce0 100644 --- a/script.js +++ b/script.js @@ -1,8 +1,13 @@ const URL = "192.168.185.20"; +const INFO_POSITION = 0x01; +const INFO_LED = 0x02; + +const DEBUG = false; let socketMoteur = undefined; let socketInfos = undefined; let moteur = undefined; +let position = undefined; const connectSockets = () => { @@ -11,6 +16,7 @@ const connectSockets = () => { } socketMoteur = new WebSocket("ws://" + URL + "/motors.ws"); socketInfos = new WebSocket("ws://" + URL + "/infos.ws"); + socketInfos.binaryType = "arraybuffer"; socketMoteur.onclose = function(event) { if (event.wasClean) { @@ -26,6 +32,7 @@ const connectSockets = () => { socketInfos.onopen = function(e) { console.log("[socketInfos] Connection established"); + position.reset(); }; socketInfos.onclose = function(event) { @@ -75,40 +82,69 @@ class Moteur { } avancer(v) { - this.send(-1, -1, 1); + this.send(-1, -1, v); } reculer(v) { - this.send(1, 1, 1); + this.send(1, 1, v); } droite(v) { - this.send(1, -1, 1); + this.send(1, -1, v); } gauche(v) { - this.send(-1, 1, 1); + this.send(-1, 1, v); } - stop(v) { + stop() { this.send(0, 0, 0); } } +class Position { + posX = 0; + posY = 0; + Rot = 0; + constructor(socket) { + socket.addEventListener("message", e => { + const view = new DataView(e.data); + let pos = 0; + + const mask = view.getUint8(0); + + if (mask & INFO_POSITION) { + const position_x = view.getFloat32(1); + const position_y = view.getFloat32(5); + const position_a = view.getFloat32(9); + + if (DEBUG) { + console.log( + {"posX": position_x, "posY": position_y, "Rot": position_a } + ); + } + } + }); + } + reset() { + try { + const request = new XMLHttpRequest(); + request.open("GET", "http://" + URL + "/position/reset"); + request.send(); + } catch (e) {} + } +} function run() { connectSockets(); moteur = new Moteur(); - - socketMoteur.onmessage = function(event) { - console.log(`[message] Data received from server: ${event.data}`); - }; + position = new Position(socketInfos); }