-
Notifications
You must be signed in to change notification settings - Fork 1
/
copilot.html
395 lines (363 loc) · 9.5 KB
/
copilot.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<html>
<head>
<title>Donkey Copilot</title>
<style>
html,body,main {
margin: 0;
overscroll-behavior: none;
overflow: hidden;
height 100%;
}
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
background: #70ad47;
width: 20%;
height: 80%;
border-radius: 30%;
</style>
<script src="./p5.min.js"></script>
</head>
<body>
<main>
</main>
<script>
function setup(){
URLparam = location.search;//URL Parameter
if(URLparam.match(/demo/)=='demo'){
demoMode=true;
V1 = document.createElement("video");
V1.src = './tub_movie_mft2022_1.mp4';
V1.hidden = true;
V1.autoplay = true;
V1.muted = true;
V1.pause();
}else{
demoMode=false;
}
tmp = URLparam.match(/ip=([0-9a-z\.\:]+).*$/,"$1");//IP address+port for debug
if(tmp){
raspIP=tmp[1]; //For debug on host pc
}else{
raspIP=location.host;//For Raspberry Pi
}
document.body.style.overflow = "hidden";
document.body.style.height = "100%";
// Create Y-slider
ySlider = createSlider(-1.0, 1.0, 0,0.01);
ySlider.elt.style.opacity=0.6;
ySlider.elt.style.webkitAppearance='none';
ySlider.elt.style.transform='rotate(270deg)'
ySlider.elt.style.backgroundColor='#696969';
ySlider.elt.style.borderRadius='40px';
ySlider.elt.addEventListener('change', ReleaseY);
xSlider = createSlider(-1.0, 1.0, 0,0.01);
// Create X-slider
xSlider.elt.style.opacity=0.6;
xSlider.elt.style.webkitAppearance='none';
xSlider.elt.style.borderRadius='40px';
xSlider.elt.style.backgroundColor='#696969';
xSlider.elt.addEventListener('change', ReleaseX);
// Create full screen button
buttonFS= createButton('Full Screen');
buttonFS.elt.style.borderRadius='20px';
buttonFS.elt.style.color='#000000';
buttonFS.elt.style.backgroundColor='#fffffff';
buttonFS.elt.style.opacity=0.5;
buttonFS.mousePressed(changeFS);
// create Rec button
buttonRec = createButton('REC');
buttonRec.elt.style.borderRadius='20px';
buttonRec.elt.style.color='#0000000';
buttonRec.elt.style.opacity=0.5;
buttonRec.mousePressed(changeREC);
// create Auto Rec button
buttonARec = createButton('Auto REC');
buttonARec.elt.style.borderRadius='20px';
buttonARec.elt.style.color='#000000';
buttonARec.elt.style.backgroundColor='#ffffff';
buttonARec.elt.style.opacity=0.5;
buttonARec.mousePressed(changeAREC);
// create AutoPilot button
buttonDM = createButton('Manual');
buttonDM.elt.style.borderRadius='20px';
buttonDM.elt.style.color='#000000';
buttonDM.elt.style.opacity=0.5;
buttonDM.elt.style.backgroundColor='#70ad47';
buttonDM.mousePressed(changeDM);
// create Set Max accel button
buttonSA = createButton('Set Max');
buttonSA.elt.style.borderRadius='20px';
buttonSA.elt.style.color='#000000';
buttonSA.elt.style.backgroundColor='#ffffff';
buttonSA.elt.style.opacity=0.5;
buttonSA.touchStarted(changeSA);
DM='user';// user|local|local_angle
ButtonX0 = false;
ButtonB0 = false;
AccelStep = 0.05;
SteeringStep = 0.05;
RecSW = false;
ARecSW = false;
MaxAccel = 100;
maxAsp = 2.22;//Max aspect
sc=(windowWidth/1920); //Screen Scale
canv=createCanvas(windowWidth, windowHeight);
if(!demoMode){
img = new Image();
img.src ='http://'+raspIP+'/video';
socket = new WebSocket('ws://'+raspIP+'/wsDrive');
}
window.addEventListener('touchmove', function(event) {
event.preventDefault();
});
}
function draw() { //Draw Loop
// Display video
background(0);
if(demoMode){//Demo movie
xOffset = 20+Math.round(xSlider.value()*20.0);
let ctx = canv.canvas.getContext("2d");
ctx.drawImage(V1,xOffset,0,V1.videoWidth-40,V1.videoHeight, 0,0,1920*sc, 1080*sc);
var spd;
if(DM!="local"){
spd = Math.round(ySlider.value()*10.0)/10.0;
}else{
spd = 1.0;
}
if(spd!=0.0 && frameRate()>0 && V1.readyState>=3){
V1.currentTime = V1.currentTime+spd/frameRate();
}
if(V1.currentTime>=V1.duration){
V1.currentTime = 0;
}
}else{
let ctx = canv.canvas.getContext("2d");
ctx.drawImage(img,0 , 0,img.width,img.height, 0, 0, 1920*sc, 1080*sc);
}
//Get GamePad state
Pads = navigator.getGamepads();
if (Pads[0]){ //If existing GamePad
deadband=0.05;
AnalogX = Pads[0].axes[2];
if (abs(AnalogX)<deadband){
AnalogX = 0;
}
AnalogY = -Pads[0].axes[1];
if (abs(AnalogY)<deadband){
AnalogY = 0;
}
ButtonA = Pads[0].buttons[0].value==1;
ButtonB = Pads[0].buttons[1].value==1;
ButtonX = Pads[0].buttons[3].value==1;
ButtonY = Pads[0].buttons[4].value==1;
ButtonDir = abs(int((Pads[0].axes[9]) * 7 / 2 + 0.5)-5);//Cross button
ButtonLeft = ButtonDir==2;
ButtonRight = ButtonDir==5;
if (ButtonLeft){//Cross Button
xSlider.value(xSlider.value()-SteeringStep);
}else if (ButtonRight){
xSlider.value(xSlider.value()+SteeringStep);
}else{
xSlider.value(AnalogX); //Analog Stick R
}
if (ButtonY){
ySlider.value(ySlider.value()+AccelStep );
}else if(ButtonA){
ySlider.value(ySlider.value()-AccelStep );
}else{
ySlider.value(AnalogY); //Analog Stick L
}
if(ButtonX && !ButtonX0){
changeDM();
}
ButtonX0 = ButtonX;
if(ButtonB && !ButtonB0){
changeREC();
}
ButtonB0 = ButtonB;
}
//Set Slider UI
ySlider.position(100*sc, 550*sc);
ySlider.size(400*sc,200*sc);
xSlider.position(1350*sc, 550*sc);
xSlider.size(400*sc,200*sc);
//Set Button UI
buttonFS.position(1750*sc, 10*sc);//Auto/Manual
buttonFS.size(150*sc,100*sc);
buttonFS.elt.style.fontSize=32*sc;
buttonDM.position(550*sc, 750*sc);
buttonDM.size(700*sc,100*sc);
buttonDM.elt.style.fontSize=48*sc;
buttonRec.position(1500*sc, 10*sc);//Auto/Manual
buttonRec.size(200*sc,100*sc);
buttonRec.elt.style.fontSize=48*sc;
buttonARec.position(1250*sc, 10*sc);//Auto/Manual
buttonARec.size(200*sc,100*sc);
buttonARec.elt.style.fontSize=32*sc;
buttonSA.position(1100*sc, 600*sc);//Auto/Manual
buttonSA.size(150*sc,100*sc);
buttonSA.elt.style.fontSize=32*sc;
if(ySlider.value()!=0 && DM=='local'){//Set AutoSteering mode
DM='local_angle';
buttonDM.elt.textContent="Auto Steering"
}
if(xSlider.value()!=0 && DM!='user'){//Set Manual mode
DM='user';
buttonDM.elt.textContent="Manual"
}
// Display Text
textAlign(CENTER, CENTER);
fill(200,200,200,200);
strokeJoin(ROUND);
stroke(0, 0, 0);
strokeWeight(4);
// Display Caption
push();
fill(200);
textAlign(CENTER,CENTER);
textStyle(ITALIC);
textSize(54*sc);
text('Donkey Copilot' ,900*sc, 50*sc);
textSize(28*sc);
strokeWeight(3);
text('Powered by TatamiRacer' ,900*sc, 100*sc);
pop();
// Display FPS / MAX Accel /GamePad
push();
textSize(40*sc);
text("FPS "+str(int(frameRate())) ,150*sc, 40*sc);
if (Pads[0]){ //If existing GamePad
text('GamePad' ,350*sc, 40*sc);
}
text(str(MaxAccel) ,1170*sc, 570*sc);
pop();
lv = ySlider.value()*100;//Slider Level
if(abs(lv)>MaxAccel){
lv = Math.sign(lv)*abs(MaxAccel);
}
if(DM=='user'|| DM=='local_angle'){ //Manual mode or Auto Steering
//Accel Guage
push();
textStyle(NORMAL);
textSize(98*sc);
stroke(0, 0, 0, 255);
if(lv>=0){
fill(255, 255, 255, 140);
}else{
fill(255, 0, 0,140);
}
text(int (lv),900*sc, 650*sc);//Accel
noFill();
strokeCap(SQUARE);
strokeWeight(40*sc);
stroke(0, 255, 0, 200);
arc(900*sc, 650*sc, 320*sc, 200*sc, PI, PI+PI*abs(lv)*0.01);//Low Accel
if(abs(lv)>=40){//Mid Accel
stroke(255, 255, 0, 200);
arc(900*sc, 650*sc, 320*sc, 200*sc, PI+PI*0.4, PI+PI*abs(lv)*0.01);
}
if(abs(lv)>=80){//High Accel
stroke(255,0,0, 200);
arc(900*sc, 650*sc, 320*sc, 200*sc, PI+PI*0.8, PI+PI*abs(lv)*0.01);
}
pop();
}
if(DM=='user'){ //Manual mode
// Direction Guage
push();
strokeWeight(2);
translate(900*sc, 200*sc);
textStyle(NORMAL);
textSize(48*sc);
text(int (xSlider.value()*100),0*sc, 50*sc);
stroke(112, 173, 71, 255);
noFill();
arc(0, 0, 180*sc, 150*sc, PI, PI/2+PI*1.5);
fill(112, 173, 71, 150);//#70ad47
rotate((PI /5) * xSlider.value());
translate(0, -30*sc);
triangle(0, -40*sc, -20*sc, 40*sc, 20*sc, 40*sc);
pop();
}
//Recording
if(ARecSW){ //Auto Recording
if (abs(lv)>0){
RecSW = true;
}else{
RecSW = false;
}
}
// Button Color
if(RecSW){
buttonRec.elt.style.backgroundColor='#dc143c';
}else{
buttonRec.elt.style.backgroundColor='#ffffff';
}
if(DM=="local" || DM=='local_angle'){
buttonDM.elt.style.backgroundColor='#70ad47';
}else{
buttonDM.elt.style.backgroundColor='#ffa500';
}
//Send message to DonkeyCar
data = JSON.stringify({ 'angle': xSlider.value(), //Steering(-1..+1)
'throttle':lv/100, //Accel(-1..+1)
'drive_mode': DM, //user|local|local_angle
'recording': RecSW}) //true|false
if(!demoMode){
try{
socket.send(data);
}catch{
}
}
}
function ReleaseX(){ //Slider release
xSlider.value(0);
}
function ReleaseY(){ //Slider release
ySlider.value(0);
}
function changeFS(){ //Full screen button
let fs = fullscreen();
fullscreen(!fs);
}
function changeDM(){ //Drive mode button
if(DM=="local" || DM=='local_angle'){
DM='user';
buttonDM.elt.textContent="Manual"
}else{
DM='local';
buttonDM.elt.textContent="Auto"
}
}
function changeREC(){ //Rec button
if(!ARecSW){ //AutoRec
RecSW=!RecSW;
}
}
function changeAREC(){ //Auto Rec button
ARecSW=!ARecSW;
if(ARecSW){
buttonARec.elt.style.backgroundColor='#dc143c';
}else{
buttonARec.elt.style.backgroundColor='#ffffff';
}
}
function changeSA(){ //Set Max accel level
if(abs(ySlider.value())>0.1){
MaxAccel=int(abs(ySlider.value())*100);
}else{
MaxAccel=100;
}
}
function windowResized() { // Resize
asp=windowWidth/windowHeight;//Aspect ratio
resizeCanvas(windowWidth, windowHeight);
if(asp<=maxAsp){ // Screen scale factor
sc = windowWidth/1920;
}else{
sc = (windowWidth/1920)/(asp/maxAsp);
};
}
</script>
</body>
</html>