-
Notifications
You must be signed in to change notification settings - Fork 0
/
lidar_wrtc.py
211 lines (168 loc) · 6.99 KB
/
lidar_wrtc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import asyncio
import json
import serial_asyncio_fast
import numpy as np
import struct
from enum import Enum
from json import JSONEncoder
import os
from aiohttp import web
from aiortc import RTCPeerConnection, RTCSessionDescription
PRINT_DEBUG = False
SERVER_IP = "0.0.0.0"
SERVER_PORT = 8080
SERIAL_PORT = "/dev/ttyUSB0"
SERIAL_BAUDRATE=230400
MEASUREMENTS_PER_PLOT = 480
MESSAGE_LENGTH = 47
MEASUREMENT_LENGTH = 12
MESSAGE_FORMAT = "<xBHH" + "HB" * MEASUREMENT_LENGTH + "HHB"
State = Enum("State", ["SYNC0", "SYNC1", "SYNC2", "LOCKED", "UPDATE_PLOT", "WS_SEND"])
state = State.SYNC0
message = b''
message_pos = 0
measurements = []
ROOT = os.path.dirname(__file__)
pc = None
dc = None
# Serial read
class SerialProtocol(asyncio.Protocol):
def connection_made(self, transport):
self.transport = transport
if PRINT_DEBUG: print('port opened', transport)
def connection_lost(self, exc):
if PRINT_DEBUG: print('port closed')
self.transport.loop.stop()
def data_received(self, data):
global state, message, message_pos, measurements, dc, pc
data_pos = 0
data_len = len(data)
if PRINT_DEBUG: print('data received', repr(data), data_len, data_pos, len(message), message_pos)
while data_pos < data_len:
if state == State.SYNC0:
message = b''
message_pos = 0
if data[data_pos:data_pos+1] == b'\x54':
message = b'\x54'
message_pos += 1
if PRINT_DEBUG: print(state, message, data_len, data_pos, len(message), message_pos)
state = State.SYNC1
else:
if PRINT_DEBUG: print(state, '\033[93m\033[1m' + "WARNING: Syncing" + '\033[0m', message, data_len, data_pos, len(message), message_pos)
data_pos += 1
elif state == State.SYNC1:
if data[data_pos:data_pos+1] == b'\x2C':
message += b'\x2C'
message_pos += 1
if PRINT_DEBUG: print(state, message, data_len, data_pos, len(message), message_pos)
state = State.SYNC2
else:
if PRINT_DEBUG: print(state, '\033[93m\033[1m' + "WARNING: Second byte not expected" + '\033[0m', message, data_len, data_pos, len(message), message_pos)
state = State.SYNC0
data_pos += 1
elif state == State.SYNC2:
if (data_len - data_pos) < (MESSAGE_LENGTH - message_pos):
message += data[data_pos:]
message_pos += (data_len - data_pos)
data_pos = data_len
if PRINT_DEBUG: print(state, "Message à completer", message, data_len, data_pos, len(message), message_pos)
else:
message += data[data_pos:(data_pos + MESSAGE_LENGTH - message_pos)]
data_pos += (MESSAGE_LENGTH - message_pos)
measurements += parse_lidar_data(message)
if PRINT_DEBUG: print(state, "Message complet", message, data_len, data_pos, len(message), message_pos)
if len(measurements) > MEASUREMENTS_PER_PLOT:
state = State.WS_SEND
else:
state = State.SYNC0
elif state == State.WS_SEND:
full_xy = get_xy_data(measurements)
numpyData = {"array": full_xy}
encodedNumpyData = json.dumps(numpyData, cls=NumpyArrayEncoder)
if dc != None:
if dc.readyState == "open":
dc.send(encodedNumpyData)
else:
if PRINT_DEBUG: print("NO DATA CHANNEL")
measurements = []
state = State.SYNC0
def parse_lidar_data(data):
# Extract data
length, speed, start_angle, *pos_data, stop_angle, timestamp, crc = \
struct.unpack(MESSAGE_FORMAT, data)
# Scale values
start_angle = float(start_angle) / 100.0
stop_angle = float(stop_angle) / 100.0
# Unwrap angle if needed and calculate angle step size
if stop_angle < start_angle:
stop_angle += 360.0
step_size = (stop_angle - start_angle) / (MEASUREMENT_LENGTH - 1)
# Get the angle for each measurement in packet
angle = [start_angle + step_size * i for i in range(0,MEASUREMENT_LENGTH)]
distance = pos_data[0::2] # in millimeters
if PRINT_DEBUG:
print("PARSING", length, speed, start_angle, *pos_data, stop_angle, timestamp, crc)
return list(zip(angle, distance))
def get_xy_data(measurements):
# Unpack the tuples
angle = np.array([measurement[0] for measurement in measurements])
distance = np.array([measurement[1] for measurement in measurements])
# Convert to cartesian coordinates in meters
x = np.sin(np.radians(angle)) * (distance / 1000.0)
y = np.cos(np.radians(angle)) * (distance / 1000.0)
if PRINT_DEBUG:
print(x, y)
print(np.dstack((x, y)))
return np.dstack((x, y))
class NumpyArrayEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, np.ndarray):
return obj.tolist()
return JSONEncoder.default(self, obj)
# WebRTC send
async def offer(request):
params = await request.json()
offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
if PRINT_DEBUG: print("Created for", request.remote)
@pc.on("connectionstatechange")
async def on_connectionstatechange():
if PRINT_DEBUG: print("Connection state is", pc.connectionState)
if pc.connectionState == "failed":
await pc.close()
pc.discard(pc)
await pc.setRemoteDescription(offer)
answer = await pc.createAnswer()
await pc.setLocalDescription(answer)
return web.Response(
content_type="application/json",
text=json.dumps(
{"sdp": pc.localDescription.sdp, "type": pc.localDescription.type}
),
)
async def on_shutdown(app):
pc.close()
async def index(request):
content = open(os.path.join(ROOT, "index.html"), "r").read()
return web.Response(content_type="text/html", text=content)
async def main():
global pc, dc
loop = asyncio.get_event_loop()
transport, protocol = await serial_asyncio_fast.create_serial_connection(loop, SerialProtocol, SERIAL_PORT, baudrate=SERIAL_BAUDRATE)
pc = RTCPeerConnection()
dc = pc.createDataChannel("lidar", negotiated=True, ordered=True, id=2) #
@dc.on("open")
def on_open():
print("Data channel opened")
app = web.Application()
app.on_shutdown.append(on_shutdown)
app.router.add_get("/", index)
app.router.add_post("/offer", offer)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, host=SERVER_IP, port=SERVER_PORT)
await site.start()
print("Server started. Point browser to", site.name)
await asyncio.Event().wait()
loop.close()
if __name__ == "__main__":
asyncio.run(main())