178 lines
3.8 KiB
JavaScript
178 lines
3.8 KiB
JavaScript
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 led = undefined;
|
|
let position = undefined;
|
|
|
|
|
|
const connectSockets = () => {
|
|
if ((socketMoteur != undefined) || (socketInfos != undefined)) {
|
|
disconnectSocket();
|
|
}
|
|
socketMoteur = new WebSocket("ws://" + URL + "/motors.ws");
|
|
socketInfos = new WebSocket("ws://" + URL + "/infos.ws");
|
|
socketInfos.binaryType = "arraybuffer";
|
|
|
|
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");
|
|
position.reset();
|
|
};
|
|
|
|
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, v);
|
|
}
|
|
|
|
reculer(v) {
|
|
this.send(1, 1, v);
|
|
}
|
|
|
|
droite(v) {
|
|
this.send(1, -1, v);
|
|
}
|
|
|
|
gauche(v) {
|
|
this.send(-1, 1, v);
|
|
}
|
|
|
|
stop() {
|
|
this.send(0, 0, 0);
|
|
}
|
|
|
|
|
|
}
|
|
|
|
class Led {
|
|
|
|
http = undefined;
|
|
|
|
send(color){
|
|
this.http = new XMLHttpRequest();
|
|
this.http.open('POST', 'http://' + URL + '/led/set_color', true);
|
|
this.http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
|
this.http.send('ledcolor=' + color);
|
|
}
|
|
|
|
red(){
|
|
this.send("#ff0000")
|
|
}
|
|
|
|
green(){
|
|
this.send("#00ff00")
|
|
}
|
|
|
|
blue(){
|
|
this.send("#0000ff")
|
|
}
|
|
|
|
}
|
|
|
|
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();
|
|
led = new Led();
|
|
position = new Position(socketInfos);
|
|
}
|
|
|
|
|