Récup infos + moteur

This commit is contained in:
Nogard
2025-03-22 15:28:33 +01:00
parent aa017a2715
commit bf51e5292b
2 changed files with 54 additions and 9 deletions
+9
View File
@@ -0,0 +1,9 @@
<html>
<head>
<script src="./script.js"></script>
<script>
run();
</script>
</head>
</html>
+45 -9
View File
@@ -1,8 +1,13 @@
const URL = "192.168.185.20"; const URL = "192.168.185.20";
const INFO_POSITION = 0x01;
const INFO_LED = 0x02;
const DEBUG = false;
let socketMoteur = undefined; let socketMoteur = undefined;
let socketInfos = undefined; let socketInfos = undefined;
let moteur = undefined; let moteur = undefined;
let position = undefined;
const connectSockets = () => { const connectSockets = () => {
@@ -11,6 +16,7 @@ const connectSockets = () => {
} }
socketMoteur = new WebSocket("ws://" + URL + "/motors.ws"); socketMoteur = new WebSocket("ws://" + URL + "/motors.ws");
socketInfos = new WebSocket("ws://" + URL + "/infos.ws"); socketInfos = new WebSocket("ws://" + URL + "/infos.ws");
socketInfos.binaryType = "arraybuffer";
socketMoteur.onclose = function(event) { socketMoteur.onclose = function(event) {
if (event.wasClean) { if (event.wasClean) {
@@ -26,6 +32,7 @@ const connectSockets = () => {
socketInfos.onopen = function(e) { socketInfos.onopen = function(e) {
console.log("[socketInfos] Connection established"); console.log("[socketInfos] Connection established");
position.reset();
}; };
socketInfos.onclose = function(event) { socketInfos.onclose = function(event) {
@@ -75,40 +82,69 @@ class Moteur {
} }
avancer(v) { avancer(v) {
this.send(-1, -1, 1); this.send(-1, -1, v);
} }
reculer(v) { reculer(v) {
this.send(1, 1, 1); this.send(1, 1, v);
} }
droite(v) { droite(v) {
this.send(1, -1, 1); this.send(1, -1, v);
} }
gauche(v) { gauche(v) {
this.send(-1, 1, 1); this.send(-1, 1, v);
} }
stop(v) { stop() {
this.send(0, 0, 0); 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() { function run() {
connectSockets(); connectSockets();
moteur = new Moteur(); moteur = new Moteur();
position = new Position(socketInfos);
socketMoteur.onmessage = function(event) {
console.log(`[message] Data received from server: ${event.data}`);
};
} }