115 lines
2.3 KiB
JavaScript
115 lines
2.3 KiB
JavaScript
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}`);
|
|
};
|
|
}
|
|
|
|
|