Simulateur
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const URL = "192.168.185.20";
|
||||
const URL = "192.168.185.166";
|
||||
const INFO_POSITION = 0x01;
|
||||
const INFO_LED = 0x02;
|
||||
|
||||
@@ -9,6 +9,7 @@ let socketInfos = undefined;
|
||||
let moteur = undefined;
|
||||
let led = undefined;
|
||||
let position = undefined;
|
||||
let pid = undefined;
|
||||
|
||||
|
||||
const connectSockets = () => {
|
||||
@@ -34,6 +35,7 @@ const connectSockets = () => {
|
||||
socketInfos.onopen = function(e) {
|
||||
console.log("[socketInfos] Connection established");
|
||||
position.reset();
|
||||
led.green();
|
||||
};
|
||||
|
||||
socketInfos.onclose = function(event) {
|
||||
@@ -101,7 +103,75 @@ class Moteur {
|
||||
stop() {
|
||||
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;
|
||||
|
||||
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);
|
||||
async send(color){
|
||||
await fetch('http://' + URL + '/led/set_color', {
|
||||
method: 'post',
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
mode: "no-cors",
|
||||
body: 'ledcolor=' + color
|
||||
});
|
||||
}
|
||||
|
||||
red(){
|
||||
this.send("#ff0000")
|
||||
this.send("#ff0000");
|
||||
}
|
||||
|
||||
green(){
|
||||
this.send("#00ff00")
|
||||
this.send("#00ff00");
|
||||
}
|
||||
|
||||
blue(){
|
||||
this.send("#0000ff")
|
||||
this.send("#0000ff");
|
||||
}
|
||||
|
||||
off(){
|
||||
this.send("#000000");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -144,34 +220,69 @@ class Position {
|
||||
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);
|
||||
const posX = view.getFloat32(1);
|
||||
const poxY = view.getFloat32(5);
|
||||
const posR = view.getFloat32(9);
|
||||
|
||||
if (DEBUG) {
|
||||
console.log(
|
||||
{"posX": position_x, "posY": position_y, "Rot": position_a }
|
||||
{"posX": posX, "posY": poxY, "Rot": posR}
|
||||
);
|
||||
}
|
||||
|
||||
pid.get(posX, this.posY, posR);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
reset() {
|
||||
try {
|
||||
const request = new XMLHttpRequest();
|
||||
request.open("GET", "http://" + URL + "/position/reset");
|
||||
request.send();
|
||||
} catch (e) {}
|
||||
async reset() {
|
||||
await fetch("http://" + URL + "/position/reset", {
|
||||
method: 'get',
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
mode: "no-cors"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function run() {
|
||||
async function run() {
|
||||
|
||||
|
||||
connectSockets();
|
||||
moteur = new Moteur();
|
||||
led = new Led();
|
||||
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);
|
||||
});
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -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()
|
||||
Reference in New Issue
Block a user