-
Notifications
You must be signed in to change notification settings - Fork 2
/
vehicle.html
212 lines (190 loc) · 7.45 KB
/
vehicle.html
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
212
<html>
<head>
<title>Vehicle Basic</title>
<style>
body,
div,
img {
outline: none;
margin: 0;
padding: 0;
background-color: black;
}
#stream {
pointer-events: none;
overflow: hidden;
height: 100vh;
width: auto;
object-fit: cover;
/* upside down camera flip */
transform: scaleX(-1) scaleY(-1);
}
#container {
overflow: hidden;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
min-height: 100vh;
}
#status {
overflow: hidden;
position: absolute;
left: 10px;
top: 10px;
text-shadow:
-1px 0px 0px black,
1px 0px 0px black,
0px -1px 0px black,
0px 1px 0px black;
color: white;
font-family: "Roboto", "Comic Sans", sans-serif;
font-size: 12px;
}
</style>
</head>
<body>
<div id="container">
<img id="stream" src="/mjpeg_stream" />
</div>
<span id="status"></span>
<script defer>
const SERVER_WS_URL
= (window.location.protocol === "https:" ? "wss:" : "ws:")
+ `//${window.location.hostname}:${window.location.port}/ws`;
const CONTROLLER_TYPE_GAMEPAD = 0;
const CONTROLLER_TYPE_THRUSTMASTER_WHEEL = 1;
const PWM_UPDATE_INTERVAL_MS = 20; // 20 ms is the pwm period
const PWM_ADJUST_INCREMENT = 0.001;
const stream = document.getElementById("stream");
const status = document.getElementById("status");
let wakeLock = null;
let webSocket = null;
let gamepadIndex = null;
let controllerType = CONTROLLER_TYPE_GAMEPAD;
let steeringCenter = 0;
let throttleMax = 1;
let systemStatus = {
battery: {
busVoltage: 0,
shuntVoltage: 0,
batteryVoltage: 0,
cellVoltage: 0,
current: 0,
power: 0,
chargePercents: 0,
}
}
window.addEventListener("gamepadconnected", (e) => {
gamepadIndex = e.gamepad.index;
const gamepadId = e.gamepad.id.toLocaleLowerCase();
if (gamepadId.includes("product: b669"))
controllerType = CONTROLLER_TYPE_THRUSTMASTER_WHEEL;
else
controllerType = CONTROLLER_TYPE_GAMEPAD;
});
window.addEventListener("gamepaddisconnected", (e) => {
if (e.gamepad.index === gamepadIndex) {
gamepadIndex = null;
}
});
async function toggleFullScreenWithWakeLock() {
if (!document.fullscreenElement) {
try {
await document.documentElement.requestFullscreen();
wakeLock = await navigator.wakeLock.request('screen');
} catch (err) {
}
} else if (document.exitFullscreen) {
try {
await document.exitFullscreen();
if (!wakeLock) { return; }
await wakeLock.release();
wakeLock = null;
} catch (err) {
}
}
}
function initWebSocket() {
try {
webSocket = new WebSocket(SERVER_WS_URL);
webSocket.onopen = onWebSocketOpen;
webSocket.onclose = onWebSocketClose;
webSocket.onmessage = onWebSocketMessage;
} catch (e) {
console.log("WebSocket connection error:", e);
webSocket = null;
setTimeout(initWebSocket, 5000);
}
}
function onWebSocketOpen(e) {
console.log("WebSocket connection established.");
reconnectMjpegStream();
}
function reconnectMjpegStream() {
const src = stream.src;
stream.src = "";
stream.src = src;
}
function onWebSocketClose(e) {
console.log("WebSocket connection closed.");
webSocket = null;
setTimeout(initWebSocket, 1000);
}
function onWebSocketMessage(e) {
systemStatus = JSON.parse(e.data);
}
function onWebSocketError(e) {
console.log("Websocket error:", e);
}
function update() {
let steering = steeringCenter;
let throttle = 0;
if (gamepadIndex !== null) {
const gamepad = navigator.getGamepads()[gamepadIndex];
const buttons = gamepad.buttons;
const axes = gamepad.axes;
if (controllerType === CONTROLLER_TYPE_THRUSTMASTER_WHEEL) {
steeringCenter -= buttons[4].pressed ? PWM_ADJUST_INCREMENT : 0; // A button
steeringCenter += buttons[8].pressed ? PWM_ADJUST_INCREMENT : 0; // B button
steering += axes[0]; // Wheel
const breakInput = (2 - (axes[1] + 1)) / 2; // Break pedal
const throttleInput = (2 - (axes[5] + 1)) / 2; // Throttle pedal
throttle = -breakInput + throttleInput;
} else {
throttleMax += buttons[12].pressed ? PWM_ADJUST_INCREMENT : 0; // Up arrow
throttleMax -= buttons[13].pressed ? PWM_ADJUST_INCREMENT : 0; // Down arrow
steeringCenter -= buttons[14].pressed ? PWM_ADJUST_INCREMENT : 0; // Left arrow
steeringCenter += buttons[15].pressed ? PWM_ADJUST_INCREMENT : 0; // Right arrow
steering += axes[0]; // Left Stick
const breakInput = buttons[6].value; // L2
const throttleInput = buttons[7].value; // R2
throttle = -breakInput + (throttleInput * throttleInput);
}
}
if (webSocket !== null && webSocket.readyState === WebSocket.OPEN) {
steering = Math.round(steering * 1000) / 1000;
throttle = Math.round(throttle * throttleMax * 1000) / 1000;
const vehicleState = {
inputs: [steering, throttle],
};
webSocket.send(JSON.stringify(vehicleState));
}
status.innerHTML =
`Gamepad: ${gamepadIndex !== null ? "connected" : "disconnected"}<br>
Vehicle control: ${webSocket !== null && webSocket.readyState === WebSocket.OPEN ? "connected" : "disconnected"}<br>
Steering center: ${steeringCenter.toFixed(3)}<br>
Steering: ${steering.toFixed(3)}<br>
Throttle max: ${throttleMax.toFixed(3)}<br>
Throttle: ${throttle.toFixed(3)}<br>
Battery Voltage: ${systemStatus.battery.batteryVoltage.toFixed(3)}<br>
Cell Voltage: ${systemStatus.battery.cellVoltage.toFixed(3)}<br>
Current: ${systemStatus.battery.current.toFixed(3)}<br>
Charge: ${Math.round(systemStatus.battery.chargePercents)}%<br>`;
}
document.body.addEventListener('click', toggleFullScreenWithWakeLock);
initWebSocket();
setInterval(update, PWM_UPDATE_INTERVAL_MS);
</script>
</body>
</html>