UI / Python Websocket
This commit is contained in:
2
PointCloudWeb.Scanner/0yGrad-pc.csv
Normal file
2
PointCloudWeb.Scanner/0yGrad-pc.csv
Normal file
@@ -0,0 +1,2 @@
|
||||
x,y,z
|
||||
143, 1307, 2
|
||||
|
32401
PointCloudWeb.Scanner/Esszimmer.txt
Normal file
32401
PointCloudWeb.Scanner/Esszimmer.txt
Normal file
File diff suppressed because it is too large
Load Diff
6480
PointCloudWeb.Scanner/datafile.csv
Normal file
6480
PointCloudWeb.Scanner/datafile.csv
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
1
PointCloudWeb.Scanner/lidar.csv
Normal file
1
PointCloudWeb.Scanner/lidar.csv
Normal file
@@ -0,0 +1 @@
|
||||
1,0.109375001,1315.000057
|
||||
|
@@ -2,11 +2,16 @@
|
||||
import serial
|
||||
import time
|
||||
import PyLidar3
|
||||
import asyncio
|
||||
import websockets
|
||||
|
||||
arduino = serial.Serial(port='COM8', baudrate=9600)
|
||||
lidar = PyLidar3.YdLidarX4(port='COM6',chunk_size=20000) #PyLidar3.your_version_of_lidar(port,chunk_size)
|
||||
#arduino = serial.Serial(port='COM5', baudrate=9600)
|
||||
#lidar = PyLidar3.YdLidarX4(port='COM6',chunk_size=20000) #PyLidar3.your_version_of_lidar(port,chunk_size)
|
||||
|
||||
f = open("PoinCloudWeb.Scanner\datafile.txt","wt")
|
||||
f = open("PointCloudWeb.Scanner\datafile.txt","wt")
|
||||
f.write("y, x, z\n")
|
||||
|
||||
progress = 0
|
||||
|
||||
print("Start ...")
|
||||
time.sleep(2)
|
||||
@@ -35,7 +40,7 @@ def filterY(data):
|
||||
|
||||
def senddata(data,posy):
|
||||
for x,y in data.items():
|
||||
f.write("y:" + str(posy) + "x:" + str(x) + "d:" + str(y) + "\n")
|
||||
f.write(str(posy) + ", " + str(x) + ", " + str(y) + "\n")
|
||||
|
||||
def startScaner(mode):
|
||||
if(lidar.Connect()):
|
||||
@@ -68,21 +73,53 @@ def startScaner(mode):
|
||||
setY(0)
|
||||
|
||||
else:
|
||||
print("Mode Error")
|
||||
print("mode error")
|
||||
|
||||
# data = next(gen)
|
||||
# #print(data)
|
||||
# for x,y in data.items():
|
||||
# f.write("a:" + str(x) + " d:" + str(y) + "\n")
|
||||
f.close()
|
||||
lidar.StopScanning()
|
||||
lidar.Disconnect()
|
||||
print("Scaner gestoppt")
|
||||
print("scan stoped")
|
||||
else:
|
||||
print("Error connecting to device")
|
||||
|
||||
while True:
|
||||
startScaner(input("Scan Modus(0,1,2):"))
|
||||
async def wsfilter(websocket, message):
|
||||
command = message[message.find("<")+1:message.find(">")]
|
||||
value = message[message.find("><")+2:message.find(">", message.find("><")+2)]
|
||||
#print(command + " / " + value)
|
||||
await wsaction(websocket, command,value)
|
||||
|
||||
async def wsaction(websocket, command, value):
|
||||
if(command == "start"):
|
||||
if(value == "0"):
|
||||
await websocket.send("start scan resolution 0")
|
||||
elif(value =="1"):
|
||||
await websocket.send("start scan resolution 1")
|
||||
elif(value =="2"):
|
||||
await websocket.send("start scan resolution 2")
|
||||
else:
|
||||
await websocket.send("mode error")
|
||||
elif(command == "status"):
|
||||
await websocket.send("Status ...")
|
||||
else:
|
||||
await websocket.send("command error")
|
||||
#muss noch was passieren
|
||||
|
||||
async def wscom(websocket, path):
|
||||
print("connected")
|
||||
while True:
|
||||
data = await websocket.recv()
|
||||
await wsfilter(websocket, data)
|
||||
print({data})
|
||||
#await websocket.send(data)
|
||||
|
||||
async def main():
|
||||
server = await websockets.serve(wscom, 'localhost', 6789)
|
||||
await server.wait_closed()
|
||||
asyncio.run(main())
|
||||
|
||||
#while True:
|
||||
# startScaner(input("Scan Modus(0,1,2):"))
|
||||
#wsfilter(input("Befehlt Eingeben:"))
|
||||
# for x in range(18):
|
||||
# setY(x*10)
|
||||
# time.sleep(1)
|
||||
20
PointCloudWeb.Scanner/test/index.html
Normal file
20
PointCloudWeb.Scanner/test/index.html
Normal file
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>WebSocket demo</title>
|
||||
</head>
|
||||
<body>
|
||||
<script>
|
||||
var ws = new WebSocket("ws://127.0.0.1:5678/"),
|
||||
messages = document.createElement('ul');
|
||||
ws.onmessage = function (event) {
|
||||
var messages = document.getElementsByTagName('ul')[0],
|
||||
message = document.createElement('li'),
|
||||
content = document.createTextNode(event.data);
|
||||
message.appendChild(content);
|
||||
messages.appendChild(message);
|
||||
};
|
||||
document.body.appendChild(messages);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,21 +1,21 @@
|
||||
import PyLidar3
|
||||
import time # Time module
|
||||
#Serial port to which lidar connected, Get it from device manager windows
|
||||
#In linux type in terminal -- ls /dev/tty*
|
||||
#port = input("Enter port name which lidar is connected:") #windows
|
||||
#port = "/dev/ttyUSB0" #linux
|
||||
f = open("PoinCloudWeb.Scanner\datafile.txt","wt")
|
||||
Obj = PyLidar3.YdLidarX4(port='COM6',chunk_size=20000) #PyLidar3.your_version_of_lidar(port,chunk_size)
|
||||
if(Obj.Connect()):
|
||||
print(Obj.GetDeviceInfo())
|
||||
gen = Obj.StartScanning()
|
||||
t = time.time() # start time
|
||||
data = next(gen)
|
||||
#print(data)
|
||||
for x,y in data.items():
|
||||
f.write(str(x) + " / " + str(y) + "\n")
|
||||
f.close()
|
||||
Obj.StopScanning()
|
||||
Obj.Disconnect()
|
||||
else:
|
||||
import PyLidar3
|
||||
import time # Time module
|
||||
#Serial port to which lidar connected, Get it from device manager windows
|
||||
#In linux type in terminal -- ls /dev/tty*
|
||||
#port = input("Enter port name which lidar is connected:") #windows
|
||||
#port = "/dev/ttyUSB0" #linux
|
||||
f = open("PoinCloudWeb.Scanner\datafile.txt","wt")
|
||||
Obj = PyLidar3.YdLidarX4(port='COM6',chunk_size=20000) #PyLidar3.your_version_of_lidar(port,chunk_size)
|
||||
if(Obj.Connect()):
|
||||
print(Obj.GetDeviceInfo())
|
||||
gen = Obj.StartScanning()
|
||||
t = time.time() # start time
|
||||
data = next(gen)
|
||||
#print(data)
|
||||
for x,y in data.items():
|
||||
f.write(str(x) + " / " + str(y) + "\n")
|
||||
f.close()
|
||||
Obj.StopScanning()
|
||||
Obj.Disconnect()
|
||||
else:
|
||||
print("Error connecting to device")
|
||||
69
PointCloudWeb.Scanner/test/ws.py
Normal file
69
PointCloudWeb.Scanner/test/ws.py
Normal file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# WS server example that synchronizes state across clients
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import websockets
|
||||
|
||||
logging.basicConfig()
|
||||
|
||||
STATE = {"value": 0}
|
||||
|
||||
USERS = set()
|
||||
|
||||
|
||||
def state_event():
|
||||
return json.dumps({"type": "state", **STATE})
|
||||
|
||||
|
||||
def users_event():
|
||||
return json.dumps({"type": "users", "count": len(USERS)})
|
||||
|
||||
|
||||
async def notify_state():
|
||||
if USERS: # asyncio.wait doesn't accept an empty list
|
||||
message = state_event()
|
||||
await asyncio.wait([user.send(message) for user in USERS])
|
||||
|
||||
|
||||
async def notify_users():
|
||||
if USERS: # asyncio.wait doesn't accept an empty list
|
||||
message = users_event()
|
||||
await asyncio.wait([user.send(message) for user in USERS])
|
||||
|
||||
|
||||
async def register(websocket):
|
||||
USERS.add(websocket)
|
||||
await notify_users()
|
||||
|
||||
|
||||
async def unregister(websocket):
|
||||
USERS.remove(websocket)
|
||||
await notify_users()
|
||||
|
||||
|
||||
async def counter(websocket, path):
|
||||
# register(websocket) sends user_event() to websocket
|
||||
await register(websocket)
|
||||
try:
|
||||
await websocket.send(state_event())
|
||||
async for message in websocket:
|
||||
data = json.loads(message)
|
||||
if data["action"] == "minus":
|
||||
STATE["value"] -= 1
|
||||
await notify_state()
|
||||
elif data["action"] == "plus":
|
||||
STATE["value"] += 1
|
||||
await notify_state()
|
||||
else:
|
||||
logging.error("unsupported event: %s", data)
|
||||
finally:
|
||||
await unregister(websocket)
|
||||
|
||||
|
||||
start_server = websockets.serve(counter, "localhost", 6789)
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(start_server)
|
||||
asyncio.get_event_loop().run_forever()
|
||||
Reference in New Issue
Block a user