Simulateur

This commit is contained in:
Nogard
2025-03-22 22:19:05 +01:00
parent 8ee12a22b1
commit 50a1933e93
2 changed files with 178 additions and 20 deletions
+131 -20
View File
@@ -1,4 +1,4 @@
const URL = "192.168.185.20"; const URL = "192.168.185.166";
const INFO_POSITION = 0x01; const INFO_POSITION = 0x01;
const INFO_LED = 0x02; const INFO_LED = 0x02;
@@ -9,6 +9,7 @@ let socketInfos = undefined;
let moteur = undefined; let moteur = undefined;
let led = undefined; let led = undefined;
let position = undefined; let position = undefined;
let pid = undefined;
const connectSockets = () => { const connectSockets = () => {
@@ -34,6 +35,7 @@ const connectSockets = () => {
socketInfos.onopen = function(e) { socketInfos.onopen = function(e) {
console.log("[socketInfos] Connection established"); console.log("[socketInfos] Connection established");
position.reset(); position.reset();
led.green();
}; };
socketInfos.onclose = function(event) { socketInfos.onclose = function(event) {
@@ -101,7 +103,75 @@ class Moteur {
stop() { stop() {
this.send(0, 0, 0); this.send(0, 0, 0);
} }
}
class PID {
kpx = 0;
kix = 0;
kdx = 0;
kpy = 0;
kiy = 0;
kdy = 0;
kpr = 0;
kir = 0;
kdr = 0;
px = 0;
ix = 0;
dx = 0;
py = 0;
iy = 0;
dy = 0;
pr = 0;
ir = 0;
dr = 0;
tagetX = 0;
tagetY = 0;
targetR = 0;
run = false;
lastTime = undefined;
set(targetX, targetY, targetR) {
this.targetX = targetX;
this.px = 0;
this.ix = 0;
this.dx = 0;
this.targetY = targetY;
this.py = 0;
this.iy = 0;
this.dy = 0;
this.targetR = targetR;
this.pr = 0;
this.ir = 0;
this.dr = 0;
this.lastTime = Date.now().valueOf();
this.run = true;
}
get(currentX, currentY, currentR) {
if (!this.run) {
return;
}
const currentTime = Date.now().valueOf();
const deltaTime = (currentTime - this.lastTime) / 1000.0;
this.p
console.log("Time delta : " + deltaTime);
this.lastTime = currentTime;
}
} }
@@ -109,23 +179,29 @@ class Led {
http = undefined; http = undefined;
send(color){ async send(color){
this.http = new XMLHttpRequest(); await fetch('http://' + URL + '/led/set_color', {
this.http.open('POST', 'http://' + URL + '/led/set_color', true); method: 'post',
this.http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); headers: { "Content-Type": "application/x-www-form-urlencoded" },
this.http.send('ledcolor=' + color); mode: "no-cors",
body: 'ledcolor=' + color
});
} }
red(){ red(){
this.send("#ff0000") this.send("#ff0000");
} }
green(){ green(){
this.send("#00ff00") this.send("#00ff00");
} }
blue(){ blue(){
this.send("#0000ff") this.send("#0000ff");
}
off(){
this.send("#000000");
} }
} }
@@ -144,34 +220,69 @@ class Position {
const mask = view.getUint8(0); const mask = view.getUint8(0);
if (mask & INFO_POSITION) { if (mask & INFO_POSITION) {
const position_x = view.getFloat32(1); const posX = view.getFloat32(1);
const position_y = view.getFloat32(5); const poxY = view.getFloat32(5);
const position_a = view.getFloat32(9); const posR = view.getFloat32(9);
if (DEBUG) { if (DEBUG) {
console.log( console.log(
{"posX": position_x, "posY": position_y, "Rot": position_a } {"posX": posX, "posY": poxY, "Rot": posR}
); );
} }
pid.get(posX, this.posY, posR);
} }
}); });
} }
reset() { async reset() {
try { await fetch("http://" + URL + "/position/reset", {
const request = new XMLHttpRequest(); method: 'get',
request.open("GET", "http://" + URL + "/position/reset"); headers: { "Content-Type": "application/x-www-form-urlencoded" },
request.send(); mode: "no-cors"
} catch (e) {} });
} }
} }
function run() { async function run() {
connectSockets(); connectSockets();
moteur = new Moteur(); moteur = new Moteur();
led = new Led(); led = new Led();
position = new Position(socketInfos); position = new Position(socketInfos);
pid = new PID();
led.red();
/*
const socket = new WebSocket("ws://127.0.0.1:1337/ws");
socket.onclose = function(event) {
if (event.wasClean) {
console.log(`[socket] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
console.log('[socket] Connection died');
}
};
socket.onopen = function(e) {
console.log("[socket] Connection established");
socket.send("Test");
};
socket.onerror = function(error) {
console.log(`[socket] ERROR : ${error}`);
};
socket.addEventListener("message", e => {
const data = e.data;
console.log(data);
});
*/
} }
+47
View File
@@ -0,0 +1,47 @@
#!/usr/bin/python3.7
import aiohttp
from aiohttp import web, WSCloseCode
import asyncio
async def http_handler(request):
return web.Response(text='Hello, world')
async def websocket_handler(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close':
await ws.close()
else:
await ws.send_str('some websocket message payload')
elif msg.type == aiohttp.WSMsgType.ERROR:
print('ws connection closed with exception %s' % ws.exception())
return ws
def create_runner():
app = web.Application()
app.add_routes([
web.get('/', http_handler),
web.get('/ws', websocket_handler),
])
return web.AppRunner(app)
async def start_server(host="127.0.0.1", port=1337):
runner = create_runner()
await runner.setup()
site = web.TCPSite(runner, host, port)
await site.start()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(start_server())
loop.run_forever()