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 new file mode 100644 index 0000000..99ef0fe --- /dev/null +++ b/script.js @@ -0,0 +1,114 @@ +const URL = "192.168.185.20"; + +let socketMoteur = undefined; +let socketInfos = undefined; +let moteur = undefined; + + +const connectSockets = () => { + if ((socketMoteur != undefined) || (socketInfos != undefined)) { + disconnectSocket(); + } + socketMoteur = new WebSocket("ws://" + URL + "/motors.ws"); + socketInfos = new WebSocket("ws://" + URL + "/infos.ws"); + + socketMoteur.onclose = function(event) { + if (event.wasClean) { + console.log(`[socketMoteur] Connection closed cleanly, code=${event.code} reason=${event.reason}`); + } else { + console.log('[socketMoteur] Connection died'); + } + }; + + socketMoteur.onopen = function(e) { + console.log("[socketMoteur] Connection established"); + }; + + socketInfos.onopen = function(e) { + console.log("[socketInfos] Connection established"); + }; + + socketInfos.onclose = function(event) { + if (event.wasClean) { + console.log(`[socketInfos] Connection closed cleanly, code=${event.code} reason=${event.reason}`); + } else { + console.log('[socketInfos] Connection died'); + } + }; + + socketMoteur.onerror = function(error) { + console.log(`[socketMoteur] ERROR : ${error}`); + }; + + socketInfos.onerror = function(error) { + console.log(`[socketInfos] ERROR : ${error}`); + }; +} + +const disconnectSocket = () => { + if (socketMoteur != undefined) { + socketMoteur.close(); + } + if (socketInfos != undefined) { + socketInfos.close(); + } + + socketMoteur = undefined; + socketInfos = undefined; +} + + + + +class Moteur { + + motors_buf = new Float32Array(2); + + send(d, g, v) { + if (socketMoteur == undefined) { + return; + } + this.motors_buf[0] = d * v; + this.motors_buf[1] = g * v; + + socketMoteur.send(this.motors_buf); + } + + avancer(v) { + this.send(-1, -1, 1); + } + + reculer(v) { + this.send(1, 1, 1); + } + + droite(v) { + this.send(1, -1, 1); + } + + gauche(v) { + this.send(-1, 1, 1); + } + + stop(v) { + this.send(0, 0, 0); + } + + +} + + + + + + +function run() { + connectSockets(); + moteur = new Moteur(); + + socketMoteur.onmessage = function(event) { + console.log(`[message] Data received from server: ${event.data}`); + }; +} + +